Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to upload file using scp with minimum number of arguments #951

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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