From 442b63ec58074c3bfa50ddbe6d9cfee30934f4a1 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Sat, 2 Mar 2024 09:52:12 +0100 Subject: [PATCH] [MSHARED-1330] Always overwrite files (#97) --- .../shared/filtering/FilteringUtils.java | 21 +++++++++++++++++++ .../shared/filtering/FilteringUtilsTest.java | 17 +++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java b/src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java index 3d4fa86..6c1f3d3 100644 --- a/src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java +++ b/src/main/java/org/apache/maven/shared/filtering/FilteringUtils.java @@ -26,6 +26,8 @@ import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.NoSuchFileException; +import java.nio.file.attribute.PosixFilePermission; +import java.util.EnumSet; import java.util.StringTokenizer; import java.util.regex.Pattern; @@ -305,6 +307,8 @@ static boolean isEmpty(final String string) { * @throws IOException if an IO error occurs during copying or filtering */ public static void copyFile(File from, File to, String encoding, FilterWrapper[] wrappers) throws IOException { + setReadWritePermissions(to); + if (wrappers == null || wrappers.length == 0) { try (OutputStream os = new CachingOutputStream(to.toPath())) { Files.copy(from.toPath(), os); @@ -371,6 +375,23 @@ private static void copyFilePermissions(File source, File destination) throws IO } } + @SuppressWarnings("ResultOfMethodCallIgnored") + private static void setReadWritePermissions(File file) throws IOException { + if (file.exists()) { + try { + Files.setPosixFilePermissions( + file.toPath(), + EnumSet.of( + PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, + PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, + PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_WRITE)); + } catch (UnsupportedOperationException e) { + file.setReadable(true); + file.setWritable(true); + } + } + } + private static Charset charset(String encoding) { if (encoding == null || encoding.isEmpty()) { return Charset.defaultCharset(); diff --git a/src/test/java/org/apache/maven/shared/filtering/FilteringUtilsTest.java b/src/test/java/org/apache/maven/shared/filtering/FilteringUtilsTest.java index d6a64a7..5392b3d 100644 --- a/src/test/java/org/apache/maven/shared/filtering/FilteringUtilsTest.java +++ b/src/test/java/org/apache/maven/shared/filtering/FilteringUtilsTest.java @@ -30,6 +30,7 @@ import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; /** * @author John Casey @@ -148,4 +149,20 @@ void escapeWindowsPathNotAtBeginning() throws Exception { "jdbc:derby:C:\\\\Users\\\\Administrator/test;create=true", FilteringUtils.escapeWindowsPath("jdbc:derby:C:\\Users\\Administrator/test;create=true")); } + + // MSHARED-1330 + @Test + void copyReadOnlyFileTwice() throws Exception { + File temp = File.createTempFile("pre-", ".txt"); + temp.setReadOnly(); + + File out = File.createTempFile("out-", ".txt"); + out.delete(); + + FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]); + assertFalse(out.canWrite()); + + FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]); + assertFalse(out.canWrite()); + } }