This is an automated email from the ASF dual-hosted git repository. vy pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push: new 937c877 LOG4J2-2828 Preserve file permissions after rollover in RollingRandomAccessFileAppender. (#364) 937c877 is described below commit 937c8771128300217bc48a2b4368dd101195d0f9 Author: Volkan Yazıcı <volkan.yaz...@gmail.com> AuthorDate: Wed Jun 17 12:40:52 2020 +0200 LOG4J2-2828 Preserve file permissions after rollover in RollingRandomAccessFileAppender. (#364) --- .../rolling/RollingRandomAccessFileManager.java | 4 + .../RollingRandomAccessFileManagerTest.java | 92 +++++++++++++++++++--- 2 files changed, 84 insertions(+), 12 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 f5f71d3..b956239 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; @@ -149,6 +150,9 @@ public class RollingRandomAccessFileManager extends RollingFileManager { private void createFileAfterRollover(final 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 8fa07b9..e1a1dd3 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 @@ -17,6 +17,26 @@ package org.apache.logging.log4j.core.appender.rolling; +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; + +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 static org.apache.logging.log4j.hamcrest.FileMatchers.beforeNow; import static org.apache.logging.log4j.hamcrest.FileMatchers.hasLength; import static org.apache.logging.log4j.hamcrest.FileMatchers.isEmpty; @@ -25,21 +45,10 @@ 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.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.RandomAccessFile; -import java.util.concurrent.locks.LockSupport; - -import org.apache.logging.log4j.core.util.Closer; -import org.apache.logging.log4j.core.util.NullOutputStream; -import org.apache.logging.log4j.util.Strings; -import org.junit.Test; - /** * Tests the RollingRandomAccessFileManager class. */ @@ -198,4 +207,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); + + } + }