go-sshclient
go-sshclient copied to clipboard
When uploading/downloading, make sure to truncate the remote/local file when opened.
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.
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:
- 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)
- 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!