From 0eba88bbfe44279a89bff5a356d8b5fb36323975 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 30 Nov 2018 17:37:55 +0000 Subject: [PATCH] sftp: check directory is empty before issuing rmdir Some SFTP servers allow rmdir on full directories which is allowed according to the RFC so make sure we don't accidentally delete data here. See: https://forum.rclone.org/t/rmdir-and-delete-empty-src-dirs-file-does-not-exist/7737 --- backend/sftp/sftp.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go index cfa6d4007..e37d83f9c 100644 --- a/backend/sftp/sftp.go +++ b/backend/sftp/sftp.go @@ -594,6 +594,16 @@ func (f *Fs) Mkdir(dir string) error { // Rmdir removes the root directory of the Fs object func (f *Fs) Rmdir(dir string) error { + // Check to see if directory is empty as some servers will + // delete recursively with RemoveDirectory + entries, err := f.List(dir) + if err != nil { + return errors.Wrap(err, "Rmdir") + } + if len(entries) != 0 { + return fs.ErrorDirectoryNotEmpty + } + // Remove the directory root := path.Join(f.root, dir) c, err := f.getSftpConnection() if err != nil {