From a2d42093220b6f7e3c33af40138c001dd4e7b398 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Fri, 6 Nov 2020 20:04:52 +0100 Subject: [PATCH] Don't recurse in local backend's List if not required Due to the return if !isFile, the IsDir branch in List was never taken and subdirectories were traversed recursively. Also replaced isFile by an IsRegular check, which has been equivalent since Go 1.12 (golang/go@a2a3dd00c934fa15ad880ee5fe1f64308cbc73a7). --- internal/backend/local/local.go | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/internal/backend/local/local.go b/internal/backend/local/local.go index 5261a0852..1dd0abe90 100644 --- a/internal/backend/local/local.go +++ b/internal/backend/local/local.go @@ -220,32 +220,27 @@ func (b *Local) Remove(ctx context.Context, h restic.Handle) error { return fs.Remove(fn) } -func isFile(fi os.FileInfo) bool { - return fi.Mode()&(os.ModeType|os.ModeCharDevice) == 0 -} - // List runs fn for each file in the backend which has the type t. When an // error occurs (or fn returns an error), List stops and returns it. func (b *Local) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error { debug.Log("List %v", t) basedir, subdirs := b.Basedir(t) + atBasedir := true err := fs.Walk(basedir, func(path string, fi os.FileInfo, err error) error { debug.Log("walk on %v\n", path) if err != nil { return err } - if path == basedir { + switch { + case atBasedir: // Skip basedir itself. + atBasedir = false return nil - } - - if !isFile(fi) { - return nil - } - - if fi.IsDir() && !subdirs { + case fi.IsDir() && !subdirs: return filepath.SkipDir + case !fi.Mode().IsRegular(): + return nil } debug.Log("send %v\n", filepath.Base(path))