Skip to content

Commit

Permalink
GH-8563: Fix SFTP RENAME with unconditional DEL (#8671)
Browse files Browse the repository at this point in the history
Fixes #8563

* Rework the `SftpSession.rename()` logic to be similar to what is there in the `FtpSession`, for example:
try to remove a remote target file before renaming.
* Fix `SftpSession.remove()` to check for `SftpConstants.SSH_FX_NO_SUCH_FILE` response status
to return `false`
  • Loading branch information
artembilan committed Jul 12, 2023
1 parent c5f6c12 commit 9597b7a
Showing 1 changed file with 15 additions and 15 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -65,7 +65,18 @@ public SftpSession(SftpClient sftpClient) {

@Override
public boolean remove(String path) throws IOException {
this.sftpClient.remove(path);
try {
this.sftpClient.remove(path);
}
catch (SftpException sftpEx) {
if (SftpConstants.SSH_FX_NO_SUCH_FILE == sftpEx.getStatus()) {
return false;
}
else {
throw sftpEx;
}
}

return true;
}

Expand Down Expand Up @@ -174,19 +185,8 @@ public void rename(String pathFrom, String pathTo) throws IOException {
this.sftpClient.rename(pathFrom, pathTo, SftpClient.CopyMode.Overwrite);
}
else {
try {
this.sftpClient.rename(pathFrom, pathTo);
}
catch (SftpException sftpex) {
if (SftpConstants.SSH_FX_FILE_ALREADY_EXISTS == sftpex.getStatus()) {
remove(pathTo);
// attempt to rename again
this.sftpClient.rename(pathFrom, pathTo);
}
else {
throw sftpex;
}
}
remove(pathTo);
this.sftpClient.rename(pathFrom, pathTo);
}
}

Expand Down

0 comments on commit 9597b7a

Please sign in to comment.