Skip to content

Commit

Permalink
use nio.Files.createTempFile rather than File.createTempFile
Browse files Browse the repository at this point in the history
  • Loading branch information
bodewig committed May 5, 2020
1 parent 041b058 commit a8645a1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/main/org/apache/tools/ant/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
Expand Down Expand Up @@ -100,6 +105,13 @@ public class FileUtils {
*/
public static final long NTFS_FILE_TIMESTAMP_GRANULARITY = 1;

private static final FileAttribute[] TMPFILE_ATTRIBUTES =
new FileAttribute[] {
PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE))
};
private static final FileAttribute[] NO_TMPFILE_ATTRIBUTES = new FileAttribute[0];

/**
* A one item cache for fromUri.
* fromUri is called for each element when parsing ant build
Expand Down Expand Up @@ -893,6 +905,10 @@ public String toVMSPath(File f) {
* yield a different file name.
* </p>
*
* <p>If the filesystem where the temporary file is created
* supports POSIX permissions, the file will only be readable and
* writable by the current user.</p>
*
* @param prefix file name prefix.
* @param suffix
* file extension; include the '.'.
Expand All @@ -916,6 +932,10 @@ public File createTempFile(String prefix, String suffix, File parentDir) {
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
*
* <p>If the filesystem where the temporary file is created
* supports POSIX permissions, the file will only be readable and
* writable by the current user.</p>
*
* @param prefix file name prefix.
* @param suffix file extension; include the '.'.
* @param parentDir Directory to create the temporary file in;
Expand Down Expand Up @@ -947,6 +967,10 @@ public File createTempFile(String prefix, String suffix, File parentDir,
* exist before this method was invoked, any subsequent invocation
* of this method will yield a different file name.</p>
*
* <p>If the filesystem where the temporary file is created
* supports POSIX permissions, the file will only be readable and
* writable by the current user.</p>
*
* @param project reference to the current Ant project.
* @param prefix file name prefix.
* @param suffix file extension; include the '.'.
Expand Down Expand Up @@ -984,7 +1008,12 @@ public File createTempFile(final Project project, String prefix, String suffix,

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);
Expand Down Expand Up @@ -1015,6 +1044,10 @@ public File createTempFile(final Project project, String prefix, String suffix,
* yield a different file name.
* </p>
*
* <p>If the filesystem where the temporary file is created
* supports POSIX permissions, the file will only be readable and
* writable by the current user.</p>
*
* @param prefix file name prefix.
* @param suffix
* file extension; include the '.'.
Expand Down
13 changes: 13 additions & 0 deletions src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@
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;
Expand All @@ -40,7 +43,9 @@
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;
Expand Down Expand Up @@ -370,6 +375,14 @@ public void testCreateTempFile() throws IOException {
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<PosixFilePermission> perm = attributes.readAttributes().permissions();
assertThat(perm,
containsInAnyOrder(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
assertThat(perm, hasSize(2));
}
tmp1.delete();

// null parent dir, project without magic property
Expand Down

0 comments on commit a8645a1

Please sign in to comment.