-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
527 lines (444 loc) · 15.3 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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
package mcbridgefs
import (
"context"
"fmt"
"hash/fnv"
"mime"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/apex/log"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/materials-commons/gomcdb/mcmodel"
"github.com/materials-commons/mcbridgefs/pkg/fs/bridgefs"
"gorm.io/gorm"
)
type Node struct {
file *mcmodel.File
*bridgefs.BridgeNode
}
var (
uid, gid uint32
mcfsRoot string
db *gorm.DB
transferRequest mcmodel.TransferRequest
openedFilesTracker *OpenFilesTracker
txRetryCount int
fileStore *FileStore
)
func init() {
// Get current user so we can set the uid and gid to use
u, err := user.Current()
if err != nil {
panic(err)
}
uid32, _ := strconv.ParseUint(u.Uid, 10, 32)
gid32, _ := strconv.ParseUint(u.Gid, 10, 32)
uid = uint32(uid32)
gid = uint32(gid32)
// All updates and creates to the database are wrapped in a transaction. These transactions may need to be
// retried, especially when they fail because two transactions are deadlocked trying to acquire a lock on
// a foreign table reference.
txRetryCount64, err := strconv.ParseInt(os.Getenv("MC_TX_RETRY"), 10, 32)
if err != nil || txRetryCount64 < 3 {
txRetryCount64 = 3
}
txRetryCount = int(txRetryCount64)
// Track any files that this instance writes to/create, so that if another instance does the same
// each of them will see their versions of the file, rather than intermixing them.
openedFilesTracker = NewOpenFilesTracker()
}
func CreateFS(fsRoot string, dB *gorm.DB, tr mcmodel.TransferRequest) *Node {
mcfsRoot = fsRoot
db = dB
transferRequest = tr
fileStore = NewFileStore(dB, fsRoot, &transferRequest)
return rootNode()
}
func rootNode() *Node {
bridgeRoot, err := bridgefs.NewBridgeRoot(os.Getenv("MCFS_DIR"), nil, nil)
if err != nil {
log.Fatalf("Failed to create root node: %s", err)
}
return &Node{
BridgeNode: bridgeRoot.(*bridgefs.BridgeNode),
}
}
func (n *Node) newNode() *Node {
return &Node{
BridgeNode: bridgefs.NewBridgeNode(n.BridgeNode).(*bridgefs.BridgeNode),
}
}
// Readdir reads the corresponding directory and returns its entries
func (n *Node) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
// Directories can have a large amount of files. To speed up processing
// Readdir uses queries that don't retrieve either the underlying directory
// for a mcmodel.File, or the underlying file for a mcmodel.TransferRequestFile.
// However, the path for the directory is still needed. This is accessed
// off of the underlying mcmodel.File by the FullPath() routine which is
// used the inodeHash() and getMode() methods. To work around this we
// create a single directory (see dirToUse below), and assign this as the
// directory for all mcmodel.File entries.
dirPath := filepath.Join("/", n.Path(n.Root()))
dirToUse := &mcmodel.File{Path: dirPath}
dir, err := n.getMCDir("")
if err != nil {
return nil, syscall.ENOENT
}
files, err := fileStore.ListDirectory(dir)
if err != nil {
return nil, syscall.ENOENT
}
filesList := make([]fuse.DirEntry, 0, len(files))
for _, f := range files {
f.Directory = dirToUse
entry := fuse.DirEntry{
Mode: n.getMode(&f),
Name: f.Name,
Ino: n.inodeHash(&f),
}
filesList = append(filesList, entry)
}
return fs.NewListDirStream(filesList), fs.OK
}
// Opendir just returns success
func (n *Node) Opendir(ctx 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(ctx context.Context, attr string, dest []byte) (uint32, syscall.Errno) {
//fmt.Println("Getxattr")
return 0, fs.OK
}
// Getattr gets attributes about the file
func (n *Node) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
//fmt.Println("Getattr:", n.Path(n.Root()), n.IsDir())
// Owner is always the process the bridge is running as
out.Uid = uid
out.Gid = gid
if n.IsDir() {
now := time.Now()
out.SetTimes(&now, &now, &now)
return fs.OK
}
file, err := fileStore.GetFileByPath(filepath.Join("/", n.Path(n.Root())))
if err != nil {
log.Errorf("Getattr: GetFileByPath failed (%s): %s\n", filepath.Join("/", n.Path(n.Root())), err)
return syscall.ENOENT
}
st := syscall.Stat_t{}
if err := syscall.Lstat(file.ToUnderlyingFilePath(mcfsRoot), &st); err != nil {
log.Errorf("Getattr: Lstat failed (%s): %s\n", file.ToUnderlyingFilePath(mcfsRoot), err)
return fs.ToErrno(err)
}
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) (*fs.Inode, syscall.Errno) {
path := filepath.Join("/", n.Path(n.Root()), name)
f, err := fileStore.GetFileByPath(path)
if err != nil {
return nil, syscall.ENOENT
}
out.Uid = uid
out.Gid = gid
if f.IsFile() {
out.Size = f.Size
}
now := time.Now()
out.SetTimes(&now, &f.UpdatedAt, &now)
node := n.newNode()
node.file = f
return n.NewInode(ctx, node, fs.StableAttr{Mode: n.getMode(f), Ino: n.inodeHash(f)}), fs.OK
}
// getMCDir looks a directory up in the database.
func (n *Node) getMCDir(name string) (*mcmodel.File, error) {
path := filepath.Join("/", n.Path(n.Root()), name)
return fileStore.FindDirByPath(transferRequest.ProjectID, path)
}
// 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, mode uint32, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
path := filepath.Join("/", n.Path(n.Root()), name)
parent, err := n.getMCDir("")
if err != nil {
return nil, syscall.EINVAL
}
dir, err := fileStore.CreateDirectory(parent.ID, path, name)
if err != nil {
return nil, syscall.EINVAL
}
out.Uid = uid
out.Gid = gid
now := time.Now()
out.SetTimes(&now, &now, &now)
node := n.newNode()
node.file = dir
return n.NewInode(ctx, node, fs.StableAttr{Mode: n.getMode(dir), Ino: n.inodeHash(dir)}), fs.OK
}
func (n *Node) Rmdir(ctx context.Context, name string) syscall.Errno {
fmt.Printf("Rmdir %s/%s\n", 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) {
f, err := n.createNewMCFile(name)
if err != nil {
log.Errorf("Create - failed creating new file (%s): %s", name, err)
return nil, nil, 0, syscall.EIO
}
path := filepath.Join("/", n.Path(n.Root()), name)
openedFilesTracker.Store(path, f)
flags = flags &^ syscall.O_APPEND
fd, err := syscall.Open(f.ToUnderlyingFilePath(mcfsRoot), int(flags)|os.O_CREATE, mode)
if err != nil {
log.Errorf(" Create - syscall.Open failed:", 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()
node.file = f
out.FromStat(&statInfo)
return n.NewInode(ctx, node, fs.StableAttr{Mode: n.getMode(f), Ino: n.inodeHash(f)}), NewFileHandle(fd, flags, path), 0, fs.OK
}
// Open will open an existing file.
func (n *Node) Open(ctx context.Context, flags uint32) (fh fs.FileHandle, fuseFlags uint32, errno syscall.Errno) {
var (
err error
newFile *mcmodel.File
)
path := filepath.Join("/", n.Path(n.Root()))
switch flags & syscall.O_ACCMODE {
case syscall.O_RDONLY:
newFile = getFromOpenedFiles(path)
case syscall.O_WRONLY:
newFile = getFromOpenedFiles(path)
if newFile == nil {
newFile, err = n.createNewMCFileVersion()
if err != nil {
// TODO: What error should be returned?
return nil, 0, syscall.EIO
}
openedFilesTracker.Store(path, newFile)
}
flags = flags &^ syscall.O_CREAT
flags = flags &^ syscall.O_APPEND
case syscall.O_RDWR:
newFile = getFromOpenedFiles(path)
if newFile == nil {
newFile, err = n.createNewMCFileVersion()
if err != nil {
// TODO: What error should be returned?
return nil, 0, syscall.EIO
}
openedFilesTracker.Store(path, newFile)
}
flags = flags &^ syscall.O_CREAT
flags = flags &^ syscall.O_APPEND
default:
return
}
filePath := n.file.ToUnderlyingFilePath(mcfsRoot)
if newFile != nil {
filePath = newFile.ToUnderlyingFilePath(mcfsRoot)
}
fd, err := syscall.Open(filePath, int(flags), 0)
if err != nil {
return nil, 0, fs.ToErrno(err)
}
fhandle := NewFileHandle(fd, flags, path)
return fhandle, 0, fs.OK
}
// Setattr will set attributes on a file. Currently the only attribute supported is setting the size. This is
// done by calling Ftruncate.
func (n *Node) Setattr(ctx context.Context, f fs.FileHandle, in *fuse.SetAttrIn, out *fuse.AttrOut) syscall.Errno {
if sz, ok := in.GetSize(); ok {
fh := f.(*FileHandle)
return fs.ToErrno(syscall.Ftruncate(fh.Fd, int64(sz)))
}
return fs.OK
}
// Release will close the file handle and update meta data about the file in the database
func (n *Node) Release(ctx context.Context, f fs.FileHandle) syscall.Errno {
bridgeFH, ok := f.(fs.FileReleaser)
if !ok {
return syscall.EINVAL
}
// Call the underling fileHandle to close the actual file
if err := bridgeFH.Release(ctx); err != fs.OK {
return err
}
// If the file was opened only for read then there is no meta data that needs to be updated.
fh := bridgeFH.(*FileHandle)
if fh.Flags&syscall.O_ACCMODE == syscall.O_RDONLY {
return fs.OK
}
// If we are here then the file was opened with a write flag. In this case we need to update the
// file size, set this as the current file, and if a new checksum was computed, set the checksum.
// TODO: is n.file even valid anymore?
fileToUpdate := n.file
fpath := filepath.Join("/", n.Path(n.Root()))
nf := openedFilesTracker.Get(fpath)
if nf != nil && nf.File != nil {
fileToUpdate = nf.File
}
var checksum string
if nf != nil {
checksum = fmt.Sprintf("%x", nf.hasher.Sum(nil))
}
return fs.ToErrno(fileStore.MarkFileReleased(fileToUpdate, checksum))
}
// createNewMCFileVersion creates a new file version if there isn't already a version of the file
// file associated with this transfer request instance. It checks the openedFilesTracker to determine
// if a new version has already been created. If a new version was already created then it will return
// that version. Otherwise it will create a new version and add it to the OpenedFilesTracker. In
// addition when a new version is created, the associated on disk directory is created and an empty
// file is written to it.
func (n *Node) createNewMCFileVersion() (*mcmodel.File, error) {
// First check if there is already a version of this file being written to for this upload context.
existing := getFromOpenedFiles(filepath.Join("/", n.Path(n.Root()), n.file.Name))
if existing != nil {
return existing, nil
}
var err error
// There isn't an existing upload, so create a new one
newFile := &mcmodel.File{
ProjectID: n.file.ProjectID,
Name: n.file.Name,
DirectoryID: n.file.DirectoryID,
Size: 0,
Checksum: "",
MimeType: n.file.MimeType,
OwnerID: n.file.OwnerID,
Current: false,
}
newFile, err = fileStore.CreateNewFile(newFile, n.file.Directory)
if err != nil {
return nil, err
}
// Create the empty file for new version
f, err := os.OpenFile(newFile.ToUnderlyingFilePath(mcfsRoot), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Errorf("os.OpenFile failed (%s): %s\n", newFile.ToUnderlyingFilePath(mcfsRoot), err)
return nil, err
}
defer f.Close()
return newFile, nil
}
// createNewMCFile will create a new mcmodel.File entry for the directory associated
// with the Node. It will create the directory where the file can be written to.
func (n *Node) createNewMCFile(name string) (*mcmodel.File, error) {
dir, err := n.getMCDir("")
if err != nil {
return nil, err
}
file := &mcmodel.File{
ProjectID: transferRequest.ProjectID,
Name: name,
DirectoryID: dir.ID,
Size: 0,
Checksum: "",
MimeType: getMimeType(name),
OwnerID: transferRequest.OwnerID,
Current: false,
}
return fileStore.CreateNewFile(file, dir)
}
// getMimeType will determine the type of a file from its extension. It strips out the extra information
// such as the charset and just returns the underlying type. It returns "unknown" for the mime type if
// the mime package is unable to determine the type.
func getMimeType(name string) string {
mimeType := mime.TypeByExtension(filepath.Ext(name))
if mimeType == "" {
return "unknown"
}
semicolon := strings.Index(mimeType, ";")
if semicolon == -1 {
return strings.TrimSpace(mimeType)
}
return strings.TrimSpace(mimeType[:semicolon])
}
func (n *Node) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno {
fmt.Printf("Rename: %s/%s to %s/%s\n", n.Path(n.Root()), name, newParent.EmbeddedInode().Path(n.Root()), newName)
fromPath := filepath.Join("/", n.Path(n.Root()))
toPath := filepath.Join("/", newParent.EmbeddedInode().Path(n.Root()))
dir, err := n.getMCDir("")
if err != nil {
return syscall.ENOENT
}
var f mcmodel.File
err = db.Preload("Directory").
Where("directory_id = ?", dir.ID).
Where("project_id = ?", transferRequest.ProjectID).
Where("name = ?", name).
Where("current = ?", true).
Find(&f).Error
switch {
case err != nil:
return syscall.ENOENT
case f.IsDir():
return n.renameDir(fromPath, toPath, name, newName, f)
default:
// f is a file
return n.renameFile(fromPath, toPath, name, newName, f)
}
}
func (n *Node) renameDir(fromPath, toPath, name, toName string, f mcmodel.File) syscall.Errno {
if fromPath == toPath {
// not being moved to another directory. Just rename directory and all descendant directory
// paths
}
return syscall.EPERM
}
func (n *Node) renameFile(fromPath, toPath, name, toName string, f mcmodel.File) syscall.Errno {
if fromPath == toPath {
// not being moved to another directory. Just rename file and all its previous versions
}
return syscall.EPERM
}
func (n *Node) Unlink(ctx context.Context, name string) syscall.Errno {
fmt.Printf("Unlink: %s/%s\n", n.Path(n.Root()), name)
return syscall.EPERM
}
// 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 the file path.
func (n *Node) inodeHash(entry *mcmodel.File) uint64 {
if entry == nil {
return 1
}
h := fnv.New64a()
_, _ = h.Write([]byte(entry.FullPath()))
return h.Sum64()
}
// getFromOpenedFiles returns the mcmodel.File from the openedFilesTracker. It handles
// the case where the path wasn't found.
func getFromOpenedFiles(path string) *mcmodel.File {
val := openedFilesTracker.Get(path)
if val != nil {
return val.File
}
return nil
}