-
Notifications
You must be signed in to change notification settings - Fork 6
/
dir.go
330 lines (297 loc) · 9.11 KB
/
dir.go
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"syscall"
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
"golang.org/x/net/context"
)
// This is a temporal fix: don't rewrite the targets of symbolic links by
// default. Some applications check when they create a symbolic link that
// the value they specified as target is actually the one the file system uses.
// TODO: this should be a configurable feature for the ClueFS.
const rewriteSymlinkTargets = false
var skipDirEntry func(n string) bool
func init() {
switch runtime.GOOS {
case "darwin":
// On Darwin we skip all directory entries starting by '._'
skipDirEntry = func(n string) bool {
return strings.HasPrefix(n, "._")
}
default:
skipDirEntry = func(n string) bool {
return false
}
}
}
type Dir struct {
*Node
*Handle
ProcessInfo
// mutex protects the entries map
mutex sync.RWMutex
// entries maps the name of a node with its node object
entries map[string]interface{}
}
func NewDir(parent string, name string, fs *ClueFS) *Dir {
return &Dir{
Node: NewNode(parent, name, fs),
Handle: &Handle{},
entries: make(map[string]interface{}, 32),
}
}
func (d Dir) String() string {
return fmt.Sprintf("[%s %s %s]", d.Node, d.Handle, d.ProcessInfo)
}
func (d *Dir) SetProcessInfo(h fuse.Header) {
d.ProcessInfo = ProcessInfo{Uid: h.Uid, Gid: h.Gid, Pid: h.Pid}
}
// saveEntry saves a *File or *Dir object associated to a
// name in this directory
func (d *Dir) saveEntry(name string, entry interface{}) {
d.mutex.Lock()
defer d.mutex.Unlock()
d.entries[name] = entry
}
// dropEntry deletes the *File or *Dir object associated to a
// name in this directory
func (d *Dir) dropEntry(name string) interface{} {
d.mutex.Lock()
defer d.mutex.Unlock()
if e := d.entries[name]; e != nil {
delete(d.entries, name)
return e
}
return nil
}
// getEntry returns the *File or *Dir object currently associated
// to a name in this directory. It may return nil if there is
// no such object associate to name
func (d *Dir) getEntry(name string) interface{} {
d.mutex.RLock()
defer d.mutex.RUnlock()
return d.entries[name]
}
func (d *Dir) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fusefs.Handle, error) {
op := NewOpenOp(req, d.path)
defer trace(op)
newdir := NewDir(d.parent, d.name, d.fs)
size, err := newdir.doOpen(d.path, req.Flags)
if err != nil {
return nil, err
}
newdir.SetProcessInfo(req.Header)
resp.Handle = fuse.HandleID(newdir.handleID)
op.FileSize = size
op.BlockSize = newdir.blksize
op.OpenID = newdir.handleID
return newdir, nil
}
func (d *Dir) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if !d.isOpen() {
return nil
}
defer trace(NewReleaseOp(req, d.path, d.handleID))
if req.ReleaseFlags&fuse.ReleaseFlush != 0 {
d.doSync()
}
return d.doClose()
}
func (d *Dir) Lookup(ctx context.Context, req *fuse.LookupRequest, resp *fuse.LookupResponse) (fusefs.Node, error) {
if skipDirEntry(req.Name) {
return nil, fuse.ENOENT
}
path := filepath.Join(d.path, req.Name)
isDir := false
defer trace(NewLookupOp(req, path, isDir))
var st syscall.Stat_t
if err := syscall.Lstat(path, &st); err != nil {
return nil, fuse.ENOENT
}
resp.Attr = statToFuseAttr(st)
resp.Node = fuse.NodeID(resp.Attr.Inode)
// TODO: should we overwrite resp.EntryValid?
// resp.EntryValid = time.Duration(500) * time.Millisecond
// Is there any saved entry for the name being looked up?
// If so, return it
isDir = resp.Attr.Mode.IsDir()
if e := d.getEntry(req.Name); e != nil {
switch e.(type) {
case *Dir:
return e.(*Dir), nil
case *File:
return e.(*File), nil
}
}
// No saved entry found. Save it and return the appropriate
// file or directory
if isDir {
dd := NewDir(d.path, req.Name, d.fs)
d.saveEntry(req.Name, dd)
return dd, nil
}
ff := NewFile(d.path, req.Name, d.fs)
d.saveEntry(req.Name, ff)
return ff, nil
}
func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
if !d.isOpen() {
return nil, fuse.ENOTSUP
}
defer trace(NewReadDirOp(d.path, d.ProcessInfo, d.handleID))
names, err := d.file.Readdirnames(0)
if err != nil {
return nil, fuse.EIO
}
result := make([]fuse.Dirent, 0, len(names)+2)
for _, n := range names {
if skipDirEntry(n) {
continue
}
entry := getFuseDirent(filepath.Join(d.path, n), n)
result = append(result, entry)
}
// Add '.' and '..' to the result
dots := make([]fuse.Dirent, 2)
dots[0] = getFuseDirent(d.path, ".")
if len(d.parent) > 0 {
dots[1] = getFuseDirent(d.parent, "..")
} else {
dots[1] = dots[0]
dots[1].Name = ".."
}
return append(result, dots...), nil
}
func (d *Dir) Mkdir(ctx context.Context, req *fuse.MkdirRequest) (fusefs.Node, error) {
path := filepath.Join(d.path, req.Name)
defer trace(NewMkdirOp(req, path, req.Mode))
if err := os.Mkdir(path, req.Mode); err != nil {
return nil, osErrorToFuseError(err)
}
newdir := NewDir(d.path, req.Name, d.fs)
d.saveEntry(req.Name, newdir)
return newdir, nil
}
func (d *Dir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
path := filepath.Join(d.path, req.Name)
defer trace(NewRemoveOp(req, path))
if err := os.Remove(path); err != nil {
return osErrorToFuseError(err)
}
d.dropEntry(req.Name)
return nil
}
func (d *Dir) Create(ctx context.Context, req *fuse.CreateRequest, resp *fuse.CreateResponse) (fusefs.Node, fusefs.Handle, error) {
path := filepath.Join(d.path, req.Name)
op := NewCreateOp(req, path)
defer trace(op)
h := NewHandle()
if err := h.doCreate(path, req.Flags, req.Mode); err != nil {
return nil, nil, err
}
newfile := NewFileWithHandle(d.path, req.Name, d.fs, h)
d.saveEntry(req.Name, newfile)
op.OpenID = newfile.handleID
return newfile, newfile, nil
}
func (d *Dir) Symlink(ctx context.Context, req *fuse.SymlinkRequest) (fusefs.Node, error) {
absNewName := filepath.Join(d.path, req.NewName)
targetIsDir := false
defer trace(NewSymlinkOp(req, absNewName, req.Target, targetIsDir))
linkTarget, absTarget := req.Target, req.Target
if rewriteSymlinkTargets {
// Make sure the target of the symbolic link which will be created
// is jailed within the boundaries of the shadow file system. That is,
// the link target path file name uses a path under the shadow file
// system and not under this file system mount point.
// The goal of this is for the symbolic link not to be broken when
// ClueFS is unmounted.
// In the link target path, replace this file system's mount directory by
// the corresponding directory in the shadow file system. Do this
// rewriting of the symbolic link target path only when the target
// of the symbolic link an absolute path. When it is a relative path,
// it will be considered to be relative to the directory when the
// symbolic link is created.
if !filepath.IsAbs(req.Target) {
absTarget = filepath.Join(d.path, req.Target)
}
// If the link target path is under the mount directory, rewrite it
// to be under the shadow directory
if strings.HasPrefix(absTarget, d.fs.mountDir) {
absTarget = strings.Replace(absTarget, d.fs.mountDir, d.fs.shadowDir, 1)
linkTarget = absTarget
}
}
// Does the link target actually exist?
if info, err := os.Lstat(absTarget); err == nil {
// The symbolic link target does exist
targetIsDir = info.IsDir()
}
// Create the symbolic link: absNewName --> linkTarget
if err := os.Symlink(linkTarget, absNewName); err != nil {
return nil, osErrorToFuseError(err)
}
if targetIsDir {
entry := NewDir(d.path, req.NewName, d.fs)
d.saveEntry(req.NewName, entry)
return entry, nil
}
entry := NewFile(d.path, req.NewName, d.fs)
d.saveEntry(req.NewName, entry)
return entry, nil
}
func (d *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDir fusefs.Node) error {
destDir, ok := newDir.(*Dir)
if !ok {
return fuse.EIO
}
oldpath := filepath.Join(d.path, req.OldName)
newpath := filepath.Join(destDir.path, req.NewName)
defer trace(NewRenameOp(req, oldpath, newpath))
if err := os.Rename(oldpath, newpath); err != nil {
return osErrorToFuseError(err)
}
// Delete the name of the node just renamed from the set of entries
// of the source directory and add the new name to the entries of the
// destination directory.
// In addition, set the new parent and new name of the renamed node
if e := d.dropEntry(req.OldName); e != nil {
switch e.(type) {
case *Dir:
// The renamed node is a directory
dd := e.(*Dir)
dd.setParentAndName(destDir.path, req.NewName)
destDir.saveEntry(req.NewName, dd)
case *File:
// The renamed node is a file
ff := e.(*File)
ff.setParentAndName(destDir.path, req.NewName)
destDir.saveEntry(req.NewName, ff)
default:
// should not happen. Do nothing
}
}
return nil
}
// osErrorToFuseError converts an os.PathError, os.LinkError or
// syscall.Errno into an error
func osErrorToFuseError(err error) error {
if err == nil {
return nil
}
errno := syscall.EIO
if patherr, ok := err.(*os.PathError); ok {
errno = patherr.Err.(syscall.Errno)
} else if linkerr, ok := err.(*os.LinkError); ok {
errno = linkerr.Err.(syscall.Errno)
} else if _, ok := err.(*syscall.Errno); ok {
errno = err.(syscall.Errno)
}
return fuse.Errno(errno)
}