You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2020/06/17 08:25:43 UTC

[logging-log4j2] branch LOG4J2-2828 created (now 52696e7)

This is an automated email from the ASF dual-hosted git repository.

vy pushed a change to branch LOG4J2-2828
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git.


      at 52696e7  LOG4J2-2828 Retain file permissions after rollover in RollingRandomAccessFileAppender.

This branch includes the following new commits:

     new 52696e7  LOG4J2-2828 Retain file permissions after rollover in RollingRandomAccessFileAppender.

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[logging-log4j2] 01/01: LOG4J2-2828 Retain file permissions after rollover in RollingRandomAccessFileAppender.

Posted by vy...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch LOG4J2-2828
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 52696e7d6b12a7fa86428b18326a13b685191c06
Author: Volkan Yazıcı <vo...@gmail.com>
AuthorDate: Wed Jun 17 10:25:14 2020 +0200

    LOG4J2-2828 Retain file permissions after rollover in RollingRandomAccessFileAppender.
---
 .../rolling/RollingRandomAccessFileManager.java    |  4 ++
 .../RollingRandomAccessFileManagerTest.java        | 73 ++++++++++++++++++++--
 2 files changed, 73 insertions(+), 4 deletions(-)

diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
index ad7df84..7f7bf85 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManager.java
@@ -22,6 +22,7 @@ import java.io.OutputStream;
 import java.io.RandomAccessFile;
 import java.io.Serializable;
 import java.nio.ByteBuffer;
+import java.nio.file.Paths;
 
 import org.apache.logging.log4j.core.Layout;
 import org.apache.logging.log4j.core.LoggerContext;
@@ -140,6 +141,9 @@ public class RollingRandomAccessFileManager extends RollingFileManager {
 
     private void createFileAfterRollover(String fileName) throws IOException {
         this.randomAccessFile = new RandomAccessFile(fileName, "rw");
+        if (isAttributeViewEnabled()) {
+            defineAttributeView(Paths.get(fileName));
+        }
         if (isAppend()) {
             randomAccessFile.seek(randomAccessFile.length());
         }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
index 8f7f717..c87907a 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingRandomAccessFileManagerTest.java
@@ -23,19 +23,25 @@ import static org.apache.logging.log4j.hamcrest.FileMatchers.isEmpty;
 import static org.apache.logging.log4j.hamcrest.FileMatchers.lastModified;
 import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.lessThanOrEqualTo;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.RandomAccessFile;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.attribute.PosixFileAttributeView;
+import java.nio.file.attribute.PosixFilePermission;
+import java.nio.file.attribute.PosixFilePermissions;
+import java.util.Set;
 import java.util.concurrent.locks.LockSupport;
+import java.util.zip.Deflater;
 
+import org.apache.logging.log4j.core.config.DefaultConfiguration;
 import org.apache.logging.log4j.core.util.Closer;
+import org.apache.logging.log4j.core.util.FileUtils;
 import org.apache.logging.log4j.core.util.NullOutputStream;
 import org.apache.logging.log4j.util.Strings;
 import org.junit.Test;
@@ -198,4 +204,63 @@ public class RollingRandomAccessFileManagerTest {
         assertThat(file, lastModified(equalTo(manager.getFileTime())));
     }
 
+    @Test
+    public void testRolloverRetainsFileAttributes() throws Exception {
+
+        // Short-circuit if host doesn't support file attributes.
+        if (!FileUtils.isFilePosixAttributeViewSupported()) {
+            return;
+        }
+
+        // Create the initial file.
+        final File file = File.createTempFile("log4j2", "test");
+        LockSupport.parkNanos(1000000); // 1 millisec
+
+        // Set the initial file attributes.
+        final String filePermissionsString = "rwxrwxrwx";
+        final Set<PosixFilePermission> filePermissions =
+                PosixFilePermissions.fromString(filePermissionsString);
+        FileUtils.defineFilePosixAttributeView(file.toPath(), filePermissions, null, null);
+
+        // Create the manager.
+        final RolloverStrategy rolloverStrategy = DefaultRolloverStrategy
+                .newBuilder()
+                .setMax("7")
+                .setMin("1")
+                .setFileIndex("max")
+                .setStopCustomActionsOnError(false)
+                .setConfig(new DefaultConfiguration())
+                .build();
+        final RollingRandomAccessFileManager manager =
+                RollingRandomAccessFileManager.getRollingRandomAccessFileManager(
+                        file.getAbsolutePath(),
+                        Strings.EMPTY,
+                        true,
+                        true,
+                        RollingRandomAccessFileManager.DEFAULT_BUFFER_SIZE,
+                        new SizeBasedTriggeringPolicy(Long.MAX_VALUE),
+                        rolloverStrategy,
+                        null,
+                        null,
+                        filePermissionsString,
+                        null,
+                        null,
+                        null);
+        assertNotNull(manager);
+        manager.initialize();
+
+        // Trigger a rollover.
+        manager.rollover();
+
+        // Verify the rolled over file attributes.
+        final Set<PosixFilePermission> actualFilePermissions = Files
+                .getFileAttributeView(
+                        Paths.get(manager.getFileName()),
+                        PosixFileAttributeView.class)
+                .readAttributes()
+                .permissions();
+        assertEquals(filePermissions, actualFilePermissions);
+
+    }
+
 }