LOG4J2-1173 Fixed rollover error when copying to a directory mapped to a
remote Linux host.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/220d6b85
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/220d6b85
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/220d6b85

Branch: refs/heads/LOG4J-1181
Commit: 220d6b850cff04b0cd835667a342082326f0b6b9
Parents: 2855e97
Author: rpopma <[email protected]>
Authored: Sun Nov 29 12:54:04 2015 +0900
Committer: rpopma <[email protected]>
Committed: Sun Nov 29 12:54:04 2015 +0900

----------------------------------------------------------------------
 .../rolling/action/FileRenameAction.java        | 31 +++++++++++++++-----
 src/changes/changes.xml                         |  3 ++
 2 files changed, 26 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/220d6b85/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java
index 32cff00..4337eec 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/FileRenameAction.java
@@ -17,8 +17,10 @@
 package org.apache.logging.log4j.core.appender.rolling.action;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.nio.file.Files;
+import java.nio.channels.FileChannel;
 
 /**
  * File rename action.
@@ -43,8 +45,8 @@ public class FileRenameAction extends AbstractAction {
     /**
      * Creates an FileRenameAction.
      *
-     * @param src              current file name.
-     * @param dst              new file name.
+     * @param src current file name.
+     * @param dst new file name.
      * @param renameEmptyFiles if true, rename file even if empty, otherwise 
delete empty files.
      */
     public FileRenameAction(final File src, final File dst, final boolean 
renameEmptyFiles) {
@@ -65,6 +67,7 @@ public class FileRenameAction extends AbstractAction {
 
     /**
      * Gets the destination.
+     * 
      * @return the destination.
      */
     public File getDestination() {
@@ -73,6 +76,7 @@ public class FileRenameAction extends AbstractAction {
 
     /**
      * Gets the source.
+     * 
      * @return the source.
      */
     public File getSource() {
@@ -81,6 +85,7 @@ public class FileRenameAction extends AbstractAction {
 
     /**
      * Whether to rename empty files. If true, rename empty files, otherwise 
delete empty files.
+     * 
      * @return Whether to rename empty files.
      */
     public boolean isRenameEmptyFiles() {
@@ -90,8 +95,8 @@ public class FileRenameAction extends AbstractAction {
     /**
      * Rename file.
      *
-     * @param source           current file name.
-     * @param destination      new file name.
+     * @param source current file name.
+     * @param destination new file name.
      * @param renameEmptyFiles if true, rename file even if empty, otherwise 
delete empty files.
      * @return true if successfully renamed.
      */
@@ -115,7 +120,7 @@ public class FileRenameAction extends AbstractAction {
                         return source.delete();
                     } catch (final IOException iex) {
                         LOGGER.error("Unable to rename file {} to {} - {}", 
source.getAbsolutePath(),
-                            destination.getAbsolutePath(), iex.getMessage());
+                                destination.getAbsolutePath(), 
iex.getMessage());
                     }
                 }
                 return true;
@@ -125,7 +130,7 @@ public class FileRenameAction extends AbstractAction {
                     return source.delete();
                 } catch (final IOException iex) {
                     LOGGER.error("Unable to rename file {} to {} - {}", 
source.getAbsolutePath(),
-                        destination.getAbsolutePath(), iex.getMessage());
+                            destination.getAbsolutePath(), iex.getMessage());
                 }
             }
         } else {
@@ -143,7 +148,17 @@ public class FileRenameAction extends AbstractAction {
         if (!destination.exists()) {
             destination.createNewFile();
         }
-        Files.copy(source.toPath(), destination.toPath());
+
+        // LOG4J2-1173: Files.copy(source.toPath(), destination.toPath()) 
throws IOException
+        // when copying to a directory mapped to a remote Linux host
+
+        try (FileInputStream srcStream = new FileInputStream(source);
+                FileOutputStream destStream = new 
FileOutputStream(destination);
+                FileChannel srcChannel = srcStream.getChannel();
+                FileChannel destChannel = destStream.getChannel();) {
+
+            destChannel.transferFrom(srcChannel, 0, srcChannel.size());
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/220d6b85/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5829213..9ebbb46 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -24,6 +24,9 @@
   </properties>
   <body>
     <release version="2.5" date="2015-MM-DD" description="GA Release 2.5">
+      <action issue="LOG4J2-1173" dev="rpopma" type="fix">
+        Fixed rollover error when copying to a directory mapped to a remote 
Linux host.
+      </action>
       <action issue="LOG4J2-435" dev="rpopma" type="add" due-to="Robert 
Schaft">
         Added support for custom delete actions triggered by a rollover.
       </action>

Reply via email to