filer_delete_entry.go 3.7 KB

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