From b263b1e282f73b82c5dd87d8808681819d13f2af Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Fri, 27 Jul 2001 08:24:59 +0000 Subject: [PATCH] Add failonerror attribute to . Submitted by: Steve Loughran I've modified Steve's original submission to ensure quiet and failonerror don't get set to true at the same time - also fixed the logging a little as messages about failures to delete a file would go to verbose logging or swallowed inconsistently in quiet mode. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@269397 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/delete.html | 15 +- docs/manual/CoreTasks/untar.html | 2 +- .../org/apache/tools/ant/taskdefs/Delete.java | 135 ++++++++++++------ 3 files changed, 109 insertions(+), 43 deletions(-) diff --git a/docs/manual/CoreTasks/delete.html b/docs/manual/CoreTasks/delete.html index c2f9e214b..96de2d750 100644 --- a/docs/manual/CoreTasks/delete.html +++ b/docs/manual/CoreTasks/delete.html @@ -43,10 +43,21 @@ To remove empty directories, use the includeEmptyDirs atribute.

This means that if a file or directory cannot be deleted, then no error is reported. This setting emulates the -f option to the Unix "rm" command. - ("true"/"false"). - Default is "false" meaning things are "noisy". + ("true"/"false"). + Default is "false" meaning things are "noisy". + Setting this to true, implies setting failonerror to false. No + + failonerror + + This flag (which is only of relevance if 'quiet' is false), + controls whether an error -such as a failure to delete a file- + stops the build task, or is merely reported to the screen. + The default is "true" + + No + includeEmptyDirs Set to "true" to delete empty directories when diff --git a/docs/manual/CoreTasks/untar.html b/docs/manual/CoreTasks/untar.html index ce08510a1..f2c183589 100644 --- a/docs/manual/CoreTasks/untar.html +++ b/docs/manual/CoreTasks/untar.html @@ -37,7 +37,7 @@ carried from tarfile.

overwrite - Overwrite files, even if they are newer than the + Overwrite files, even if they are newer than the corresponding entries in the archive (true or false, default is true). No diff --git a/src/main/org/apache/tools/ant/taskdefs/Delete.java b/src/main/org/apache/tools/ant/taskdefs/Delete.java index 24b368c5b..0ca04bbb7 100644 --- a/src/main/org/apache/tools/ant/taskdefs/Delete.java +++ b/src/main/org/apache/tools/ant/taskdefs/Delete.java @@ -79,32 +79,33 @@ public class Delete extends MatchingTask { protected File dir = null; protected Vector filesets = new Vector(); protected boolean usedMatchingTask = false; - protected boolean includeEmpty = false; // by default, remove matching empty dirs + protected boolean includeEmpty = false; // by default, remove matching empty dirs private int verbosity = Project.MSG_VERBOSE; private boolean quiet = false; + private boolean failonerror = true; /** * Set the name of a single file to be removed. - * + * * @param file the file to be deleted */ public void setFile(File file) { this.file = file; - } + } /** * Set the directory from which files are to be deleted - * + * * @param dir the directory path. */ public void setDir(File dir) { this.dir = dir; - } + } /** * Used to force listing of all names of deleted files. - * + * * @param verbose "true" or "on" */ public void setVerbose(boolean verbose) { @@ -112,21 +113,33 @@ public class Delete extends MatchingTask { this.verbosity = Project.MSG_INFO; } else { this.verbosity = Project.MSG_VERBOSE; - } - } + } + } /** - * If the file does not exist, do not display a diagnostic + * If the file does not exist, do not display a diagnostic * message or modify the exit status to reflect an error. * This means that if a file or directory cannot be deleted, - * then no error is reported. This setting emulates the + * then no error is reported. This setting emulates the * -f option to the Unix "rm" command. * Default is false meaning things are "noisy" * @param quiet "true" or "on" */ public void setQuiet(boolean quiet) { this.quiet = quiet; - } + if (quiet) { + this.failonerror = false; + } + } + + /** + * this flag means 'note errors to the output, but keep going' + * @param failonerror true or false + */ + public void setFailOnError(boolean failonerror) { + this.failonerror=failonerror; + } + /** * Used to delete empty directories. @@ -141,7 +154,7 @@ public class Delete extends MatchingTask { public void addFileset(FileSet set) { filesets.addElement(set); } - + /** * add a name entry on the include list */ @@ -149,7 +162,7 @@ public class Delete extends MatchingTask { usedMatchingTask = true; return super.createInclude(); } - + /** * add a name entry on the exclude list */ @@ -191,7 +204,7 @@ public class Delete extends MatchingTask { /** * Sets whether default exclusions should be used or not. * - * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions + * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions * should be used, "false"|"off"|"no" when they * shouldn't be used. */ @@ -204,7 +217,7 @@ public class Delete extends MatchingTask { * Sets the name of the file containing the includes patterns. * * @param includesfile A string containing the filename to fetch - * the include patterns from. + * the include patterns from. */ public void setIncludesfile(File includesfile) { usedMatchingTask = true; @@ -215,7 +228,7 @@ public class Delete extends MatchingTask { * Sets the name of the file containing the includes patterns. * * @param excludesfile A string containing the filename to fetch - * the include patterns from. + * the include patterns from. */ public void setExcludesfile(File excludesfile) { usedMatchingTask = true; @@ -232,7 +245,13 @@ public class Delete extends MatchingTask { if (file == null && dir == null && filesets.size() == 0) { throw new BuildException("At least one of the file or dir attributes, or a fileset element, must be set."); - } + } + + if (quiet && failonerror) { + throw new BuildException("quiet and failonerror cannot both be set to true", + location); + } + // delete the single file if (file != null) { @@ -241,13 +260,19 @@ public class Delete extends MatchingTask { log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute. Use dir instead."); } else { log("Deleting: " + file.getAbsolutePath()); - - if (!file.delete() && !quiet) { - throw new BuildException("Unable to delete file " + file.getAbsolutePath()); - } - } + + if (!file.delete()) { + String message="Unable to delete file " + file.getAbsolutePath(); + if(failonerror) + throw new BuildException(message); + else + log(message, + quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); + } + } } else { - log("Could not find file " + file.getAbsolutePath() + " to delete.", Project.MSG_VERBOSE); + log("Could not find file " + file.getAbsolutePath() + " to delete.", + Project.MSG_VERBOSE); } } @@ -255,8 +280,8 @@ public class Delete extends MatchingTask { if (dir != null && dir.exists() && dir.isDirectory() && !usedMatchingTask) { /* If verbosity is MSG_VERBOSE, that mean we are doing regular logging - (backwards as that sounds). In that case, we want to print one - message about deleting the top of the directory tree. Otherwise, + (backwards as that sounds). In that case, we want to print one + message about deleting the top of the directory tree. Otherwise, the removeDir method will handle messages for _all_ directories. */ if (verbosity == Project.MSG_VERBOSE) { @@ -275,10 +300,11 @@ public class Delete extends MatchingTask { removeFiles(fs.getDir(project), files, dirs); } catch (BuildException be) { // directory doesn't exist or is not readable - if (!quiet) { + if (failonerror) { throw be; } else { - log(be.getMessage(), Project.MSG_VERBOSE); + log(be.getMessage(), + quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); } } } @@ -292,14 +318,15 @@ public class Delete extends MatchingTask { removeFiles(dir, files, dirs); } catch (BuildException be) { // directory doesn't exist or is not readable - if (!quiet) { + if (failonerror) { throw be; } else { - log(be.getMessage(), Project.MSG_VERBOSE); + log(be.getMessage(), + quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); } } } - } + } //************************************************************************ // protected and private methods @@ -315,25 +342,47 @@ public class Delete extends MatchingTask { removeDir(f); } else { log("Deleting " + f.getAbsolutePath(), verbosity); - if (!f.delete() && !quiet) { - throw new BuildException("Unable to delete file " + f.getAbsolutePath()); + if (!f.delete()) { + String message="Unable to delete file " + f.getAbsolutePath(); + if(failonerror) + throw new BuildException(message); + else + log(message, + quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); } } } log("Deleting directory " + d.getAbsolutePath(), verbosity); - if (!d.delete() && !quiet) { - throw new BuildException("Unable to delete directory " + dir.getAbsolutePath()); + if (!d.delete()) { + String message="Unable to delete directory " + dir.getAbsolutePath(); + if(failonerror) + throw new BuildException(message); + else + log(message, + quiet ? Project.MSG_VERBOSE : Project.MSG_WARN); } } + /** + * remove an array of files in a directory, and a list of subdirectories + * which will only be deleted if 'includeEmpty' is true + * @param d directory to work from + * @param files array of files to delete; can be of zero length + * @param dirs array of directories to delete; can of zero length + */ protected void removeFiles(File d, String[] files, String[] dirs) { if (files.length > 0) { log("Deleting " + files.length + " files from " + d.getAbsolutePath()); for (int j=0; j 0) { - log("Deleted " + dirCount + " director" + - (dirCount==1 ? "y" : "ies") + + log("Deleted " + dirCount + " director" + + (dirCount==1 ? "y" : "ies") + " from " + d.getAbsolutePath()); } }