ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject cvs commit: jakarta-ant/src/testcases/org/apache/tools/ant DirectoryScannerTest.java
Date Fri, 10 May 2002 12:53:58 GMT
bodewig     02/05/10 05:53:58

  Modified:    .        Tag: ANT_15_BRANCH WHATSNEW
               docs/manual/CoreTypes Tag: ANT_15_BRANCH dirset.html
                        fileset.html
               src/main/org/apache/tools/ant Tag: ANT_15_BRANCH
                        DirectoryScanner.java
               src/main/org/apache/tools/ant/types Tag: ANT_15_BRANCH
                        AbstractFileSet.java
               src/main/org/apache/tools/ant/util Tag: ANT_15_BRANCH
                        FileUtils.java
               src/testcases/org/apache/tools/ant Tag: ANT_15_BRANCH
                        DirectoryScannerTest.java
  Log:
  Add a new followsymlinks attribute to fileset and friends.
  
  Based on a submission by:          Patrick (Gus) Heck <patrick.heck@olin.edu>
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.263.2.10 +3 -0      jakarta-ant/WHATSNEW
  
  Index: WHATSNEW
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/WHATSNEW,v
  retrieving revision 1.263.2.9
  retrieving revision 1.263.2.10
  diff -u -r1.263.2.9 -r1.263.2.10
  --- WHATSNEW	10 May 2002 11:33:54 -0000	1.263.2.9
  +++ WHATSNEW	10 May 2002 12:53:57 -0000	1.263.2.10
  @@ -36,6 +36,9 @@
   
   * z/OS now gets detected by the os condition.
   
  +* <fileset> and <dirset> now have an optional followsymlink attribute
  +that can prevent Ant from following symbolic links on some platforms.
  +
   Changes from Ant 1.4.1 to 1.5beta1
   ==================================
   
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.1   +6 -0      jakarta-ant/docs/manual/CoreTypes/dirset.html
  
  Index: dirset.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTypes/dirset.html,v
  retrieving revision 1.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- dirset.html	23 Apr 2002 13:49:47 -0000	1.4
  +++ dirset.html	10 May 2002 12:53:57 -0000	1.4.2.1
  @@ -63,6 +63,12 @@
        <code>false</code>|<code>no</code>|<code>off</code>).</td>
       <td valign="top" align="center">No; defaults to true.</td>
     </tr>
  +  <tr>
  +    <td valign="top">followsymlinks</td>
  +    <td valign="top">Shall symbolic links be followed? Defaults to
  +      true. See <a href="fileset.html#symlink">fileset's documentation</a>.</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>
   </table>
   
   <h4>Examples</h4>
  
  
  
  1.12.2.2  +12 -0     jakarta-ant/docs/manual/CoreTypes/fileset.html
  
  Index: fileset.html
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/docs/manual/CoreTypes/fileset.html,v
  retrieving revision 1.12.2.1
  retrieving revision 1.12.2.2
  diff -u -r1.12.2.1 -r1.12.2.2
  --- fileset.html	3 May 2002 09:40:30 -0000	1.12.2.1
  +++ fileset.html	10 May 2002 12:53:57 -0000	1.12.2.2
  @@ -68,7 +68,19 @@
           Defaults to true.</td>
       <td valign="top" align="center">No</td>
     </tr>
  +  <tr>
  +    <td valign="top">followsymlinks</td>
  +    <td valign="top">Shall symbolic links be followed? Defaults to
  +      true. See <a href="symlink">below</a>.</td>
  +    <td valign="top" align="center">No</td>
  +  </tr>
   </table>
  +
  +<p><a name="symlink"><b>Note</b></a>: All files/directories
for which
  +the canonical path is different from its path are considered symbolic
  +links.  On Unix systems this usually means the file really is a
  +symbolic links but it may lead to false results on other
  +platforms.</p>
   
   <h4>Examples</h4>
   <blockquote><pre>
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.25.2.1  +48 -0     jakarta-ant/src/main/org/apache/tools/ant/DirectoryScanner.java
  
  Index: DirectoryScanner.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/DirectoryScanner.java,v
  retrieving revision 1.25
  retrieving revision 1.25.2.1
  diff -u -r1.25 -r1.25.2.1
  --- DirectoryScanner.java	30 Apr 2002 22:38:35 -0000	1.25
  +++ DirectoryScanner.java	10 May 2002 12:53:57 -0000	1.25.2.1
  @@ -55,12 +55,14 @@
   package org.apache.tools.ant;
   
   import java.io.File;
  +import java.io.IOException;
   import java.util.Vector;
   import java.util.StringTokenizer;
   
   import org.apache.tools.ant.types.selectors.SelectorScanner;
   import org.apache.tools.ant.types.selectors.FileSelector;
   import org.apache.tools.ant.types.selectors.SelectorUtils;
  +import org.apache.tools.ant.util.FileUtils;
   
   /**
    * Class for scanning a directory for files/directories which match certain
  @@ -229,6 +231,16 @@
        */
       protected boolean isCaseSensitive = true;
   
  +    /**
  +     * Whether or not symbolic links should be followed.
  +     *
  +     * @since Ant 1.5
  +     */
  +    private boolean followSymlinks = true;
  +
  +    /** Helper. */
  +    private static final FileUtils fileUtils = FileUtils.newFileUtils();
  +
       /** Whether or not everything tested so far has been included. */
       protected boolean everythingIncluded = true;
   
  @@ -411,6 +423,15 @@
       }
   
       /**
  +     * Sets whether or not symbolic links should be followed.
  +     *
  +     * @param followSymlinks whether or not symbolic links should be followed
  +     */
  +    public void setFollowSymlinks(boolean followSymlinks) {
  +        this.followSymlinks = followSymlinks;
  +    }
  +
  +    /**
        * Sets the list of include patterns to use. All '/' and '\' characters
        * are replaced by <code>File.separatorChar</code>, so the separator used
        * need not match <code>File.separatorChar</code>.
  @@ -617,6 +638,33 @@
                */
               throw new BuildException("IO error scanning directory "
                                        + dir.getAbsolutePath());
  +        }
  +
  +        if (!followSymlinks) {
  +            Vector noLinks = new Vector();
  +            for (int i = 0; i < newfiles.length; i++) {
  +                try {
  +                    if (fileUtils.isSymbolicLink(dir, newfiles[i])) {
  +                        String name = vpath + newfiles[i];
  +                        File   file = new File(dir, newfiles[i]);
  +                        if (file.isDirectory()) {
  +                            dirsExcluded.addElement(name);
  +                        } else {
  +                            filesExcluded.addElement(name);
  +                        }
  +                    } else {
  +                        noLinks.add(newfiles[i]);
  +                    }
  +                } catch (IOException ioe) {
  +                    String msg = "IOException caught while checking "
  +                        + "for links, couldn't get cannonical path!";
  +                    // will be caught and redirected to Ant's logging system
  +                    System.err.println(msg);
  +                    noLinks.add(newfiles[i]);
  +                }
  +            }
  +            newfiles = new String[noLinks.size()]; 
  +            noLinks.copyInto(newfiles);
           }
   
           for (int i = 0; i < newfiles.length; i++) {
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.5.2.1   +11 -2     jakarta-ant/src/main/org/apache/tools/ant/types/AbstractFileSet.java
  
  Index: AbstractFileSet.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/types/AbstractFileSet.java,v
  retrieving revision 1.5
  retrieving revision 1.5.2.1
  diff -u -r1.5 -r1.5.2.1
  --- AbstractFileSet.java	30 Apr 2002 22:38:35 -0000	1.5
  +++ AbstractFileSet.java	10 May 2002 12:53:57 -0000	1.5.2.1
  @@ -89,7 +89,7 @@
       private File dir;
       private boolean useDefaultExcludes = true;
       private boolean isCaseSensitive = true;
  -
  +    private boolean followSymlinks = true;
   
       public AbstractFileSet() {
           super();
  @@ -101,6 +101,7 @@
           this.additionalPatterns = fileset.additionalPatterns;
           this.useDefaultExcludes = fileset.useDefaultExcludes;
           this.isCaseSensitive = fileset.isCaseSensitive;
  +        this.followSymlinks = fileset.followSymlinks;
           setProject(fileset.getProject());
       }
   
  @@ -276,7 +277,14 @@
           this.isCaseSensitive = isCaseSensitive;
       }
   
  -
  +    /**
  +     * Sets whether or not symbolic links should be followed.
  +     *
  +     * @param followSymlinks whether or not symbolic links should be followed
  +     */
  +    public void setFollowSymlinks(boolean followSymlinks) {
  +        this.followSymlinks = followSymlinks;
  +    }
   
       /**
        * sets the name used for this datatype instance.
  @@ -327,6 +335,7 @@
   
           DirectoryScanner ds = new DirectoryScanner();
           setupDirectoryScanner(ds, p);
  +        ds.setFollowSymlinks(followSymlinks);
           ds.scan();
           return ds;
       }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.25.2.1  +20 -1     jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java
  
  Index: FileUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/util/FileUtils.java,v
  retrieving revision 1.25
  retrieving revision 1.25.2.1
  diff -u -r1.25 -r1.25.2.1
  --- FileUtils.java	23 Apr 2002 08:15:52 -0000	1.25
  +++ FileUtils.java	10 May 2002 12:53:58 -0000	1.25.2.1
  @@ -95,7 +95,7 @@
    * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a>
    * @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a> 
    *
  - * @version $Revision: 1.25 $
  + * @version $Revision: 1.25.2.1 $
    */
   
   public class FileUtils {
  @@ -829,6 +829,25 @@
               return true;
           }
           return false;
  +    }
  +
  +    /**
  +     * Checks whether a given file is a symbolic link.
  +     *
  +     * <p>It doesn't really test for symbolic links but whether the
  +     * canonical and absolute paths of the file are identical - this
  +     * may lead to false positives on some platforms.</p>
  +     *
  +     * @param parent the parent directory of the file to test
  +     * @param name the name of the file to test.
  +     *
  +     * @since Ant 1.5
  +     */
  +    public boolean isSymbolicLink(File parent, String name)
  +        throws IOException {
  +        File resolvedParent = new File(parent.getCanonicalPath());
  +        File toTest = new File(resolvedParent, name);
  +        return !toTest.getAbsolutePath().equals(toTest.getCanonicalPath());
       }
   
   }
  
  
  
  No                   revision
  
  
  No                   revision
  
  
  1.4.2.1   +93 -1     jakarta-ant/src/testcases/org/apache/tools/ant/DirectoryScannerTest.java
  
  Index: DirectoryScannerTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/testcases/org/apache/tools/ant/DirectoryScannerTest.java,v
  retrieving revision 1.4
  retrieving revision 1.4.2.1
  diff -u -r1.4 -r1.4.2.1
  --- DirectoryScannerTest.java	10 Jan 2002 10:13:11 -0000	1.4
  +++ DirectoryScannerTest.java	10 May 2002 12:53:58 -0000	1.4.2.1
  @@ -1,7 +1,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -54,9 +54,12 @@
   
   package org.apache.tools.ant;
   
  +import org.apache.tools.ant.taskdefs.condition.Os;
  +
   import junit.framework.TestCase;
   import junit.framework.AssertionFailedError;
   import java.io.File;
  +import java.io.IOException;
   
   /**
    * JUnit 3 testcases for org.apache.tools.ant.DirectoryScanner
  @@ -66,6 +69,95 @@
   public class DirectoryScannerTest extends TestCase {
   
       public DirectoryScannerTest(String name) {super(name);}
  +
  +
  +    // keep track of what operating systems are supported here.
  +    private boolean supportsSymlinks = Os.isFamily("unix");
  +
  +    /**
  +     * Test case for setFollowLinks() and associated funtionality.
  +     * Only supports test on linux, at the moment because Java has
  +     * no real notion of symlinks built in, so an os-specfic call
  +     * to Runtime.exec() must be made to create a link to test against.
  +     */
  +
  +    public void testSetFollowLinks() {
  +        if (supportsSymlinks) {
  +            try {
  +                // add conditions and more commands as soon as the need arises
  +                String[] command = new String[] {
  +                    "ln", "-s", "ant", "src/main/org/apache/tools/ThisIsALink"
  +                };
  +                try {
  +                    Runtime.getRuntime().exec(command);
  +                    // give ourselves some time for the system call
  +                    // to execute... tweak if you have a really over
  +                    // loaded system.
  +                    Thread.sleep(1000);    
  +                } catch (IOException ioe) {
  +                    fail("IOException making link "+ioe);
  +                } catch (InterruptedException ie) {
  +                }
  +                
  +                File dir = new File("src/main/org/apache/tools");
  +                DirectoryScanner ds = new DirectoryScanner();
  +
  +                // followLinks should be true by default, but if this ever
  +                // changes we will need this line.
  +                ds.setFollowSymlinks(true);
  +                
  +                ds.setBasedir(dir);
  +                ds.setExcludes(new String[] {"ant/**"});
  +                ds.scan();
  +            
  +                boolean haveZipPackage = false;
  +                boolean haveTaskdefsPackage = false;
  +
  +                String[] included = ds.getIncludedDirectories();
  +                for (int i=0; i<included.length; i++) {
  +                    if (included[i].equals("zip")) {
  +                        haveZipPackage = true;
  +                    } else if (included[i].equals("ThisIsALink"+File.separator+
  +                                                  "taskdefs")) {
  +                        haveTaskdefsPackage = true;
  +                    }
  +                }
  +
  +                // if we followed the symlink we just made we should
  +                // bypass the excludes.
  +                
  +                assertTrue("(1) zip package included", haveZipPackage);
  +                assertTrue("(1) taskdefs package included", 
  +                           haveTaskdefsPackage);
  +
  +            
  +                ds = new DirectoryScanner();
  +                ds.setFollowSymlinks(false);
  +                
  +                ds.setBasedir(dir);
  +                ds.setExcludes(new String[] {"ant/**"});
  +                ds.scan();
  +            
  +                haveZipPackage = false;
  +                haveTaskdefsPackage = false;
  +                included = ds.getIncludedDirectories();
  +                for (int i=0; i<included.length; i++) {
  +                    if (included[i].equals("zip")) {
  +                        haveZipPackage = true;
  +                    } else if (included[i].equals("ThisIsALink"+File.separator+
  +                                                  "taskdefs")) {
  +                        haveTaskdefsPackage = true;
  +                    }
  +                }
  +                assertTrue("(2) zip package included", haveZipPackage);
  +                assertTrue("(2) taskdefs package not included", 
  +                           !haveTaskdefsPackage);
  +                
  +            } finally {
  +                (new File("src/main/org/apache/tools/ThisIsALink")).delete();
  +            } 
  +        }
  +    }
   
       /**
        * Test inspired by Bug#1415.
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message