diff --git a/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java b/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java index 60c7764..3982175 100644 --- a/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java +++ b/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java @@ -35,6 +35,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.Sets; + import org.apache.commons.lang.StringUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; @@ -312,6 +313,33 @@ public String getQueryFileRegex() { return queryFileRegex; } + private String createNewFile(String fileName) throws Exception { + final int maxRetries = 100; + int dotIndex = fileName.lastIndexOf('.'); + String fileNameWithoutExtension = dotIndex == -1 ? fileName : fileName.substring(0, dotIndex); + String fileNameExtension = dotIndex == -1 ? "" : fileName.substring(dotIndex); + + for (int count = 1; count <= maxRetries; count++) { + String currFileName = fileNameWithoutExtension+count+fileNameExtension; + File newFile = new File(currFileName); + + // The current file already exists, try to delete the existing one + if (newFile.exists() && !newFile.delete()) { + System.out.println( " Unable to delete " + currFileName + ", trying another file name"); + continue; + } + // We were not able to create new file + if (!newFile.createNewFile()) { + System.out.println("Unable to create " + currFileName + " , trying another file name"); + continue; + } + System.out.println("Successfully created new file " + currFileName); + return currFileName; + } + throw new Exception("Unable to create alternative file for " + fileName + + " after " + maxRetries + " attempts"); + } + public void execute() throws BuildException { if (getTemplatePath().equals("")) { throw new BuildException("No templatePath attribute specified"); @@ -436,8 +464,10 @@ public void execute() throws BuildException { if (logFile != null) { File lf = new File(logFile); if (lf.exists()) { + System.out.println("Log file already exists: " + lf.getCanonicalPath()); if (!lf.delete()) { - throw new Exception("Could not delete log file " + lf.getCanonicalPath()); + System.out.println("Could not delete log file " + lf.getCanonicalPath()); + logFile = createNewFile(logFile); } } @@ -458,14 +488,8 @@ public void execute() throws BuildException { String qFileNamesFile = qFileNames.getCanonicalPath(); if (qFileNames.exists()) { - if (!qFileNames.delete()) { - throw new Exception("Could not delete old query file names containing file " + - qFileNamesFile); - } - } - if (!qFileNames.createNewFile()) { - throw new Exception("Could not create query file names containing file " + - qFileNamesFile); + System.out.println("Query file names containing file already exists: " + qFileNamesFile); + qFileNamesFile = createNewFile(qFileNamesFile); } FileWriter fw = new FileWriter(qFileNames.getCanonicalFile());