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.

106 lines
3.1 KiB

6 years ago
6 years ago
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. moveErr := fs.moveEntry(ctx, oldParent, oldEntry, filer2.FullPath(filepath.ToSlash(req.NewDirectory)), req.NewName)
  22. if moveErr != nil {
  23. fs.filer.RollbackTransaction(ctx)
  24. return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, err)
  25. } else {
  26. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  27. fs.filer.RollbackTransaction(ctx)
  28. return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, err)
  29. }
  30. }
  31. return &filer_pb.AtomicRenameEntryResponse{}, nil
  32. }
  33. func (fs *FilerServer) moveEntry(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string) error {
  34. if entry.IsDirectory() {
  35. if err := fs.moveFolderSubEntries(ctx, oldParent, entry, newParent, newName); err != nil {
  36. return err
  37. }
  38. }
  39. return fs.moveSelfEntry(ctx, oldParent, entry, newParent, newName)
  40. }
  41. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string) error {
  42. currentDirPath := oldParent.Child(entry.Name())
  43. newDirPath := newParent.Child(newName)
  44. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  45. lastFileName := ""
  46. includeLastFile := false
  47. for {
  48. entries, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024)
  49. if err != nil {
  50. return err
  51. }
  52. println("found", len(entries), "entries under", currentDirPath)
  53. for _, item := range entries {
  54. lastFileName = item.Name()
  55. println("processing", lastFileName)
  56. err := fs.moveEntry(ctx, currentDirPath, item, newDirPath, item.Name())
  57. if err != nil {
  58. return err
  59. }
  60. }
  61. if len(entries) < 1024 {
  62. break
  63. }
  64. }
  65. return nil
  66. }
  67. func (fs *FilerServer) moveSelfEntry(ctx context.Context, oldParent filer2.FullPath, entry *filer2.Entry, newParent filer2.FullPath, newName string) error {
  68. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  69. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  70. // add to new directory
  71. createErr := fs.filer.CreateEntry(ctx, &filer2.Entry{
  72. FullPath: newPath,
  73. Attr: entry.Attr,
  74. Chunks: entry.Chunks,
  75. })
  76. if createErr != nil {
  77. return createErr
  78. }
  79. // delete old entry
  80. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false)
  81. if deleteErr != nil {
  82. return deleteErr
  83. }
  84. return nil
  85. }