filer_grpc_server_rename.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package weed_server
  2. import (
  3. "context"
  4. "fmt"
  5. "path/filepath"
  6. "time"
  7. "github.com/seaweedfs/seaweedfs/weed/filer"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/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, req.OldName); 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, req.OldName); 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.GetChunks(),
  134. Extended: entry.Extended,
  135. Content: entry.Content,
  136. HardLinkCounter: entry.HardLinkCounter,
  137. HardLinkId: entry.HardLinkId,
  138. Remote: entry.Remote,
  139. Quota: entry.Quota,
  140. }
  141. if createErr := fs.filer.CreateEntry(ctx, newEntry, false, false, signatures, false, fs.filer.MaxFilenameLength); createErr != nil {
  142. return createErr
  143. }
  144. if stream != nil {
  145. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  146. Directory: string(oldParent),
  147. EventNotification: &filer_pb.EventNotification{
  148. OldEntry: &filer_pb.Entry{
  149. Name: entry.Name(),
  150. },
  151. NewEntry: newEntry.ToProtoEntry(),
  152. DeleteChunks: false,
  153. NewParentPath: string(newParent),
  154. IsFromOtherCluster: false,
  155. Signatures: nil,
  156. },
  157. TsNs: time.Now().UnixNano(),
  158. }); err != nil {
  159. return err
  160. }
  161. }
  162. if moveFolderSubEntries != nil {
  163. if moveChildrenErr := moveFolderSubEntries(); moveChildrenErr != nil {
  164. return moveChildrenErr
  165. }
  166. }
  167. // delete old entry
  168. ctx = context.WithValue(ctx, "OP", "MV")
  169. deleteErr := fs.filer.DeleteEntryMetaAndData(ctx, oldPath, false, false, false, false, signatures, 0)
  170. if deleteErr != nil {
  171. return deleteErr
  172. }
  173. if stream != nil {
  174. if err := stream.Send(&filer_pb.StreamRenameEntryResponse{
  175. Directory: string(oldParent),
  176. EventNotification: &filer_pb.EventNotification{
  177. OldEntry: &filer_pb.Entry{
  178. Name: entry.Name(),
  179. },
  180. NewEntry: nil,
  181. DeleteChunks: false,
  182. NewParentPath: "",
  183. IsFromOtherCluster: false,
  184. Signatures: nil,
  185. },
  186. TsNs: time.Now().UnixNano(),
  187. }); err != nil {
  188. return err
  189. }
  190. }
  191. return nil
  192. }