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

5 years ago
5 years ago
  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 bool, ignoreRecursiveError, shouldDeleteChunks 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)
  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)
  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 bool, ignoreRecursiveError, shouldDeleteChunks 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. return nil, fmt.Errorf("fail to delete non-empty folder: %s", entry.FullPath)
  58. }
  59. for _, sub := range entries {
  60. lastFileName = sub.Name()
  61. var dirChunks []*filer_pb.FileChunk
  62. if sub.IsDirectory() {
  63. dirChunks, err = f.doBatchDeleteFolderMetaAndData(ctx, sub, isRecursive, ignoreRecursiveError, shouldDeleteChunks)
  64. f.cacheDelDirectory(string(sub.FullPath))
  65. f.NotifyUpdateEvent(sub, nil, 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. 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. return chunks, nil
  83. }
  84. func (f *Filer) doDeleteEntryMetaAndData(ctx context.Context, entry *Entry, shouldDeleteChunks bool) (err error) {
  85. glog.V(3).Infof("deleting entry %v, delete chunks: %v", entry.FullPath, shouldDeleteChunks)
  86. if storeDeletionErr := f.store.DeleteEntry(ctx, entry.FullPath); storeDeletionErr != nil {
  87. return fmt.Errorf("filer store delete: %v", storeDeletionErr)
  88. }
  89. if entry.IsDirectory() {
  90. f.cacheDelDirectory(string(entry.FullPath))
  91. }
  92. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  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. }