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.

98 lines
2.8 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. "github.com/seaweedfs/fuse"
  5. "github.com/seaweedfs/fuse/fs"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
  11. newDir := newDirectory.(*Dir)
  12. newPath := util.NewFullPath(newDir.FullPath(), req.NewName)
  13. oldPath := util.NewFullPath(dir.FullPath(), req.OldName)
  14. glog.V(4).Infof("dir Rename %s => %s", oldPath, newPath)
  15. // find local old entry
  16. oldEntry, err := dir.wfs.metaCache.FindEntry(context.Background(), oldPath)
  17. if err != nil {
  18. glog.Errorf("dir Rename can not find source %s : %v", oldPath, err)
  19. return fuse.ENOENT
  20. }
  21. // update remote filer
  22. err = dir.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  23. ctx, cancel := context.WithCancel(context.Background())
  24. defer cancel()
  25. request := &filer_pb.AtomicRenameEntryRequest{
  26. OldDirectory: dir.FullPath(),
  27. OldName: req.OldName,
  28. NewDirectory: newDir.FullPath(),
  29. NewName: req.NewName,
  30. }
  31. _, err := client.AtomicRenameEntry(ctx, request)
  32. if err != nil {
  33. glog.Errorf("dir AtomicRenameEntry %s => %s : %v", oldPath, newPath, err)
  34. return fuse.EXDEV
  35. }
  36. return nil
  37. })
  38. if err != nil {
  39. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  40. return fuse.EIO
  41. }
  42. // TODO: replicate renaming logic on filer
  43. if err := dir.wfs.metaCache.DeleteEntry(context.Background(), oldPath); err != nil {
  44. glog.V(0).Infof("dir Rename delete local %s => %s : %v", oldPath, newPath, err)
  45. return fuse.EIO
  46. }
  47. oldEntry.FullPath = newPath
  48. if err := dir.wfs.metaCache.InsertEntry(context.Background(), oldEntry); err != nil {
  49. glog.V(0).Infof("dir Rename insert local %s => %s : %v", oldPath, newPath, err)
  50. return fuse.EIO
  51. }
  52. oldFsNode := NodeWithId(oldPath.AsInode())
  53. newFsNode := NodeWithId(newPath.AsInode())
  54. dir.wfs.Server.InvalidateInternalNode(oldFsNode, newFsNode, func(internalNode fs.Node) {
  55. if file, ok := internalNode.(*File); ok {
  56. glog.V(4).Infof("internal file node %s", file.Name)
  57. file.Name = req.NewName
  58. file.id = uint64(newFsNode)
  59. file.dir = newDir
  60. }
  61. if dir, ok := internalNode.(*Dir); ok {
  62. glog.V(4).Infof("internal dir node %s", dir.name)
  63. dir.name = req.NewName
  64. dir.id = uint64(newFsNode)
  65. dir.parent = newDir
  66. }
  67. })
  68. // change file handle
  69. dir.wfs.handlesLock.Lock()
  70. defer dir.wfs.handlesLock.Unlock()
  71. inodeId := oldPath.AsInode()
  72. existingHandle, found := dir.wfs.handles[inodeId]
  73. glog.V(4).Infof("has open filehandle %s: %v", oldPath, found)
  74. if !found || existingHandle == nil {
  75. return nil
  76. }
  77. glog.V(4).Infof("opened filehandle %s => %s", oldPath, newPath)
  78. delete(dir.wfs.handles, inodeId)
  79. dir.wfs.handles[newPath.AsInode()] = existingHandle
  80. return nil
  81. }