filer_search.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package filer
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "path/filepath"
  6. "strings"
  7. )
  8. func splitPattern(pattern string) (prefix string, restPattern string) {
  9. position := strings.Index(pattern, "*")
  10. if position >= 0 {
  11. return pattern[:position], pattern[position:]
  12. }
  13. position = strings.Index(pattern, "?")
  14. if position >= 0 {
  15. return pattern[:position], pattern[position:]
  16. }
  17. return "", restPattern
  18. }
  19. // For now, prefix and namePattern are mutually exclusive
  20. func (f *Filer) ListDirectoryEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, namePattern string, namePatternExclude string) (entries []*Entry, hasMore bool, err error) {
  21. _, err = f.StreamListDirectoryEntries(ctx, p, startFileName, inclusive, limit+1, prefix, namePattern, namePatternExclude, func(entry *Entry) bool {
  22. entries = append(entries, entry)
  23. return true
  24. })
  25. hasMore = int64(len(entries)) >= limit+1
  26. if hasMore {
  27. entries = entries[:limit]
  28. }
  29. return entries, hasMore, err
  30. }
  31. // For now, prefix and namePattern are mutually exclusive
  32. func (f *Filer) StreamListDirectoryEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, namePattern string, namePatternExclude string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
  33. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  34. p = p[0 : len(p)-1]
  35. }
  36. prefixInNamePattern, restNamePattern := splitPattern(namePattern)
  37. if prefixInNamePattern != "" {
  38. prefix = prefixInNamePattern
  39. }
  40. var missedCount int64
  41. missedCount, lastFileName, err = f.doListPatternMatchedEntries(ctx, p, startFileName, inclusive, limit, prefix, restNamePattern, namePatternExclude, eachEntryFunc)
  42. for missedCount > 0 && err == nil {
  43. missedCount, lastFileName, err = f.doListPatternMatchedEntries(ctx, p, lastFileName, false, missedCount, prefix, restNamePattern, namePatternExclude, eachEntryFunc)
  44. }
  45. return
  46. }
  47. func (f *Filer) doListPatternMatchedEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix, restNamePattern string, namePatternExclude string, eachEntryFunc ListEachEntryFunc) (missedCount int64, lastFileName string, err error) {
  48. if len(restNamePattern) == 0 && len(namePatternExclude) == 0 {
  49. lastFileName, err = f.doListValidEntries(ctx, p, startFileName, inclusive, limit, prefix, eachEntryFunc)
  50. return 0, lastFileName, err
  51. }
  52. lastFileName, err = f.doListValidEntries(ctx, p, startFileName, inclusive, limit, prefix, func(entry *Entry) bool {
  53. nameToTest := entry.Name()
  54. if len(namePatternExclude) > 0 {
  55. if matched, matchErr := filepath.Match(namePatternExclude, nameToTest); matchErr == nil && matched {
  56. missedCount++
  57. return true
  58. }
  59. }
  60. if len(restNamePattern) > 0 {
  61. if matched, matchErr := filepath.Match(restNamePattern, nameToTest[len(prefix):]); matchErr == nil && !matched {
  62. missedCount++
  63. return true
  64. }
  65. }
  66. if !eachEntryFunc(entry) {
  67. return false
  68. }
  69. return true
  70. })
  71. if err != nil {
  72. return
  73. }
  74. return
  75. }
  76. func (f *Filer) doListValidEntries(ctx context.Context, p util.FullPath, startFileName string, inclusive bool, limit int64, prefix string, eachEntryFunc ListEachEntryFunc) (lastFileName string, err error) {
  77. var expiredCount int64
  78. expiredCount, lastFileName, err = f.doListDirectoryEntries(ctx, p, startFileName, inclusive, limit, prefix, eachEntryFunc)
  79. for expiredCount > 0 && err == nil {
  80. expiredCount, lastFileName, err = f.doListDirectoryEntries(ctx, p, lastFileName, false, expiredCount, prefix, eachEntryFunc)
  81. }
  82. return
  83. }