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.

211 lines
6.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/filer"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func (fs *FilerServer) AtomicRenameEntry(ctx context.Context, req *filer_pb.AtomicRenameEntryRequest) (*filer_pb.AtomicRenameEntryResponse, error) {
  13. glog.V(1).Infof("AtomicRenameEntry %v", req)
  14. oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
  15. newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
  16. if err := fs.filer.CanRename(oldParent, newParent); err != nil {
  17. return nil, err
  18. }
  19. ctx, err := fs.filer.BeginTransaction(ctx)
  20. if err != nil {
  21. return nil, err
  22. }
  23. oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
  24. if err != nil {
  25. fs.filer.RollbackTransaction(ctx)
  26. return nil, fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
  27. }
  28. moveErr := fs.moveEntry(ctx, nil, oldParent, oldEntry, newParent, req.NewName, req.Signatures)
  29. if moveErr != nil {
  30. fs.filer.RollbackTransaction(ctx)
  31. return nil, fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
  32. } else {
  33. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  34. fs.filer.RollbackTransaction(ctx)
  35. return nil, fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
  36. }
  37. }
  38. return &filer_pb.AtomicRenameEntryResponse{}, nil
  39. }
  40. func (fs *FilerServer) StreamRenameEntry(req *filer_pb.StreamRenameEntryRequest, stream filer_pb.SeaweedFiler_StreamRenameEntryServer) (err error) {
  41. glog.V(1).Infof("StreamRenameEntry %v", req)
  42. oldParent := util.FullPath(filepath.ToSlash(req.OldDirectory))
  43. newParent := util.FullPath(filepath.ToSlash(req.NewDirectory))
  44. if err := fs.filer.CanRename(oldParent, newParent); err != nil {
  45. return err
  46. }
  47. ctx := context.Background()
  48. ctx, err = fs.filer.BeginTransaction(ctx)
  49. if err != nil {
  50. return err
  51. }
  52. oldEntry, err := fs.filer.FindEntry(ctx, oldParent.Child(req.OldName))
  53. if err != nil {
  54. fs.filer.RollbackTransaction(ctx)
  55. return fmt.Errorf("%s/%s not found: %v", req.OldDirectory, req.OldName, err)
  56. }
  57. moveErr := fs.moveEntry(ctx, stream, oldParent, oldEntry, newParent, req.NewName, req.Signatures)
  58. if moveErr != nil {
  59. fs.filer.RollbackTransaction(ctx)
  60. return fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
  61. } else {
  62. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  63. fs.filer.RollbackTransaction(ctx)
  64. return fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
  65. }
  66. }
  67. return nil
  68. }
  69. func (fs *FilerServer) moveEntry(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, signatures []int32) error {
  70. if err := fs.moveSelfEntry(ctx, stream, oldParent, entry, newParent, newName, func() error {
  71. if entry.IsDirectory() {
  72. if err := fs.moveFolderSubEntries(ctx, stream, oldParent, entry, newParent, newName, signatures); err != nil {
  73. return err
  74. }
  75. }
  76. return nil
  77. }, signatures); err != nil {
  78. return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(entry.Name()), newParent.Child(newName), err)
  79. }
  80. return nil
  81. }
  82. func (fs *FilerServer) moveFolderSubEntries(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, signatures []int32) error {
  83. currentDirPath := oldParent.Child(entry.Name())
  84. newDirPath := newParent.Child(newName)
  85. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  86. lastFileName := ""
  87. includeLastFile := false
  88. for {
  89. entries, hasMore, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024, "", "", "")
  90. if err != nil {
  91. return err
  92. }
  93. // println("found", len(entries), "entries under", currentDirPath)
  94. for _, item := range entries {
  95. lastFileName = item.Name()
  96. // println("processing", lastFileName)
  97. err := fs.moveEntry(ctx, stream, currentDirPath, item, newDirPath, item.Name(), signatures)
  98. if err != nil {
  99. return err
  100. }
  101. }
  102. if !hasMore {
  103. break
  104. }
  105. }
  106. return nil
  107. }
  108. func (fs *FilerServer) moveSelfEntry(ctx context.Context, stream filer_pb.SeaweedFiler_StreamRenameEntryServer, oldParent util.FullPath, entry *filer.Entry, newParent util.FullPath, newName string, moveFolderSubEntries func() error, signatures []int32) error {
  109. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  110. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  111. if oldPath == newPath {
  112. glog.V(1).Infof("skip moving entry %s => %s", oldPath, newPath)
  113. return nil
  114. }
  115. // add to new directory
  116. newEntry := &filer.Entry{
  117. FullPath: newPath,
  118. Attr: entry.Attr,
  119. Chunks: entry.Chunks,
  120. Extended: entry.Extended,
  121. Content: entry.Content,
  122. }
  123. if createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, signatures); createErr != nil {
  124. return createErr
  125. }
  126. if stream != nil {
  127. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  128. Directory: string(newParent),
  129. EventNotification: &filer_pb.EventNotification{
  130. OldEntry: &filer_pb.Entry{
  131. Name: entry.Name(),
  132. },
  133. NewEntry: newEntry.ToProtoEntry(),
  134. DeleteChunks: false,
  135. NewParentPath: string(newParent),
  136. IsFromOtherCluster: false,
  137. Signatures: nil,
  138. },
  139. TsNs: time.Now().UnixNano(),
  140. }); err != nil {
  141. return err
  142. }
  143. }
  144. if moveFolderSubEntries != nil {
  145. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  146. return moveChildrenErr
  147. }
  148. }
  149. // delete old entry
  150. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, signatures)
  151. if deleteErr != nil {
  152. return deleteErr
  153. }
  154. if stream != nil {
  155. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  156. Directory: string(oldParent),
  157. EventNotification: &filer_pb.EventNotification{
  158. OldEntry: &filer_pb.Entry{
  159. Name: entry.Name(),
  160. },
  161. NewEntry: nil,
  162. DeleteChunks: false,
  163. NewParentPath: "",
  164. IsFromOtherCluster: false,
  165. Signatures: nil,
  166. },
  167. TsNs: time.Now().UnixNano(),
  168. }); err != nil {
  169. return err
  170. }
  171. }
  172. return nil
  173. }