Skip to content

Commit 9597b7a

Browse files
authored
GH-8563: Fix SFTP RENAME with unconditional DEL (#8671)
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`
1 parent c5f6c12 commit 9597b7a

File tree

1 file changed

+15
-15
lines changed
  • spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session

1 file changed

+15
-15
lines changed

spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,18 @@ public SftpSession(SftpClient sftpClient) {
6565

6666
@Override
6767
public boolean remove(String path) throws IOException {
68-
this.sftpClient.remove(path);
68+
try {
69+
this.sftpClient.remove(path);
70+
}
71+
catch (SftpException sftpEx) {
72+
if (SftpConstants.SSH_FX_NO_SUCH_FILE == sftpEx.getStatus()) {
73+
return false;
74+
}
75+
else {
76+
throw sftpEx;
77+
}
78+
}
79+
6980
return true;
7081
}
7182

@@ -174,19 +185,8 @@ public void rename(String pathFrom, String pathTo) throws IOException {
174185
this.sftpClient.rename(pathFrom, pathTo, SftpClient.CopyMode.Overwrite);
175186
}
176187
else {
177-
try {
178-
this.sftpClient.rename(pathFrom, pathTo);
179-
}
180-
catch (SftpException sftpex) {
181-
if (SftpConstants.SSH_FX_FILE_ALREADY_EXISTS == sftpex.getStatus()) {
182-
remove(pathTo);
183-
// attempt to rename again
184-
this.sftpClient.rename(pathFrom, pathTo);
185-
}
186-
else {
187-
throw sftpex;
188-
}
189-
}
188+
remove(pathTo);
189+
this.sftpClient.rename(pathFrom, pathTo);
190190
}
191191
}
192192

0 commit comments

Comments
 (0)