dir_rename.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // fmt.Printf("rename path: %v => %v\n", oldPath, newPath)
  53. dir.wfs.fsNodeCache.Move(oldPath, newPath)
  54. // change file handle
  55. dir.wfs.handlesLock.Lock()
  56. defer dir.wfs.handlesLock.Unlock()
  57. inodeId := oldPath.AsInode()
  58. existingHandle, found := dir.wfs.handles[inodeId]
  59. if !found || existingHandle == nil {
  60. return err
  61. }
  62. delete(dir.wfs.handles, inodeId)
  63. dir.wfs.handles[newPath.AsInode()] = existingHandle
  64. return err
  65. }