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

Minor improvement for SCP preserve flag: #680

Merged
merged 1 commit into from
Apr 13, 2021
Merged
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
46 changes: 27 additions & 19 deletions src/main/java/net/schmizz/sshj/xfer/scp/SCPUploadClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ public synchronized int copy(LocalSourceFile sourceFile, String remotePath)
return copy(sourceFile, remotePath, ScpCommandLine.EscapeMode.SingleQuote);
}

public synchronized int copy(LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode)
throws IOException {
public synchronized int copy (LocalSourceFile sourceFile, String remotePath, ScpCommandLine.EscapeMode escapeMode) 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();
try {
startCopy(sourceFile, remotePath, escapeMode);
startCopy(sourceFile, remotePath, escapeMode, preserveTimes);
} finally {
engine.exit();
}
Expand All @@ -58,40 +62,44 @@ public void setUploadFilter(LocalFileFilter uploadFilter) {
this.uploadFilter = uploadFilter;
}

private void startCopy(LocalSourceFile sourceFile, String targetPath, ScpCommandLine.EscapeMode escapeMode)
throws IOException {
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.PRESERVE_TIMES, sourceFile.providesAtimeMtime())
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
.and(ScpCommandLine.Arg.RECURSIVE)
.and(ScpCommandLine.Arg.LIMIT, String.valueOf(bandwidthLimit), (bandwidthLimit > 0));
if (preserveTimes) {
commandLine.and(ScpCommandLine.Arg.PRESERVE_TIMES, sourceFile.providesAtimeMtime());
}
commandLine.withPath(targetPath, escapeMode);
engine.execSCPWith(commandLine);
engine.check("Start status OK");
process(engine.getTransferListener(), sourceFile);
process(engine.getTransferListener(), sourceFile, preserveTimes);
}

private void process(TransferListener listener, LocalSourceFile f)
throws IOException {
private void process(TransferListener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException {
if (f.isDirectory()) {
sendDirectory(listener.directory(f.getName()), f);
sendDirectory(listener.directory(f.getName()), f, preserveTimes);
} else if (f.isFile()) {
sendFile(listener.file(f.getName(), f.getLength()), f);
sendFile(listener.file(f.getName(), f.getLength()), f, preserveTimes);
} else
throw new IOException(f + " is not a regular file or directory");
}

private void sendDirectory(TransferListener listener, LocalSourceFile f)
throws IOException {
private void sendDirectory(TransferListener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException {
preserveTimeIfPossible(f);
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
for (LocalSourceFile child : f.getChildren(uploadFilter))
process(listener, child);
process(listener, child, preserveTimes);
engine.sendMessage("E");
}

private void sendFile(StreamCopier.Listener listener, LocalSourceFile f)
throws IOException {
preserveTimeIfPossible(f);
private void sendFile(StreamCopier.Listener listener, LocalSourceFile f, boolean preserveTimes)
throws IOException {
if(preserveTimes) {
preserveTimeIfPossible(f);
}
final InputStream src = f.getInputStream();
try {
engine.sendMessage("C0" + getPermString(f) + " " + f.getLength() + " " + f.getName());
Expand Down