Browse Source

Bug 7552 auditing of <exec> - 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 <apply> 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
master
Stefan Bodewig 23 years ago
parent
commit
727faae625
2 changed files with 51 additions and 23 deletions
  1. +46
    -18
      src/main/org/apache/tools/ant/taskdefs/ExecTask.java
  2. +5
    -5
      src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java

+ 46
- 18
src/main/org/apache/tools/ant/taskdefs/ExecTask.java View File

@@ -59,6 +59,7 @@ import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Commandline; import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.Environment; import org.apache.tools.ant.types.Environment;
import org.apache.tools.ant.util.StringUtils;


import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@@ -81,8 +82,6 @@ import java.io.FileNotFoundException;
*/ */
public class ExecTask extends Task { public class ExecTask extends Task {


private static String lSep = System.getProperty("line.separator");

private String os; private String os;
private File out; private File out;
private File dir; private File dir;
@@ -98,16 +97,32 @@ public class ExecTask extends Task {
private boolean failIfExecFails=true; private boolean failIfExecFails=true;
private boolean append = false; 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; private boolean vmLauncher = true;


/** /**
* Timeout in milliseconds after which the process will be killed. * Timeout in milliseconds after which the process will be killed.
*
* @since Ant 1.5
*/ */
public void setTimeout(Long value) { public void setTimeout(Long value) {
timeout = 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. * The command to execute.
*/ */
@@ -123,7 +138,8 @@ public class ExecTask extends Task {
} }


/** /**
* Only execute the process if <code>os.name</code> is included in this string.
* Only execute the process if <code>os.name</code> is included in
* this string.
*/ */
public void setOs(String os) { public void setOs(String os) {
this.os = os; this.os = os;
@@ -185,7 +201,8 @@ public class ExecTask extends Task {
/** /**
* fill a property in with a result. * fill a property in with a result.
* when no property is defined: failure to execute * when no property is defined: failure to execute
* @since 1.5
*
* @since Ant 1.5
*/ */
public void setResultProperty(String resultProperty) { public void setResultProperty(String resultProperty) {
this.resultProperty=resultProperty; this.resultProperty=resultProperty;
@@ -222,9 +239,14 @@ public class ExecTask extends Task {
* Do the work. * Do the work.
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
File savedDir = dir; // possibly altered in prepareExec
checkConfiguration(); checkConfiguration();
if (isValidOs()) { if (isValidOs()) {
runExec(prepareExec());
try {
runExec(prepareExec());
} finally {
dir = savedDir;
}
} }
} }


@@ -272,13 +294,13 @@ public class ExecTask extends Task {
protected Execute prepareExec() throws BuildException { protected Execute prepareExec() throws BuildException {
// default directory to the project's base directory // default directory to the project's base directory
if (dir == null) { if (dir == null) {
dir = project.getBaseDir();
dir = project.getBaseDir();
} }
// show the command // show the command
log(cmdl.toString(), Project.MSG_VERBOSE); log(cmdl.toString(), Project.MSG_VERBOSE);


Execute exe = new Execute(createHandler(), createWatchdog()); Execute exe = new Execute(createHandler(), createWatchdog());
exe.setAntRun(project);
exe.setAntRun(getProject());
exe.setWorkingDirectory(dir); exe.setWorkingDirectory(dir);
exe.setVMLauncher(vmLauncher); exe.setVMLauncher(vmLauncher);
String[] environment = env.getVariables(); 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 { protected final void runExecute(Execute exe) throws IOException {
int err = -1; // assume the worst int err = -1; // assume the worst
@@ -307,7 +330,8 @@ public class ExecTask extends Task {
maybeSetResultPropertyValue(err); maybeSetResultPropertyValue(err);
if (err != 0) { if (err != 0) {
if (failOnError) { if (failOnError) {
throw new BuildException(taskType + " returned: "+err, location);
throw new BuildException(taskType + " returned: "+err,
location);
} else { } else {
log("Result: " + err, Project.MSG_ERR); log("Result: " + err, Project.MSG_ERR);
} }
@@ -319,7 +343,7 @@ public class ExecTask extends Task {
StringBuffer val = new StringBuffer(); StringBuffer val = new StringBuffer();
while ((line = in.readLine()) != null) { while ((line = in.readLine()) != null) {
if (val.length() != 0) { if (val.length() != 0) {
val.append(lSep);
val.append(StringUtils.LINE_SEP);
} }
val.append(line); 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 { protected void runExec(Execute exe) throws BuildException {
exe.setCommandline(cmdl.getCommandline()); exe.setCommandline(cmdl.getCommandline());
@@ -336,7 +361,8 @@ public class ExecTask extends Task {
runExecute(exe); runExecute(exe);
} catch (IOException e) { } catch (IOException e) {
if (failIfExecFails) { if (failIfExecFails) {
throw new BuildException("Execute failed: "+e.toString(),e, location);
throw new BuildException("Execute failed: "+e.toString(), e,
location);
} else { } else {
log("Execute failed: "+e.toString(), Project.MSG_ERR); 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); log("Output redirected to " + out, Project.MSG_VERBOSE);
return new PumpStreamHandler(fos); return new PumpStreamHandler(fos);
} catch (FileNotFoundException fne) { } catch (FileNotFoundException fne) {
throw new BuildException("Cannot write to "+out, fne, location);
throw new BuildException("Cannot write to "+out, fne,
location);
} catch (IOException ioe) { } 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) { } else if (outputprop != null) {
baos = new ByteArrayOutputStream(); baos = new ByteArrayOutputStream();
@@ -375,7 +403,7 @@ public class ExecTask extends Task {
*/ */
protected ExecuteWatchdog createWatchdog() throws BuildException { protected ExecuteWatchdog createWatchdog() throws BuildException {
if (timeout == null) { if (timeout == null) {
return null;
return null;
} }
return new ExecuteWatchdog(timeout.longValue()); return new ExecuteWatchdog(timeout.longValue());
} }
@@ -386,10 +414,10 @@ public class ExecTask extends Task {
protected void logFlush() { protected void logFlush() {
try { try {
if (fos != null) { if (fos != null) {
fos.close();
fos.close();
} }
if (baos != null) { if (baos != null) {
baos.close();
baos.close();
} }
} catch (IOException io) {} } catch (IOException io) {}
} }


+ 5
- 5
src/main/org/apache/tools/ant/taskdefs/ExecuteOn.java View File

@@ -144,8 +144,8 @@ public class ExecuteOn extends ExecTask {
*/ */
public Commandline.Marker createSrcfile() { public Commandline.Marker createSrcfile() {
if (srcFilePos != null) { 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(); srcFilePos = cmdl.createMarker();
return srcFilePos; return srcFilePos;
@@ -157,8 +157,8 @@ public class ExecuteOn extends ExecTask {
*/ */
public Commandline.Marker createTargetfile() { public Commandline.Marker createTargetfile() {
if (targetFilePos != null) { 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(); targetFilePos = cmdl.createMarker();
srcIsFirst = (srcFilePos != null); srcIsFirst = (srcFilePos != null);
@@ -193,7 +193,7 @@ public class ExecuteOn extends ExecTask {
if (mapperElement == null) { if (mapperElement == null) {
throw new BuildException("no mapper specified", location); throw new BuildException("no mapper specified", location);
} }
if (mapperElement == null) {
if (destDir == null) {
throw new BuildException("no dest attribute specified", throw new BuildException("no dest attribute specified",
location); location);
} }


Loading…
Cancel
Save