filer_delete_entry.go 5.1 KB

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