You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/hanwen/go-fuse/v2/fuse"
  9. "time"
  10. )
  11. /**
  12. * Create and open a file
  13. *
  14. * If the file does not exist, first create it with the specified
  15. * mode, and then open it.
  16. *
  17. * If this method is not implemented or under Linux kernel
  18. * versions earlier than 2.6.15, the mknod() and open() methods
  19. * will be called instead.
  20. */
  21. func (wfs *WFS) Create(cancel <-chan struct{}, in *fuse.CreateIn, name string, out *fuse.CreateOut) (code fuse.Status) {
  22. // if implemented, need to use
  23. // inode := wfs.inodeToPath.Lookup(entryFullPath)
  24. // to ensure nlookup counter
  25. return fuse.ENOSYS
  26. }
  27. /** Create a file node
  28. *
  29. * This is called for creation of all non-directory, non-symlink
  30. * nodes. If the filesystem defines a create() method, then for
  31. * regular files that will be called instead.
  32. */
  33. func (wfs *WFS) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
  34. if s := checkName(name); s != fuse.OK {
  35. return s
  36. }
  37. newEntry := &filer_pb.Entry{
  38. Name: name,
  39. IsDirectory: false,
  40. Attributes: &filer_pb.FuseAttributes{
  41. Mtime: time.Now().Unix(),
  42. Crtime: time.Now().Unix(),
  43. FileMode: uint32(toFileMode(in.Mode) &^ wfs.option.Umask),
  44. Uid: in.Uid,
  45. Gid: in.Gid,
  46. Collection: wfs.option.Collection,
  47. Replication: wfs.option.Replication,
  48. TtlSec: wfs.option.TtlSec,
  49. },
  50. }
  51. dirFullPath := wfs.inodeToPath.GetPath(in.NodeId)
  52. entryFullPath := dirFullPath.Child(name)
  53. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  54. wfs.mapPbIdFromLocalToFiler(newEntry)
  55. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  56. request := &filer_pb.CreateEntryRequest{
  57. Directory: string(dirFullPath),
  58. Entry: newEntry,
  59. Signatures: []int32{wfs.signature},
  60. }
  61. glog.V(1).Infof("mknod: %v", request)
  62. if err := filer_pb.CreateEntry(client, request); err != nil {
  63. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  64. return err
  65. }
  66. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  67. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  68. }
  69. return nil
  70. })
  71. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  72. if err != nil {
  73. return fuse.EIO
  74. }
  75. inode := wfs.inodeToPath.Lookup(entryFullPath)
  76. wfs.outputPbEntry(out, inode, newEntry)
  77. return fuse.OK
  78. }
  79. /** Remove a file */
  80. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  81. dirFullPath := wfs.inodeToPath.GetPath(header.NodeId)
  82. entryFullPath := dirFullPath.Child(name)
  83. entry, status := wfs.maybeLoadEntry(entryFullPath)
  84. if status != fuse.OK {
  85. return status
  86. }
  87. // first, ensure the filer store can correctly delete
  88. glog.V(3).Infof("remove file: %v", entryFullPath)
  89. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  90. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  91. if err != nil {
  92. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  93. return fuse.ENOENT
  94. }
  95. // then, delete meta cache
  96. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  97. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  98. return fuse.EIO
  99. }
  100. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  101. wfs.inodeToPath.RemovePath(entryFullPath)
  102. // TODO handle open files, hardlink
  103. return fuse.OK
  104. }