Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

swift: add ListP interface - #4788

dougalc9ce686231c5
diff --git a/backend/swift/swift.go b/backend/swift/swift.go
index 4169f2cb3..d4d5c5fc9 100644
--- a/backend/swift/swift.go
+++ b/backend/swift/swift.go
@@ -773,21 +773,20 @@ func (f *Fs) list(ctx context.Context, container, directory, prefix string, addC
}
// listDir lists a single directory
-func (f *Fs) listDir(ctx context.Context, container, directory, prefix string, addContainer bool) (entries fs.DirEntries, err error) {
+func (f *Fs) listDir(ctx context.Context, container, directory, prefix string, addContainer bool, callback func(fs.DirEntry) error) (err error) {
if container == "" {
- return nil, fs.ErrorListBucketRequired
+ return fs.ErrorListBucketRequired
}
// List the objects
err = f.list(ctx, container, directory, prefix, addContainer, false, false, func(entry fs.DirEntry) error {
- entries = append(entries, entry)
- return nil
+ return callback(entry)
})
if err != nil {
- return nil, err
+ return err
}
// container must be present if listing succeeded
f.cache.MarkOK(container)
- return entries, nil
+ return nil
}
// listContainers lists the containers
@@ -818,14 +817,46 @@ func (f *Fs) listContainers(ctx context.Context) (entries fs.DirEntries, err err
// 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)
container, directory := f.split(dir)
if container == "" {
if directory != "" {
- return nil, fs.ErrorListBucketRequired
+ return fs.ErrorListBucketRequired
+ }
+ entries, err := f.listContainers(ctx)
+ if err != nil {
+ return err
+ }
+ for _, entry := range entries {
+ err = list.Add(entry)
+ if err != nil {
+ return err
+ }
+ }
+ } else {
+ err := f.listDir(ctx, container, directory, f.rootDirectory, f.rootContainer == "", list.Add)
+ if err != nil {
+ return err
}
- return f.listContainers(ctx)
}
- return f.listDir(ctx, container, directory, f.rootDirectory, f.rootContainer == "")
+ return list.Flush()
}
// ListR lists the objects and directories of the Fs starting
@@ -1650,6 +1681,7 @@ var (
_ fs.PutStreamer = &Fs{}
_ fs.Copier = &Fs{}
_ fs.ListRer = &Fs{}
+ _ fs.ListPer = &Fs{}
_ fs.Object = &Object{}
_ fs.MimeTyper = &Object{}
)