-
Notifications
You must be signed in to change notification settings - Fork 5
/
fsops.go
442 lines (363 loc) · 11.9 KB
/
fsops.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package nodefs
// This file contains FileSystemConnector's implementation of
// RawFileSystem
import (
"bytes"
"fmt"
"log"
"strings"
"time"
"github.com/hanwen/go-fuse/fuse"
)
// Returns the RawFileSystem so it can be mounted.
func (c *FileSystemConnector) RawFS() fuse.RawFileSystem {
return (*rawBridge)(c)
}
type rawBridge FileSystemConnector
func (c *rawBridge) Fsync(input *fuse.FsyncIn) fuse.Status {
return fuse.ENOSYS
}
func (c *rawBridge) SetDebug(debug bool) {
c.fsConn().SetDebug(debug)
}
func (c *rawBridge) FsyncDir(input *fuse.FsyncIn) fuse.Status {
return fuse.ENOSYS
}
func (c *rawBridge) fsConn() *FileSystemConnector {
return (*FileSystemConnector)(c)
}
func (c *rawBridge) String() string {
if c.rootNode == nil || c.rootNode.mount == nil {
return "go-fuse:unmounted"
}
name := fmt.Sprintf("%T", c.rootNode.Node())
name = strings.TrimLeft(name, "*")
return name
}
func (c *rawBridge) Init(s *fuse.Server) {
c.server = s
}
func (c *FileSystemConnector) lookupMountUpdate(out *fuse.Attr, mount *fileSystemMount) (node *Inode, code fuse.Status) {
code = mount.mountInode.Node().GetAttr(out, nil, nil)
if !code.Ok() {
log.Println("Root getattr should not return error", code)
out.Mode = fuse.S_IFDIR | 0755
return mount.mountInode, fuse.OK
}
return mount.mountInode, fuse.OK
}
func (c *FileSystemConnector) internalLookup(out *fuse.Attr, parent *Inode, name string, header *fuse.InHeader) (node *Inode, code fuse.Status) {
child := parent.GetChild(name)
if child != nil && child.mountPoint != nil {
return c.lookupMountUpdate(out, child.mountPoint)
}
if child != nil {
parent = nil
}
if child != nil {
code = child.fsInode.GetAttr(out, nil, &header.Context)
} else {
child, code = parent.fsInode.Lookup(out, name, &header.Context)
}
return child, code
}
func (c *rawBridge) Lookup(header *fuse.InHeader, name string, out *fuse.EntryOut) (code fuse.Status) {
parent := c.toInode(header.NodeId)
if !parent.IsDir() {
log.Printf("Lookup %q called on non-Directory node %d", name, header.NodeId)
return fuse.ENOTDIR
}
outAttr := (*fuse.Attr)(&out.Attr)
child, code := c.fsConn().internalLookup(outAttr, parent, name, header)
if code == fuse.ENOENT && parent.mount.negativeEntry(out) {
return fuse.OK
}
if !code.Ok() {
return code
}
if child == nil {
log.Println("Lookup returned fuse.OK with nil child", name)
}
child.mount.fillEntry(out)
out.NodeId, out.Generation = c.fsConn().lookupUpdate(child)
out.Ino = out.NodeId
return fuse.OK
}
func (c *rawBridge) Forget(nodeID, nlookup uint64) {
c.fsConn().forgetUpdate(nodeID, int(nlookup))
}
func (c *rawBridge) GetAttr(input *fuse.GetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
node := c.toInode(input.NodeId)
var f File
if input.Flags()&fuse.FUSE_GETATTR_FH != 0 {
if opened := node.mount.getOpenedFile(input.Fh()); opened != nil {
f = opened.WithFlags.File
}
}
dest := (*fuse.Attr)(&out.Attr)
code = node.fsInode.GetAttr(dest, f, &input.Context)
if !code.Ok() {
return code
}
node.mount.fillAttr(out, input.NodeId)
return fuse.OK
}
func (c *rawBridge) OpenDir(input *fuse.OpenIn, out *fuse.OpenOut) (code fuse.Status) {
node := c.toInode(input.NodeId)
stream, err := node.fsInode.OpenDir(&input.Context)
if err != fuse.OK {
return err
}
stream = append(stream, node.getMountDirEntries()...)
de := &connectorDir{
node: node.Node(),
stream: append(stream,
fuse.DirEntry{Mode: fuse.S_IFDIR, Name: "."},
fuse.DirEntry{Mode: fuse.S_IFDIR, Name: ".."}),
rawFS: c,
}
h, opened := node.mount.registerFileHandle(node, de, nil, input.Flags)
out.OpenFlags = opened.FuseFlags
out.Fh = h
return fuse.OK
}
func (c *rawBridge) ReadDir(input *fuse.ReadIn, out *fuse.DirEntryList) fuse.Status {
node := c.toInode(input.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
return opened.dir.ReadDir(input, out)
}
func (c *rawBridge) ReadDirPlus(input *fuse.ReadIn, out *fuse.DirEntryList) fuse.Status {
node := c.toInode(input.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
return opened.dir.ReadDirPlus(input, out)
}
func (c *rawBridge) Open(input *fuse.OpenIn, out *fuse.OpenOut) (status fuse.Status) {
node := c.toInode(input.NodeId)
f, code := node.fsInode.Open(input.Flags, &input.Context)
if !code.Ok() || f == nil {
return code
}
h, opened := node.mount.registerFileHandle(node, nil, f, input.Flags)
out.OpenFlags = opened.FuseFlags
out.Fh = h
return fuse.OK
}
func (c *rawBridge) SetAttr(input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
node := c.toInode(input.NodeId)
var f File
if input.Valid&fuse.FATTR_FH != 0 {
opened := node.mount.getOpenedFile(input.Fh)
f = opened.WithFlags.File
}
if code.Ok() && input.Valid&fuse.FATTR_MODE != 0 {
permissions := uint32(07777) & input.Mode
code = node.fsInode.Chmod(f, permissions, &input.Context)
}
if code.Ok() && (input.Valid&(fuse.FATTR_UID|fuse.FATTR_GID) != 0) {
code = node.fsInode.Chown(f, uint32(input.Uid), uint32(input.Gid), &input.Context)
}
if code.Ok() && input.Valid&fuse.FATTR_SIZE != 0 {
code = node.fsInode.Truncate(f, input.Size, &input.Context)
}
if code.Ok() && (input.Valid&(fuse.FATTR_ATIME|fuse.FATTR_MTIME|fuse.FATTR_ATIME_NOW|fuse.FATTR_MTIME_NOW) != 0) {
now := time.Now()
var atime *time.Time
var mtime *time.Time
if input.Valid&fuse.FATTR_ATIME != 0 {
if input.Valid&fuse.FATTR_ATIME_NOW != 0 {
atime = &now
} else {
t := time.Unix(int64(input.Atime), int64(input.Atimensec))
atime = &t
}
}
if input.Valid&fuse.FATTR_MTIME != 0 {
if input.Valid&fuse.FATTR_MTIME_NOW != 0 {
mtime = &now
} else {
t := time.Unix(int64(input.Mtime), int64(input.Mtimensec))
mtime = &t
}
}
code = node.fsInode.Utimens(f, atime, mtime, &input.Context)
}
if !code.Ok() {
return code
}
// Must call GetAttr(); the filesystem may override some of
// the changes we effect here.
attr := (*fuse.Attr)(&out.Attr)
code = node.fsInode.GetAttr(attr, nil, &input.Context)
if code.Ok() {
node.mount.fillAttr(out, input.NodeId)
}
return code
}
func (c *rawBridge) Fallocate(input *fuse.FallocateIn) (code fuse.Status) {
n := c.toInode(input.NodeId)
opened := n.mount.getOpenedFile(input.Fh)
return n.fsInode.Fallocate(opened, input.Offset, input.Length, input.Mode, &input.Context)
}
func (c *rawBridge) Readlink(header *fuse.InHeader) (out []byte, code fuse.Status) {
n := c.toInode(header.NodeId)
return n.fsInode.Readlink(&header.Context)
}
func (c *rawBridge) Mknod(input *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
parent := c.toInode(input.NodeId)
child, code := parent.fsInode.Mknod(name, input.Mode, uint32(input.Rdev), &input.Context)
if code.Ok() {
c.childLookup(out, child, &input.Context)
code = child.fsInode.GetAttr((*fuse.Attr)(&out.Attr), nil, &input.Context)
}
return code
}
func (c *rawBridge) Mkdir(input *fuse.MkdirIn, name string, out *fuse.EntryOut) (code fuse.Status) {
parent := c.toInode(input.NodeId)
child, code := parent.fsInode.Mkdir(name, input.Mode, &input.Context)
if code.Ok() {
c.childLookup(out, child, &input.Context)
code = child.fsInode.GetAttr((*fuse.Attr)(&out.Attr), nil, &input.Context)
}
return code
}
func (c *rawBridge) Unlink(header *fuse.InHeader, name string) (code fuse.Status) {
parent := c.toInode(header.NodeId)
return parent.fsInode.Unlink(name, &header.Context)
}
func (c *rawBridge) Rmdir(header *fuse.InHeader, name string) (code fuse.Status) {
parent := c.toInode(header.NodeId)
return parent.fsInode.Rmdir(name, &header.Context)
}
func (c *rawBridge) Symlink(header *fuse.InHeader, pointedTo string, linkName string, out *fuse.EntryOut) (code fuse.Status) {
parent := c.toInode(header.NodeId)
child, code := parent.fsInode.Symlink(linkName, pointedTo, &header.Context)
if code.Ok() {
c.childLookup(out, child, &header.Context)
code = child.fsInode.GetAttr((*fuse.Attr)(&out.Attr), nil, &header.Context)
}
return code
}
func (c *rawBridge) Rename(input *fuse.RenameIn, oldName string, newName string) (code fuse.Status) {
oldParent := c.toInode(input.NodeId)
child := oldParent.GetChild(oldName)
if child == nil {
return fuse.ENOENT
}
if child.mountPoint != nil {
return fuse.EBUSY
}
newParent := c.toInode(input.Newdir)
if oldParent.mount != newParent.mount {
return fuse.EXDEV
}
return oldParent.fsInode.Rename(oldName, newParent.fsInode, newName, &input.Context)
}
func (c *rawBridge) Link(input *fuse.LinkIn, name string, out *fuse.EntryOut) (code fuse.Status) {
existing := c.toInode(input.Oldnodeid)
parent := c.toInode(input.NodeId)
if existing.mount != parent.mount {
return fuse.EXDEV
}
child, code := parent.fsInode.Link(name, existing.fsInode, &input.Context)
if code.Ok() {
c.childLookup(out, child, &input.Context)
code = child.fsInode.GetAttr((*fuse.Attr)(&out.Attr), nil, &input.Context)
}
return code
}
func (c *rawBridge) Access(input *fuse.AccessIn) (code fuse.Status) {
n := c.toInode(input.NodeId)
return n.fsInode.Access(input.Mask, &input.Context)
}
func (c *rawBridge) Create(input *fuse.CreateIn, name string, out *fuse.CreateOut) (code fuse.Status) {
parent := c.toInode(input.NodeId)
f, child, code := parent.fsInode.Create(name, uint32(input.Flags), input.Mode, &input.Context)
if !code.Ok() {
return code
}
c.childLookup(&out.EntryOut, child, &input.Context)
handle, opened := parent.mount.registerFileHandle(child, nil, f, input.Flags)
out.OpenOut.OpenFlags = opened.FuseFlags
out.OpenOut.Fh = handle
return code
}
func (c *rawBridge) Release(input *fuse.ReleaseIn) {
if input.Fh != 0 {
node := c.toInode(input.NodeId)
opened := node.mount.unregisterFileHandle(input.Fh, node)
opened.WithFlags.File.Release()
}
}
func (c *rawBridge) ReleaseDir(input *fuse.ReleaseIn) {
if input.Fh != 0 {
node := c.toInode(input.NodeId)
node.mount.unregisterFileHandle(input.Fh, node)
}
}
func (c *rawBridge) GetXAttrSize(header *fuse.InHeader, attribute string) (sz int, code fuse.Status) {
node := c.toInode(header.NodeId)
data, errno := node.fsInode.GetXAttr(attribute, &header.Context)
return len(data), errno
}
func (c *rawBridge) GetXAttrData(header *fuse.InHeader, attribute string) (data []byte, code fuse.Status) {
node := c.toInode(header.NodeId)
return node.fsInode.GetXAttr(attribute, &header.Context)
}
func (c *rawBridge) RemoveXAttr(header *fuse.InHeader, attr string) fuse.Status {
node := c.toInode(header.NodeId)
return node.fsInode.RemoveXAttr(attr, &header.Context)
}
func (c *rawBridge) SetXAttr(input *fuse.SetXAttrIn, attr string, data []byte) fuse.Status {
node := c.toInode(input.NodeId)
return node.fsInode.SetXAttr(attr, data, int(input.Flags), &input.Context)
}
func (c *rawBridge) ListXAttr(header *fuse.InHeader) (data []byte, code fuse.Status) {
node := c.toInode(header.NodeId)
attrs, code := node.fsInode.ListXAttr(&header.Context)
if code != fuse.OK {
return nil, code
}
b := bytes.NewBuffer([]byte{})
for _, v := range attrs {
b.Write([]byte(v))
b.WriteByte(0)
}
return b.Bytes(), code
}
////////////////
// files.
func (c *rawBridge) Write(input *fuse.WriteIn, data []byte) (written uint32, code fuse.Status) {
node := c.toInode(input.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
var f File
if opened != nil {
f = opened.WithFlags.File
}
return node.Node().Write(f, data, int64(input.Offset), &input.Context)
}
func (c *rawBridge) Read(input *fuse.ReadIn, buf []byte) (fuse.ReadResult, fuse.Status) {
node := c.toInode(input.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
var f File
if opened != nil {
f = opened.WithFlags.File
}
return node.Node().Read(f, buf, int64(input.Offset), &input.Context)
}
func (c *rawBridge) StatFs(header *fuse.InHeader, out *fuse.StatfsOut) fuse.Status {
node := c.toInode(header.NodeId)
s := node.Node().StatFs()
if s == nil {
return fuse.ENOSYS
}
*out = *s
return fuse.OK
}
func (c *rawBridge) Flush(input *fuse.FlushIn) fuse.Status {
node := c.toInode(input.NodeId)
opened := node.mount.getOpenedFile(input.Fh)
if opened != nil {
return opened.WithFlags.File.Flush()
}
return fuse.OK
}