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.

183 lines
5.8 KiB

5 years ago
4 years ago
4 years ago
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/util"
  10. )
  11. type HardLinkId []byte
  12. func (f *Filer) DeleteEntryMetaAndData(ctx context.Context, p util.FullPath, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isFromOtherCluster bool, signatures []int32) (err error) {
  13. if p == "/" {
  14. return nil
  15. }
  16. entry, findErr := f.FindEntry(ctx, p)
  17. if findErr != nil {
  18. return findErr
  19. }
  20. isDeleteCollection := f.isBucket(entry)
  21. var chunks []*filer_pb.FileChunk
  22. var hardLinkIds []HardLinkId
  23. chunks = append(chunks, entry.Chunks...)
  24. if entry.IsDirectory() {
  25. // delete the folder children, not including the folder itself
  26. var dirChunks []*filer_pb.FileChunk
  27. var dirHardLinkIds []HardLinkId
  28. dirChunks, dirHardLinkIds, err = f.doBatchDeleteFolderMetaAndData(ctx, entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks && !isDeleteCollection, isFromOtherCluster, signatures)
  29. if err != nil {
  30. glog.V(0).Infof("delete directory %s: %v", p, err)
  31. return fmt.Errorf("delete directory %s: %v", p, err)
  32. }
  33. chunks = append(chunks, dirChunks...)
  34. hardLinkIds = append(hardLinkIds, dirHardLinkIds...)
  35. }
  36. // delete the file or folder
  37. err = f.doDeleteEntryMetaAndData(ctx, entry, shouldDeleteChunks, isFromOtherCluster, signatures)
  38. if err != nil {
  39. return fmt.Errorf("delete file %s: %v", p, err)
  40. }
  41. if shouldDeleteChunks && !isDeleteCollection {
  42. f.DirectDeleteChunks(chunks)
  43. }
  44. // A case not handled:
  45. // what if the chunk is in a different collection?
  46. if shouldDeleteChunks {
  47. f.maybeDeleteHardLinks(hardLinkIds)
  48. }
  49. if isDeleteCollection {
  50. collectionName := entry.Name()
  51. f.doDeleteCollection(collectionName)
  52. f.deleteBucket(collectionName)
  53. } else {
  54. parent, _ := p.DirAndName()
  55. if err := f.removeEmptyParentFolder(ctx, util.FullPath(parent)); err != nil {
  56. glog.Errorf("clean up empty folders for %s : %v", p, err)
  57. }
  58. }
  59. return nil
  60. }
  61. func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isFromOtherCluster bool, signatures []int32) (chunks []*filer_pb.FileChunk, hardlinkIds []HardLinkId, err error) {
  62. lastFileName := ""
  63. includeLastFile := false
  64. for {
  65. entries, err := f.ListDirectoryEntries(ctx, entry.FullPath, lastFileName, includeLastFile, PaginationSize, "")
  66. if err != nil {
  67. glog.Errorf("list folder %s: %v", entry.FullPath, err)
  68. return nil, nil, fmt.Errorf("list folder %s: %v", entry.FullPath, err)
  69. }
  70. if lastFileName == "" && !isRecursive && len(entries) > 0 {
  71. // only for first iteration in the loop
  72. glog.Errorf("deleting a folder %s has children: %+v ...", entry.FullPath, entries[0].Name())
  73. return nil, nil, fmt.Errorf("fail to delete non-empty folder: %s", entry.FullPath)
  74. }
  75. for _, sub := range entries {
  76. lastFileName = sub.Name()
  77. var dirChunks []*filer_pb.FileChunk
  78. var dirHardLinkIds []HardLinkId
  79. if sub.IsDirectory() {
  80. dirChunks, dirHardLinkIds, err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks, false, nil)
  81. chunks = append(chunks, dirChunks...)
  82. hardlinkIds = append(hardlinkIds, dirHardLinkIds...)
  83. } else {
  84. f.NotifyUpdateEvent(ctx, sub, nil, shouldDeleteChunks, isFromOtherCluster, nil)
  85. if len(sub.HardLinkId) != 0 {
  86. // hard link chunk data are deleted separately
  87. hardlinkIds = append(hardlinkIds, sub.HardLinkId)
  88. } else {
  89. chunks = append(chunks, sub.Chunks...)
  90. }
  91. }
  92. if err != nil && !ignoreRecursiveError {
  93. return nil, nil, err
  94. }
  95. }
  96. if len(entries) < PaginationSize {
  97. break
  98. }
  99. }
  100. glog.V(3).Infof("deleting directory %v delete %d chunks: %v", entry.FullPath, len(chunks), shouldDeleteChunks)
  101. if storeDeletionErr := f.Store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
  102. return nil, nil, fmt.Errorf("filer store delete: %v", storeDeletionErr)
  103. }
  104. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  105. return chunks, hardlinkIds, nil
  106. }
  107. func (f *Filer) doDeleteEntryMetaAndData(ctx context.Context, entry *Entry, shouldDeleteChunks bool, isFromOtherCluster bool, signatures []int32) (err error) {
  108. glog.V(3).Infof("deleting entry %v, delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  109. if storeDeletionErr := f.Store.DeleteOneEntry(ctx, entry); storeDeletionErr != nil {
  110. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  111. }
  112. if !entry.IsDirectory() {
  113. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  114. }
  115. return nil
  116. }
  117. func (f *Filer) doDeleteCollection(collectionName string) (err error) {
  118. return f.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  119. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  120. Name: collectionName,
  121. })
  122. if err != nil {
  123. glog.Infof("delete collection %s: %v", collectionName, err)
  124. }
  125. return err
  126. })
  127. }
  128. func (f *Filer) maybeDeleteHardLinks(hardLinkIds []HardLinkId) {
  129. for _, hardLinkId := range hardLinkIds {
  130. if err := f.Store.DeleteHardLink(context.Background(), hardLinkId); err != nil {
  131. glog.Errorf("delete hard link id %d : %v", hardLinkId, err)
  132. }
  133. }
  134. }
  135. func (f *Filer) removeEmptyParentFolder(ctx context.Context, dir util.FullPath) error {
  136. if !strings.HasPrefix(string(dir), f.DirBucketsPath) {
  137. return nil
  138. }
  139. parent, _ := dir.DirAndName()
  140. if parent == f.DirBucketsPath {
  141. // should not delete bucket itself
  142. return nil
  143. }
  144. entries, err := f.ListDirectoryEntries(ctx, dir, "", false, 1, "")
  145. if err != nil {
  146. return err
  147. }
  148. if len(entries) > 0 {
  149. return nil
  150. }
  151. if err := f.Store.DeleteEntry(ctx, dir); err != nil {
  152. return err
  153. }
  154. return f.removeEmptyParentFolder(ctx, util.FullPath(parent))
  155. }