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.

130 lines
3.8 KiB

5 years ago
5 years ago
4 years ago
3 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/seaweedfs/fuse"
  6. "github.com/seaweedfs/fuse/fs"
  7. "io"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
  13. newDir := newDirectory.(*Dir)
  14. newPath := util.NewFullPath(newDir.FullPath(), req.NewName)
  15. oldPath := util.NewFullPath(dir.FullPath(), req.OldName)
  16. glog.V(4).Infof("dir Rename %s => %s", oldPath, newPath)
  17. // update remote filer
  18. err := dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  19. ctx, cancel := context.WithCancel(context.Background())
  20. defer cancel()
  21. request := &filer_pb.StreamRenameEntryRequest{
  22. OldDirectory: dir.FullPath(),
  23. OldName: req.OldName,
  24. NewDirectory: newDir.FullPath(),
  25. NewName: req.NewName,
  26. Signatures: []int32{dir.wfs.signature},
  27. }
  28. stream, err := client.StreamRenameEntry(ctx, request)
  29. if err != nil {
  30. glog.Errorf("dir AtomicRenameEntry %s => %s : %v", oldPath, newPath, err)
  31. return fuse.EXDEV
  32. }
  33. for {
  34. resp, recvErr := stream.Recv()
  35. if recvErr != nil {
  36. if recvErr == io.EOF {
  37. break
  38. } else {
  39. return recvErr
  40. }
  41. }
  42. if err = dir.handleRenameResponse(ctx, resp); err != nil {
  43. return err
  44. }
  45. }
  46. return nil
  47. })
  48. if err != nil {
  49. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  50. return fuse.EIO
  51. }
  52. return nil
  53. }
  54. func (dir *Dir) handleRenameResponse(ctx context.Context, resp *filer_pb.StreamRenameEntryResponse) error {
  55. // comes from filer StreamRenameEntry, can only be create or delete entry
  56. if resp.EventNotification.NewEntry != nil {
  57. // with new entry, the old entry name also exists. This is the first step to create new entry
  58. newEntry := filer.FromPbEntry(resp.EventNotification.NewParentPath, resp.EventNotification.NewEntry)
  59. if err := dir.wfs.metaCache.AtomicUpdateEntryFromFiler(ctx, "", newEntry); err != nil {
  60. return err
  61. }
  62. oldParent, newParent := util.FullPath(resp.Directory), util.FullPath(resp.EventNotification.NewParentPath)
  63. oldName, newName := resp.EventNotification.OldEntry.Name, resp.EventNotification.NewEntry.Name
  64. oldPath := oldParent.Child(oldName)
  65. newPath := newParent.Child(newName)
  66. oldFsNode := NodeWithId(oldPath.AsInode())
  67. newFsNode := NodeWithId(newPath.AsInode())
  68. newDirNode, found := dir.wfs.Server.FindInternalNode(NodeWithId(newParent.AsInode()))
  69. var newDir *Dir
  70. if found {
  71. newDir = newDirNode.(*Dir)
  72. }
  73. dir.wfs.Server.InvalidateInternalNode(oldFsNode, newFsNode, func(internalNode fs.Node) {
  74. if file, ok := internalNode.(*File); ok {
  75. glog.V(4).Infof("internal file node %s", oldParent.Child(oldName))
  76. file.Name = newName
  77. file.id = uint64(newFsNode)
  78. if found {
  79. file.dir = newDir
  80. }
  81. }
  82. if dir, ok := internalNode.(*Dir); ok {
  83. glog.V(4).Infof("internal dir node %s", oldParent.Child(oldName))
  84. dir.name = newName
  85. dir.id = uint64(newFsNode)
  86. if found {
  87. dir.parent = newDir
  88. }
  89. }
  90. })
  91. // change file handle
  92. inodeId := oldPath.AsInode()
  93. dir.wfs.handlesLock.Lock()
  94. if existingHandle, found := dir.wfs.handles[inodeId]; found && existingHandle == nil {
  95. glog.V(4).Infof("opened file handle %s => %s", oldPath, newPath)
  96. delete(dir.wfs.handles, inodeId)
  97. dir.wfs.handles[newPath.AsInode()] = existingHandle
  98. }
  99. dir.wfs.handlesLock.Unlock()
  100. } else if resp.EventNotification.OldEntry != nil {
  101. // without new entry, only old entry name exists. This is the second step to delete old entry
  102. if err := dir.wfs.metaCache.AtomicUpdateEntryFromFiler(ctx, util.NewFullPath(resp.Directory, resp.EventNotification.OldEntry.Name), nil); err != nil {
  103. return err
  104. }
  105. }
  106. return nil
  107. }