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

GH-427: Read initial ACK on channel open prior to direct stream upload & close streams prior to exit code handling #464

Merged
merged 2 commits into from
Apr 27, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,17 @@ public void upload(
String cmd = ScpClient.createSendCommand(remote, options);
ClientSession session = getClientSession();
ChannelExec channel = openCommandChannel(session, cmd);
try (InputStream invOut = channel.getInvertedOut();
OutputStream invIn = channel.getInvertedIn()) {
// NOTE: we use a mock file system since we expect no invocations for it
ScpHelper helper = new ScpHelper(session, invOut, invIn, new MockFileSystem(remote), opener, listener);
Path mockPath = new MockPath(remote);
helper.sendStream(new DefaultScpStreamResolver(name, mockPath, perms, time, size, local, cmd),
options.contains(Option.PreserveAttributes), ScpHelper.DEFAULT_SEND_BUFFER_SIZE);
try {
try (InputStream invOut = channel.getInvertedOut();
OutputStream invIn = channel.getInvertedIn()) {
// NOTE: we use a mock file system since we expect no invocations for it
ScpHelper helper = new ScpHelper(session, invOut, invIn, new MockFileSystem(remote), opener, listener);
Path mockPath = new MockPath(remote);
DefaultScpStreamResolver resolver = new DefaultScpStreamResolver(name, mockPath, perms, time, size, local, cmd);
helper.readAndValidateOperationAck(cmd, resolver);
helper.sendStream(resolver, options.contains(Option.PreserveAttributes),
ScpHelper.DEFAULT_SEND_BUFFER_SIZE);
}
handleCommandExitStatus(cmd, channel);
} finally {
channel.close(false);
Expand Down
21 changes: 11 additions & 10 deletions sshd-scp/src/main/java/org/apache/sshd/scp/common/ScpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,8 @@ public String readLine(boolean canEof) throws IOException {
}

public void send(Collection<String> paths, boolean recursive, boolean preserve, int bufferSize) throws IOException {
ScpAckInfo ackInfo = readAck(false);
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("send({}) ACK={}", paths, ackInfo);
}
validateOperationReadyCode("send", "Paths", ackInfo);
readAndValidateOperationAck("send", "Paths");

LinkOption[] options = IoUtils.getLinkOptions(true);
for (String pattern : paths) {
Expand Down Expand Up @@ -464,11 +460,7 @@ public void send(Collection<String> paths, boolean recursive, boolean preserve,

public void sendPaths(Collection<? extends Path> paths, boolean recursive, boolean preserve, int bufferSize)
throws IOException {
ScpAckInfo ackInfo = readAck(false);
if (log.isDebugEnabled()) {
log.debug("sendPaths({}) ACK={}", paths, ackInfo);
}
validateOperationReadyCode("sendPaths", "Paths", ackInfo);
readAndValidateOperationAck("sendPaths", "Paths");

LinkOption[] options = IoUtils.getLinkOptions(true);
for (Path file : paths) {
Expand Down Expand Up @@ -750,6 +742,15 @@ public ScpAckInfo readAck(boolean canEof) throws IOException {
return ScpAckInfo.readAck(in, csIn, canEof);
}

public void readAndValidateOperationAck(String cmd, Object location) throws IOException {
ScpAckInfo ackInfo = readAck(false);
boolean debugEnabled = log.isDebugEnabled();
if (debugEnabled) {
log.debug("readAndValidateOperationAck({}) ACK={}", location, ackInfo);
}
validateOperationReadyCode(cmd, location, ackInfo);
}

@Override
public String toString() {
return getClass().getSimpleName() + "[" + getSession() + "]";
Expand Down