diff --git a/src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java b/src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java index b92f742dc..e0d9148c3 100644 --- a/src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java +++ b/src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java @@ -44,14 +44,14 @@ public synchronized int copy(LocalSourceFile sourceFile, String remotePath) } public synchronized int copy (LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) throws IOException { - return copy(sourceFile, remotePath, escapeMode, true); + return copy(sourceFile, remotePath, escapeMode, true, true); } - public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes) - throws IOException { + public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes, boolean defaultArgs) + throws IOException { engine.cleanSlate(); try { - startCopy(sourceFile, remotePath, escapeMode, preserveTimes); + startCopy(sourceFile, remotePath, escapeMode, preserveTimes, defaultArgs); } finally { engine.exit(); } @@ -62,11 +62,9 @@ public void setUploadFilter(LocalFileFilter uploadFilter) { this.uploadFilter = uploadFilter; } - private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes) - throws IOException { - ScpCommandLine commandLine = ScpCommandLine.with(ScpCommandLine.Arg.SINK) - .and(ScpCommandLine.Arg.RECURSIVE) - .and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0)); + private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes, boolean defaultArgs) + throws IOException { + ScpCommandLine commandLine = initialScpArguments(defaultArgs); if (preserveTimes) { commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime()); } @@ -76,6 +74,13 @@ private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommand process(engine.getTransferListener(), sourceFile, preserveTimes); } + protected ScpCommandLine initialScpArguments(boolean defaultArgs) { + if (!defaultArgs) return ScpCommandLine.with(ScpCommandLine.Arg.SINK); + return ScpCommandLine.with(ScpCommandLine.Arg.SINK) + .and(ScpCommandLine.Arg.RECURSIVE) + .and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0)); + } + private void process(TransferListener listener, LocalSourceFile f, boolean preserveTimes) throws IOException { if (f.isDirectory()) { diff --git a/src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java b/src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java index e8119fa04..c46ad0bf1 100644 --- a/src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java +++ b/src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java @@ -18,6 +18,7 @@ import com.hierynomus.sshj.test.SshServerExtension; import com.hierynomus.sshj.test.util.FileUtil; import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.xfer.FileSystemFile; import org.hamcrest.CoreMatchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -87,6 +88,19 @@ public void shouldSCPUploadFileWithBandwidthLimit() throws IOException { assertTrue(Files.exists(targetFile)); } + @Test + public void shouldSCPUploadFileWithoutArgs() throws IOException { + // scp without any option + SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer(); + assertFalse(Files.exists(targetFile)); + scpFileTransfer.newSCPUploadClient().copy( + new FileSystemFile(sourceFile.toAbsolutePath().toString()) , + targetDir.toAbsolutePath().toString(), + ScpCommandLine.EscapeMode.SingleQuote, + false, false); + assertTrue(Files.exists(targetFile)); + } + @Test public void shouldSCPDownloadFile() throws IOException { SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer();