filer_delete_entry.go 4.8 KB

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