From 938aa626c2b6a4d285f0a481be32eb6eecb26333 Mon Sep 17 00:00:00 2001 From: Timothy Bish Date: Tue, 10 Nov 2015 11:07:18 -0500 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-6039 If the rename option fails, then we must try a complete copy since the move can cross file systems. --- .../java/org/apache/activemq/util/IOHelper.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/activemq-broker/src/main/java/org/apache/activemq/util/IOHelper.java b/activemq-broker/src/main/java/org/apache/activemq/util/IOHelper.java index a1ba3ecfb0..fb0784c950 100644 --- a/activemq-broker/src/main/java/org/apache/activemq/util/IOHelper.java +++ b/activemq-broker/src/main/java/org/apache/activemq/util/IOHelper.java @@ -23,12 +23,15 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; import java.util.Stack; /** - * + * Collection of File and Folder utility methods. */ public final class IOHelper { @@ -186,7 +189,16 @@ public final class IOHelper { public static void moveFile(File src, File targetDirectory) throws IOException { if (!src.renameTo(new File(targetDirectory, src.getName()))) { - throw new IOException("Failed to move " + src + " to " + targetDirectory); + + // If rename fails we must do a true deep copy instead. + Path sourcePath = src.toPath(); + Path targetDirPath = targetDirectory.toPath(); + + try { + Files.move(sourcePath, targetDirPath.resolve(sourcePath.getFileName()), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException ex) { + throw new IOException("Failed to move " + src + " to " + targetDirectory + " - " + ex.getMessage()); + } } }