package filer import ( "context" "fmt" "strings" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/util" ) // StreamListRecursiveEntries lists up to limit entries below p in path order // with the store's RecursiveLister, leaving out expired entries and deleting // them as a directory listing does. A subtree in or holding a remote mount is // declined: its directory listings fetch from the remote. func (f *Filer) StreamListRecursiveEntries(ctx context.Context, p, startPath util.FullPath, prefix string, limit int64, eachEntryFunc ListEachEntryFunc) (lastPath util.FullPath, err error) { lister, ok := f.Store.(RecursiveLister) if !ok { return "", ErrUnsupportedRecursiveListing } if p = util.FullPath(strings.TrimSuffix(string(p), "/")); p == "" { p = "/" } if f.RemoteStorage != nil { for _, mount := range f.RemoteStorage.MountedDirectories() { if mount == p || mount.IsUnder(p) || p.IsUnder(mount) { return "", fmt.Errorf("%w: %s is remote mounted", ErrUnsupportedRecursiveListing, mount) } } } lastPath = startPath for limit > 0 { var expiredEntries []*Entry read, stopped := int64(0), false page := min(limit, int64(PaginationSize)) var pageLast util.FullPath pageLast, err = lister.ListRecursivePrefixedEntries(ctx, p, lastPath, prefix, page, func(entry *Entry) (bool, error) { read++ if ctxErr := ctx.Err(); ctxErr != nil { return false, ctxErr } if listingExpired(entry) { expiredEntries = append(expiredEntries, entry) return true, nil } limit-- more, eachErr := eachEntryFunc(entry) stopped = !more || limit == 0 return !stopped, eachErr }) if pageLast != "" { lastPath = pageLast } for _, entry := range expiredEntries { if delErr := f.deleteExpiredEntry(ctx, entry.FullPath); delErr != nil { glog.ErrorfCtx(ctx, "list %s recursively: delete expired %s: %v", p, entry.FullPath, delErr) } } if err != nil || stopped || read < page { return lastPath, err } } return lastPath, nil }