Skip to content

Commit 87ac51d

Browse files
committed
fallback to a separate owner-only tempdir if possible
1 parent 08a6e42 commit 87ac51d

File tree

3 files changed

+105
-5
lines changed

3 files changed

+105
-5
lines changed

src/main/org/apache/tools/ant/MagicNames.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,5 +337,15 @@ private MagicNames() {
337337
* @since Ant 1.10.8
338338
*/
339339
public static final String TMPDIR = "ant.tmpdir";
340+
341+
/**
342+
* Magic property that will be set to override java.io.tmpdir
343+
* system property as the location for Ant's default temporary
344+
* directory if a temp file is created and {@link #TMPDIR} is not
345+
* set.
346+
* Value: {@value}
347+
* @since Ant 1.10.9
348+
*/
349+
public static final String AUTO_TMPDIR = "ant.auto.tmpdir";
340350
}
341351

src/main/org/apache/tools/ant/util/FileUtils.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public class FileUtils {
110110
PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ,
111111
PosixFilePermission.OWNER_WRITE))
112112
};
113+
private static final FileAttribute[] TMPDIR_ATTRIBUTES =
114+
new FileAttribute[] {
115+
PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ,
116+
PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE))
117+
};
113118
private static final FileAttribute[] NO_TMPFILE_ATTRIBUTES = new FileAttribute[0];
114119

115120
/**
@@ -991,14 +996,35 @@ public File createTempFile(String prefix, String suffix, File parentDir,
991996
public File createTempFile(final Project project, String prefix, String suffix,
992997
final File parentDir, final boolean deleteOnExit, final boolean createFile) {
993998
File result;
994-
final String parent;
999+
String p = null;
9951000
if (parentDir != null) {
996-
parent = parentDir.getPath();
1001+
p = parentDir.getPath();
9971002
} else if (project != null && project.getProperty(MagicNames.TMPDIR) != null) {
998-
parent = project.getProperty(MagicNames.TMPDIR);
999-
} else {
1000-
parent = System.getProperty("java.io.tmpdir");
1003+
p = project.getProperty(MagicNames.TMPDIR);
1004+
} else if (project != null && deleteOnExit) {
1005+
if (project.getProperty(MagicNames.AUTO_TMPDIR) != null) {
1006+
p = project.getProperty(MagicNames.AUTO_TMPDIR);
1007+
} else {
1008+
final Path systemTempDirPath =
1009+
new File(System.getProperty("java.io.tmpdir")).toPath();
1010+
final PosixFileAttributeView systemTempDirPosixAttributes =
1011+
Files.getFileAttributeView(systemTempDirPath, PosixFileAttributeView.class);
1012+
if (systemTempDirPosixAttributes != null) {
1013+
// no reason to create an extra temp dir if we cannot set permissions
1014+
try {
1015+
final File projectTempDir = Files.createTempDirectory(systemTempDirPath,
1016+
"ant", TMPDIR_ATTRIBUTES)
1017+
.toFile();
1018+
projectTempDir.deleteOnExit();
1019+
p = projectTempDir.getAbsolutePath();
1020+
project.setProperty(MagicNames.AUTO_TMPDIR, p);
1021+
} catch (IOException ex) {
1022+
// silently fall back to system temp directory
1023+
}
1024+
}
1025+
}
10011026
}
1027+
final String parent = p != null ? p : System.getProperty("java.io.tmpdir");
10021028
if (prefix == null) {
10031029
prefix = NULL_PLACEHOLDER;
10041030
}

src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import static org.junit.Assert.assertEquals;
5151
import static org.junit.Assert.assertFalse;
5252
import static org.junit.Assert.assertNotEquals;
53+
import static org.junit.Assert.assertNotNull;
54+
import static org.junit.Assert.assertNull;
5355
import static org.junit.Assert.assertThat;
5456
import static org.junit.Assert.assertTrue;
5557
import static org.junit.Assume.assumeFalse;
@@ -450,6 +452,68 @@ public void testCreateTempFile() throws IOException {
450452
tmp2.getAbsolutePath()));
451453
}
452454

455+
@Test
456+
public void createTempFileUsesAntTmpDirIfSetAndDeleteOnExitIsTrue() throws IOException {
457+
final Project project = new Project();
458+
final File projectTmpDir = folder.newFolder("subdir");
459+
project.setProperty("ant.tmpdir", projectTmpDir.getAbsolutePath());
460+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, true, true);
461+
assertTrue(tmpFile + " must be child of " + projectTmpDir,
462+
tmpFile.getAbsolutePath().startsWith(projectTmpDir.getAbsolutePath()));
463+
}
464+
465+
@Test
466+
public void createTempFileUsesAntTmpDirIfSetAndDeleteOnExitIsFalse() throws IOException {
467+
final Project project = new Project();
468+
final File projectTmpDir = folder.newFolder("subdir");
469+
project.setProperty("ant.tmpdir", projectTmpDir.getAbsolutePath());
470+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, false, true);
471+
assertTrue(tmpFile + " must be child of " + projectTmpDir,
472+
tmpFile.getAbsolutePath().startsWith(projectTmpDir.getAbsolutePath()));
473+
}
474+
475+
@Test
476+
public void createTempFileCreatesAutoTmpDirIfDeleteOnExitIsTrueOnUnix() throws IOException {
477+
assumeFalse("Test doesn't run on DOS", Os.isFamily("dos"));
478+
final Project project = new Project();
479+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, true, true);
480+
final String autoTempDir = project.getProperty("ant.auto.tmpdir");
481+
assertNotNull(autoTempDir);
482+
assertTrue(tmpFile + " must be child of " + autoTempDir,
483+
tmpFile.getAbsolutePath().startsWith(autoTempDir));
484+
}
485+
486+
@Test
487+
public void createTempFileDoesntCreateAutoTmpDirIfDeleteOnExitIsFalse() throws IOException {
488+
final Project project = new Project();
489+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, false, true);
490+
assertNull(project.getProperty("ant.auto.tmpdir"));
491+
}
492+
493+
@Test
494+
public void createTempFileReusesAutoTmpDirIfDeleteOnExitIsTrueOnUnix() throws IOException {
495+
assumeFalse("Test doesn't run on DOS", Os.isFamily("dos"));
496+
final Project project = new Project();
497+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, true, true);
498+
final String autoTempDir = project.getProperty("ant.auto.tmpdir");
499+
assertNotNull(autoTempDir);
500+
final File tmpFile2 = getFileUtils().createTempFile(project, null, null, null, true, true);
501+
assertTrue(tmpFile2 + " must be child of " + autoTempDir,
502+
tmpFile2.getAbsolutePath().startsWith(autoTempDir));
503+
}
504+
505+
@Test
506+
public void createTempFileDoesntReusesAutoTmpDirIfDeleteOnExitIsFalse() throws IOException {
507+
assumeFalse("Test doesn't run on DOS", Os.isFamily("dos"));
508+
final Project project = new Project();
509+
final File tmpFile = getFileUtils().createTempFile(project, null, null, null, true, true);
510+
final String autoTempDir = project.getProperty("ant.auto.tmpdir");
511+
assertNotNull(autoTempDir);
512+
final File tmpFile2 = getFileUtils().createTempFile(project, null, null, null, false, true);
513+
assertFalse(tmpFile2 + " must not be child of " + autoTempDir,
514+
tmpFile2.getAbsolutePath().startsWith(autoTempDir));
515+
}
516+
453517
/**
454518
* Test contentEquals
455519
*/

0 commit comments

Comments
 (0)