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.

102 lines
2.5 KiB

7 years ago
  1. package filesys
  2. import (
  3. "bazil.org/fuse"
  4. "bazil.org/fuse/fs"
  5. "context"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "path/filepath"
  9. )
  10. func (dir *Dir) Rename(ctx context.Context, req *fuse.RenameRequest, newDirectory fs.Node) error {
  11. newDir := newDirectory.(*Dir)
  12. return dir.wfs.withFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  13. // find existing entry
  14. request := &filer_pb.LookupDirectoryEntryRequest{
  15. Directory: dir.Path,
  16. Name: req.OldName,
  17. }
  18. glog.V(4).Infof("find existing directory entry: %v", request)
  19. resp, err := client.LookupDirectoryEntry(ctx, request)
  20. if err != nil {
  21. glog.V(0).Infof("renaming find %s/%s: %v", dir.Path, req.OldName, err)
  22. return fuse.ENOENT
  23. }
  24. entry := resp.Entry
  25. glog.V(4).Infof("found existing directory entry resp: %+v", resp)
  26. return moveEntry(ctx, client, dir.Path, entry, newDir.Path, req.NewName)
  27. })
  28. }
  29. func moveEntry(ctx context.Context, client filer_pb.SeaweedFilerClient, oldParent string, entry *filer_pb.Entry, newParent, newName string) error {
  30. if entry.IsDirectory {
  31. currentDirPath := filepath.Join(oldParent, entry.Name)
  32. request := &filer_pb.ListEntriesRequest{
  33. Directory: currentDirPath,
  34. }
  35. glog.V(4).Infof("read directory: %v", request)
  36. resp, err := client.ListEntries(ctx, request)
  37. if err != nil {
  38. glog.V(0).Infof("list %s: %v", oldParent, err)
  39. return fuse.EIO
  40. }
  41. for _, item := range resp.Entries {
  42. err := moveEntry(ctx, client, currentDirPath, item, filepath.Join(newParent, newName), item.Name)
  43. if err != nil {
  44. return err
  45. }
  46. }
  47. }
  48. // add to new directory
  49. {
  50. request := &filer_pb.CreateEntryRequest{
  51. Directory: newParent,
  52. Entry: &filer_pb.Entry{
  53. Name: newName,
  54. IsDirectory: entry.IsDirectory,
  55. Attributes: entry.Attributes,
  56. Chunks: entry.Chunks,
  57. },
  58. }
  59. glog.V(1).Infof("create new entry: %v", request)
  60. if _, err := client.CreateEntry(ctx, request); err != nil {
  61. glog.V(0).Infof("renaming create %s/%s: %v", newParent, newName, err)
  62. return fuse.EIO
  63. }
  64. }
  65. // delete old entry
  66. {
  67. request := &filer_pb.DeleteEntryRequest{
  68. Directory: oldParent,
  69. Name: entry.Name,
  70. IsDirectory: entry.IsDirectory,
  71. IsDeleteData: false,
  72. }
  73. glog.V(1).Infof("remove old entry: %v", request)
  74. _, err := client.DeleteEntry(ctx, request)
  75. if err != nil {
  76. glog.V(0).Infof("renaming delete %s/%s: %v", oldParent, entry.Name, err)
  77. return fuse.EIO
  78. }
  79. }
  80. return nil
  81. }