Skip to content

Commit 93046f3

Browse files
committed
Provide public SCP command line builder (Fixes \#951)
Signed-off-by: Jeroen van Erp <[email protected]>
1 parent 54376b7 commit 93046f3

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import net.schmizz.sshj.xfer.LocalFileFilter;
2121
import net.schmizz.sshj.xfer.LocalSourceFile;
2222
import net.schmizz.sshj.xfer.TransferListener;
23+
import net.schmizz.sshj.xfer.scp.ScpCommandLine.Arg;
2324

2425
import java.io.IOException;
2526
import java.io.InputStream;
@@ -43,37 +44,44 @@ public synchronized int copy(LocalSourceFile sourceFile, String remotePath)
4344
return copy(sourceFile, remotePath, ScpCommandLine.EscapeMode.SingleQuote);
4445
}
4546

46-
public synchronized int copy (LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) throws IOException {
47+
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode)
48+
throws IOException {
4749
return copy(sourceFile, remotePath, escapeMode, true);
4850
}
4951

5052
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
51-
throws IOException {
53+
throws IOException {
54+
ScpCommandLine commandLine = ScpCommandLine.with(ScpCommandLine.Arg.SINK)
55+
.and(ScpCommandLine.Arg.RECURSIVE)
56+
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
57+
if (preserveTimes) {
58+
commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime());
59+
}
60+
return copy(sourceFile, remotePath, escapeMode, commandLine);
61+
}
62+
63+
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, ScpCommandLine commandLine)
64+
throws IOException {
5265
engine.cleanSlate();
5366
try {
54-
startCopy(sourceFile, remotePath, escapeMode, preserveTimes);
67+
commandLine.withPath(remotePath, escapeMode);
68+
startCopy(sourceFile, commandLine);
5569
} finally {
5670
engine.exit();
5771
}
5872
return engine.getExitStatus();
5973
}
6074

75+
6176
public void setUploadFilter(LocalFileFilter uploadFilter) {
6277
this.uploadFilter = uploadFilter;
6378
}
6479

65-
private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
80+
private void startCopy(LocalSourceFile sourceFile, ScpCommandLine commandLine)
6681
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));
70-
if (preserveTimes) {
71-
commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime());
72-
}
73-
commandLine.withPath(targetPath, escapeMode);
7482
engine.execSCPWith(commandLine);
7583
engine.check("Start status OK");
76-
process(engine.getTransferListener(), sourceFile, preserveTimes);
84+
process(engine.getTransferListener(), sourceFile, commandLine.has(Arg.PRESERVE_TIMES));
7785
}
7886

7987
private void process(TransferListener listener, LocalSourceFile f, boolean preserveTimes)

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ScpCommandLine {
2424
private static final String SCP_COMMAND = "scp";
2525
private EscapeMode mode;
2626

27-
enum Arg {
27+
public enum Arg {
2828
SOURCE('f'),
2929
SINK('t'),
3030
RECURSIVE('r'),
@@ -77,19 +77,19 @@ String escapedPath(String path) {
7777
ScpCommandLine() {
7878
}
7979

80-
static ScpCommandLine with(Arg name) {
80+
public static ScpCommandLine with(Arg name) {
8181
return with(name, null, true);
8282
}
8383

84-
static ScpCommandLine with(Arg name, String value) {
84+
public static ScpCommandLine with(Arg name, String value) {
8585
return with(name, value, true);
8686
}
8787

88-
static ScpCommandLine with(Arg name, boolean accept) {
88+
public static ScpCommandLine with(Arg name, boolean accept) {
8989
return with(name, null, accept);
9090
}
9191

92-
static ScpCommandLine with(Arg name, String value, boolean accept) {
92+
public static ScpCommandLine with(Arg name, String value, boolean accept) {
9393
ScpCommandLine commandLine = new ScpCommandLine();
9494
commandLine.addArgument(name, value, accept);
9595
return commandLine;
@@ -101,22 +101,22 @@ private void addArgument(Arg name, String value, boolean accept) {
101101
}
102102
}
103103

104-
ScpCommandLine and(Arg name) {
104+
public ScpCommandLine and(Arg name) {
105105
addArgument(name, null, true);
106106
return this;
107107
}
108108

109-
ScpCommandLine and(Arg name, String value) {
109+
public ScpCommandLine and(Arg name, String value) {
110110
addArgument(name, value, true);
111111
return this;
112112
}
113113

114-
ScpCommandLine and(Arg name, boolean accept) {
114+
public ScpCommandLine and(Arg name, boolean accept) {
115115
addArgument(name, null, accept);
116116
return this;
117117
}
118118

119-
ScpCommandLine and(Arg name, String value, boolean accept) {
119+
public ScpCommandLine and(Arg name, String value, boolean accept) {
120120
addArgument(name, value, accept);
121121
return this;
122122
}
@@ -127,6 +127,10 @@ ScpCommandLine withPath(String path, EscapeMode mode) {
127127
return this;
128128
}
129129

130+
boolean has(Arg arg) {
131+
return arguments.containsKey(arg);
132+
}
133+
130134
String toCommandLine() {
131135
final StringBuilder cmd = new StringBuilder(SCP_COMMAND);
132136
for (Arg arg : arguments.keySet()) {

0 commit comments

Comments
 (0)