filer_delete_entry.go 4.0 KB

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