30th-dguv commented on code in PR #20100:
URL: https://github.com/apache/camel/pull/20100#discussion_r2581426003


##########
components/camel-smb/src/main/java/org/apache/camel/component/smb/SmbOperations.java:
##########
@@ -192,27 +193,63 @@ public boolean existsFile(String name) throws 
GenericFileOperationFailedExceptio
 
     @Override
     public boolean renameFile(String from, String to) throws 
GenericFileOperationFailedException {
+        boolean renamed;
         connectIfNecessary();
-        try (File src
-                = share.openFile(from, EnumSet.of(AccessMask.GENERIC_READ, 
AccessMask.DELETE), null,
-                        SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, 
null)) {
-
-            try (File dst
-                    = share.openFile(to, EnumSet.of(AccessMask.GENERIC_WRITE), 
EnumSet.of(FileAttributes.FILE_ATTRIBUTE_NORMAL),
-                            SMB2ShareAccess.ALL, 
SMB2CreateDisposition.FILE_CREATE,
-                            
EnumSet.of(SMB2CreateOptions.FILE_DIRECTORY_FILE))) {
-
-                src.remoteCopyTo(dst);
-            } catch (Exception e) {
-                throw new GenericFileOperationFailedException(e.getMessage(), 
e);
+        try (File src = share.openFile(from, 
EnumSet.of(AccessMask.GENERIC_READ, AccessMask.DELETE), null,
+                SMB2ShareAccess.ALL, SMB2CreateDisposition.FILE_OPEN, null)) {
+            if (configuration.isRenameUsingCopy()) {
+                renamed = copyAndDeleteRenameStrategy(src, to);
+            } else {
+                renamed = atomicRenameFile(src, to);
+                if (!renamed && configuration.isCopyAndDeleteOnRenameFail()) {
+                    // we could not rename using atomic rename strategy, so 
lets fallback and do a copy/delete approach.
+                    LOG.debug("Cannot rename file from: {} to: {}, will now 
use a copy/delete approach instead",
+                            src.getUncPath(), to);
+                    renamed = copyAndDeleteRenameStrategy(src, to);
+                }
             }
-
-            src.deleteOnClose();
         } catch (SMBRuntimeException smbre) {
             safeDisconnect(smbre);
             throw smbre;
         }
-        return true;
+        return renamed;
+    }
+
+    public boolean atomicRenameFile(File src, String to)
+            throws GenericFileOperationFailedException {
+        try {
+            src.rename(to);
+            LOG.debug("Renamed file: {} to: {} using atomic rename", 
src.getUncPath(), to);
+            return true;
+        } catch (Throwable e) {
+            if (configuration.isCopyAndDeleteOnRenameFail()) {
+                // Fallback is enabled, log warning and return false to 
trigger fallback
+                LOG.warn(
+                        "Failed to rename file: {} to: {} using atomic rename. 
Will fallback to copy+delete strategy. Reason: {}",
+                        src.getUncPath(), to, e.getMessage());

Review Comment:
   It could be critically important (security, consistency etc.). 
   If it will be too noisy one can always disable this feature over the route 
configuration.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to