filer_delete_entry.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package filer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  8. "github.com/seaweedfs/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.GetChunks())
  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. }
  56. return nil
  57. }
  58. func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isDeletingBucket, isFromOtherCluster bool, signatures []int32, onChunksFn OnChunksFunc, onHardLinkIdsFn OnHardLinkIdsFunc) (err error) {
  59. lastFileName := ""
  60. includeLastFile := false
  61. if !isDeletingBucket || !f.Store.CanDropWholeBucket() {
  62. for {
  63. entries, _, err := f.ListDirectoryEntries(ctx, entry.FullPath, lastFileName, includeLastFile, PaginationSize, "", "", "")
  64. if err != nil {
  65. glog.Errorf("list folder %s: %v", entry.FullPath, err)
  66. return fmt.Errorf("list folder %s: %v", entry.FullPath, err)
  67. }
  68. if lastFileName == "" && !isRecursive && len(entries) > 0 {
  69. // only for first iteration in the loop
  70. glog.V(0).Infof("deleting a folder %s has children: %+v ...", entry.FullPath, entries[0].Name())
  71. return fmt.Errorf("%s: %s", MsgFailDelNonEmptyFolder, entry.FullPath)
  72. }
  73. for _, sub := range entries {
  74. lastFileName = sub.Name()
  75. if sub.IsDirectory() {
  76. subIsDeletingBucket := f.isBucket(sub)
  77. err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks, subIsDeletingBucket, false, nil, onChunksFn, onHardLinkIdsFn)
  78. } else {
  79. f.NotifyUpdateEvent(ctx, sub, nil, shouldDeleteChunks, isFromOtherCluster, nil)
  80. if len(sub.HardLinkId) != 0 {
  81. // hard link chunk data are deleted separately
  82. err = onHardLinkIdsFn([]HardLinkId{sub.HardLinkId})
  83. } else {
  84. err = onChunksFn(sub.GetChunks())
  85. }
  86. }
  87. if err != nil && !ignoreRecursiveError {
  88. return err
  89. }
  90. }
  91. if len(entries) < PaginationSize {
  92. break
  93. }
  94. }
  95. }
  96. glog.V(3).Infof("deleting directory %v delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  97. if storeDeletionErr := f.Store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
  98. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  99. }
  100. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  101. return nil
  102. }
  103. func (f *Filer) doDeleteEntryMetaAndData(ctx context.Context, entry *Entry, shouldDeleteChunks bool, isFromOtherCluster bool, signatures []int32) (err error) {
  104. glog.V(3).Infof("deleting entry %v, delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  105. if storeDeletionErr := f.Store.DeleteOneEntry(ctx, entry); storeDeletionErr != nil {
  106. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  107. }
  108. if !entry.IsDirectory() {
  109. f.NotifyUpdateEvent(ctx, entry, nil, shouldDeleteChunks, isFromOtherCluster, signatures)
  110. }
  111. return nil
  112. }
  113. func (f *Filer) doDeleteCollection(collectionName string) (err error) {
  114. return f.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  115. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  116. Name: collectionName,
  117. })
  118. if err != nil {
  119. glog.Infof("delete collection %s: %v", collectionName, err)
  120. }
  121. return err
  122. })
  123. }
  124. func (f *Filer) maybeDeleteHardLinks(hardLinkIds []HardLinkId) {
  125. for _, hardLinkId := range hardLinkIds {
  126. if err := f.Store.DeleteHardLink(context.Background(), hardLinkId); err != nil {
  127. glog.Errorf("delete hard link id %d : %v", hardLinkId, err)
  128. }
  129. }
  130. }