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.

126 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. chunks = append(chunks, dirChunks...)
  65. } else {
  66. chunks = append(chunks, sub.Chunks...)
  67. }
  68. if err != nil && !ignoreRecursiveError {
  69. return nil, err
  70. }
  71. }
  72. if len(entries) < PaginationSize {
  73. break
  74. }
  75. }
  76. f.cacheDelDirectory(string(entry.FullPath))
  77. glog.V(3).Infof("deleting directory %v delete %d chunks: %v", entry.FullPath, len(chunks), shouldDeleteChunks)
  78. if storeDeletionErr := f.store.DeleteFolderChildren(ctx, entry.FullPath); storeDeletionErr != nil {
  79. return nil, fmt.Errorf("filer store delete: %v", storeDeletionErr)
  80. }
  81. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  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. f.NotifyUpdateEvent(entry, nil, shouldDeleteChunks)
  90. return nil
  91. }
  92. func (f *Filer) doDeleteCollection(collectionName string) (err error) {
  93. return f.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  94. _, err := client.CollectionDelete(context.Background(), &master_pb.CollectionDeleteRequest{
  95. Name: collectionName,
  96. })
  97. if err != nil {
  98. glog.Infof("delete collection %s: %v", collectionName, err)
  99. }
  100. return err
  101. })
  102. }