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.

128 lines
3.8 KiB

  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(ctx, 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. for _, child := range entries {
  57. println("existing children", child.Name())
  58. }
  59. return nil, fmt.Errorf("fail to delete non-empty folder: %s", entry.FullPath)
  60. }
  61. for _, sub := range entries {
  62. lastFileName = sub.Name()
  63. var dirChunks []*filer_pb.FileChunk
  64. if sub.IsDirectory() {
  65. dirChunks, err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks)
  66. chunks = append(chunks, dirChunks...)
  67. } else {
  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. f.cacheDelDirectory(string(entry.FullPath))
  79. glog.V(3).Infof("deleting directory %v delete %d chunks: %v", entry.FullPath, len(chunks), shouldDeleteChunks)
  80. if storeDeletionErr := f.store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
  81. return nil, fmt.Errorf("filer store delete: %v", storeDeletionErr)
  82. }
  83. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  84. return chunks, nil
  85. }
  86. func (f *Filer) doDeleteEntryMetaAndData(ctx context.Context, entry *Entry, shouldDeleteChunks bool) (err error) {
  87. glog.V(3).Infof("deleting entry %v, delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  88. if storeDeletionErr := f.store.DeleteEntry(ctx, entry.FullPath); storeDeletionErr != nil {
  89. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  90. }
  91. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  92. return nil
  93. }
  94. func (f *Filer) doDeleteCollection(ctx context.Context, collectionName string) (err error) {
  95. return f.MasterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  96. _, err := client.CollectionDelete(ctx, &master_pb.CollectionDeleteRequest{
  97. Name: collectionName,
  98. })
  99. if err != nil {
  100. glog.Infof("delete collection %s: %v", collectionName, err)
  101. }
  102. return err
  103. })
  104. }