weedfs_file_mkrm.go 3.8 KB

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