2018-01-12 17:30:54 +01:00
|
|
|
// Package walk walks directories
|
|
|
|
package walk
|
2017-02-24 23:51:01 +01:00
|
|
|
|
|
|
|
import (
|
2019-06-17 10:34:30 +02:00
|
|
|
"context"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-06-06 17:40:00 +02:00
|
|
|
"path"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2017-02-24 23:51:01 +01:00
|
|
|
"sync"
|
2017-06-30 14:37:29 +02:00
|
|
|
"time"
|
2017-02-24 23:51:01 +01:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/dirtree"
|
|
|
|
"github.com/rclone/rclone/fs/filter"
|
|
|
|
"github.com/rclone/rclone/fs/list"
|
2017-02-24 23:51:01 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrorSkipDir is used as a return value from Walk to indicate that the
|
|
|
|
// directory named in the call is to be skipped. It is not returned as
|
|
|
|
// an error by any function.
|
|
|
|
var ErrorSkipDir = errors.New("skip this directory")
|
|
|
|
|
2017-06-06 17:40:00 +02:00
|
|
|
// ErrorCantListR is returned by WalkR if the underlying Fs isn't
|
|
|
|
// capable of doing a recursive listing.
|
|
|
|
var ErrorCantListR = errors.New("recursive directory listing not available")
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// Func is the type of the function called for directory
|
2017-02-24 23:51:01 +01:00
|
|
|
// visited by Walk. The path argument contains remote path to the directory.
|
|
|
|
//
|
|
|
|
// If there was a problem walking to directory named by path, the
|
|
|
|
// incoming error will describe the problem and the function can
|
|
|
|
// decide how to handle that error (and Walk will not descend into
|
|
|
|
// that directory). If an error is returned, processing stops. The
|
|
|
|
// sole exception is when the function returns the special value
|
|
|
|
// ErrorSkipDir. If the function returns ErrorSkipDir, Walk skips the
|
|
|
|
// directory's contents entirely.
|
2018-01-12 17:30:54 +01:00
|
|
|
type Func func(path string, entries fs.DirEntries, err error) error
|
2017-02-24 23:51:01 +01:00
|
|
|
|
|
|
|
// Walk lists the directory.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// It calls fn for each tranche of DirEntries read.
|
|
|
|
//
|
|
|
|
// Note that fn will not be called concurrently whereas the directory
|
|
|
|
// listing will proceed concurrently.
|
|
|
|
//
|
2022-08-05 17:35:41 +02:00
|
|
|
// Parent directories are always listed before their children.
|
2017-02-24 23:51:01 +01:00
|
|
|
//
|
2019-08-06 23:21:19 +02:00
|
|
|
// This is implemented by WalkR if Config.UseListR is true
|
2017-06-06 17:40:00 +02:00
|
|
|
// and f supports it and level > 1, or WalkN otherwise.
|
|
|
|
//
|
2019-02-13 18:14:51 +01:00
|
|
|
// If --files-from and --no-traverse is set then a DirTree will be
|
|
|
|
// constructed with just those files in and then walked with WalkR
|
2018-10-19 18:41:14 +02:00
|
|
|
//
|
2020-12-02 17:20:58 +01:00
|
|
|
// Note: this will flag filter-aware backends!
|
|
|
|
//
|
2017-02-24 23:51:01 +01:00
|
|
|
// NB (f, path) to be replaced by fs.Dir at some point
|
2019-06-17 10:34:30 +02:00
|
|
|
func Walk(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, fn Func) error {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(ctx)
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
2022-09-05 17:19:50 +02:00
|
|
|
ctx = filter.SetUseFilter(ctx, f.Features().FilterAware && !includeAll) // make filter-aware backends constrain List
|
2020-11-26 18:10:41 +01:00
|
|
|
if ci.NoTraverse && fi.HaveFilesFrom() {
|
|
|
|
return walkR(ctx, f, path, includeAll, maxLevel, fn, fi.MakeListR(ctx, f.NewObject))
|
2018-10-19 18:41:14 +02:00
|
|
|
}
|
2019-01-21 11:02:23 +01:00
|
|
|
// FIXME should this just be maxLevel < 0 - why the maxLevel > 1
|
2020-11-05 12:33:32 +01:00
|
|
|
if (maxLevel < 0 || maxLevel > 1) && ci.UseListR && f.Features().ListR != nil {
|
2019-06-17 10:34:30 +02:00
|
|
|
return walkListR(ctx, f, path, includeAll, maxLevel, fn)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
return walkListDirSorted(ctx, f, path, includeAll, maxLevel, fn)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 11:02:23 +01:00
|
|
|
// ListType is uses to choose which combination of files or directories is requires
|
|
|
|
type ListType byte
|
|
|
|
|
|
|
|
// Types of listing for ListR
|
|
|
|
const (
|
|
|
|
ListObjects ListType = 1 << iota // list objects only
|
|
|
|
ListDirs // list dirs only
|
|
|
|
ListAll = ListObjects | ListDirs // list files and dirs
|
|
|
|
)
|
|
|
|
|
|
|
|
// Objects returns true if the list type specifies objects
|
|
|
|
func (l ListType) Objects() bool {
|
|
|
|
return (l & ListObjects) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dirs returns true if the list type specifies dirs
|
|
|
|
func (l ListType) Dirs() bool {
|
|
|
|
return (l & ListDirs) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter in (inplace) to only contain the type of list entry required
|
|
|
|
func (l ListType) Filter(in *fs.DirEntries) {
|
|
|
|
if l == ListAll {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
out := (*in)[:0]
|
|
|
|
for _, entry := range *in {
|
|
|
|
switch entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
if l.Objects() {
|
|
|
|
out = append(out, entry)
|
|
|
|
}
|
|
|
|
case fs.Directory:
|
|
|
|
if l.Dirs() {
|
|
|
|
out = append(out, entry)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
fs.Errorf(nil, "Unknown object type %T", entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*in = out
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListR lists the directory recursively.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
2021-11-04 12:50:43 +01:00
|
|
|
// If synthesizeDirs is set then for bucket-based remotes it will
|
2019-01-21 11:02:23 +01:00
|
|
|
// synthesize directories from the file structure. This uses extra
|
|
|
|
// memory so don't set this if you don't need directories, likewise do
|
|
|
|
// set this if you are interested in directories.
|
|
|
|
//
|
|
|
|
// It calls fn for each tranche of DirEntries read. Note that these
|
|
|
|
// don't necessarily represent a directory
|
|
|
|
//
|
|
|
|
// Note that fn will not be called concurrently whereas the directory
|
|
|
|
// listing will proceed concurrently.
|
|
|
|
//
|
|
|
|
// Directories are not listed in any particular order so you can't
|
|
|
|
// rely on parents coming before children or alphabetical ordering
|
|
|
|
//
|
|
|
|
// This is implemented by using ListR on the backend if possible and
|
|
|
|
// efficient, otherwise by Walk.
|
|
|
|
//
|
2020-12-02 17:20:58 +01:00
|
|
|
// Note: this will flag filter-aware backends
|
|
|
|
//
|
2019-01-21 11:02:23 +01:00
|
|
|
// NB (f, path) to be replaced by fs.Dir at some point
|
2019-06-17 10:34:30 +02:00
|
|
|
func ListR(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, listType ListType, fn fs.ListRCallback) error {
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
2019-01-21 11:02:23 +01:00
|
|
|
// FIXME disable this with --no-fast-list ??? `--disable ListR` will do it...
|
|
|
|
doListR := f.Features().ListR
|
|
|
|
|
|
|
|
// Can't use ListR if...
|
|
|
|
if doListR == nil || // ...no ListR
|
2020-11-26 18:10:41 +01:00
|
|
|
fi.HaveFilesFrom() || // ...using --files-from
|
2019-01-21 11:02:23 +01:00
|
|
|
maxLevel >= 0 || // ...using bounded recursion
|
2020-11-26 18:10:41 +01:00
|
|
|
len(fi.Opt.ExcludeFile) > 0 || // ...using --exclude-file
|
|
|
|
fi.UsesDirectoryFilters() { // ...using any directory filters
|
2019-06-17 10:34:30 +02:00
|
|
|
return listRwalk(ctx, f, path, includeAll, maxLevel, listType, fn)
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
2022-09-05 17:19:50 +02:00
|
|
|
ctx = filter.SetUseFilter(ctx, f.Features().FilterAware && !includeAll) // make filter-aware backends constrain List
|
2019-06-17 10:34:30 +02:00
|
|
|
return listR(ctx, f, path, includeAll, listType, fn, doListR, listType.Dirs() && f.Features().BucketBased)
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// listRwalk walks the file tree for ListR using Walk
|
2020-12-02 17:20:58 +01:00
|
|
|
// Note: this will flag filter-aware backends (via Walk)
|
2019-06-17 10:34:30 +02:00
|
|
|
func listRwalk(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, listType ListType, fn fs.ListRCallback) error {
|
2019-01-21 11:02:23 +01:00
|
|
|
var listErr error
|
2019-06-17 10:34:30 +02:00
|
|
|
walkErr := Walk(ctx, f, path, includeAll, maxLevel, func(path string, entries fs.DirEntries, err error) error {
|
2019-01-21 11:02:23 +01:00
|
|
|
// Carry on listing but return the error at the end
|
|
|
|
if err != nil {
|
|
|
|
listErr = err
|
2021-12-08 17:14:45 +01:00
|
|
|
err = fs.CountError(ctx, err)
|
2019-01-21 11:02:23 +01:00
|
|
|
fs.Errorf(path, "error listing: %v", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
listType.Filter(&entries)
|
|
|
|
return fn(entries)
|
|
|
|
})
|
|
|
|
if listErr != nil {
|
|
|
|
return listErr
|
|
|
|
}
|
|
|
|
return walkErr
|
|
|
|
}
|
|
|
|
|
2021-11-04 12:50:43 +01:00
|
|
|
// dirMap keeps track of directories made for bucket-based remotes.
|
2019-01-21 11:02:23 +01:00
|
|
|
// true => directory has been sent
|
|
|
|
// false => directory has been seen but not sent
|
|
|
|
type dirMap struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
m map[string]bool
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a new dirMap
|
|
|
|
func newDirMap(root string) *dirMap {
|
|
|
|
return &dirMap{
|
|
|
|
m: make(map[string]bool),
|
|
|
|
root: root,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// add adds a directory and parents with sent
|
|
|
|
func (dm *dirMap) add(dir string, sent bool) {
|
|
|
|
for {
|
|
|
|
if dir == dm.root || dir == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentSent, found := dm.m[dir]
|
|
|
|
if found {
|
|
|
|
// If it has been sent already then nothing more to do
|
|
|
|
if currentSent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If not sent already don't override
|
|
|
|
if !sent {
|
|
|
|
return
|
|
|
|
}
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
// currentSent == false && sent == true so needs overriding
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
|
|
|
dm.m[dir] = sent
|
|
|
|
// Add parents in as unsent
|
|
|
|
dir = parentDir(dir)
|
|
|
|
sent = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:54:43 +02:00
|
|
|
// parentDir finds the parent directory of path
|
|
|
|
func parentDir(entryPath string) string {
|
|
|
|
dirPath := path.Dir(entryPath)
|
|
|
|
if dirPath == "." {
|
|
|
|
dirPath = ""
|
|
|
|
}
|
|
|
|
return dirPath
|
|
|
|
}
|
|
|
|
|
2019-01-21 11:02:23 +01:00
|
|
|
// add all the directories in entries and their parents to the dirMap
|
|
|
|
func (dm *dirMap) addEntries(entries fs.DirEntries) error {
|
|
|
|
dm.mu.Lock()
|
|
|
|
defer dm.mu.Unlock()
|
|
|
|
for _, entry := range entries {
|
|
|
|
switch x := entry.(type) {
|
|
|
|
case fs.Object:
|
|
|
|
dm.add(parentDir(x.Remote()), false)
|
|
|
|
case fs.Directory:
|
|
|
|
dm.add(x.Remote(), true)
|
|
|
|
default:
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("unknown object type %T", entry)
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// send any missing parents to fn
|
|
|
|
func (dm *dirMap) sendEntries(fn fs.ListRCallback) (err error) {
|
|
|
|
// Count the strings first so we allocate the minimum memory
|
|
|
|
n := 0
|
|
|
|
for _, sent := range dm.m {
|
|
|
|
if !sent {
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if n == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dirs := make([]string, 0, n)
|
|
|
|
// Fill the dirs up then sort it
|
|
|
|
for dir, sent := range dm.m {
|
|
|
|
if !sent {
|
|
|
|
dirs = append(dirs, dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(dirs)
|
|
|
|
// Now convert to bulkier Dir in batches and send
|
|
|
|
now := time.Now()
|
|
|
|
list := NewListRHelper(fn)
|
|
|
|
for _, dir := range dirs {
|
|
|
|
err = list.Add(fs.NewDir(dir, now))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// listR walks the file tree using ListR
|
2019-06-17 10:34:30 +02:00
|
|
|
func listR(ctx context.Context, f fs.Fs, path string, includeAll bool, listType ListType, fn fs.ListRCallback, doListR fs.ListRFn, synthesizeDirs bool) error {
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
|
|
|
includeDirectory := fi.IncludeDirectory(ctx, f)
|
2019-01-21 11:02:23 +01:00
|
|
|
if !includeAll {
|
2020-11-26 18:10:41 +01:00
|
|
|
includeAll = fi.InActive()
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
|
|
|
var dm *dirMap
|
|
|
|
if synthesizeDirs {
|
|
|
|
dm = newDirMap(path)
|
|
|
|
}
|
|
|
|
var mu sync.Mutex
|
2019-06-17 10:34:30 +02:00
|
|
|
err := doListR(ctx, path, func(entries fs.DirEntries) (err error) {
|
2019-01-21 11:02:23 +01:00
|
|
|
if synthesizeDirs {
|
|
|
|
err = dm.addEntries(entries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listType.Filter(&entries)
|
|
|
|
if !includeAll {
|
|
|
|
filteredEntries := entries[:0]
|
|
|
|
for _, entry := range entries {
|
|
|
|
var include bool
|
|
|
|
switch x := entry.(type) {
|
|
|
|
case fs.Object:
|
2020-11-26 18:10:41 +01:00
|
|
|
include = fi.IncludeObject(ctx, x)
|
2019-01-21 11:02:23 +01:00
|
|
|
case fs.Directory:
|
|
|
|
include, err = includeDirectory(x.Remote())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
default:
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("unknown object type %T", entry)
|
2019-01-21 11:02:23 +01:00
|
|
|
}
|
|
|
|
if include {
|
|
|
|
filteredEntries = append(filteredEntries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entries = filteredEntries
|
|
|
|
}
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
return fn(entries)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if synthesizeDirs {
|
|
|
|
err = dm.sendEntries(fn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// walkListDirSorted lists the directory.
|
2017-06-06 17:40:00 +02:00
|
|
|
//
|
|
|
|
// It implements Walk using non recursive directory listing.
|
2019-06-17 10:34:30 +02:00
|
|
|
func walkListDirSorted(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, fn Func) error {
|
|
|
|
return walk(ctx, f, path, includeAll, maxLevel, fn, list.DirSorted)
|
2017-02-24 23:51:01 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// walkListR lists the directory.
|
2017-06-06 17:40:00 +02:00
|
|
|
//
|
|
|
|
// It implements Walk using recursive directory listing if
|
|
|
|
// available, or returns ErrorCantListR if not.
|
2019-06-17 10:34:30 +02:00
|
|
|
func walkListR(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, fn Func) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
listR := f.Features().ListR
|
|
|
|
if listR == nil {
|
|
|
|
return ErrorCantListR
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
return walkR(ctx, f, path, includeAll, maxLevel, fn, listR)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
type listDirFunc func(ctx context.Context, fs fs.Fs, includeAll bool, dir string) (entries fs.DirEntries, err error)
|
2017-02-24 23:51:01 +01:00
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
func walk(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, fn Func, listDir listDirFunc) error {
|
2017-02-24 23:51:01 +01:00
|
|
|
var (
|
2020-11-05 12:33:32 +01:00
|
|
|
wg sync.WaitGroup // sync closing of go routines
|
|
|
|
traversing sync.WaitGroup // running directory traversals
|
|
|
|
doClose sync.Once // close the channel once
|
|
|
|
mu sync.Mutex // stop fn being called concurrently
|
|
|
|
ci = fs.GetConfig(ctx) // current config
|
2017-02-24 23:51:01 +01:00
|
|
|
)
|
|
|
|
// listJob describe a directory listing that needs to be done
|
|
|
|
type listJob struct {
|
|
|
|
remote string
|
|
|
|
depth int
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:33:32 +01:00
|
|
|
in := make(chan listJob, ci.Checkers)
|
2017-02-24 23:51:01 +01:00
|
|
|
errs := make(chan error, 1)
|
|
|
|
quit := make(chan struct{})
|
|
|
|
closeQuit := func() {
|
|
|
|
doClose.Do(func() {
|
|
|
|
close(quit)
|
|
|
|
go func() {
|
2018-03-02 18:01:58 +01:00
|
|
|
for range in {
|
2017-02-24 23:51:01 +01:00
|
|
|
traversing.Done()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
})
|
|
|
|
}
|
2020-11-05 12:33:32 +01:00
|
|
|
for i := 0; i < ci.Checkers; i++ {
|
2017-02-24 23:51:01 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case job, ok := <-in:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
entries, err := listDir(ctx, f, includeAll, job.remote)
|
2017-06-15 17:40:56 +02:00
|
|
|
var jobs []listJob
|
|
|
|
if err == nil && job.depth != 0 {
|
2018-01-12 17:30:54 +01:00
|
|
|
entries.ForDir(func(dir fs.Directory) {
|
2017-06-15 17:40:56 +02:00
|
|
|
// Recurse for the directory
|
|
|
|
jobs = append(jobs, listJob{
|
|
|
|
remote: dir.Remote(),
|
|
|
|
depth: job.depth - 1,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-02-24 23:51:01 +01:00
|
|
|
mu.Lock()
|
|
|
|
err = fn(job.remote, entries, err)
|
|
|
|
mu.Unlock()
|
2017-06-15 17:40:56 +02:00
|
|
|
// NB once we have passed entries to fn we mustn't touch it again
|
2017-02-24 23:51:01 +01:00
|
|
|
if err != nil && err != ErrorSkipDir {
|
|
|
|
traversing.Done()
|
2021-12-08 17:14:45 +01:00
|
|
|
err = fs.CountError(ctx, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
fs.Errorf(job.remote, "error listing: %v", err)
|
2017-02-24 23:51:01 +01:00
|
|
|
closeQuit()
|
|
|
|
// Send error to error channel if space
|
|
|
|
select {
|
|
|
|
case errs <- err:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2017-06-15 17:40:56 +02:00
|
|
|
if err == nil && len(jobs) > 0 {
|
2017-02-24 23:51:01 +01:00
|
|
|
traversing.Add(len(jobs))
|
|
|
|
go func() {
|
|
|
|
// Now we have traversed this directory, send these
|
|
|
|
// jobs off for traversal in the background
|
|
|
|
for _, newJob := range jobs {
|
|
|
|
in <- newJob
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
traversing.Done()
|
|
|
|
case <-quit:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
// Start the process
|
|
|
|
traversing.Add(1)
|
|
|
|
in <- listJob{
|
|
|
|
remote: path,
|
|
|
|
depth: maxLevel - 1,
|
|
|
|
}
|
|
|
|
traversing.Wait()
|
|
|
|
close(in)
|
|
|
|
wg.Wait()
|
|
|
|
close(errs)
|
|
|
|
// return the first error returned or nil
|
|
|
|
return <-errs
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:54:43 +02:00
|
|
|
func walkRDirTree(ctx context.Context, f fs.Fs, startPath string, includeAll bool, maxLevel int, listR fs.ListRFn) (dirtree.DirTree, error) {
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
2019-06-27 16:54:43 +02:00
|
|
|
dirs := dirtree.New()
|
2017-11-09 10:28:36 +01:00
|
|
|
// Entries can come in arbitrary order. We use toPrune to keep
|
|
|
|
// all directories to exclude later.
|
|
|
|
toPrune := make(map[string]bool)
|
2020-11-26 18:10:41 +01:00
|
|
|
includeDirectory := fi.IncludeDirectory(ctx, f)
|
2017-06-11 23:43:31 +02:00
|
|
|
var mu sync.Mutex
|
2019-06-17 10:34:30 +02:00
|
|
|
err := listR(ctx, startPath, func(entries fs.DirEntries) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
2017-06-06 17:40:00 +02:00
|
|
|
for _, entry := range entries {
|
|
|
|
slashes := strings.Count(entry.Remote(), "/")
|
2022-05-20 15:54:04 +02:00
|
|
|
excluded := true
|
2017-06-06 17:40:00 +02:00
|
|
|
switch x := entry.(type) {
|
2018-01-12 17:30:54 +01:00
|
|
|
case fs.Object:
|
2017-06-06 17:40:00 +02:00
|
|
|
// Make sure we don't delete excluded files if not required
|
2020-11-26 18:10:41 +01:00
|
|
|
if includeAll || fi.IncludeObject(ctx, x) {
|
2017-06-06 17:40:00 +02:00
|
|
|
if maxLevel < 0 || slashes <= maxLevel-1 {
|
2019-06-27 16:54:43 +02:00
|
|
|
dirs.Add(x)
|
2022-05-20 15:54:04 +02:00
|
|
|
excluded = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Make sure we include any parent directories of excluded objects
|
|
|
|
if excluded {
|
|
|
|
dirPath := parentDir(x.Remote())
|
|
|
|
slashes--
|
|
|
|
if maxLevel >= 0 {
|
2017-06-06 17:40:00 +02:00
|
|
|
for ; slashes > maxLevel-1; slashes-- {
|
|
|
|
dirPath = parentDir(dirPath)
|
|
|
|
}
|
|
|
|
}
|
2022-05-20 15:54:04 +02:00
|
|
|
inc, err := includeDirectory(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if inc || includeAll {
|
|
|
|
// If the directory doesn't exist already, create it
|
|
|
|
_, obj := dirs.Find(dirPath)
|
|
|
|
if obj == nil {
|
|
|
|
dirs.AddDir(fs.NewDir(dirPath, time.Now()))
|
|
|
|
}
|
|
|
|
}
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
2017-11-09 10:28:36 +01:00
|
|
|
// Check if we need to prune a directory later.
|
2020-11-26 18:10:41 +01:00
|
|
|
if !includeAll && len(fi.Opt.ExcludeFile) > 0 {
|
2017-11-09 10:28:36 +01:00
|
|
|
basename := path.Base(x.Remote())
|
2022-06-08 09:29:01 +02:00
|
|
|
for _, excludeFile := range fi.Opt.ExcludeFile {
|
|
|
|
if basename == excludeFile {
|
|
|
|
excludeDir := parentDir(x.Remote())
|
|
|
|
toPrune[excludeDir] = true
|
|
|
|
}
|
2017-11-09 10:28:36 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
case fs.Directory:
|
2017-11-09 10:28:36 +01:00
|
|
|
inc, err := includeDirectory(x.Remote())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if includeAll || inc {
|
2017-06-06 17:40:00 +02:00
|
|
|
if maxLevel < 0 || slashes <= maxLevel-1 {
|
|
|
|
if slashes == maxLevel-1 {
|
|
|
|
// Just add the object if at maxLevel
|
2019-06-27 16:54:43 +02:00
|
|
|
dirs.Add(x)
|
2017-06-06 17:40:00 +02:00
|
|
|
} else {
|
2019-06-27 16:54:43 +02:00
|
|
|
dirs.AddDir(x)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 14:57:38 +01:00
|
|
|
default:
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("unknown object type %T", entry)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-06-27 16:54:43 +02:00
|
|
|
dirs.CheckParents(startPath)
|
2017-06-06 17:40:00 +02:00
|
|
|
if len(dirs) == 0 {
|
2017-11-09 10:27:31 +01:00
|
|
|
dirs[startPath] = nil
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
2017-11-09 10:28:36 +01:00
|
|
|
err = dirs.Prune(toPrune)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-06 17:40:00 +02:00
|
|
|
dirs.Sort()
|
|
|
|
return dirs, nil
|
|
|
|
}
|
|
|
|
|
2017-07-24 23:52:18 +02:00
|
|
|
// Create a DirTree using List
|
2019-06-27 16:54:43 +02:00
|
|
|
func walkNDirTree(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, listDir listDirFunc) (dirtree.DirTree, error) {
|
|
|
|
dirs := make(dirtree.DirTree)
|
2018-01-12 17:30:54 +01:00
|
|
|
fn := func(dirPath string, entries fs.DirEntries, err error) error {
|
2017-07-24 23:52:18 +02:00
|
|
|
if err == nil {
|
|
|
|
dirs[dirPath] = entries
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2019-06-17 10:34:30 +02:00
|
|
|
err := walk(ctx, f, path, includeAll, maxLevel, fn, listDir)
|
2017-07-24 23:52:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return dirs, nil
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:43:31 +02:00
|
|
|
// NewDirTree returns a DirTree filled with the directory listing
|
2017-07-24 23:52:18 +02:00
|
|
|
// 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.
|
|
|
|
//
|
2019-02-03 17:06:04 +01:00
|
|
|
// This is implemented by WalkR if f supports ListR and level > 1, or
|
|
|
|
// WalkN otherwise.
|
2017-07-24 23:52:18 +02:00
|
|
|
//
|
2019-02-13 18:14:51 +01:00
|
|
|
// If --files-from and --no-traverse is set then a DirTree will be
|
|
|
|
// constructed with just those files in.
|
2018-10-19 18:41:14 +02:00
|
|
|
//
|
2017-07-24 23:52:18 +02:00
|
|
|
// NB (f, path) to be replaced by fs.Dir at some point
|
2019-06-27 16:54:43 +02:00
|
|
|
func NewDirTree(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int) (dirtree.DirTree, error) {
|
2020-11-05 12:33:32 +01:00
|
|
|
ci := fs.GetConfig(ctx)
|
2020-11-26 18:10:41 +01:00
|
|
|
fi := filter.GetConfig(ctx)
|
2019-10-14 17:06:13 +02:00
|
|
|
// if --no-traverse and --files-from build DirTree just from files
|
2020-11-26 18:10:41 +01:00
|
|
|
if ci.NoTraverse && fi.HaveFilesFrom() {
|
|
|
|
return walkRDirTree(ctx, f, path, includeAll, maxLevel, fi.MakeListR(ctx, f.NewObject))
|
2018-10-19 18:41:14 +02:00
|
|
|
}
|
2019-10-14 17:06:13 +02:00
|
|
|
// if have ListR; and recursing; and not using --files-from; then build a DirTree with ListR
|
2020-11-26 18:10:41 +01:00
|
|
|
if ListR := f.Features().ListR; (maxLevel < 0 || maxLevel > 1) && ListR != nil && !fi.HaveFilesFrom() {
|
2019-06-17 10:34:30 +02:00
|
|
|
return walkRDirTree(ctx, f, path, includeAll, maxLevel, ListR)
|
2017-06-11 23:43:31 +02:00
|
|
|
}
|
2019-10-14 17:06:13 +02:00
|
|
|
// otherwise just use List
|
2019-06-17 10:34:30 +02:00
|
|
|
return walkNDirTree(ctx, f, path, includeAll, maxLevel, list.DirSorted)
|
2017-06-06 17:40:00 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 10:34:30 +02:00
|
|
|
func walkR(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int, fn Func, listR fs.ListRFn) error {
|
|
|
|
dirs, err := walkRDirTree(ctx, f, path, includeAll, maxLevel, listR)
|
2017-06-06 17:40:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
skipping := false
|
|
|
|
skipPrefix := ""
|
2018-01-12 17:30:54 +01:00
|
|
|
emptyDir := fs.DirEntries{}
|
2017-06-06 17:40:00 +02:00
|
|
|
for _, dirPath := range dirs.Dirs() {
|
|
|
|
if skipping {
|
|
|
|
// Skip over directories as required
|
|
|
|
if strings.HasPrefix(dirPath, skipPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
skipping = false
|
|
|
|
}
|
|
|
|
entries := dirs[dirPath]
|
|
|
|
if entries == nil {
|
|
|
|
entries = emptyDir
|
|
|
|
}
|
|
|
|
err = fn(dirPath, entries, nil)
|
|
|
|
if err == ErrorSkipDir {
|
|
|
|
skipping = true
|
|
|
|
skipPrefix = dirPath
|
|
|
|
if skipPrefix != "" {
|
|
|
|
skipPrefix += "/"
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-21 11:02:23 +01:00
|
|
|
// GetAll runs ListR getting all the results
|
2019-06-17 10:34:30 +02:00
|
|
|
func GetAll(ctx context.Context, f fs.Fs, path string, includeAll bool, maxLevel int) (objs []fs.Object, dirs []fs.Directory, err error) {
|
|
|
|
err = ListR(ctx, f, path, includeAll, maxLevel, ListAll, func(entries fs.DirEntries) error {
|
2017-02-24 23:51:01 +01:00
|
|
|
for _, entry := range entries {
|
|
|
|
switch x := entry.(type) {
|
2018-01-12 17:30:54 +01:00
|
|
|
case fs.Object:
|
2017-02-24 23:51:01 +01:00
|
|
|
objs = append(objs, x)
|
2018-01-12 17:30:54 +01:00
|
|
|
case fs.Directory:
|
2017-02-24 23:51:01 +01:00
|
|
|
dirs = append(dirs, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2017-06-11 23:43:31 +02:00
|
|
|
|
|
|
|
// ListRHelper is used in the implementation of ListR to accumulate DirEntries
|
|
|
|
type ListRHelper struct {
|
2018-01-12 17:30:54 +01:00
|
|
|
callback fs.ListRCallback
|
|
|
|
entries fs.DirEntries
|
2017-06-11 23:43:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewListRHelper should be called from ListR with the callback passed in
|
2018-01-12 17:30:54 +01:00
|
|
|
func NewListRHelper(callback fs.ListRCallback) *ListRHelper {
|
2017-06-11 23:43:31 +02:00
|
|
|
return &ListRHelper{
|
|
|
|
callback: callback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// send sends the stored entries to the callback if there are >= max
|
|
|
|
// entries.
|
|
|
|
func (lh *ListRHelper) send(max int) (err error) {
|
|
|
|
if len(lh.entries) >= max {
|
|
|
|
err = lh.callback(lh.entries)
|
|
|
|
lh.entries = lh.entries[:0]
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an entry to the stored entries and send them if there are more
|
|
|
|
// than a certain amount
|
2018-01-12 17:30:54 +01:00
|
|
|
func (lh *ListRHelper) Add(entry fs.DirEntry) error {
|
2017-06-11 23:43:31 +02:00
|
|
|
if entry == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
lh.entries = append(lh.entries, entry)
|
|
|
|
return lh.send(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the stored entries (if any) sending them to the callback
|
|
|
|
func (lh *ListRHelper) Flush() error {
|
|
|
|
return lh.send(1)
|
|
|
|
}
|