From d5ff7104e53d55a454208922682e3dd26bbbb138 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 24 Jul 2017 22:52:18 +0100 Subject: [PATCH] fs: Implement NewDirTree for non --fast-list --- fs/walk.go | 40 ++++++++++++++++++++++++++++++++-------- fs/walk_test.go | 20 ++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) 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 } -// NewDirTree returns a DirTree filled with the directory listing -// using the parameters supplied. This will return ErrorCantListR for -// remotes which don't support ListR. -func NewDirTree(f Fs, path string, includeAll bool, maxLevel int) (DirTree, error) { - listR := f.Features().ListR - if listR == nil { - return nil, ErrorCantListR +// 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 } - return walkRDirTree(f, path, includeAll, maxLevel, listR) + 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. +// +// 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) { + if ListR := f.Features().ListR; (maxLevel < 0 || maxLevel > 1) && Config.UseListR && ListR != nil { + 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")