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.

111 lines
2.8 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
  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 := wfs.inodeToPath.GetPath(in.NodeId)
  36. entryFullPath := dirFullPath.Child(name)
  37. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  38. wfs.mapPbIdFromLocalToFiler(newEntry)
  39. defer wfs.mapPbIdFromFilerToLocal(newEntry)
  40. request := &filer_pb.CreateEntryRequest{
  41. Directory: string(dirFullPath),
  42. Entry: newEntry,
  43. Signatures: []int32{wfs.signature},
  44. }
  45. glog.V(1).Infof("mkdir: %v", request)
  46. if err := filer_pb.CreateEntry(client, request); err != nil {
  47. glog.V(0).Infof("mkdir %s: %v", entryFullPath, err)
  48. return err
  49. }
  50. if err := wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry)); err != nil {
  51. return fmt.Errorf("local mkdir dir %s: %v", entryFullPath, err)
  52. }
  53. return nil
  54. })
  55. glog.V(0).Infof("mkdir %s: %v", entryFullPath, err)
  56. if err != nil {
  57. return fuse.EIO
  58. }
  59. inode := wfs.inodeToPath.Lookup(entryFullPath, true)
  60. wfs.outputPbEntry(out, inode, newEntry)
  61. return fuse.OK
  62. }
  63. /** Remove a directory */
  64. func (wfs *WFS) Rmdir(cancel <-chan struct{}, header *fuse.InHeader, name string) (code fuse.Status) {
  65. if name == "." {
  66. return fuse.Status(syscall.EINVAL)
  67. }
  68. if name == ".." {
  69. return fuse.Status(syscall.ENOTEMPTY)
  70. }
  71. dirFullPath := wfs.inodeToPath.GetPath(header.NodeId)
  72. entryFullPath := dirFullPath.Child(name)
  73. glog.V(3).Infof("remove directory: %v", entryFullPath)
  74. ignoreRecursiveErr := true // ignore recursion error since the OS should manage it
  75. err := filer_pb.Remove(wfs, string(dirFullPath), name, true, true, ignoreRecursiveErr, false, []int32{wfs.signature})
  76. if err != nil {
  77. glog.V(0).Infof("remove %s: %v", entryFullPath, err)
  78. if strings.Contains(err.Error(), filer.MsgFailDelNonEmptyFolder) {
  79. return fuse.Status(syscall.ENOTEMPTY)
  80. }
  81. return fuse.ENOENT
  82. }
  83. wfs.metaCache.DeleteEntry(context.Background(), entryFullPath)
  84. wfs.inodeToPath.RemovePath(entryFullPath)
  85. return fuse.OK
  86. }