-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
403 lines (333 loc) · 10.5 KB
/
node.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
package mcfs
import (
"context"
"hash/fnv"
"os"
"os/user"
"path/filepath"
"strconv"
"sync"
"syscall"
"time"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/materials-commons/hydra/pkg/clog"
"github.com/materials-commons/hydra/pkg/mcdb/mcmodel"
)
type NewFileHandleFN func(fd, flags int, path string, file *mcmodel.File) fs.FileHandle
type RootData struct {
mcfsRoot string
mcfsapi MCFSApi
uid uint32
gid uint32
newFileHandle NewFileHandleFN
mu sync.Mutex
}
type Node struct {
fs.Inode
RootData *RootData
}
func CreateFS(fsRoot string, mcfsapi MCFSApi, fn NewFileHandleFN) (*Node, error) {
u, err := user.Current()
if err != nil {
return nil, err
}
uid32, _ := strconv.ParseUint(u.Uid, 10, 32)
gid32, _ := strconv.ParseUint(u.Gid, 10, 32)
rootData := &RootData{
mcfsRoot: fsRoot,
uid: uint32(uid32),
gid: uint32(gid32),
mcfsapi: mcfsapi,
newFileHandle: fn,
}
n := &Node{RootData: rootData}
return n, nil
}
func (n *Node) newNode() *Node {
return &Node{
RootData: n.RootData,
}
}
// Readdir reads the corresponding directory and returns its files
func (n *Node) Readdir(_ context.Context) (ds fs.DirStream, errno syscall.Errno) {
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Readdir panicked")
ds = nil
errno = syscall.ENOENT
}
}()
dirPath := filepath.Join("/", n.Path(n.Root()))
files, err := n.RootData.mcfsapi.Readdir(dirPath)
if err != nil {
return nil, syscall.ENOENT
}
filesList := make([]fuse.DirEntry, 0, len(files))
for _, f := range files {
entry := fuse.DirEntry{
Mode: n.getMode(&f),
Name: f.Name,
Ino: n.inodeHash(dirPath, &f),
}
filesList = append(filesList, entry)
}
return fs.NewListDirStream(filesList), fs.OK
}
// Opendir just returns success
func (n *Node) Opendir(_ context.Context) syscall.Errno {
return fs.OK
}
// Getxattr returns extra attributes. This is used by lstat. There are no extra attributes to
// return, so we always return a 0 for buffer length and success.
func (n *Node) Getxattr(_ context.Context, _ string, _ []byte) (uint32, syscall.Errno) {
//clog.Global().Error("Node.Getxattr")
return 0, fs.OK
}
// Getattr gets attributes about the file
func (n *Node) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) (errno syscall.Errno) {
clog.Global().Debug("Node.Getattr")
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Getattr panicked")
errno = syscall.ENOENT
}
}()
// Owner is always the process the bridge is running as
out.Uid = n.RootData.uid
out.Gid = n.RootData.gid
if n.IsDir() {
now := time.Now()
out.SetTimes(&now, &now, &now)
return fs.OK
}
if fops, ok := f.(fs.FileGetattrer); ok {
return fops.Getattr(ctx, out)
}
// If we are here then f was nil, so we have to do lookups based on the path
path := filepath.Join("/", n.Path(n.Root()))
realPath, err := n.RootData.mcfsapi.GetRealPath(path)
if err != nil {
return syscall.ENOENT
}
st := syscall.Stat_t{}
if err := syscall.Lstat(realPath, &st); err != nil {
clog.Global().Errorf("Node.Getattr: Lstat failed path %s: %s", realPath, err)
//f, err := n.RootData.mcfsapi.Lookup(path)
//if err != nil {
// return syscall.ENOENT
//}
out.Size = uint64(0)
return fs.OK
}
out.FromStat(&st)
return fs.OK
}
// Lookup will return information about the current entry.
func (n *Node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (inode *fs.Inode, errno syscall.Errno) {
n.RootData.mu.Lock()
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Lookup panicked")
inode = nil
errno = syscall.ENOENT
}
n.RootData.mu.Unlock()
}()
dirPath := filepath.Join("/", n.Path(n.Root()))
path := filepath.Join(dirPath, name)
if path == "/" {
// Root dir, send back a default entry
node := n.newNode()
mode := 0755 | uint32(syscall.S_IFDIR)
return n.NewInode(ctx, node, fs.StableAttr{Mode: mode}), fs.OK
}
f, err := n.RootData.mcfsapi.Lookup(path)
if err != nil {
clog.Global().Debugf("Lookup failed %s: %s\n", path, err)
return nil, syscall.ENOENT
}
out.Uid = n.RootData.uid
out.Gid = n.RootData.gid
if f.IsFile() {
out.Size = f.Size
}
now := time.Now()
out.SetTimes(&now, &f.UpdatedAt, &now)
node := n.newNode()
return n.NewInode(ctx, node, fs.StableAttr{Mode: n.getMode(f)}), fs.OK
}
// Mkdir will create a new directory. If an attempt is made to create an existing directory then it will return
// the existing directory rather than returning an error.
func (n *Node) Mkdir(ctx context.Context, name string, _ uint32, out *fuse.EntryOut) (inode *fs.Inode, errno syscall.Errno) {
n.RootData.mu.Lock()
defer func() {
if r := recover(); r != nil {
clog.Global().Error("Node.Mkdir panicked")
inode = nil
errno = syscall.EINVAL
}
n.RootData.mu.Unlock()
}()
clog.Global().Debugf("Node.Mkdir %s", name)
path := filepath.Join("/", n.Path(n.Root()), name)
dir, err := n.RootData.mcfsapi.Mkdir(path)
if err != nil {
return nil, syscall.EINVAL
}
out.Uid = n.RootData.uid
out.Gid = n.RootData.gid
now := time.Now()
out.SetTimes(&now, &now, &now)
node := n.newNode()
return n.NewInode(ctx, node, fs.StableAttr{Mode: n.getMode(dir)}), fs.OK
}
func (n *Node) Rmdir(_ context.Context, name string) syscall.Errno {
clog.Global().Errorf("Node.Rmdir %s name %s", n.Path(n.Root()), name)
return syscall.EIO
}
// Create will create a new file. At this point the file shouldn't exist. However, because multiple users could be
// uploading files, there is a chance it does exist. If that happens then a new version of the file is created instead.
func (n *Node) Create(ctx context.Context, name string, flags uint32, mode uint32, out *fuse.EntryOut) (inode *fs.Inode, fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Create panicked")
inode = nil
fh = nil
fuseFlags = 0
errno = syscall.EIO
}
}()
fpath := filepath.Join("/", n.Path(n.Root()), name)
f, err := n.RootData.mcfsapi.Create(fpath)
if err != nil {
clog.Global().Errorf("Node.Create - failed creating new file %s: %s\n", fpath, err)
return nil, nil, 0, syscall.EIO
}
flags = flags &^ syscall.O_APPEND
fd, err := syscall.Open(f.ToUnderlyingFilePath(n.RootData.mcfsRoot), int(flags)|os.O_CREATE, mode)
if err != nil {
clog.Global().Errorf("Node.Create - syscall.Open failed %s: %s\n", f.ToUnderlyingFilePath(n.RootData.mcfsRoot), err)
return nil, nil, 0, syscall.EIO
}
statInfo := syscall.Stat_t{}
if err := syscall.Fstat(fd, &statInfo); err != nil {
// TODO - Remove newly created file version in db
_ = syscall.Close(fd)
return nil, nil, 0, fs.ToErrno(err)
}
node := n.newNode()
out.FromStat(&statInfo)
fhandle := n.RootData.newFileHandle(fd, int(flags), fpath, f)
stableAttr := fs.StableAttr{Mode: n.getMode(f)}
return n.NewInode(ctx, node, stableAttr), fhandle, 0, fs.OK
}
// Open will open an existing file. It will create a new file if the underlying mcfsapi.Open indicates
// a new version was created.
func (n *Node) Open(_ context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Open panicked")
fh = nil
fuseFlags = 0
errno = syscall.EIO
}
}()
path := filepath.Join("/", n.Path(n.Root()))
clog.Global().Debugf("Node.Open %s", path)
omode := flags & syscall.O_ACCMODE
f, isNewFile, err := n.RootData.mcfsapi.Open(path, int(flags))
if err != nil {
return nil, 0, syscall.EIO
}
if omode == syscall.O_WRONLY || omode == syscall.O_RDWR {
if isNewFile {
flags = flags &^ syscall.O_CREAT
}
}
filePath := f.ToUnderlyingFilePath(n.RootData.mcfsRoot)
clog.Global().Debugf("call syscall.Open (%s) with flags (%v) %s\n", path, flags, filePath)
fd, err := syscall.Open(filePath, int(flags), 0755)
if err != nil {
clog.Global().Debugf("syscall.Open (%s) (%v) %s: %s\n", path, flags, filePath, err)
return nil, 0, fs.ToErrno(err)
}
fhandle := n.RootData.newFileHandle(fd, int(flags), path, f)
return fhandle, 0, fs.OK
}
func (n *Node) Setattr(ctx context.Context, f fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) (errno syscall.Errno) {
defer func() {
if r := recover(); r != nil {
// If there is a panic then for now say that we don't support this call
clog.Global().Errorf("Node.Setattr panicked")
errno = syscall.ENOTSUP
}
}()
path := filepath.Join("/", n.Path(n.Root()))
clog.Global().Debugf("Node.Setattr %s", path)
if fops, ok := f.(fs.FileSetattrer); ok {
return fops.Setattr(ctx, in, out)
}
// If we are then the file handle is null, so we have to do this by
// getting paths. This will fail if the file is not known.
realPath, err := n.RootData.mcfsapi.GetKnownFileRealPath(path)
if err != nil {
return syscall.ENOENT
}
if sz, ok := in.GetSize(); ok {
if err := syscall.Truncate(realPath, int64(sz)); err != nil {
return fs.ToErrno(err)
}
st := syscall.Stat_t{}
if err := syscall.Lstat(realPath, &st); err != nil {
return fs.ToErrno(err)
}
out.FromStat(&st)
return fs.OK
}
return syscall.ENOTSUP
}
func (n *Node) Rename(_ context.Context, _ string, _ fs.InodeEmbedder, _ string, _ uint32) syscall.Errno {
clog.Global().Error("Node.Rename")
return syscall.EPERM
}
func (n *Node) Unlink(_ context.Context, name string) syscall.Errno {
clog.Global().Errorf("Node.Unlink %s, name %s", n.Path(n.Root()), name)
return syscall.EPERM
}
func (n *Node) Statfs(_ context.Context, out *fuse.StatfsOut) (errno syscall.Errno) {
defer func() {
if r := recover(); r != nil {
clog.Global().Errorf("Node.Statfs panicked")
// If there is a panic then for now just return success
errno = fs.OK
}
}()
s := syscall.Statfs_t{}
if err := syscall.Statfs(n.RootData.mcfsRoot, &s); err != nil {
return fs.ToErrno(err)
}
out.FromStatfsT(&s)
return fs.OK
}
// getMode returns the mode for the file. It checks if the underlying mcmodel.File is
// a file or directory entry.
func (n *Node) getMode(entry *mcmodel.File) uint32 {
if entry == nil {
return 0755 | uint32(syscall.S_IFDIR)
}
if entry.IsDir() {
return 0755 | uint32(syscall.S_IFDIR)
}
return 0644 | uint32(syscall.S_IFREG)
}
// inodeHash creates a new inode id from the file path.
func (n *Node) inodeHash(dirPath string, entry *mcmodel.File) uint64 {
if entry == nil {
return 1
}
h := fnv.New64a()
p := filepath.Join(dirPath, entry.FullPath())
_, _ = h.Write([]byte(p))
return h.Sum64()
}