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.

124 lines
3.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
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. "os"
  6. "strings"
  7. "syscall"
  8. "time"
  9. "github.com/hanwen/go-fuse/v2/fuse"
  10. "github.com/seaweedfs/seaweedfs/weed/filer"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  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. SkipCheckParentDirectory: true,
  51. }
  52. glog.V(1).Infof("mkdir: %v", request)
  53. if err := filer_pb.CreateEntry(client, request); err != nil {
  54. glog.V(0).Infof("mkdir %s: %v", entryFullPath, err)
  55. return err
  56. }
  57. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  58. return fmt.Errorf("local mkdir dir %s: %v", entryFullPath, err)
  59. }
  60. return nil
  61. })
  62. glog.V(3).Infof("mkdir %s: %v", entryFullPath, err)
  63. if err != nil {
  64. return fuse.EIO
  65. }
  66. inode := wfs.inodeToPath.Lookup(entryFullPath, newEntry.Attributes.Crtime, true, false, 0, true)
  67. wfs.outputPbEntry(out, inode, newEntry)
  68. return fuse.OK
  69. }
  70. /** Remove a directory */
  71. func (wfs *WFS) Rmdir(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  72. if name == "." {
  73. return fuse.Status(syscall.EINVAL)
  74. }
  75. if name == ".." {
  76. return fuse.Status(syscall.ENOTEMPTY)
  77. }
  78. dirFullPath, code := wfs.inodeToPath.GetPath(header.NodeId)
  79. if code != fuse.OK {
  80. return
  81. }
  82. entryFullPath := dirFullPath.Child(name)
  83. glog.V(3).Infof("remove directory: %v", entryFullPath)
  84. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  85. err := filer_pb.Remove(wfs, string(dirFullPath), name, true, false, ignoreRecursiveErr, false, []int32{wfs.signature})
  86. if err != nil {
  87. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  88. if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  89. return fuse.Status(syscall.ENOTEMPTY)
  90. }
  91. return fuse.ENOENT
  92. }
  93. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  94. wfs.inodeToPath.RemovePath(entryFullPath)
  95. return fuse.OK
  96. }