filer_grpc_server_rename.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if oldPath == newPath {
  79. glog.V(1).Infof("skip moving entry %s => %s", oldPath, newPath)
  80. return nil
  81. }
  82. // add to new directory
  83. newEntry := &filer2.Entry{
  84. FullPath: newPath,
  85. Attr: entry.Attr,
  86. Chunks: entry.Chunks,
  87. }
  88. createErr := fs.filer.CreateEntry(ctx, newEntry)
  89. if createErr != nil {
  90. return createErr
  91. }
  92. // delete old entry
  93. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false)
  94. if deleteErr != nil {
  95. return deleteErr
  96. }
  97. events.oldEntries = append(events.oldEntries, entry)
  98. events.newEntries = append(events.newEntries, newEntry)
  99. return nil
  100. }
  101. type MoveEvents struct {
  102. oldEntries []*filer2.Entry
  103. newEntries []*filer2.Entry
  104. }