Hi,
if you try to retrieve a remote file, for which you
have a local copy, if that file doesnt exist, it'll
truncate the file you store locally, which shouldnt
happen. If the file doesnt exist remotely, you
shouldnt touch the local copy.
I found the problem to be in the SftpClient::get()
methods, specifically:
public FileAttributes get(String remote, String local,
FileTransferProgress progress)
in this method, after the if (!localPath.exists())
block, we find.
FileOutputStream out = new FileOutputStream(localPath);
this actually truncates the file, ready for writing new
content to it. But this is done BEFORE you know the
remote file exists. Which means, effectively, you
truncate, then look whether you can transfer anything.
The fix is to move this to AFTER
SftpFileInputStream in = new
SftpFileInputStream(sftp.openFile(
remotePath,
SftpSubsystemClient.OPEN_READ));
in method
public FileAttributes get(String remote, OutputStream
local,
FileTransferProgress progress);
but we can't change the signature of this method,
because another few functions need it, but we can't
augment the implementation either, because the line we
want to insert, needs to be in the middle of the
method, so I havent figured out a better way of doing
this except to duplicate method
public FileAttributes get(String remote, OutputStream
local,
FileTransferProgress progress);
into
public FileAttributes get(String remote, File local,
FileTransferProgress progress);
and then inside that method, which is identical to the
other, except for these lines:
SftpFileInputStream in = new SftpFileInputStream
(sftp.openFile(remotePath, SftpSubsystemClient.OPEN_READ));
FileOutputStream out = new FileOutputStream(local);
transferFile(in, out, progress);
You will find a patch attached which should be applied
by putting into the same directory as SftpClient.java
and applying it like so:
patch < SftpClient.diff
which will apply the patch, now if you try to retrieve
a remote copy of a local file and it fails, your local
copy will not be zero'd out.
can this please be tested and applied to the main
source tree. thanks!
hope you like it