From e6e6069ecfeead4b2d581f3b30041706bf12fd2a Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Wed, 10 May 2023 11:34:20 +0100 Subject: [PATCH] sftp: don't stat directories before listing them Before this change we ran stat on the directory to see if it existed. Not only is this inefficient it isn't allowed by some SFTP servers. See: https://forum.rclone.org/t/stat-failed-error-on-sftp/38045 --- backend/sftp/sftp.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index 6127a2333..f4a21b3c7 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -1169,13 +1169,6 @@ func (f *Fs) dirExists(ctx context.Context, dir string) (bool, error) { // found. func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) { root := path.Join(f.absRoot, dir) - ok, err := f.dirExists(ctx, root) - if err != nil { - return nil, fmt.Errorf("List failed: %w", err) - } - if !ok { - return nil, fs.ErrorDirNotFound - } sftpDir := root if sftpDir == "" { sftpDir = "." @@ -1187,6 +1180,9 @@ func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err e infos, err := c.sftpClient.ReadDir(sftpDir) f.putSftpConnection(&c, err) if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil, fs.ErrorDirNotFound + } return nil, fmt.Errorf("error listing %q: %w", dir, err) } for _, info := range infos {