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

6 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/filer2"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "path/filepath"
  9. )
  10. func (fs *FilerServer) AtomicRenameEntry(ctx context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
  11. glog.V(1).Infof("AtomicRenameEntry %v", req)
  12. ctx, err := fs.filer.BeginTransaction(ctx)
  13. if err != nil {
  14. return nil, err
  15. }
  16. oldParent := filer2.FullPath(filepath.ToSlash(req.OldDirectory))
  17. oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
  18. if err != nil {
  19. fs.filer.RollbackTransaction(ctx)
  20. return nil, fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
  21. }
  22. var events MoveEvents
  23. moveErr := fs.moveEntry(ctx, oldParent, oldEntry, filer2.FullPath(filepath.ToSlash(req.NewDirectory)), req.NewName, &events)
  24. if moveErr != nil {
  25. fs.filer.RollbackTransaction(ctx)
  26. return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, err)
  27. } else {
  28. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  29. fs.filer.RollbackTransaction(ctx)
  30. return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, err)
  31. }
  32. }
  33. for _, entry := range events.newEntries {
  34. fs.filer.NotifyUpdateEvent(nil, entry, false)
  35. }
  36. for _, entry := range events.oldEntries {
  37. fs.filer.NotifyUpdateEvent(entry, nil, false)
  38. }
  39. return &filer_pb.AtomicRenameEntryResponse{}, nil
  40. }
  41. func (fs *FilerServer) moveEntry(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string, events *MoveEvents) 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 fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName, events)
  48. }
  49. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.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 filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string, events *MoveEvents) error {
  76. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  77. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  78. // add to new directory
  79. newEntry := &filer2.Entry{
  80. FullPath: newPath,
  81. Attr: entry.Attr,
  82. Chunks: entry.Chunks,
  83. }
  84. createErr := fs.filer.CreateEntry(ctx, newEntry)
  85. if createErr != nil {
  86. return createErr
  87. }
  88. // delete old entry
  89. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false)
  90. if deleteErr != nil {
  91. return deleteErr
  92. }
  93. events.oldEntries = append(events.oldEntries, entry)
  94. events.newEntries = append(events.newEntries, newEntry)
  95. return nil
  96. }
  97. type MoveEvents struct {
  98. oldEntries []*filer2.Entry
  99. newEntries []*filer2.Entry
  100. }