From 7a279d2789cfef281e6623d61f42c8a15853a26b Mon Sep 17 00:00:00 2001 From: dougal Date: Thu, 4 Sep 2025 16:01:33 +0100 Subject: [PATCH] B2: add ListP interface - #4788 --- backend/b2/b2.go | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/backend/b2/b2.go b/backend/b2/b2.go index e54d7a439..d87948e37 100644 --- a/backend/b2/b2.go +++ b/backend/b2/b2.go @@ -847,7 +847,7 @@ func (f *Fs) itemToDirEntry(ctx context.Context, remote string, object *api.File } // listDir lists a single directory -func (f *Fs) listDir(ctx context.Context, bucket, directory, prefix string, addBucket bool) (entries fs.DirEntries, err error) { +func (f *Fs) listDir(ctx context.Context, bucket, directory, prefix string, addBucket bool, callback func(fs.DirEntry) error) (err error) { last := "" err = f.list(ctx, bucket, directory, prefix, f.rootBucket == "", false, 0, f.opt.Versions, false, func(remote string, object *api.File, isDirectory bool) error { entry, err := f.itemToDirEntry(ctx, remote, object, isDirectory, &last) @@ -855,16 +855,16 @@ func (f *Fs) listDir(ctx context.Context, bucket, directory, prefix string, addB return err } if entry != nil { - entries = append(entries, entry) + return callback(entry) } return nil }) if err != nil { - return nil, err + return err } // bucket must be present if listing succeeded f.cache.MarkOK(bucket) - return entries, nil + return nil } // listBuckets returns all the buckets to out @@ -890,14 +890,46 @@ func (f *Fs) listBuckets(ctx context.Context) (entries fs.DirEntries, err error) // This should return ErrDirNotFound if the directory isn't // found. func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) { + return list.WithListP(ctx, dir, f) +} + +// ListP lists the objects and directories of the Fs starting +// from dir non recursively into out. +// +// dir should be "" to start from the root, and should not +// have trailing slashes. +// +// This should return ErrDirNotFound if the directory isn't +// found. +// +// It should call callback for each tranche of entries read. +// These need not be returned in any particular order. If +// callback returns an error then the listing will stop +// immediately. +func (f *Fs) ListP(ctx context.Context, dir string, callback fs.ListRCallback) error { + list := list.NewHelper(callback) bucket, directory := f.split(dir) if bucket == "" { if directory != "" { - return nil, fs.ErrorListBucketRequired + return fs.ErrorListBucketRequired + } + entries, err := f.listBuckets(ctx) + if err != nil { + return err + } + for _, entry := range entries { + err = list.Add(entry) + if err != nil { + return err + } + } + } else { + err := f.listDir(ctx, bucket, directory, f.rootDirectory, f.rootBucket == "", list.Add) + if err != nil { + return err } - return f.listBuckets(ctx) } - return f.listDir(ctx, bucket, directory, f.rootDirectory, f.rootBucket == "") + return list.Flush() } // ListR lists the objects and directories of the Fs starting @@ -2428,6 +2460,7 @@ var ( _ fs.PutStreamer = &Fs{} _ fs.CleanUpper = &Fs{} _ fs.ListRer = &Fs{} + _ fs.ListPer = &Fs{} _ fs.PublicLinker = &Fs{} _ fs.OpenChunkWriter = &Fs{} _ fs.Commander = &Fs{}