Skip to content

Conversation

@jlesage
Copy link

@jlesage jlesage commented Oct 19, 2023

This fixes issue when replacing an existing remote/local file. If the file is not first truncated, the resulting file will be incorrect when new file is smaller than the existing one.

@helloyi
Copy link
Owner

helloyi commented Nov 30, 2023

Sorry for the late reply.

Implicitly truncating files is usually not safe, and this decision is usually left to the developer.
There are two options to fix this problem:

  1. Explicitly truncate the file using the Truncate API
// upload
sftp.Truncate(remotePath, 0 /*size*/) or sftp.Remove(remotePath)
sftp.Upload(localPath, remotePath)

// download
os.Truncate(localPath, 0 /*size*/) or os.Remove(localPath)
sftp.Download(remotePath, localPath)
  1. Add a truncated version of the upload/download API in go-sshclient (explicitly indicating that it will overwrite existing files)
// force upload and overwrite existing remote files
sftp.ForceUpload(localPath, remotePath)

// force download and overwrite existing local files
sftp.ForceDownload(remotePath, localPath)

If you are interested, please add ForceUpload and ForceDownload APIs.
looking forward to your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants