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.

160 lines
5.3 KiB

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