Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

sftp: fix move to allow overwriting existing files

Nick Craig-Woodaaacfa51a079
diff --git a/backend/sftp/sftp.go b/backend/sftp/sftp.go
index 3554343b8..6338534bf 100644
--- a/backend/sftp/sftp.go
+++ b/backend/sftp/sftp.go
@@ -1329,10 +1329,17 @@ func (f *Fs) Move(ctx context.Context, src fs.Object, remote string) (fs.Object,
if err != nil {
return nil, fmt.Errorf("Move: %w", err)
}
- err = c.sftpClient.Rename(
- srcObj.path(),
- path.Join(f.absRoot, remote),
- )
+ srcPath, dstPath := srcObj.path(), path.Join(f.absRoot, remote)
+ if _, ok := c.sftpClient.HasExtension("[email protected]"); ok {
+ err = c.sftpClient.PosixRename(srcPath, dstPath)
+ } else {
+ // If haven't got PosixRename then remove source first before renaming
+ err = c.sftpClient.Remove(dstPath)
+ if err != nil && !errors.Is(err, iofs.ErrNotExist) {
+ fs.Errorf(f, "Move: Failed to remove existing file %q: %v", dstPath, err)
+ }
+ err = c.sftpClient.Rename(srcPath, dstPath)
+ }
f.putSftpConnection(&c, err)
if err != nil {
return nil, fmt.Errorf("Move Rename failed: %w", err)