git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@608939 13f79535-47bb-0310-9956-ffa450edef68master
@@ -1190,8 +1190,7 @@ public class Execute { | |||
*/ | |||
private File createCommandFile(String[] cmd, String[] env) | |||
throws IOException { | |||
File script = FILE_UTILS.createTempFile("ANT", ".COM", null); | |||
script.deleteOnExit(); | |||
File script = FILE_UTILS.createTempFile("ANT", ".COM", null, true, true); | |||
PrintWriter out = null; | |||
try { | |||
out = new PrintWriter(new FileWriter(script)); | |||
@@ -343,7 +343,7 @@ public class FixCRLF extends MatchingTask implements ChainableReader { | |||
fcv = new Vector(1); | |||
fcv.add(fc); | |||
} | |||
File tmpFile = FILE_UTILS.createTempFileName("fixcrlf", "", null, true); | |||
File tmpFile = FILE_UTILS.createTempFile("fixcrlf", "", null, true, false); | |||
try { | |||
FILE_UTILS.copyFile(srcFile, tmpFile, null, fcv, false, false, | |||
encoding, outputEncoding == null ? encoding : outputEncoding, | |||
@@ -1694,8 +1694,7 @@ public class Javadoc extends Task { | |||
* if requested. | |||
*/ | |||
if (useExternalFile) { | |||
tmpList = FILE_UTILS.createTempFile("javadoc", "", null); | |||
tmpList.deleteOnExit(); | |||
tmpList = FILE_UTILS.createTempFile("javadoc", "", null, true, true); | |||
toExecute.createArgument() | |||
.setValue("@" + tmpList.getAbsolutePath()); | |||
srcListWriter = new PrintWriter( | |||
@@ -1882,8 +1881,7 @@ public class Javadoc extends Task { | |||
PrintWriter optionsListWriter = null; | |||
try { | |||
optionsTmpFile = FILE_UTILS.createTempFile( | |||
"javadocOptions", "", null); | |||
optionsTmpFile.deleteOnExit(); | |||
"javadocOptions", "", null, true, true); | |||
String[] listOpt = toExecute.getArguments(); | |||
toExecute.clearArgs(); | |||
toExecute.createArgument().setValue( | |||
@@ -600,7 +600,7 @@ public class Replace extends MatchingTask { | |||
in = new FileInput(src); | |||
temp = FILE_UTILS.createTempFile("rep", ".tmp", | |||
src.getParentFile()); | |||
src.getParentFile(), false, true); | |||
out = new FileOutput(temp); | |||
int repCountStart = replaceCount; | |||
@@ -155,15 +155,8 @@ public class TempFile extends Task { | |||
if (destDir == null) { | |||
destDir = getProject().resolveFile("."); | |||
} | |||
File tfile; | |||
if (createFile) { | |||
tfile = FILE_UTILS.createTempFile(prefix, suffix, destDir, | |||
deleteOnExit); | |||
} else { | |||
tfile = FILE_UTILS.createTempFileName(prefix, suffix, destDir, | |||
deleteOnExit); | |||
} | |||
File tfile = FILE_UTILS.createTempFile(prefix, suffix, destDir, | |||
deleteOnExit, createFile); | |||
getProject().setNewProperty(property, tfile.toString()); | |||
} | |||
} |
@@ -579,8 +579,8 @@ public class Zip extends MatchingTask { | |||
/** rename the zip file. */ | |||
private File renameFile() { | |||
File renamedFile = FILE_UTILS.createTempFileName( | |||
"zip", ".tmp", zipFile.getParentFile(), true); | |||
File renamedFile = FILE_UTILS.createTempFile( | |||
"zip", ".tmp", zipFile.getParentFile(), true, false); | |||
try { | |||
FILE_UTILS.rename(zipFile, renamedFile); | |||
} catch (SecurityException e) { | |||
@@ -466,8 +466,7 @@ public abstract class DefaultCompilerAdapter implements CompilerAdapter { | |||
PrintWriter out = null; | |||
try { | |||
tmpFile = FILE_UTILS.createTempFile( | |||
"files", "", getJavac().getTempdir()); | |||
tmpFile.deleteOnExit(); | |||
"files", "", getJavac().getTempdir(), true, true); | |||
out = new PrintWriter(new FileWriter(tmpFile)); | |||
for (int i = firstFileName; i < args.length; i++) { | |||
if (quoteFiles && args[i].indexOf(" ") > -1) { | |||
@@ -224,8 +224,7 @@ public class CvsTagDiff extends AbstractCvsTask { | |||
setCommand(""); | |||
File tmpFile = null; | |||
try { | |||
tmpFile = FILE_UTILS.createTempFile("cvstagdiff", ".log", null); | |||
tmpFile.deleteOnExit(); | |||
tmpFile = FILE_UTILS.createTempFile("cvstagdiff", ".log", null, true, true); | |||
setOutput(tmpFile); | |||
// run the cvs command | |||
@@ -177,8 +177,7 @@ public class Cab extends MatchingTask { | |||
*/ | |||
protected File createListFile(Vector files) | |||
throws IOException { | |||
File listFile = FILE_UTILS.createTempFile("ant", "", null); | |||
listFile.deleteOnExit(); | |||
File listFile = FILE_UTILS.createTempFile("ant", "", null, true, true); | |||
PrintWriter writer = new PrintWriter(new FileOutputStream(listFile)); | |||
@@ -314,8 +313,7 @@ public class Cab extends MatchingTask { | |||
exec.setDir(baseDir); | |||
if (!doVerbose) { | |||
outFile = FILE_UTILS.createTempFile("ant", "", null); | |||
outFile.deleteOnExit(); | |||
outFile = FILE_UTILS.createTempFile("ant", "", null, true, true); | |||
exec.setOutput(outFile); | |||
} | |||
@@ -323,8 +323,7 @@ public class ReplaceRegExp extends Task { | |||
*/ | |||
protected void doReplace(File f, int options) | |||
throws IOException { | |||
File temp = FILE_UTILS.createTempFile("replace", ".txt", null); | |||
temp.deleteOnExit(); | |||
File temp = FILE_UTILS.createTempFile("replace", ".txt", null, true, true); | |||
Reader r = null; | |||
Writer w = null; | |||
@@ -346,7 +346,7 @@ public class NetCommand { | |||
//afterwards | |||
FileOutputStream fos = null; | |||
temporaryCommandFile = FILE_UTILS.createTempFile("cmd", ".txt", null); | |||
temporaryCommandFile = FILE_UTILS.createTempFile("cmd", ".txt", null, false, true); | |||
owner.log("Using response file " + temporaryCommandFile, Project.MSG_VERBOSE); | |||
try { | |||
@@ -1145,7 +1145,7 @@ public class JUnitTask extends Task { | |||
private File createTempPropertiesFile(String prefix) { | |||
File propsFile = | |||
FILE_UTILS.createTempFile(prefix, ".properties", | |||
tmpDir != null ? tmpDir : getProject().getBaseDir(), true); | |||
tmpDir != null ? tmpDir : getProject().getBaseDir(), true, true); | |||
return propsFile; | |||
} | |||
@@ -1827,9 +1827,9 @@ public class FTP | |||
FTPFile [] theFiles = null; | |||
final int maxIterations = 1000; | |||
for (int counter = 1; counter < maxIterations; counter++) { | |||
File localFile = FILE_UTILS.createTempFileName( | |||
File localFile = FILE_UTILS.createTempFile( | |||
"ant" + Integer.toString(counter), ".tmp", | |||
null, false); | |||
null, false, false); | |||
String fileName = localFile.getName(); | |||
boolean found = false; | |||
try { | |||
@@ -409,8 +409,8 @@ public class Symlink extends DispatchTask { | |||
File canfil = linkfil.getCanonicalFile(); | |||
// rename the resource, thus breaking the link: | |||
File temp = FILE_UTILS.createTempFileName("symlink", ".tmp", | |||
canfil.getParentFile(), false); | |||
File temp = FILE_UTILS.createTempFile("symlink", ".tmp", | |||
canfil.getParentFile(), false, false); | |||
try { | |||
try { | |||
FILE_UTILS.rename(canfil, temp); | |||
@@ -449,7 +449,7 @@ public class ModifiedSelector extends BaseExtendSelector | |||
// How to handle non-file-Resources? I copy temporarily the | |||
// resource to a file and use the file-implementation. | |||
FileUtils fu = FileUtils.getFileUtils(); | |||
File tmpFile = fu.createTempFileName("modified-", ".tmp", null, true); | |||
File tmpFile = fu.createTempFile("modified-", ".tmp", null, true, false); | |||
Resource tmpResource = new FileResource(tmpFile); | |||
ResourceUtils.copyResource(resource, tmpResource); | |||
boolean isSelected = isSelected(tmpFile.getParentFile(), | |||
@@ -784,24 +784,32 @@ public class FileUtils { | |||
} | |||
/** | |||
* Create a temporary file in a given directory. | |||
* | |||
* <p>The file denoted by the returned abstract pathname did not | |||
* exist before this method was invoked, any subsequent invocation | |||
* of this method will yield a different file name.</p> | |||
* | |||
* <p>As of ant 1.8 the file is actually created.</p> | |||
* | |||
* @param prefix prefix before the random number. | |||
* @param suffix file extension; include the '.'. | |||
* @param parentDir Directory to create the temporary file in; | |||
* java.io.tmpdir used if not specified. | |||
* | |||
* @return a File reference to the new temporary file. | |||
* @since Ant 1.5 | |||
* Create a File object for a temporary file in a given directory. Without | |||
* actually creating the file. | |||
* | |||
* <p> | |||
* The file denoted by the returned abstract pathname did not exist before | |||
* this method was invoked, any subsequent invocation of this method will | |||
* yield a different file name. | |||
* </p> | |||
* <p> | |||
* The filename is prefixNNNNNsuffix where NNNN is a random number. | |||
* </p> | |||
* | |||
* @param prefix | |||
* prefix before the random number. | |||
* @param suffix | |||
* file extension; include the '.'. | |||
* @param parentDir | |||
* Directory to create the temporary file in; java.io.tmpdir used | |||
* if not specified. | |||
* | |||
* @deprecated since ant 1.8 use createTempFile(String, String, File, | |||
* boolean, boolean) instead. | |||
* @return a File reference to the new, nonexistent temporary file. | |||
*/ | |||
public File createTempFile(String prefix, String suffix, File parentDir) { | |||
return createTempFile(prefix, suffix, parentDir, false); | |||
return createTempFile(prefix, suffix, parentDir, false, false); | |||
} | |||
/** | |||
@@ -811,29 +819,43 @@ public class FileUtils { | |||
* exist before this method was invoked, any subsequent invocation | |||
* of this method will yield a different file name.</p> | |||
* | |||
* <p>As of ant 1.8 the file is actually created.</p> | |||
* | |||
* @param prefix prefix before the random number. | |||
* @param suffix file extension; include the '.'. | |||
* @param parentDir Directory to create the temporary file in; | |||
* java.io.tmpdir used if not specified. | |||
* @param deleteOnExit whether to set the tempfile for deletion on | |||
* normal VM exit. | |||
* @param createFile true if the file must actually be created. If false | |||
* chances exist that a file with the same name is created in the time | |||
* between invoking this method and the moment the file is actually created. | |||
* If possible set to true. | |||
* | |||
* @return a File reference to the new temporary file. | |||
* @since Ant 1.7 | |||
* @since Ant 1.8 | |||
*/ | |||
public File createTempFile(String prefix, String suffix, File parentDir, | |||
boolean deleteOnExit) { | |||
boolean deleteOnExit, boolean createFile) { | |||
File result = null; | |||
String parent = (parentDir == null) | |||
? System.getProperty("java.io.tmpdir") | |||
: parentDir.getPath(); | |||
try { | |||
result = File.createTempFile(prefix, suffix, new File(parent)); | |||
} catch (IOException e) { | |||
throw new BuildException("Could not create tempfile in " + parent, e); | |||
String parent = (parentDir == null) ? System | |||
.getProperty("java.io.tmpdir") : parentDir.getPath(); | |||
if (createFile) { | |||
try { | |||
result = File.createTempFile(prefix, suffix, new File(parent)); | |||
} catch (IOException e) { | |||
throw new BuildException("Could not create tempfile in " | |||
+ parent, e); | |||
} | |||
} else { | |||
DecimalFormat fmt = new DecimalFormat("#####"); | |||
synchronized (rand) { | |||
do { | |||
result = new File(parent, prefix | |||
+ fmt.format(Math.abs(rand.nextInt())) + suffix); | |||
} while (result.exists()); | |||
} | |||
} | |||
if (deleteOnExit) { | |||
result.deleteOnExit(); | |||
} | |||
@@ -843,41 +865,33 @@ public class FileUtils { | |||
/** | |||
* Create a File object for a temporary file in a given directory. Without | |||
* actually creating the file. | |||
* | |||
* <p>The file denoted by the returned abstract pathname did not | |||
* exist before this method was invoked, any subsequent invocation | |||
* of this method will yield a different file name.</p> | |||
* | |||
* <p> | |||
* The file denoted by the returned abstract pathname did not exist before | |||
* this method was invoked, any subsequent invocation of this method will | |||
* yield a different file name. | |||
* </p> | |||
* <p> | |||
* The filename is prefixNNNNNsuffix where NNNN is a random number. | |||
* </p> | |||
* | |||
* @param prefix prefix before the random number. | |||
* @param suffix file extension; include the '.'. | |||
* @param parentDir Directory to create the temporary file in; | |||
* java.io.tmpdir used if not specified. | |||
* @param deleteOnExit whether to set the tempfile for deletion on | |||
* normal VM exit. | |||
* | |||
* | |||
* @param prefix | |||
* prefix before the random number. | |||
* @param suffix | |||
* file extension; include the '.'. | |||
* @param parentDir | |||
* Directory to create the temporary file in; java.io.tmpdir used | |||
* if not specified. | |||
* @param deleteOnExit | |||
* whether to set the tempfile for deletion on normal VM exit. | |||
* | |||
* @deprecated since ant 1.8 use createTempFile(String, String, File, | |||
* boolean, boolean) instead. | |||
* @return a File reference to the new, nonexistent temporary file. | |||
* @since Ant 1.8 | |||
*/ | |||
public File createTempFileName(String prefix, String suffix, | |||
public File createTempFile(String prefix, String suffix, | |||
File parentDir, boolean deleteOnExit) { | |||
File result = null; | |||
String parent = (parentDir == null) ? System | |||
.getProperty("java.io.tmpdir") : parentDir.getPath(); | |||
DecimalFormat fmt = new DecimalFormat("#####"); | |||
synchronized (rand) { | |||
do { | |||
result = new File(parent, prefix | |||
+ fmt.format(Math.abs(rand.nextInt())) + suffix); | |||
} while (result.exists()); | |||
} | |||
if (deleteOnExit) { | |||
result.deleteOnExit(); | |||
} | |||
return result; | |||
return createTempFile(prefix, suffix, parentDir, deleteOnExit, false); | |||
} | |||
/** | |||
@@ -440,7 +440,7 @@ public final class JavaEnvUtils { | |||
*/ | |||
public static File createVmsJavaOptionFile(String[] cmd) | |||
throws IOException { | |||
File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null); | |||
File script = FILE_UTILS.createTempFile("ANT", ".JAVA_OPTS", null, false, true); | |||
PrintWriter out = null; | |||
try { | |||
out = new PrintWriter(new BufferedWriter(new FileWriter(script))); | |||
@@ -347,7 +347,7 @@ public class ExecTaskTest extends BuildFileTest { | |||
return; | |||
} | |||
myBuild = new MonitoredBuild(new File(System.getProperty("root"), BUILD_FILE), "spawn"); | |||
logFile = FILE_UTILS.createTempFileName("spawn","log", project.getBaseDir(), false); | |||
logFile = FILE_UTILS.createTempFile("spawn","log", project.getBaseDir(), false, false); | |||
// this is guaranteed by FileUtils#createTempFile | |||
assertTrue("log file not existing", !logFile.exists()); | |||
// make the spawned process run 4 seconds | |||
@@ -175,7 +175,7 @@ public class JavaTest extends BuildFileTest { | |||
} | |||
public void testSpawn() { | |||
File logFile = FILE_UTILS.createTempFileName("spawn","log", project.getBaseDir(), false); | |||
File logFile = FILE_UTILS.createTempFile("spawn","log", project.getBaseDir(), false, false); | |||
// this is guaranteed by FileUtils#createTempFile | |||
assertTrue("log file not existing", !logFile.exists()); | |||
project.setProperty("logFile", logFile.getAbsolutePath()); | |||
@@ -390,7 +390,7 @@ public class ModifiedSelectorTest extends BaseSelectorTest { | |||
* configure() method of ModifiedSelector. This testcase tests that. | |||
*/ | |||
public void testCreatePropertiesCacheViaCustomSelector() { | |||
File cachefile = FILE_UTILS.createTempFileName("tmp-cache-", ".properties", null, false); | |||
File cachefile = FILE_UTILS.createTempFile("tmp-cache-", ".properties", null, false, false); | |||
try { | |||
// initialize test environment (called "bed") | |||
makeBed(); | |||
@@ -342,55 +342,50 @@ public class FileUtilsTest extends TestCase { | |||
*/ | |||
public void testCreateTempFile() | |||
{ | |||
// null parent dir | |||
File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", null, false); | |||
String tmploc = System.getProperty("java.io.tmpdir"); | |||
String name = tmp1.getName(); | |||
assertTrue("starts with pre", name.startsWith("pre")); | |||
assertTrue("ends with .suf", name.endsWith(".suf")); | |||
assertTrue("File was created", tmp1.exists()); | |||
assertEquals((new File(tmploc, tmp1.getName())).getAbsolutePath(), | |||
tmp1.getAbsolutePath()); | |||
tmp1.delete(); | |||
File dir2 = new File(tmploc+"/ant-test"); | |||
dir2.mkdir(); | |||
removeThis = dir2; | |||
File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", dir2, true); | |||
String name2 = tmp2.getName(); | |||
assertTrue("starts with pre", name2.startsWith("pre")); | |||
assertTrue("ends with .suf", name2.endsWith(".suf")); | |||
assertTrue("File was created", tmp2.exists()); | |||
assertEquals((new File(dir2, tmp2.getName())).getAbsolutePath(), | |||
tmp2.getAbsolutePath()); | |||
tmp2.delete(); | |||
dir2.delete(); | |||
} | |||
/** | |||
* Test createTempFileName | |||
*/ | |||
public void testCreateTempFileName() { | |||
// null parent dir | |||
File tmp1 = FILE_UTILS.createTempFile("pre", ".suf", null, false, true); | |||
String tmploc = System.getProperty("java.io.tmpdir"); | |||
String name = tmp1.getName(); | |||
assertTrue("starts with pre", name.startsWith("pre")); | |||
assertTrue("ends with .suf", name.endsWith(".suf")); | |||
assertTrue("File was created", tmp1.exists()); | |||
assertEquals((new File(tmploc, tmp1.getName())).getAbsolutePath(), tmp1 | |||
.getAbsolutePath()); | |||
tmp1.delete(); | |||
File dir2 = new File(tmploc + "/ant-test"); | |||
dir2.mkdir(); | |||
removeThis = dir2; | |||
File tmp2 = FILE_UTILS.createTempFile("pre", ".suf", dir2, true, true); | |||
String name2 = tmp2.getName(); | |||
assertTrue("starts with pre", name2.startsWith("pre")); | |||
assertTrue("ends with .suf", name2.endsWith(".suf")); | |||
assertTrue("File was created", tmp2.exists()); | |||
assertEquals((new File(dir2, tmp2.getName())).getAbsolutePath(), tmp2 | |||
.getAbsolutePath()); | |||
tmp2.delete(); | |||
dir2.delete(); | |||
File parent = new File((new File("/tmp")).getAbsolutePath()); | |||
File tmp1 = FILE_UTILS.createTempFileName("pre", ".suf", parent, false); | |||
tmp1 = FILE_UTILS.createTempFile("pre", ".suf", parent, false); | |||
assertTrue("new file", !tmp1.exists()); | |||
String name = tmp1.getName(); | |||
name = tmp1.getName(); | |||
assertTrue("starts with pre", name.startsWith("pre")); | |||
assertTrue("ends with .suf", name.endsWith(".suf")); | |||
assertEquals("is inside parent dir", | |||
parent.getAbsolutePath(), | |||
tmp1.getParent()); | |||
assertEquals("is inside parent dir", parent.getAbsolutePath(), tmp1 | |||
.getParent()); | |||
File tmp2 = FILE_UTILS.createTempFileName("pre", ".suf", parent, false); | |||
assertTrue("files are different", | |||
!tmp1.getAbsolutePath().equals(tmp2.getAbsolutePath())); | |||
tmp2 = FILE_UTILS.createTempFile("pre", ".suf", parent, false); | |||
assertTrue("files are different", !tmp1.getAbsolutePath().equals( | |||
tmp2.getAbsolutePath())); | |||
// null parent dir | |||
File tmp3 = FILE_UTILS.createTempFileName("pre", ".suf", null, false); | |||
String tmploc = System.getProperty("java.io.tmpdir"); | |||
assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(), | |||
tmp3.getAbsolutePath()); | |||
File tmp3 = FILE_UTILS.createTempFile("pre", ".suf", null, false); | |||
tmploc = System.getProperty("java.io.tmpdir"); | |||
assertEquals((new File(tmploc, tmp3.getName())).getAbsolutePath(), tmp3 | |||
.getAbsolutePath()); | |||
} | |||
/** | |||