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

fs: Implement NewDirTree for non --fast-list

Nick Craig-Woodd5ff7104e53d
diff --git a/fs/walk.go b/fs/walk.go
index ee5268cbf..ef3c9e093 100644
--- a/fs/walk.go
+++ b/fs/walk.go
@@ -311,15 +311,39 @@ func walkRDirTree(f Fs, path string, includeAll bool, maxLevel int, listR ListRF
return dirs, nil
}
+// Create a DirTree using List
+func walkNDirTree(f Fs, path string, includeAll bool, maxLevel int, listDir listDirFunc) (DirTree, error) {
+ dirs := make(DirTree)
+ fn := func(dirPath string, entries DirEntries, err error) error {
+ if err == nil {
+ dirs[dirPath] = entries
+ }
+ return err
+ }
+ err := walk(f, path, includeAll, maxLevel, fn, listDir)
+ if err != nil {
+ return nil, err
+ }
+ return dirs, nil
+}
+
// NewDirTree returns a DirTree filled with the directory listing
-// using the parameters supplied. This will return ErrorCantListR for
-// remotes which don't support ListR.
+// using the parameters supplied.
+//
+// If includeAll is not set it will use the filters defined.
+//
+// If maxLevel is < 0 then it will recurse indefinitely, else it will
+// only do maxLevel levels.
+//
+// This is implemented by WalkR if Config.UseRecursiveListing is true
+// and f supports it and level > 1, or WalkN otherwise.
+//
+// NB (f, path) to be replaced by fs.Dir at some point
func NewDirTree(f Fs, path string, includeAll bool, maxLevel int) (DirTree, error) {
- listR := f.Features().ListR
- if listR == nil {
- return nil, ErrorCantListR
+ if ListR := f.Features().ListR; (maxLevel < 0 || maxLevel > 1) && Config.UseListR && ListR != nil {
+ return walkRDirTree(f, path, includeAll, maxLevel, ListR)
}
- return walkRDirTree(f, path, includeAll, maxLevel, listR)
+ return walkNDirTree(f, path, includeAll, maxLevel, ListDirSorted)
}
func walkR(f Fs, path string, includeAll bool, maxLevel int, fn WalkFunc, listR ListRFn) error {
diff --git a/fs/walk_test.go b/fs/walk_test.go
index 12778e63a..6f4fb6bfd 100644
--- a/fs/walk_test.go
+++ b/fs/walk_test.go
@@ -288,6 +288,26 @@ func TestWalkRLevels(t *testing.T) { testWalkLevels(t, -1).WalkR()
func TestWalkLevelsNoRecursive10(t *testing.T) { testWalkLevels(t, 10).Walk() }
func TestWalkRLevelsNoRecursive10(t *testing.T) { testWalkLevels(t, 10).WalkR() }
+func TestWalkNDirTree(t *testing.T) {
+ ls := testWalkLevels(t, -1)
+ entries, err := walkNDirTree(nil, "", ls.includeAll, ls.maxLevel, ls.ListDir)
+ require.NoError(t, err)
+ assert.Equal(t, `/
+ A
+ a/
+a/
+ B
+ b/
+a/b/
+ C
+ c/
+a/b/c/
+ D
+ d/
+a/b/c/d/
+`, entries.String())
+}
+
func testWalkLevelsNoRecursive(t *testing.T) *listDirs {
da := newDir("a")
oA := mockObject("A")