weedfs_dir_mkrm.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/hanwen/go-fuse/v2/fuse"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/glog"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. "os"
  10. "strings"
  11. "syscall"
  12. "time"
  13. )
  14. /** Create a directory
  15. *
  16. * Note that the mode argument may not have the type specification
  17. * bits set, i.e. S_ISDIR(mode) can be false. To obtain the
  18. * correct directory type bits use mode|S_IFDIR
  19. * */
  20. func (wfs *WFS) Mkdir(cancel <-chan struct{}, in *fuse.MkdirIn, name string, out *fuse.EntryOut) (code fuse.Status) {
  21. if wfs.IsOverQuota {
  22. return fuse.Status(syscall.ENOSPC)
  23. }
  24. if s := checkName(name); s != fuse.OK {
  25. return s
  26. }
  27. newEntry := &filer_pb.Entry{
  28. Name: name,
  29. IsDirectory: true,
  30. Attributes: &filer_pb.FuseAttributes{
  31. Mtime: time.Now().Unix(),
  32. Crtime: time.Now().Unix(),
  33. FileMode: uint32(os.ModeDir) | in.Mode&^uint32(wfs.option.Umask),
  34. Uid: in.Uid,
  35. Gid: in.Gid,
  36. },
  37. }
  38. dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  39. if code != fuse.OK {
  40. return
  41. }
  42. entryFullPath := dirFullPath.Child(name)
  43. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  44. wfs.mapPbIdFromLocalToFiler(newEntry)
  45. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  46. request := &filer_pb.CreateEntryRequest{
  47. Directory: string(dirFullPath),
  48. Entry: newEntry,
  49. Signatures: []int32{wfs.signature},
  50. }
  51. glog.V(1).Infof("mkdir: %v", request)
  52. if err := filer_pb.CreateEntry(client, request); err != nil {
  53. glog.V(0).Infof("mkdir %s: %v", entryFullPath, err)
  54. return err
  55. }
  56. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  57. return fmt.Errorf("local mkdir dir %s: %v", entryFullPath, err)
  58. }
  59. return nil
  60. })
  61. glog.V(3).Infof("mkdir %s: %v", entryFullPath, err)
  62. if err != nil {
  63. return fuse.EIO
  64. }
  65. inode := wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, true, false, 0, true)
  66. wfs.outputPbEntry(out, inode, newEntry)
  67. return fuse.OK
  68. }
  69. /** Remove a directory */
  70. func (wfs *WFS) Rmdir(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  71. if name == "." {
  72. return fuse.Status(syscall.EINVAL)
  73. }
  74. if name == ".." {
  75. return fuse.Status(syscall.ENOTEMPTY)
  76. }
  77. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  78. if code != fuse.OK {
  79. return
  80. }
  81. entryFullPath := dirFullPath.Child(name)
  82. glog.V(3).Infof("remove directory: %v", entryFullPath)
  83. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  84. err := filer_pb.Remove(wfs, string(dirFullPath), name, true, false, ignoreRecursiveErr, false, []int32{wfs.signature})
  85. if err != nil {
  86. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  87. if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  88. return fuse.Status(syscall.ENOTEMPTY)
  89. }
  90. return fuse.ENOENT
  91. }
  92. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  93. wfs.inodeToPath.RemovePath(entryFullPath)
  94. return fuse.OK
  95. }