filer_grpc_server_rename.go 4.0 KB

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