weedfs_dir_mkrm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. "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 s := checkName(name); s != fuse.OK {
  22. return s
  23. }
  24. newEntry := &filer_pb.Entry{
  25. Name: name,
  26. IsDirectory: true,
  27. Attributes: &filer_pb.FuseAttributes{
  28. Mtime: time.Now().Unix(),
  29. Crtime: time.Now().Unix(),
  30. FileMode: uint32(os.ModeDir) | in.Mode&^uint32(wfs.option.Umask),
  31. Uid: in.Uid,
  32. Gid: in.Gid,
  33. },
  34. }
  35. dirFullPath, code := wfs.inodeToPath.GetPath(in.NodeId)
  36. if code != fuse.OK {
  37. return
  38. }
  39. entryFullPath := dirFullPath.Child(name)
  40. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  41. wfs.mapPbIdFromLocalToFiler(newEntry)
  42. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  43. request := &filer_pb.CreateEntryRequest{
  44. Directory: string(dirFullPath),
  45. Entry: newEntry,
  46. Signatures: []int32{wfs.signature},
  47. }
  48. glog.V(1).Infof("mkdir: %v", request)
  49. if err := filer_pb.CreateEntry(client, request); err != nil {
  50. glog.V(0).Infof("mkdir %s: %v", entryFullPath, err)
  51. return err
  52. }
  53. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  54. return fmt.Errorf("local mkdir dir %s: %v", entryFullPath, err)
  55. }
  56. return nil
  57. })
  58. glog.V(3).Infof("mkdir %s: %v", entryFullPath, err)
  59. if err != nil {
  60. return fuse.EIO
  61. }
  62. inode := wfs.inodeToPath.Lookup(entryFullPath, os.ModeDir, true, 0, true)
  63. wfs.outputPbEntry(out, inode, newEntry)
  64. return fuse.OK
  65. }
  66. /** Remove a directory */
  67. func (wfs *WFS) Rmdir(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  68. if name == "." {
  69. return fuse.Status(syscall.EINVAL)
  70. }
  71. if name == ".." {
  72. return fuse.Status(syscall.ENOTEMPTY)
  73. }
  74. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  75. if code != fuse.OK {
  76. return
  77. }
  78. entryFullPath := dirFullPath.Child(name)
  79. glog.V(3).Infof("remove directory: %v", entryFullPath)
  80. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  81. err := filer_pb.Remove(wfs, string(dirFullPath), name, true, true, ignoreRecursiveErr, false, []int32{wfs.signature})
  82. if err != nil {
  83. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  84. if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  85. return fuse.Status(syscall.ENOTEMPTY)
  86. }
  87. return fuse.ENOENT
  88. }
  89. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  90. wfs.inodeToPath.RemovePath(entryFullPath)
  91. return fuse.OK
  92. }