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.

175 lines
5.0 KiB

5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer"
  6. "math"
  7. "github.com/seaweedfs/fuse"
  8. "github.com/seaweedfs/fuse/fs"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
  14. newDir := newDirectory.(*Dir)
  15. newPath := util.NewFullPath(newDir.FullPath(), req.NewName)
  16. oldPath := util.NewFullPath(dir.FullPath(), req.OldName)
  17. glog.V(4).Infof("dir Rename %s => %s", oldPath, newPath)
  18. // find local old entry
  19. oldEntry, err := dir.wfs.metaCache.FindEntry(context.Background(), oldPath)
  20. if err != nil {
  21. glog.Errorf("dir Rename can not find source %s : %v", oldPath, err)
  22. return fuse.ENOENT
  23. }
  24. // update remote filer
  25. err = dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  26. ctx, cancel := context.WithCancel(context.Background())
  27. defer cancel()
  28. request := &filer_pb.AtomicRenameEntryRequest{
  29. OldDirectory: dir.FullPath(),
  30. OldName: req.OldName,
  31. NewDirectory: newDir.FullPath(),
  32. NewName: req.NewName,
  33. Signatures: []int32{dir.wfs.signature},
  34. }
  35. _, err := client.AtomicRenameEntry(ctx, request)
  36. if err != nil {
  37. glog.Errorf("dir AtomicRenameEntry %s => %s : %v", oldPath, newPath, err)
  38. return fuse.EXDEV
  39. }
  40. return nil
  41. })
  42. if err != nil {
  43. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  44. return fuse.EIO
  45. }
  46. err = dir.moveEntry(context.Background(), util.FullPath(dir.FullPath()), oldEntry, util.FullPath(newDir.FullPath()), req.NewName)
  47. if err != nil {
  48. glog.V(0).Infof("dir local Rename %s => %s : %v", oldPath, newPath, err)
  49. return fuse.EIO
  50. }
  51. // change file handle
  52. dir.wfs.handlesLock.Lock()
  53. defer dir.wfs.handlesLock.Unlock()
  54. inodeId := oldPath.AsInode()
  55. existingHandle, found := dir.wfs.handles[inodeId]
  56. glog.V(4).Infof("has open filehandle %s: %v", oldPath, found)
  57. if !found || existingHandle == nil {
  58. return nil
  59. }
  60. glog.V(4).Infof("opened filehandle %s => %s", oldPath, newPath)
  61. delete(dir.wfs.handles, inodeId)
  62. dir.wfs.handles[newPath.AsInode()] = existingHandle
  63. return nil
  64. }
  65. func (dir *Dir) moveEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string) error {
  66. oldName := entry.Name()
  67. if err := dir.moveSelfEntry(ctx, oldParent, entry, newParent, newName, func() error {
  68. oldFsNode := NodeWithId(oldParent.Child(oldName).AsInode())
  69. newFsNode := NodeWithId(newParent.Child(newName).AsInode())
  70. newDirNode, found := dir.wfs.Server.FindInternalNode(NodeWithId(newParent.AsInode()))
  71. var newDir *Dir
  72. if found {
  73. newDir = newDirNode.(*Dir)
  74. }
  75. dir.wfs.Server.InvalidateInternalNode(oldFsNode, newFsNode, func(internalNode fs.Node) {
  76. if file, ok := internalNode.(*File); ok {
  77. glog.V(4).Infof("internal file node %s", oldParent.Child(oldName))
  78. file.Name = newName
  79. file.id = uint64(newFsNode)
  80. if found {
  81. file.dir = newDir
  82. }
  83. }
  84. if dir, ok := internalNode.(*Dir); ok {
  85. glog.V(4).Infof("internal dir node %s", oldParent.Child(oldName))
  86. dir.name = newName
  87. dir.id = uint64(newFsNode)
  88. if found {
  89. dir.parent = newDir
  90. }
  91. }
  92. })
  93. if entry.IsDirectory() {
  94. if err := dir.moveFolderSubEntries(ctx, oldParent, oldName, newParent, newName); err != nil {
  95. return err
  96. }
  97. }
  98. return nil
  99. }); err != nil {
  100. return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(oldName), newParent.Child(newName), err)
  101. }
  102. return nil
  103. }
  104. func (dir *Dir) moveFolderSubEntries(ctx context.Context, oldParent util.FullPath, oldName string, newParent util.FullPath, newName string) error {
  105. currentDirPath := oldParent.Child(oldName)
  106. newDirPath := newParent.Child(newName)
  107. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  108. var moveErr error
  109. listErr := dir.wfs.metaCache.ListDirectoryEntries(ctx, currentDirPath, "", false, int64(math.MaxInt32), func(item *filer.Entry) bool {
  110. moveErr = dir.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name())
  111. if moveErr != nil {
  112. return false
  113. }
  114. return true
  115. })
  116. if listErr != nil {
  117. return listErr
  118. }
  119. if moveErr != nil {
  120. return moveErr
  121. }
  122. return nil
  123. }
  124. func (dir *Dir) moveSelfEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, moveFolderSubEntries func() error) error {
  125. newPath := newParent.Child(newName)
  126. oldPath := oldParent.Child(entry.Name())
  127. entry.FullPath = newPath
  128. if err := dir.wfs.metaCache.InsertEntry(ctx, entry); err != nil {
  129. glog.V(0).Infof("dir Rename insert local %s => %s : %v", oldPath, newPath, err)
  130. return fuse.EIO
  131. }
  132. if moveFolderSubEntries != nil {
  133. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  134. return moveChildrenErr
  135. }
  136. }
  137. if err := dir.wfs.metaCache.DeleteEntry(ctx, oldPath); err != nil {
  138. glog.V(0).Infof("dir Rename delete local %s => %s : %v", oldPath, newPath, err)
  139. return fuse.EIO
  140. }
  141. return nil
  142. }