Merge branch 'master' into issue-670

This commit is contained in:
Jeroen van Erp
2021-04-20 09:28:08 +02:00
committed by GitHub

View File

@@ -43,11 +43,15 @@ public class SCPUploadClient extends AbstractSCPClient {
return copy(sourceFile, remotePath, ScpCommandLine.EscapeMode.SingleQuote); return copy(sourceFile, remotePath, ScpCommandLine.EscapeMode.SingleQuote);
} }
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) public synchronized int copy (LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) throws IOException {
throws IOException { return copy(sourceFile, remotePath, escapeMode, true);
}
public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
throws IOException {
engine.cleanSlate(); engine.cleanSlate();
try { try {
startCopy(sourceFile, remotePath, escapeMode); startCopy(sourceFile, remotePath, escapeMode, preserveTimes);
} finally { } finally {
engine.exit(); engine.exit();
} }
@@ -58,40 +62,44 @@ public class SCPUploadClient extends AbstractSCPClient {
this.uploadFilter = uploadFilter; this.uploadFilter = uploadFilter;
} }
private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode) private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode, boolean preserveTimes)
throws IOException { throws IOException {
ScpCommandLine commandLine = ScpCommandLine.with(ScpCommandLine.Arg.SINK) ScpCommandLine commandLine = ScpCommandLine.with(ScpCommandLine.Arg.SINK)
.and(ScpCommandLine.Arg.RECURSIVE) .and(ScpCommandLine.Arg.RECURSIVE)
.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime()) .and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0)); if (preserveTimes) {
commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime());
}
commandLine.withPath(targetPath, escapeMode); commandLine.withPath(targetPath, escapeMode);
engine.execSCPWith(commandLine); engine.execSCPWith(commandLine);
engine.check("Start status OK"); engine.check("Start status OK");
process(engine.getTransferListener(), sourceFile); process(engine.getTransferListener(), sourceFile, preserveTimes);
} }
private void process(TransferListener listener, LocalSourceFile f) private void process(TransferListener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException { throws IOException {
if (f.isDirectory()) { if (f.isDirectory()) {
sendDirectory(listener.directory(f.getName()), f); sendDirectory(listener.directory(f.getName()), f, preserveTimes);
} else if (f.isFile()) { } else if (f.isFile()) {
sendFile(listener.file(f.getName(), f.getLength()), f); sendFile(listener.file(f.getName(), f.getLength()), f, preserveTimes);
} else } else
throw new IOException(f + " is not a regular file or directory"); throw new IOException(f + " is not a regular file or directory");
} }
private void sendDirectory(TransferListener listener, LocalSourceFile f) private void sendDirectory(TransferListener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException { throws IOException {
preserveTimeIfPossible(f); preserveTimeIfPossible(f);
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName()); engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
for (LocalSourceFile child : f.getChildren(uploadFilter)) for (LocalSourceFile child : f.getChildren(uploadFilter))
process(listener, child); process(listener, child, preserveTimes);
engine.sendMessage("E"); engine.sendMessage("E");
} }
private void sendFile(StreamCopier.Listener listener, LocalSourceFile f) private void sendFile(StreamCopier.Listener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException { throws IOException {
preserveTimeIfPossible(f); if(preserveTimes) {
preserveTimeIfPossible(f);
}
final InputStream src = f.getInputStream(); final InputStream src = f.getInputStream();
try { try {
engine.sendMessage("C0" + getPermString(f) + " " + f.getLength() + " " + f.getName()); engine.sendMessage("C0" + getPermString(f) + " " + f.getLength() + " " + f.getName());