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.

129 lines
3.5 KiB

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. return fuse.ENOSYS
  23. }
  24. /** Create a file node
  25. *
  26. * This is called for creation of all non-directory, non-symlink
  27. * nodes. If the filesystem defines a create() method, then for
  28. * regular files that will be called instead.
  29. */
  30. func (wfs *WFS) Mknod(cancel <-chan struct{}, in *fuse.MknodIn, name string, out *fuse.EntryOut) (code fuse.Status) {
  31. if s := checkName(name); s != fuse.OK {
  32. return s
  33. }
  34. newEntry := &filer_pb.Entry{
  35. Name: name,
  36. IsDirectory: false,
  37. Attributes: &filer_pb.FuseAttributes{
  38. Mtime: time.Now().Unix(),
  39. Crtime: time.Now().Unix(),
  40. FileMode: uint32(toFileMode(in.Mode) &^ wfs.option.Umask),
  41. Uid: in.Uid,
  42. Gid: in.Gid,
  43. Collection: wfs.option.Collection,
  44. Replication: wfs.option.Replication,
  45. TtlSec: wfs.option.TtlSec,
  46. },
  47. }
  48. dirFullPath := wfs.inodeToPath.GetPath(in.NodeId)
  49. entryFullPath := dirFullPath.Child(name)
  50. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  51. wfs.mapPbIdFromLocalToFiler(newEntry)
  52. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  53. request := &filer_pb.CreateEntryRequest{
  54. Directory: string(dirFullPath),
  55. Entry: newEntry,
  56. Signatures: []int32{wfs.signature},
  57. }
  58. glog.V(1).Infof("mknod: %v", request)
  59. if err := filer_pb.CreateEntry(client, request); err != nil {
  60. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  61. return err
  62. }
  63. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  64. return fmt.Errorf("local mknod %s: %v", entryFullPath, err)
  65. }
  66. return nil
  67. })
  68. glog.V(0).Infof("mknod %s: %v", entryFullPath, err)
  69. if err != nil {
  70. return fuse.EIO
  71. }
  72. inode := wfs.inodeToPath.Lookup(entryFullPath)
  73. wfs.outputPbEntry(out, inode, newEntry)
  74. return fuse.OK
  75. }
  76. /** Remove a file */
  77. func (wfs *WFS) Unlink(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  78. dirFullPath := wfs.inodeToPath.GetPath(header.NodeId)
  79. entryFullPath := dirFullPath.Child(name)
  80. entry, status := wfs.maybeLoadEntry(entryFullPath)
  81. if status != fuse.OK {
  82. return status
  83. }
  84. // first, ensure the filer store can correctly delete
  85. glog.V(3).Infof("remove file: %v", entryFullPath)
  86. isDeleteData := entry != nil && entry.HardLinkCounter <= 1
  87. err := filer_pb.Remove(wfs, string(dirFullPath), name, isDeleteData, false, false, false, []int32{wfs.signature})
  88. if err != nil {
  89. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  90. return fuse.ENOENT
  91. }
  92. // then, delete meta cache
  93. if err = wfs.metaCache.DeleteEntry(context.Background(), entryFullPath); err != nil {
  94. glog.V(3).Infof("local DeleteEntry %s: %v", entryFullPath, err)
  95. return fuse.EIO
  96. }
  97. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  98. wfs.inodeToPath.RemovePath(entryFullPath)
  99. // TODO handle open files, hardlink
  100. return fuse.OK
  101. }