filer_grpc_server_rename.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if oldEntry.IsDirectory() {
  58. // follow https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html
  59. targetDir := newParent.Child(req.NewName)
  60. newEntry, err := fs.filer.FindEntry(ctx, targetDir)
  61. if err == nil {
  62. if !newEntry.IsDirectory() {
  63. fs.filer.RollbackTransaction(ctx)
  64. return fmt.Errorf("%s is not directory", targetDir)
  65. }
  66. if entries, _, _ := fs.filer.ListDirectoryEntries(context.Background(), targetDir, "", false, 1, "", "", ""); len(entries) > 0 {
  67. return fmt.Errorf("%s is not empty", targetDir)
  68. }
  69. }
  70. }
  71. moveErr := fs.moveEntry(ctx, stream, oldParent, oldEntry, newParent, req.NewName, req.Signatures)
  72. if moveErr != nil {
  73. fs.filer.RollbackTransaction(ctx)
  74. return fmt.Errorf("%s/%s move error: %v", req.OldDirectory, req.OldName, moveErr)
  75. } else {
  76. if commitError := fs.filer.CommitTransaction(ctx); commitError != nil {
  77. fs.filer.RollbackTransaction(ctx)
  78. return fmt.Errorf("%s/%s move commit error: %v", req.OldDirectory, req.OldName, commitError)
  79. }
  80. }
  81. return nil
  82. }
  83. 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 {
  84. if err := fs.moveSelfEntry(ctx, stream, oldParent, entry, newParent, newName, func() error {
  85. if entry.IsDirectory() {
  86. if err := fs.moveFolderSubEntries(ctx, stream, oldParent, entry, newParent, newName, signatures); err != nil {
  87. return err
  88. }
  89. }
  90. return nil
  91. }, signatures); err != nil {
  92. return fmt.Errorf("fail to move %s => %s: %v", oldParent.Child(entry.Name()), newParent.Child(newName), err)
  93. }
  94. return nil
  95. }
  96. 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 {
  97. currentDirPath := oldParent.Child(entry.Name())
  98. newDirPath := newParent.Child(newName)
  99. glog.V(1).Infof("moving folder %s => %s", currentDirPath, newDirPath)
  100. lastFileName := ""
  101. includeLastFile := false
  102. for {
  103. entries, hasMore, err := fs.filer.ListDirectoryEntries(ctx, currentDirPath, lastFileName, includeLastFile, 1024, "", "", "")
  104. if err != nil {
  105. return err
  106. }
  107. // println("found", len(entries), "entries under", currentDirPath)
  108. for _, item := range entries {
  109. lastFileName = item.Name()
  110. // println("processing", lastFileName)
  111. err := fs.moveEntry(ctx, stream, currentDirPath, item, newDirPath, item.Name(), signatures)
  112. if err != nil {
  113. return err
  114. }
  115. }
  116. if !hasMore {
  117. break
  118. }
  119. }
  120. return nil
  121. }
  122. 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 {
  123. oldPath, newPath := oldParent.Child(entry.Name()), newParent.Child(newName)
  124. glog.V(1).Infof("moving entry %s => %s", oldPath, newPath)
  125. if oldPath == newPath {
  126. glog.V(1).Infof("skip moving entry %s => %s", oldPath, newPath)
  127. return nil
  128. }
  129. // add to new directory
  130. newEntry := &filer.Entry{
  131. FullPath: newPath,
  132. Attr: entry.Attr,
  133. Chunks: entry.Chunks,
  134. Extended: entry.Extended,
  135. Content: entry.Content,
  136. }
  137. if createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, signatures); createErr != nil {
  138. return createErr
  139. }
  140. if stream != nil {
  141. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  142. Directory: string(oldParent),
  143. EventNotification: &filer_pb.EventNotification{
  144. OldEntry: &filer_pb.Entry{
  145. Name: entry.Name(),
  146. },
  147. NewEntry: newEntry.ToProtoEntry(),
  148. DeleteChunks: false,
  149. NewParentPath: string(newParent),
  150. IsFromOtherCluster: false,
  151. Signatures: nil,
  152. },
  153. TsNs: time.Now().UnixNano(),
  154. }); err != nil {
  155. return err
  156. }
  157. }
  158. if moveFolderSubEntries != nil {
  159. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  160. return moveChildrenErr
  161. }
  162. }
  163. // delete old entry
  164. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, signatures)
  165. if deleteErr != nil {
  166. return deleteErr
  167. }
  168. if stream != nil {
  169. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  170. Directory: string(oldParent),
  171. EventNotification: &filer_pb.EventNotification{
  172. OldEntry: &filer_pb.Entry{
  173. Name: entry.Name(),
  174. },
  175. NewEntry: nil,
  176. DeleteChunks: false,
  177. NewParentPath: "",
  178. IsFromOtherCluster: false,
  179. Signatures: nil,
  180. },
  181. TsNs: time.Now().UnixNano(),
  182. }); err != nil {
  183. return err
  184. }
  185. }
  186. return nil
  187. }