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.

123 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. ctx, err := fs.filer.BeginTransaction(ctx)
  12. if err != nil {
  13. return nil, err
  14. }
  15. oldParent := filer2.FullPath(filepath.ToSlash(req.OldDirectory))
  16. oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
  17. if err != nil {
  18. fs.filer.RollbackTransaction(ctx)
  19. return nil, fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
  20. }
  21. var events MoveEvents
  22. moveErr := fs.moveEntry(ctx, oldParent, oldEntry, filer2.FullPath(filepath.ToSlash(req.NewDirectory)), req.NewName, &events)
  23. if moveErr != nil {
  24. fs.filer.RollbackTransaction(ctx)
  25. return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, err)
  26. } else {
  27. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  28. fs.filer.RollbackTransaction(ctx)
  29. return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, err)
  30. }
  31. }
  32. for _, entry := range events.newEntries {
  33. fs.filer.NotifyUpdateEvent(nil, entry, false)
  34. }
  35. for _, entry := range events.oldEntries {
  36. fs.filer.NotifyUpdateEvent(entry, nil, false)
  37. }
  38. return &filer_pb.AtomicRenameEntryResponse{}, nil
  39. }
  40. func (fs *FilerServer) moveEntry(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string, events *MoveEvents) error {
  41. if entry.IsDirectory() {
  42. if err := fs.moveFolderSubEntries(ctx, oldParent, entry, newParent, newName, events); err != nil {
  43. return err
  44. }
  45. }
  46. return fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName, events)
  47. }
  48. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string, events *MoveEvents) error {
  49. currentDirPath := oldParent.Child(entry.Name())
  50. newDirPath := newParent.Child(newName)
  51. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  52. lastFileName := ""
  53. includeLastFile := false
  54. for {
  55. entries, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024)
  56. if err != nil {
  57. return err
  58. }
  59. println("found", len(entries), "entries under", currentDirPath)
  60. for _, item := range entries {
  61. lastFileName = item.Name()
  62. println("processing", lastFileName)
  63. err := fs.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name(), events)
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. if len(entries) < 1024 {
  69. break
  70. }
  71. }
  72. return nil
  73. }
  74. func (fs *FilerServer) moveSelfEntry(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string, events *MoveEvents) error {
  75. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  76. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  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)
  84. if createErr != nil {
  85. return createErr
  86. }
  87. // delete old entry
  88. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, 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. }