From 727faae6253126291dd52a1103713681e925f96b Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 10 Apr 2002 10:27:52 +0000 Subject: [PATCH] Bug 7552 auditing of - dir may have been altered in execute. Fix backwards incompatible API change I've introduced last week (setTimeout's argument has been changed from Integer to Long). Fix the code in that should ensure that dest has been specified. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272335 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/tools/ant/taskdefs/ExecTask.java | 64 +++++++++++++------ .../apache/tools/ant/taskdefs/ExecuteOn.java | 10 +-- 2 files changed, 51 insertions(+), 23 deletions(-) diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java index d949de471..c720f1d99 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecTask.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecTask.java @@ -59,6 +59,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Environment; +import org.apache.tools.ant.util.StringUtils; import java.io.File; import java.io.FileOutputStream; @@ -81,8 +82,6 @@ import java.io.FileNotFoundException; */ public class ExecTask extends Task { - private static String lSep = System.getProperty("line.separator"); - private String os; private File out; private File dir; @@ -98,16 +97,32 @@ public class ExecTask extends Task { private boolean failIfExecFails=true; private boolean append = false; - /** Controls whether the VM (1.3 and above) is used to execute the command */ + /** + * Controls whether the VM (1.3 and above) is used to execute the + * command + */ private boolean vmLauncher = true; /** * Timeout in milliseconds after which the process will be killed. + * + * @since Ant 1.5 */ public void setTimeout(Long value) { timeout = value; } + /** + * Timeout in milliseconds after which the process will be killed. + */ + public void setTimeout(Integer value) { + if (value == null) { + timeout = null; + } else { + setTimeout(new Long(value.intValue())); + } + } + /** * The command to execute. */ @@ -123,7 +138,8 @@ public class ExecTask extends Task { } /** - * Only execute the process if os.name is included in this string. + * Only execute the process if os.name is included in + * this string. */ public void setOs(String os) { this.os = os; @@ -185,7 +201,8 @@ public class ExecTask extends Task { /** * fill a property in with a result. * when no property is defined: failure to execute - * @since 1.5 + * + * @since Ant 1.5 */ public void setResultProperty(String resultProperty) { this.resultProperty=resultProperty; @@ -222,9 +239,14 @@ public class ExecTask extends Task { * Do the work. */ public void execute() throws BuildException { + File savedDir = dir; // possibly altered in prepareExec checkConfiguration(); if (isValidOs()) { - runExec(prepareExec()); + try { + runExec(prepareExec()); + } finally { + dir = savedDir; + } } } @@ -272,13 +294,13 @@ public class ExecTask extends Task { protected Execute prepareExec() throws BuildException { // default directory to the project's base directory if (dir == null) { - dir = project.getBaseDir(); + dir = project.getBaseDir(); } // show the command log(cmdl.toString(), Project.MSG_VERBOSE); Execute exe = new Execute(createHandler(), createWatchdog()); - exe.setAntRun(project); + exe.setAntRun(getProject()); exe.setWorkingDirectory(dir); exe.setVMLauncher(vmLauncher); String[] environment = env.getVariables(); @@ -294,7 +316,8 @@ public class ExecTask extends Task { } /** - * A Utility method for this classes and subclasses to run an Execute instance (an external command). + * A Utility method for this classes and subclasses to run an + * Execute instance (an external command). */ protected final void runExecute(Execute exe) throws IOException { int err = -1; // assume the worst @@ -307,7 +330,8 @@ public class ExecTask extends Task { maybeSetResultPropertyValue(err); if (err != 0) { if (failOnError) { - throw new BuildException(taskType + " returned: "+err, location); + throw new BuildException(taskType + " returned: "+err, + location); } else { log("Result: " + err, Project.MSG_ERR); } @@ -319,7 +343,7 @@ public class ExecTask extends Task { StringBuffer val = new StringBuffer(); while ((line = in.readLine()) != null) { if (val.length() != 0) { - val.append(lSep); + val.append(StringUtils.LINE_SEP); } val.append(line); } @@ -328,7 +352,8 @@ public class ExecTask extends Task { } /** - * Run the command using the given Execute instance. This may be overidden by subclasses + * Run the command using the given Execute instance. This may be + * overidden by subclasses */ protected void runExec(Execute exe) throws BuildException { exe.setCommandline(cmdl.getCommandline()); @@ -336,7 +361,8 @@ public class ExecTask extends Task { runExecute(exe); } catch (IOException e) { if (failIfExecFails) { - throw new BuildException("Execute failed: "+e.toString(),e, location); + throw new BuildException("Execute failed: "+e.toString(), e, + location); } else { log("Execute failed: "+e.toString(), Project.MSG_ERR); } @@ -356,9 +382,11 @@ public class ExecTask extends Task { log("Output redirected to " + out, Project.MSG_VERBOSE); return new PumpStreamHandler(fos); } catch (FileNotFoundException fne) { - throw new BuildException("Cannot write to "+out, fne, location); + throw new BuildException("Cannot write to "+out, fne, + location); } catch (IOException ioe) { - throw new BuildException("Cannot write to "+out, ioe, location); + throw new BuildException("Cannot write to "+out, ioe, + location); } } else if (outputprop != null) { baos = new ByteArrayOutputStream(); @@ -375,7 +403,7 @@ public class ExecTask extends Task { */ protected ExecuteWatchdog createWatchdog() throws BuildException { if (timeout == null) { - return null; + return null; } return new ExecuteWatchdog(timeout.longValue()); } @@ -386,10 +414,10 @@ public class ExecTask extends Task { protected void logFlush() { try { if (fos != null) { - fos.close(); + fos.close(); } if (baos != null) { - baos.close(); + baos.close(); } } catch (IOException io) {} } diff --git a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java index 4156a7b05..6c78183ac 100644 --- a/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java +++ b/src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java @@ -144,8 +144,8 @@ public class ExecuteOn extends ExecTask { */ public Commandline.Marker createSrcfile() { if (srcFilePos != null) { - throw new BuildException(taskType + " doesn\'t support multiple srcfile elements.", - location); + throw new BuildException(taskType + " doesn\'t support multiple " + + "srcfile elements.", location); } srcFilePos = cmdl.createMarker(); return srcFilePos; @@ -157,8 +157,8 @@ public class ExecuteOn extends ExecTask { */ public Commandline.Marker createTargetfile() { if (targetFilePos != null) { - throw new BuildException(taskType + " doesn\'t support multiple targetfile elements.", - location); + throw new BuildException(taskType + " doesn\'t support multiple " + + "targetfile elements.", location); } targetFilePos = cmdl.createMarker(); srcIsFirst = (srcFilePos != null); @@ -193,7 +193,7 @@ public class ExecuteOn extends ExecTask { if (mapperElement == null) { throw new BuildException("no mapper specified", location); } - if (mapperElement == null) { + if (destDir == null) { throw new BuildException("no dest attribute specified", location); }