From aa480e92110a4c3c685b7cd89cf05702f8d8f7ae Mon Sep 17 00:00:00 2001 From: abilan Date: Mon, 10 Jul 2023 16:01:44 -0400 Subject: [PATCH] GH-8563: Fix SFTP RENAME with unconditional DEL Fixes https://github.com/spring-projects/spring-integration/issues/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` --- .../integration/sftp/session/SftpSession.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java index e26772c418e..ddbeae10d36 100644 --- a/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java +++ b/spring-integration-sftp/src/main/java/org/springframework/integration/sftp/session/SftpSession.java @@ -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. @@ -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; } @@ -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); } }