From a7d65bd51941e12cb63be968511bc0483ae654ad Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Thu, 14 Nov 2019 22:00:30 +0000 Subject: [PATCH] sftp: add --sftp-skip-links to skip symlinks and non regular files - fixes #3716 This also corrects the symlink detection logic to only check symlink files. Previous to this it was checking all directories too which was making it do more stat calls than was necessary. --- backend/sftp/sftp.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index d426b6af3..c528ee4ff 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -156,6 +156,11 @@ Home directory can be found in a shared folder called "home" Default: "", Help: "The command used to read sha1 hashes. Leave blank for autodetect.", Advanced: true, + }, { + Name: "skip_links", + Default: false, + Help: "Set to skip any symlinks and any other non regular files.", + Advanced: true, }}, } fs.Register(fsi) @@ -177,6 +182,7 @@ type Options struct { SetModTime bool `config:"set_modtime"` Md5sumCommand string `config:"md5sum_command"` Sha1sumCommand string `config:"sha1sum_command"` + SkipLinks bool `config:"skip_links"` } // Fs stores the interface to the remote SFTP files @@ -600,12 +606,16 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e remote := path.Join(dir, info.Name()) // If file is a symlink (not a regular file is the best cross platform test we can do), do a stat to // pick up the size and type of the destination, instead of the size and type of the symlink. - if !info.Mode().IsRegular() { + if !info.Mode().IsRegular() && !info.IsDir() { + if f.opt.SkipLinks { + // skip non regular file if SkipLinks is set + continue + } oldInfo := info info, err = f.stat(remote) if err != nil { if !os.IsNotExist(err) { - fs.Errorf(remote, "stat of non-regular file/dir failed: %v", err) + fs.Errorf(remote, "stat of non-regular file failed: %v", err) } info = oldInfo }