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.

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