aboutsummaryrefslogtreecommitdiff
path: root/cluefs_frontend/fe_dir.go
blob: 65d92f15ca666e151081050f73ffdfa5f56d1005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package cluefs_frontend

// frontend sits between FUSE and ClueFS
// and uses cryptfs for all crypto operations
//
//          cryptfs
//             ^
//             |
//             v
// FUSE <-> frontend <-> ClueFS
//
// This file handles directories

import (
	"fmt"
	"github.com/rfjakob/gocryptfs/cryptfs"
	"github.com/rfjakob/cluefs/lib/cluefs"
	"bazil.org/fuse"
	fusefs "bazil.org/fuse/fs"
	"golang.org/x/net/context"
)

type Dir struct {
	*cluefs.Dir
	crfs *cryptfs.CryptFS
}

func NewDir(parent string, name string, fs *FS) *Dir {
	cryptfs.Debug.Printf("NewDir parent=%s name=%s\n", parent, name)
	return &Dir {
		Dir: cluefs.NewDir(parent, name, fs.ClueFS),
		crfs: fs.CryptFS,
	}
}

func (d *Dir) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) {
	cryptfs.Debug.Printf("Open\n")
	h, err := d.Dir.Open(ctx, req, resp)
	if err != nil {
		return nil, err
	}
	clueDir := h.(*cluefs.Dir)

	return &Dir {
		Dir: clueDir,
		crfs: d.crfs,
	}, nil
}

func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fusefs.Node, error) {
	cryptfs.Debug.Printf("Lookup %s\n", req.Name)
	req.Name = d.crfs.EncryptPath(req.Name)
	node, err := d.Dir.Lookup(ctx, req, resp)
	if err != nil {
		return nil, err
	}
	clueDir, ok := node.(*cluefs.Dir)
	if ok {
		return &Dir {
			Dir: clueDir,
			crfs: d.crfs,
		}, nil
	} else {
		resp.Attr.Size = d.crfs.PlainSize(resp.Attr.Size)
		clueFile := node.(*cluefs.File)
		return &File {
			File: clueFile,
			crfs: d.crfs,
		}, nil
	}
}

func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
	cryptfs.Debug.Printf("ReadDirAll\n")
	entries, err := d.Dir.ReadDirAll(ctx)
	if err != nil {
		return nil, err
	}
	var decrypted []fuse.Dirent
	for _, e := range entries {
		if e.Name == "." || e.Name == ".." {
			decrypted = append(decrypted, e)
			continue
		}
		newName, err := d.crfs.DecryptPath(e.Name)
		if err != nil {
			fmt.Printf("ReadDirAll: Error decoding \"%s\": %s\n", e.Name, err.Error())
			continue
		}
		e.Name = newName
		decrypted = append(decrypted, e)
	}
	return decrypted, nil
}

func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fusefs.Node, error) {
	cryptfs.Debug.Printf("Mkdir %s\n", req.Name)
	req.Name = d.crfs.EncryptPath(req.Name)
	n, err := d.Dir.Mkdir(ctx, req)
	if err != nil {
		return nil, err
	}
	clueDir := n.(*cluefs.Dir)
	return &Dir {
		Dir: clueDir,
		crfs: d.crfs,
	}, nil
}

func (d *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
	cryptfs.Debug.Printf("Remove\n")
	req.Name = d.crfs.EncryptPath(req.Name)
	return d.Dir.Remove(ctx, req)
}

func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fusefs.Node, fusefs.Handle, error) {
	cryptfs.Debug.Printf("Create\n")
	req.Flags, _ = fixFlags(req.Flags)
	req.Name = d.crfs.EncryptPath(req.Name)
	n, _, err := d.Dir.Create(ctx, req, resp)
	if err != nil {
		return nil, nil, err
	}
	clueFile := n.(*cluefs.File)
	cryptFile := &File {
		File: clueFile,
		crfs: d.crfs,
	}
	return cryptFile, cryptFile, nil
}
func (d *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fusefs.Node, error) {
	req.Target = d.crfs.EncryptPath(req.Target)
	req.NewName = d.crfs.EncryptPath(req.NewName)
	node, err := d.Dir.Symlink(ctx, req)
	if err != nil {
		return nil, err
	}

	clueDir, ok := node.(*cluefs.Dir)
	if ok {
		return &Dir {
			Dir: clueDir,
			crfs: d.crfs,
		}, nil
	} else {
		clueFile := node.(*cluefs.File)
		return &File {
			File: clueFile,
			crfs: d.crfs,
		}, nil
	}
}
// We need to overwrite Readlink for both Dir and File. Do both right here to keep them in sync.
func (d *Dir) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
	dest, err := d.Dir.Readlink(ctx, req)
	if err != nil {
		return "", err
	}
	return d.crfs.DecryptPath(dest)
}
func (d *File) Readlink(ctx context.Context, req *fuse.ReadlinkRequest) (string, error) {
	dest, err := d.File.Readlink(ctx, req)
	if err != nil {
		return "", err
	}
	return d.crfs.DecryptPath(dest)
}
// We need to overwrite Rename for both Dir and File. Do both right here to keep them in sync.
func (d *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fusefs.Node) error {
	req.OldName = d.crfs.EncryptPath(req.OldName)
	req.NewName = d.crfs.EncryptPath(req.NewName)
	destDir := newDir.(*Dir)
	return d.Dir.Rename(ctx, req, destDir.Dir)
}
func (d *File) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fusefs.Node) error {
	req.OldName = d.crfs.EncryptPath(req.OldName)
	req.NewName = d.crfs.EncryptPath(req.NewName)
	destDir := newDir.(*Dir)
	return d.File.Rename(ctx, req, destDir.Dir)
}