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.

141 lines
4.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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. 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. glog.V(1).Infof("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. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  79. if oldPath == newPath {
  80. glog.V(1).Infof("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. }
  89. createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, nil)
  90. if createErr != nil {
  91. return createErr
  92. }
  93. events.newEntries = append(events.newEntries, newEntry)
  94. if moveFolderSubEntries != nil {
  95. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  96. return moveChildrenErr
  97. }
  98. }
  99. // delete old entry
  100. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, nil)
  101. if deleteErr != nil {
  102. return deleteErr
  103. }
  104. events.oldEntries = append(events.oldEntries, entry)
  105. return nil
  106. }
  107. type MoveEvents struct {
  108. oldEntries []*filer.Entry
  109. newEntries []*filer.Entry
  110. }