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.

86 lines
2.3 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. if dir.wfs.option.ReadOnly {
  12. return fuse.EPERM
  13. }
  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. }
  34. _, err := client.AtomicRenameEntry(ctx, request)
  35. if err != nil {
  36. glog.Errorf("dir AtomicRenameEntry %s => %s : %v", oldPath, newPath, err)
  37. return fuse.EXDEV
  38. }
  39. return nil
  40. })
  41. if err != nil {
  42. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  43. return fuse.EIO
  44. }
  45. // TODO: replicate renaming logic on filer
  46. if err := dir.wfs.metaCache.DeleteEntry(context.Background(), oldPath); err != nil {
  47. glog.V(0).Infof("dir Rename delete local %s => %s : %v", oldPath, newPath, err)
  48. return fuse.EIO
  49. }
  50. oldEntry.FullPath = newPath
  51. if err := dir.wfs.metaCache.InsertEntry(context.Background(), oldEntry); err != nil {
  52. glog.V(0).Infof("dir Rename insert local %s => %s : %v", oldPath, newPath, err)
  53. return fuse.EIO
  54. }
  55. // fmt.Printf("rename path: %v => %v\n", oldPath, newPath)
  56. dir.wfs.fsNodeCache.Move(oldPath, newPath)
  57. // change file handle
  58. dir.wfs.handlesLock.Lock()
  59. defer dir.wfs.handlesLock.Unlock()
  60. inodeId := oldPath.AsInode()
  61. existingHandle, found := dir.wfs.handles[inodeId]
  62. if !found || existingHandle == nil {
  63. return err
  64. }
  65. delete(dir.wfs.handles, inodeId)
  66. dir.wfs.handles[newPath.AsInode()] = existingHandle
  67. return err
  68. }