Skip to content

Commit 75bc8c1

Browse files
IgorMelnykSIMelnyk Ihor
authored andcommitted
possibility to upload file using scp with minimum arguments
1 parent f0e92c9 commit 75bc8c1

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public synchronized int copy(LocalSourceFile sourceFile, String remotePath)
4444
}
4545

4646
public synchronized int copy (LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) throws IOException {
47-
return copy(sourceFile, remotePath, escapeMode, true);
47+
return copy(sourceFile, remotePath, escapeMode, true, true);
4848
}
4949

50-
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
51-
throws IOException {
50+
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes, boolean defaultArgs)
51+
throws IOException {
5252
engine.cleanSlate();
5353
try {
54-
startCopy(sourceFile, remotePath, escapeMode, preserveTimes);
54+
startCopy(sourceFile, remotePath, escapeMode, preserveTimes, defaultArgs);
5555
} finally {
5656
engine.exit();
5757
}
@@ -62,11 +62,9 @@ public void setUploadFilter(LocalFileFilter uploadFilter) {
6262
this.uploadFilter = uploadFilter;
6363
}
6464

65-
private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
66-
throws IOException {
67-
ScpCommandLine commandLine = ScpCommandLine.with(ScpCommandLine.Arg.SINK)
68-
.and(ScpCommandLine.Arg.RECURSIVE)
69-
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
65+
private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes, boolean defaultArgs)
66+
throws IOException {
67+
ScpCommandLine commandLine = initialScpArguments(defaultArgs);
7068
if (preserveTimes) {
7169
commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime());
7270
}
@@ -76,6 +74,13 @@ private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommand
7674
process(engine.getTransferListener(), sourceFile, preserveTimes);
7775
}
7876

77+
protected ScpCommandLine initialScpArguments(boolean defaultArgs) {
78+
if (!defaultArgs) return ScpCommandLine.with(ScpCommandLine.Arg.SINK);
79+
return ScpCommandLine.with(ScpCommandLine.Arg.SINK)
80+
.and(ScpCommandLine.Arg.RECURSIVE)
81+
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
82+
}
83+
7984
private void process(TransferListener listener, LocalSourceFile f, boolean preserveTimes)
8085
throws IOException {
8186
if (f.isDirectory()) {

src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.hierynomus.sshj.test.SshServerExtension;
1919
import com.hierynomus.sshj.test.util.FileUtil;
2020
import net.schmizz.sshj.SSHClient;
21+
import net.schmizz.sshj.xfer.FileSystemFile;
2122
import org.hamcrest.CoreMatchers;
2223
import org.junit.jupiter.api.AfterEach;
2324
import org.junit.jupiter.api.BeforeEach;
@@ -87,6 +88,19 @@ public void shouldSCPUploadFileWithBandwidthLimit() throws IOException {
8788
assertTrue(Files.exists(targetFile));
8889
}
8990

91+
@Test
92+
public void shouldSCPUploadFileWithoutArgs() throws IOException {
93+
// scp without any option
94+
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer();
95+
assertFalse(Files.exists(targetFile));
96+
scpFileTransfer.newSCPUploadClient().copy(
97+
new FileSystemFile(sourceFile.toAbsolutePath().toString()) ,
98+
targetDir.toAbsolutePath().toString(),
99+
ScpCommandLine.EscapeMode.SingleQuote,
100+
false, false);
101+
assertTrue(Files.exists(targetFile));
102+
}
103+
90104
@Test
91105
public void shouldSCPDownloadFile() throws IOException {
92106
SCPFileTransfer scpFileTransfer = sshClient.newSCPFileTransfer();

0 commit comments

Comments
 (0)