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.

142 lines
3.7 KiB

3 years ago
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)),
  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, code := wfs.inodeToPath.GetPath(in.NodeId)
  52. if code != fuse.OK {
  53. return
  54. }
  55. entryFullPath := dirFullPath.Child(name)
  56. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  57. wfs.mapPbIdFromLocalToFiler(newEntry)
  58. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  59. request := &filer_pb.CreateEntryRequest{
  60. Directory: string(dirFullPath),
  61. Entry: newEntry,
  62. Signatures: []int32{wfs.signature},
  63. }
  64. glog.V(1).Infof("mknod: %v", request)
  65. if err := filer_pb.CreateEntry(client, request); err != nil {
  66. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  67. return err
  68. }
  69. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  70. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  71. }
  72. return nil
  73. })
  74. glog.V(3).Infof("mknod %s: %v", entryFullPath, err)
  75. if err != nil {
  76. return fuse.EIO
  77. }
  78. inode := wfs.inodeToPath.Lookup(entryFullPath, false, true)
  79. wfs.outputPbEntry(out, inode, newEntry)
  80. return fuse.OK
  81. }
  82. /** Remove a file */
  83. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  84. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  85. if code != fuse.OK {
  86. if code == fuse.ENOENT {
  87. return fuse.OK
  88. }
  89. return code
  90. }
  91. entryFullPath := dirFullPath.Child(name)
  92. entry, code := wfs.maybeLoadEntry(entryFullPath)
  93. if code != fuse.OK {
  94. if code == fuse.ENOENT {
  95. return fuse.OK
  96. }
  97. return code
  98. }
  99. // first, ensure the filer store can correctly delete
  100. glog.V(3).Infof("remove file: %v", entryFullPath)
  101. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  102. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  103. if err != nil {
  104. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  105. return fuse.OK
  106. }
  107. // then, delete meta cache
  108. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  109. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  110. return fuse.EIO
  111. }
  112. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  113. wfs.inodeToPath.RemovePath(entryFullPath)
  114. return fuse.OK
  115. }