From a8645a151bc706259fb1789ef587d05482d98612 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
If the filesystem where the temporary file is created + * supports POSIX permissions, the file will only be readable and + * writable by the current user.
+ * * @param prefix file name prefix. * @param suffix * file extension; include the '.'. @@ -916,6 +932,10 @@ public class FileUtils { * exist before this method was invoked, any subsequent invocation * of this method will yield a different file name. * + *If the filesystem where the temporary file is created + * supports POSIX permissions, the file will only be readable and + * writable by the current user.
+ * * @param prefix file name prefix. * @param suffix file extension; include the '.'. * @param parentDir Directory to create the temporary file in; @@ -947,6 +967,10 @@ public class FileUtils { * exist before this method was invoked, any subsequent invocation * of this method will yield a different file name. * + *If the filesystem where the temporary file is created + * supports POSIX permissions, the file will only be readable and + * writable by the current user.
+ * * @param project reference to the current Ant project. * @param prefix file name prefix. * @param suffix file extension; include the '.'. @@ -984,7 +1008,12 @@ public class FileUtils { if (createFile) { try { - result = File.createTempFile(prefix, suffix, new File(parent)); + final Path parentPath = new File(parent).toPath(); + final PosixFileAttributeView parentPosixAttributes = + Files.getFileAttributeView(parentPath, PosixFileAttributeView.class); + result = Files.createTempFile(parentPath, prefix, suffix, + parentPosixAttributes != null ? TMPFILE_ATTRIBUTES : NO_TMPFILE_ATTRIBUTES) + .toFile(); } catch (IOException e) { throw new BuildException("Could not create tempfile in " + parent, e); @@ -1015,6 +1044,10 @@ public class FileUtils { * yield a different file name. * * + *If the filesystem where the temporary file is created + * supports POSIX permissions, the file will only be readable and + * writable by the current user.
+ * * @param prefix file name prefix. * @param suffix * file extension; include the '.'. diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index fc584563d..d2ea12222 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -24,8 +24,11 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFilePermission; import java.util.Locale; import java.util.Optional; +import java.util.Set; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.MagicTestNames; @@ -40,7 +43,9 @@ import org.junit.rules.TemporaryFolder; import static org.apache.tools.ant.util.FileUtils.getFileUtils; import static org.apache.tools.ant.util.FileUtils.isCaseSensitiveFileSystem; import static org.apache.tools.ant.util.FileUtils.isContextRelativePath; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.startsWith; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -370,6 +375,14 @@ public class FileUtilsTest { assertTrue("File was created", tmp1.exists()); assertEquals((new File(tmploc, tmp1.getName())).getAbsolutePath(), tmp1.getAbsolutePath()); + final PosixFileAttributeView attributes = + Files.getFileAttributeView(tmp1.toPath(), PosixFileAttributeView.class); + if (attributes != null) { + final Set