Skip to content

Commit

Permalink
Minor improvement for SCP preserve flag: (#680)
Browse files Browse the repository at this point in the history
- Added an override for copy method, allowing the user to specify whether preserve flag is used in the SCP command.
- Propagated the preserveTime boolean to process method to skip preserveTimeIfPossible when it's not desired
  • Loading branch information
Estraysian authored Apr 13, 2021
1 parent 45b2f32 commit e283880
Showing 1 changed file with 27 additions and 19 deletions.
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

0 comments on commit e283880

Please sign in to comment.