Search in sources :

Example 1 with NoneSelector

use of org.apache.tools.ant.types.selectors.NoneSelector in project ant by apache.

the class Sync method removeOrphanFiles.

/**
 * Removes all files and folders not found as keys of a table
 * (used as a set!).
 *
 * <p>If the provided file is a directory, it is recursively
 * scanned for orphaned files which will be removed as well.</p>
 *
 * <p>If the directory is an orphan, it will also be removed.</p>
 *
 * @param  nonOrphans the table of all non-orphan <code>File</code>s.
 * @param  toDir the initial file or directory to scan or test.
 * @param  preservedDirectories will be filled with the directories
 *         matched by preserveInTarget - if any.  Will not be
 *         filled unless preserveEmptyDirs and includeEmptyDirs
 *         conflict.
 * @return the number of orphaned files and directories actually removed.
 * Position 0 of the array is the number of orphaned directories.
 * Position 1 of the array is the number or orphaned files.
 */
private int[] removeOrphanFiles(Set<String> nonOrphans, File toDir, Set<File> preservedDirectories) {
    int[] removedCount = new int[] { 0, 0 };
    String[] excls = nonOrphans.toArray(new String[nonOrphans.size() + 1]);
    // want to keep toDir itself
    excls[nonOrphans.size()] = "";
    DirectoryScanner ds;
    if (syncTarget != null) {
        FileSet fs = syncTarget.toFileSet(false);
        fs.setDir(toDir);
        // preserveInTarget would find all files we want to keep,
        // but we need to find all that we want to delete - so the
        // meaning of all patterns and selectors must be inverted
        PatternSet ps = syncTarget.mergePatterns(getProject());
        fs.appendExcludes(ps.getIncludePatterns(getProject()));
        fs.appendIncludes(ps.getExcludePatterns(getProject()));
        fs.setDefaultexcludes(!syncTarget.getDefaultexcludes());
        // selectors are implicitly ANDed in DirectoryScanner.  To
        // revert their logic we wrap them into a <none> selector
        // instead.
        FileSelector[] s = syncTarget.getSelectors(getProject());
        if (s.length > 0) {
            NoneSelector ns = new NoneSelector();
            for (FileSelector element : s) {
                ns.appendSelector(element);
            }
            fs.appendSelector(ns);
        }
        ds = fs.getDirectoryScanner(getProject());
    } else {
        ds = new DirectoryScanner();
        ds.setBasedir(toDir);
    }
    ds.addExcludes(excls);
    ds.scan();
    for (String file : ds.getIncludedFiles()) {
        File f = new File(toDir, file);
        log("Removing orphan file: " + f, Project.MSG_DEBUG);
        f.delete();
        ++removedCount[1];
    }
    String[] dirs = ds.getIncludedDirectories();
    // ds returns the directories in lexicographic order.
    // iterating through the array backwards means we are deleting
    // leaves before their parent nodes - thus making sure (well,
    // more likely) that the directories are empty when we try to
    // delete them.
    Arrays.sort(dirs, Comparator.reverseOrder());
    for (String dir : dirs) {
        File f = new File(toDir, dir);
        String[] children = f.list();
        if (children == null || children.length < 1) {
            log("Removing orphan directory: " + f, Project.MSG_DEBUG);
            f.delete();
            ++removedCount[0];
        }
    }
    Boolean ped = getExplicitPreserveEmptyDirs();
    if (ped != null && ped != myCopy.getIncludeEmptyDirs()) {
        FileSet fs = syncTarget.toFileSet(true);
        fs.setDir(toDir);
        String[] preservedDirs = fs.getDirectoryScanner(getProject()).getIncludedDirectories();
        Arrays.sort(preservedDirs, Comparator.reverseOrder());
        for (String dir : preservedDirs) {
            preservedDirectories.add(new File(toDir, dir));
        }
    }
    return removedCount;
}
Also used : AbstractFileSet(org.apache.tools.ant.types.AbstractFileSet) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) FileSelector(org.apache.tools.ant.types.selectors.FileSelector) PatternSet(org.apache.tools.ant.types.PatternSet) File(java.io.File) NoneSelector(org.apache.tools.ant.types.selectors.NoneSelector)

Aggregations

File (java.io.File)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 AbstractFileSet (org.apache.tools.ant.types.AbstractFileSet)1 FileSet (org.apache.tools.ant.types.FileSet)1 PatternSet (org.apache.tools.ant.types.PatternSet)1 FileSelector (org.apache.tools.ant.types.selectors.FileSelector)1 NoneSelector (org.apache.tools.ant.types.selectors.NoneSelector)1