3636import java .nio .file .Path ;
3737import java .nio .file .Paths ;
3838import java .nio .file .StandardOpenOption ;
39+ import java .nio .file .attribute .FileAttribute ;
40+ import java .nio .file .attribute .PosixFileAttributeView ;
41+ import java .nio .file .attribute .PosixFilePermission ;
42+ import java .nio .file .attribute .PosixFilePermissions ;
3943import java .text .DecimalFormat ;
4044import java .util .ArrayList ;
4145import java .util .Arrays ;
46+ import java .util .EnumSet ;
4247import java .util .List ;
4348import java .util .Locale ;
4449import java .util .Optional ;
@@ -100,6 +105,13 @@ public class FileUtils {
100105 */
101106 public static final long NTFS_FILE_TIMESTAMP_GRANULARITY = 1 ;
102107
108+ private static final FileAttribute [] TMPFILE_ATTRIBUTES =
109+ new FileAttribute [] {
110+ PosixFilePermissions .asFileAttribute (EnumSet .of (PosixFilePermission .OWNER_READ ,
111+ PosixFilePermission .OWNER_WRITE ))
112+ };
113+ private static final FileAttribute [] NO_TMPFILE_ATTRIBUTES = new FileAttribute [0 ];
114+
103115 /**
104116 * A one item cache for fromUri.
105117 * fromUri is called for each element when parsing ant build
@@ -893,6 +905,10 @@ public String toVMSPath(File f) {
893905 * yield a different file name.
894906 * </p>
895907 *
908+ * <p>If the filesystem where the temporary file is created
909+ * supports POSIX permissions, the file will only be readable and
910+ * writable by the current user.</p>
911+ *
896912 * @param prefix file name prefix.
897913 * @param suffix
898914 * file extension; include the '.'.
@@ -916,6 +932,10 @@ public File createTempFile(String prefix, String suffix, File parentDir) {
916932 * exist before this method was invoked, any subsequent invocation
917933 * of this method will yield a different file name.</p>
918934 *
935+ * <p>If the filesystem where the temporary file is created
936+ * supports POSIX permissions, the file will only be readable and
937+ * writable by the current user.</p>
938+ *
919939 * @param prefix file name prefix.
920940 * @param suffix file extension; include the '.'.
921941 * @param parentDir Directory to create the temporary file in;
@@ -947,6 +967,10 @@ public File createTempFile(String prefix, String suffix, File parentDir,
947967 * exist before this method was invoked, any subsequent invocation
948968 * of this method will yield a different file name.</p>
949969 *
970+ * <p>If the filesystem where the temporary file is created
971+ * supports POSIX permissions, the file will only be readable and
972+ * writable by the current user.</p>
973+ *
950974 * @param project reference to the current Ant project.
951975 * @param prefix file name prefix.
952976 * @param suffix file extension; include the '.'.
@@ -984,7 +1008,12 @@ public File createTempFile(final Project project, String prefix, String suffix,
9841008
9851009 if (createFile ) {
9861010 try {
987- result = File .createTempFile (prefix , suffix , new File (parent ));
1011+ final Path parentPath = new File (parent ).toPath ();
1012+ final PosixFileAttributeView parentPosixAttributes =
1013+ Files .getFileAttributeView (parentPath , PosixFileAttributeView .class );
1014+ result = Files .createTempFile (parentPath , prefix , suffix ,
1015+ parentPosixAttributes != null ? TMPFILE_ATTRIBUTES : NO_TMPFILE_ATTRIBUTES )
1016+ .toFile ();
9881017 } catch (IOException e ) {
9891018 throw new BuildException ("Could not create tempfile in "
9901019 + parent , e );
@@ -1015,6 +1044,10 @@ public File createTempFile(final Project project, String prefix, String suffix,
10151044 * yield a different file name.
10161045 * </p>
10171046 *
1047+ * <p>If the filesystem where the temporary file is created
1048+ * supports POSIX permissions, the file will only be readable and
1049+ * writable by the current user.</p>
1050+ *
10181051 * @param prefix file name prefix.
10191052 * @param suffix
10201053 * file extension; include the '.'.
0 commit comments