From 3c7b2626196c7e71f2c69495aea20fada9afc925 Mon Sep 17 00:00:00 2001 From: Mike Moser Date: Mon, 3 Jun 2024 13:10:50 +0000 Subject: [PATCH] NIFI-13159 Moved Remote Delete after Put for PutFTP/PutSFTP REPLACE Strategy This closes #8914 Signed-off-by: David Handermann --- .../util/file/transfer/PutFileTransfer.java | 1 - .../processors/standard/util/FTPTransfer.java | 7 +++++++ .../standard/util/SFTPTransfer.java | 7 +++++++ .../standard/util/TestServerSFTPTransfer.java | 21 ------------------- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/nifi-extension-bundles/nifi-extension-utils/nifi-file-transfer/src/main/java/org/apache/nifi/processor/util/file/transfer/PutFileTransfer.java b/nifi-extension-bundles/nifi-extension-utils/nifi-file-transfer/src/main/java/org/apache/nifi/processor/util/file/transfer/PutFileTransfer.java index cfbb0aa3e7..2888a3bc99 100644 --- a/nifi-extension-bundles/nifi-extension-utils/nifi-file-transfer/src/main/java/org/apache/nifi/processor/util/file/transfer/PutFileTransfer.java +++ b/nifi-extension-bundles/nifi-extension-utils/nifi-file-transfer/src/main/java/org/apache/nifi/processor/util/file/transfer/PutFileTransfer.java @@ -225,7 +225,6 @@ public abstract class PutFileTransfer extends AbstractPr logger.warn("Resolving conflict by rejecting {} due to conflicting filename with a directory or file already on remote server", flowFile); break; case FileTransfer.CONFLICT_RESOLUTION_REPLACE: - transfer.deleteFile(flowFile, path, fileName); destinationRelationship = REL_SUCCESS; transferFile = true; penalizeFile = false; diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/FTPTransfer.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/FTPTransfer.java index b19e4f2488..0b2c5f3650 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/FTPTransfer.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/FTPTransfer.java @@ -475,6 +475,13 @@ public class FTPTransfer implements FileTransfer { } if (!filename.equals(tempFilename)) { + try { + // file was transferred to a temporary filename, attempt to delete destination filename before rename + client.deleteFile(fullPath); + } catch (final IOException e) { + logger.debug("Failed to remove {} before renaming temporary file", fullPath, e); + } + try { logger.debug("Renaming remote path from {} to {} for {}", tempFilename, filename, flowFile); final boolean renameSuccessful = client.rename(tempFilename, filename); diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/SFTPTransfer.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/SFTPTransfer.java index 49212fa874..1bd0ec7cee 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/SFTPTransfer.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/SFTPTransfer.java @@ -785,6 +785,13 @@ public class SFTPTransfer implements FileTransfer { } if (!filename.equals(tempFilename)) { + try { + // file was transferred to a temporary filename, attempt to delete destination filename before rename + sftpClient.rm(fullPath); + } catch (final SFTPException e) { + logger.debug("Failed to remove {} before renaming temporary file", fullPath, e); + } + try { sftpClient.rename(tempPath, fullPath); } catch (final SFTPException e) { diff --git a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestServerSFTPTransfer.java b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestServerSFTPTransfer.java index 2f8603ee1e..96099f1cab 100644 --- a/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestServerSFTPTransfer.java +++ b/nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/util/TestServerSFTPTransfer.java @@ -642,27 +642,6 @@ public class TestServerSFTPTransfer { } } - @Test - public void testPutWhenFileAlreadyExists() throws IOException { - final String permissions = "rw-rw-rw-"; - - final Map properties = createBaseProperties(); - properties.put(SFTPTransfer.PERMISSIONS, permissions); - - final String fileContent = "this is a test"; - - try (final SFTPTransfer transfer = createSFTPTransfer(properties); - final InputStream in = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8))) { - - // Verify file already exists - final FileInfo fileInfoBefore = transfer.getRemoteFileInfo(null, DIR_2, FILE_1); - assertNotNull(fileInfoBefore); - - // Should fail because file already exists - assertThrows(IOException.class, () -> transfer.put(null, DIR_2, FILE_1, in)); - } - } - @Test public void testPutWhenDirectoryDoesNotExist() throws IOException { final String permissions = "rw-rw-rw-";