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.

161 lines
5.3 KiB

5 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. type HardLinkId []byte
  11. const (
  12. MsgFailDelNonEmptyFolder = "fail to delete non-empty folder"
  13. )
  14. func (f *Filer) DeleteEntryMetaAndData(ctx context.Context, p util.FullPath, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isFromOtherCluster bool, signatures []int32) (err error) {
  15. if p == "/" {
  16. return nil
  17. }
  18. entry, findErr := f.FindEntry(ctx, p)
  19. if findErr != nil {
  20. return findErr
  21. }
  22. isDeleteCollection := f.isBucket(entry)
  23. var chunks []*filer_pb.FileChunk
  24. var hardLinkIds []HardLinkId
  25. chunks = append(chunks, entry.Chunks...)
  26. if entry.IsDirectory() {
  27. // delete the folder children, not including the folder itself
  28. var dirChunks []*filer_pb.FileChunk
  29. var dirHardLinkIds []HardLinkId
  30. dirChunks, dirHardLinkIds, err = f.doBatchDeleteFolderMetaAndData(ctx, entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks && !isDeleteCollection, isDeleteCollection, isFromOtherCluster, signatures)
  31. if err != nil {
  32. glog.V(0).Infof("delete directory %s: %v", p, err)
  33. return fmt.Errorf("delete directory %s: %v", p, err)
  34. }
  35. chunks = append(chunks, dirChunks...)
  36. hardLinkIds = append(hardLinkIds, dirHardLinkIds...)
  37. }
  38. // delete the file or folder
  39. err = f.doDeleteEntryMetaAndData(ctx, entry, shouldDeleteChunks, isFromOtherCluster, signatures)
  40. if err != nil {
  41. return fmt.Errorf("delete file %s: %v", p, err)
  42. }
  43. if shouldDeleteChunks && !isDeleteCollection {
  44. f.DirectDeleteChunks(chunks)
  45. }
  46. // A case not handled:
  47. // what if the chunk is in a different collection?
  48. if shouldDeleteChunks {
  49. f.maybeDeleteHardLinks(hardLinkIds)
  50. }
  51. if isDeleteCollection {
  52. collectionName := entry.Name()
  53. f.doDeleteCollection(collectionName)
  54. f.deleteBucket(collectionName)
  55. }
  56. return nil
  57. }
  58. func (f *Filer) doBatchDeleteFolderMetaAndData(ctx context.Context, entry *Entry, isRecursive, ignoreRecursiveError, shouldDeleteChunks, isDeletingBucket, isFromOtherCluster bool, signatures []int32) (chunks []*filer_pb.FileChunk, hardlinkIds []HardLinkId, err error) {
  59. lastFileName := ""
  60. includeLastFile := false
  61. if !isDeletingBucket {
  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 nil, nil, 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.Errorf("deleting a folder %s has children: %+v ...", entry.FullPath, entries[0].Name())
  71. return nil, nil, fmt.Errorf("%s: %s", MsgFailDelNonEmptyFolder, entry.FullPath)
  72. }
  73. for _, sub := range entries {
  74. lastFileName = sub.Name()
  75. var dirChunks []*filer_pb.FileChunk
  76. var dirHardLinkIds []HardLinkId
  77. if sub.IsDirectory() {
  78. subIsDeletingBucket := f.isBucket(sub)
  79. dirChunks, dirHardLinkIds, err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks, subIsDeletingBucket, false, nil)
  80. chunks = append(chunks, dirChunks...)
  81. hardlinkIds = append(hardlinkIds, dirHardLinkIds...)
  82. } else {
  83. f.NotifyUpdateEvent(ctx, sub, nil, shouldDeleteChunks, isFromOtherCluster, nil)
  84. if len(sub.HardLinkId) != 0 {
  85. // hard link chunk data are deleted separately
  86. hardlinkIds = append(hardlinkIds, sub.HardLinkId)
  87. } else {
  88. chunks = append(chunks, sub.Chunks...)
  89. }
  90. }
  91. if err != nil && !ignoreRecursiveError {
  92. return nil, nil, err
  93. }
  94. }
  95. if len(entries) < PaginationSize {
  96. break
  97. }
  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. }