dir_rename.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.V(0).Infof("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. request := &filer_pb.AtomicRenameEntryRequest{
  24. OldDirectory: dir.FullPath(),
  25. OldName: req.OldName,
  26. NewDirectory: newDir.FullPath(),
  27. NewName: req.NewName,
  28. }
  29. _, err := client.AtomicRenameEntry(context.Background(), request)
  30. if err != nil {
  31. return fuse.EIO
  32. }
  33. return nil
  34. })
  35. if err != nil {
  36. glog.V(0).Infof("dir Rename %s => %s : %v", oldPath, newPath, err)
  37. return fuse.EIO
  38. }
  39. // TODO: replicate renaming logic on filer
  40. if err := dir.wfs.metaCache.DeleteEntry(context.Background(), oldPath); err != nil {
  41. glog.V(0).Infof("dir Rename delete local %s => %s : %v", oldPath, newPath, err)
  42. return fuse.EIO
  43. }
  44. oldEntry.FullPath = newPath
  45. if err := dir.wfs.metaCache.InsertEntry(context.Background(), oldEntry); err != nil {
  46. glog.V(0).Infof("dir Rename insert local %s => %s : %v", oldPath, newPath, err)
  47. return fuse.EIO
  48. }
  49. // fmt.Printf("rename path: %v => %v\n", oldPath, newPath)
  50. dir.wfs.fsNodeCache.Move(oldPath, newPath)
  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. if !found || existingHandle == nil {
  57. return err
  58. }
  59. delete(dir.wfs.handles, inodeId)
  60. dir.wfs.handles[newPath.AsInode()] = existingHandle
  61. return err
  62. }