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.

132 lines
3.8 KiB

5 years ago
5 years ago
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/filer2"
  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, err)
  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, err)
  32. }
  33. }
  34. for _, entry := range events.newEntries {
  35. fs.filer.NotifyUpdateEvent(nil, entry, false)
  36. }
  37. for _, entry := range events.oldEntries {
  38. fs.filer.NotifyUpdateEvent(entry, nil, false)
  39. }
  40. return &filer_pb.AtomicRenameEntryResponse{}, nil
  41. }
  42. func (fs *FilerServer) moveEntry(ctx context.Context, oldParent util.FullPath, entry *filer2.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
  43. if entry.IsDirectory() {
  44. if err := fs.moveFolderSubEntries(ctx, oldParent, entry, newParent, newName, events); err != nil {
  45. return err
  46. }
  47. }
  48. return fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName, events)
  49. }
  50. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent util.FullPath, entry *filer2.Entry, newParent util.FullPath, newName string, events *MoveEvents) error {
  51. currentDirPath := oldParent.Child(entry.Name())
  52. newDirPath := newParent.Child(newName)
  53. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  54. lastFileName := ""
  55. includeLastFile := false
  56. for {
  57. entries, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024)
  58. if err != nil {
  59. return err
  60. }
  61. // println("found", len(entries), "entries under", currentDirPath)
  62. for _, item := range entries {
  63. lastFileName = item.Name()
  64. // println("processing", lastFileName)
  65. err := fs.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name(), events)
  66. if err != nil {
  67. return err
  68. }
  69. }
  70. if len(entries) < 1024 {
  71. break
  72. }
  73. }
  74. return nil
  75. }
  76. func (fs *FilerServer) moveSelfEntry(ctx context.Context, oldParent util.FullPath, entry *filer2.Entry, newParent util.FullPath, newName string, events *MoveEvents) 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 := &filer2.Entry{
  85. FullPath: newPath,
  86. Attr: entry.Attr,
  87. Chunks: entry.Chunks,
  88. }
  89. createErr := fs.filer.CreateEntry(ctx, newEntry, false)
  90. if createErr != nil {
  91. return createErr
  92. }
  93. // delete old entry
  94. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false)
  95. if deleteErr != nil {
  96. return deleteErr
  97. }
  98. events.oldEntries = append(events.oldEntries, entry)
  99. events.newEntries = append(events.newEntries, newEntry)
  100. return nil
  101. }
  102. type MoveEvents struct {
  103. oldEntries []*filer2.Entry
  104. newEntries []*filer2.Entry
  105. }