Skip to content

Commit

Permalink
possibility to upload file using scp with minimum arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMelnykSI authored and Melnyk Ihor committed Jul 11, 2024
1 parent f0e92c9 commit 75bc8c1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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());
}
Expand All @@ -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()) {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/schmizz/sshj/xfer/scp/SCPFileTransferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 75bc8c1

Please sign in to comment.