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.

148 lines
4.1 KiB

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. 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. }