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.

81 lines
2.2 KiB

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