filer_grpc_server_rename.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "github.com/chrislusf/seaweedfs/weed/filer"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. func (fs *FilerServer) AtomicRenameEntry(ctx context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
  12. glog.V(1).Infof("AtomicRenameEntry %v", req)
  13. oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
  14. newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
  15. if err := fs.filer.CanRename(oldParent, newParent); err != nil {
  16. return nil, err
  17. }
  18. ctx, err := fs.filer.BeginTransaction(ctx)
  19. if err != nil {
  20. return nil, err
  21. }
  22. oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
  23. if err != nil {
  24. fs.filer.RollbackTransaction(ctx)
  25. return nil, fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
  26. }
  27. var events MoveEvents
  28. moveErr := fs.moveEntry(ctx, oldParent, oldEntry, newParent, req.NewName, &events)
  29. if moveErr != nil {
  30. fs.filer.RollbackTransaction(ctx)
  31. return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
  32. } else {
  33. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  34. fs.filer.RollbackTransaction(ctx)
  35. return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
  36. }
  37. }
  38. return &filer_pb.AtomicRenameEntryResponse{}, nil
  39. }
  40. func (fs *FilerServer) moveEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
  41. if err := fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName, events, func() error {
  42. if entry.IsDirectory() {
  43. if err := fs.moveFolderSubEntries(ctx, oldParent, entry, newParent, newName, events); err != nil {
  44. return err
  45. }
  46. }
  47. return nil
  48. }); err != nil {
  49. return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(entry.Name()), newParent.Child(newName), err)
  50. }
  51. return nil
  52. }
  53. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
  54. currentDirPath := oldParent.Child(entry.Name())
  55. newDirPath := newParent.Child(newName)
  56. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  57. lastFileName := ""
  58. includeLastFile := false
  59. for {
  60. entries, hasMore, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024, "", "")
  61. if err != nil {
  62. return err
  63. }
  64. // println("found", len(entries), "entries under", currentDirPath)
  65. for _, item := range entries {
  66. lastFileName = item.Name()
  67. // println("processing", lastFileName)
  68. err := fs.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name(), events)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. if !hasMore {
  74. break
  75. }
  76. }
  77. return nil
  78. }
  79. func (fs *FilerServer) moveSelfEntry(ctx context.Context, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, events *MoveEvents,
  80. moveFolderSubEntries func() error) error {
  81. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  82. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  83. if oldPath == newPath {
  84. glog.V(1).Infof("skip moving entry %s => %s", oldPath, newPath)
  85. return nil
  86. }
  87. // add to new directory
  88. newEntry := &filer.Entry{
  89. FullPath: newPath,
  90. Attr: entry.Attr,
  91. Chunks: entry.Chunks,
  92. Extended: entry.Extended,
  93. Content: entry.Content,
  94. }
  95. createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, nil)
  96. if createErr != nil {
  97. return createErr
  98. }
  99. events.newEntries = append(events.newEntries, newEntry)
  100. if moveFolderSubEntries != nil {
  101. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  102. return moveChildrenErr
  103. }
  104. }
  105. // delete old entry
  106. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, nil)
  107. if deleteErr != nil {
  108. return deleteErr
  109. }
  110. events.oldEntries = append(events.oldEntries, entry)
  111. return nil
  112. }
  113. type MoveEvents struct {
  114. oldEntries []*filer.Entry
  115. newEntries []*filer.Entry
  116. }