You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by bo...@apache.org on 2001/07/27 10:24:59 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Delete.java

bodewig     01/07/27 01:24:59

  Modified:    docs/manual/CoreTasks delete.html untar.html
               src/main/org/apache/tools/ant/taskdefs Delete.java
  Log:
  Add failonerror attribute to <delete>.
  
  Submitted by:	Steve Loughran <st...@iseran.com>
  
  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.
  
  Revision  Changes    Path
  1.4       +13 -2     jakarta-ant/docs/manual/CoreTasks/delete.html
  
  Index: delete.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/delete.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- delete.html	2001/06/13 15:29:42	1.3
  +++ delete.html	2001/07/27 08:24:59	1.4
  @@ -43,10 +43,21 @@
        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 &quot;rm&quot; command.
  -    (&quot;true&quot;/&quot;false&quot;).
  -	 Default is &quot;false&quot; meaning things are &quot;noisy&quot;.</td>
  +     (&quot;true&quot;/&quot;false&quot;).
  +     Default is &quot;false&quot; meaning things are &quot;noisy&quot;.</td>
  +     Setting this to true, implies setting failonerror to false.
       <td align="center" valign="top">No</td>
     </tr>
  +  <tr>
  +    <td valign="top">failonerror</td>
  +    <td valign="top">
  +    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 &quot;true&quot;
  +    </td>
  +    <td align="center" valign="top">No</td>
  +  </tr>  
     <tr>
       <td valign="top">includeEmptyDirs</td>
       <td valign="top">Set to &quot;true&quot; to delete empty directories when
  
  
  
  1.4       +1 -1      jakarta-ant/docs/manual/CoreTasks/untar.html
  
  Index: untar.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTasks/untar.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- untar.html	2001/07/27 06:52:50	1.3
  +++ untar.html	2001/07/27 08:24:59	1.4
  @@ -37,7 +37,7 @@
     </tr>
     <tr>
       <td valign="top">overwrite</td>
  -    <td valign="top"> Overwrite files, even if they are newer than the
  +    <td valign="top">Overwrite files, even if they are newer than the
         corresponding entries in the archive (true or false, default is
         true).</td>
       <td align="center" valign="top">No</td>
  
  
  
  1.18      +95 -40    jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Delete.java
  
  Index: Delete.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Delete.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- Delete.java	2001/03/14 13:02:52	1.17
  +++ Delete.java	2001/07/27 08:24:59	1.18
  @@ -79,32 +79,33 @@
       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,22 +113,34 @@
               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 &quot;rm&quot; command.
        * Default is false meaning things are &quot;noisy&quot;
        * @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 void addFileset(FileSet set) {
           filesets.addElement(set);
       }
  - 
  +
       /**
        * add a name entry on the include list
        */
  @@ -149,7 +162,7 @@
           usedMatchingTask = true;
           return super.createInclude();
       }
  -    
  +
       /**
        * add a name entry on the exclude list
        */
  @@ -191,7 +204,7 @@
       /**
        * 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 @@
        * 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 @@
        * 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 @@
   
           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 @@
                       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 @@
           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 @@
                   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 @@
                   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 @@
                   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<files.length; j++) {
                   File f = new File(d, files[j]);
                   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);
                   }
               }
           }
  @@ -345,8 +394,14 @@
                   String[] dirFiles = dir.list();
                   if (dirFiles == null || dirFiles.length == 0) {
                       log("Deleting " + dir.getAbsolutePath(), verbosity);
  -                    if (!dir.delete() && !quiet) {
  -                        throw new BuildException("Unable to delete directory " + dir.getAbsolutePath());
  +                    if (!dir.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);
                       } else {
                           dirCount++;
                       }
  @@ -354,8 +409,8 @@
               }
   
               if (dirCount > 0) {
  -                log("Deleted " + dirCount + " director" + 
  -                    (dirCount==1 ? "y" : "ies") + 
  +                log("Deleted " + dirCount + " director" +
  +                    (dirCount==1 ? "y" : "ies") +
                       " from " + d.getAbsolutePath());
               }
           }