Search in sources :

Example 1 with Sort

use of org.apache.tools.ant.types.resources.Sort in project ant by apache.

the class Delete method execute.

/**
 * Delete the file(s).
 *
 * @exception BuildException if an error occurs
 */
@Override
public void execute() throws BuildException {
    if (usedMatchingTask) {
        log("DEPRECATED - Use of the implicit FileSet is deprecated.  Use a nested fileset element instead.", quiet ? Project.MSG_VERBOSE : verbosity);
    }
    if (file == null && dir == null && filesets.isEmpty() && rcs == null) {
        throw new BuildException("At least one of the file or dir attributes, or a nested resource collection, must be set.");
    }
    if (quiet && failonerror) {
        throw new BuildException("quiet and failonerror cannot both be set to true", getLocation());
    }
    // delete the single file
    if (file != null) {
        if (file.exists()) {
            if (file.isDirectory()) {
                log("Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute.  Use dir instead.", quiet ? Project.MSG_VERBOSE : verbosity);
            } else {
                log("Deleting: " + file.getAbsolutePath());
                if (!delete(file)) {
                    handle("Unable to delete file " + file.getAbsolutePath());
                }
            }
        } else if (isDanglingSymlink(file)) {
            log("Trying to delete file " + file.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(file)) {
                handle("Unable to delete file " + file.getAbsolutePath());
            }
        } else {
            log("Could not find file " + file.getAbsolutePath() + " to delete.", quiet ? Project.MSG_VERBOSE : verbosity);
        }
    }
    // delete the directory
    if (dir != null && !usedMatchingTask) {
        if (dir.exists() && dir.isDirectory()) {
            /*
                  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, the removeDir
                  method will handle messages for _all_ directories.
                */
            if (verbosity == Project.MSG_VERBOSE) {
                log("Deleting directory " + dir.getAbsolutePath());
            }
            removeDir(dir);
        } else if (isDanglingSymlink(dir)) {
            log("Trying to delete directory " + dir.getAbsolutePath() + " which looks like a broken symlink.", quiet ? Project.MSG_VERBOSE : verbosity);
            if (!delete(dir)) {
                handle("Unable to delete directory " + dir.getAbsolutePath());
            }
        }
    }
    Resources resourcesToDelete = new Resources();
    resourcesToDelete.setProject(getProject());
    resourcesToDelete.setCache(true);
    Resources filesetDirs = new Resources();
    filesetDirs.setProject(getProject());
    filesetDirs.setCache(true);
    FileSet implicit = null;
    if (usedMatchingTask && dir != null && dir.isDirectory()) {
        // add the files from the default fileset:
        implicit = getImplicitFileSet();
        implicit.setProject(getProject());
        filesets.add(implicit);
    }
    final int size = filesets.size();
    for (FileSet fs : filesets) {
        if (fs.getProject() == null) {
            log("Deleting fileset with no project specified; assuming executing project", Project.MSG_VERBOSE);
            fs = (FileSet) fs.clone();
            fs.setProject(getProject());
        }
        final File fsDir = fs.getDir();
        if (!fs.getErrorOnMissingDir() && (fsDir == null || !fsDir.exists())) {
            continue;
        }
        if (fsDir == null) {
            throw new BuildException("File or Resource without directory or file specified");
        } else if (!fsDir.isDirectory()) {
            handle("Directory does not exist: " + fsDir);
        } else {
            DirectoryScanner ds = fs.getDirectoryScanner();
            // the previous line has already scanned the
            // filesystem, in order to avoid a rescan when later
            // iterating, capture the results now and store them
            final String[] files = ds.getIncludedFiles();
            resourcesToDelete.add(new ResourceCollection() {

                @Override
                public boolean isFilesystemOnly() {
                    return true;
                }

                @Override
                public int size() {
                    return files.length;
                }

                @Override
                public Iterator<Resource> iterator() {
                    return new FileResourceIterator(getProject(), fsDir, files);
                }
            });
            if (includeEmpty) {
                filesetDirs.add(new ReverseDirs(getProject(), fsDir, ds.getIncludedDirectories()));
            }
            if (removeNotFollowedSymlinks) {
                String[] n = ds.getNotFollowedSymlinks();
                if (n.length > 0) {
                    String[] links = new String[n.length];
                    System.arraycopy(n, 0, links, 0, n.length);
                    Arrays.sort(links, Comparator.reverseOrder());
                    for (String link : links) {
                        final Path filePath = Paths.get(link);
                        if (!Files.isSymbolicLink(filePath)) {
                            // it's not a symbolic link, so move on
                            continue;
                        }
                        // it's a symbolic link, so delete it
                        final boolean deleted = filePath.toFile().delete();
                        if (!deleted) {
                            handle("Could not delete symbolic link at " + filePath);
                        }
                    }
                }
            }
        }
    }
    resourcesToDelete.add(filesetDirs);
    if (rcs != null) {
        // sort first to files, then dirs
        Restrict exists = new Restrict();
        exists.add(EXISTS);
        exists.add(rcs);
        Sort s = new Sort();
        s.add(REVERSE_FILESYSTEM);
        s.add(exists);
        resourcesToDelete.add(s);
    }
    try {
        if (resourcesToDelete.isFilesystemOnly()) {
            for (Resource r : resourcesToDelete) {
                // nonexistent resources could only occur if we already
                // deleted something from a fileset:
                File f = r.as(FileProvider.class).getFile();
                if (!f.exists()) {
                    continue;
                }
                if (!f.isDirectory() || f.list().length == 0) {
                    log("Deleting " + f, verbosity);
                    if (!delete(f) && failonerror) {
                        handle("Unable to delete " + (f.isDirectory() ? "directory " : "file ") + f);
                    }
                }
            }
        } else {
            handle(getTaskName() + " handles only filesystem resources");
        }
    } catch (Exception e) {
        handle(e);
    } finally {
        if (implicit != null) {
            filesets.remove(implicit);
        }
    }
}
Also used : Path(java.nio.file.Path) FileSet(org.apache.tools.ant.types.FileSet) Resource(org.apache.tools.ant.types.Resource) FileResourceIterator(org.apache.tools.ant.types.resources.FileResourceIterator) BuildException(org.apache.tools.ant.BuildException) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) Restrict(org.apache.tools.ant.types.resources.Restrict) FileProvider(org.apache.tools.ant.types.resources.FileProvider) Sort(org.apache.tools.ant.types.resources.Sort) BuildException(org.apache.tools.ant.BuildException) Resources(org.apache.tools.ant.types.resources.Resources) File(java.io.File) ResourceCollection(org.apache.tools.ant.types.ResourceCollection)

Example 2 with Sort

use of org.apache.tools.ant.types.resources.Sort in project ant by apache.

the class ScpTest method testMultiResourceCollectionUpload.

@Test
public void testMultiResourceCollectionUpload() throws IOException {
    assertNotNull("system property scp.tmp must be set", tempDir);
    List<File> uploadList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        uploadList.add(createTemporaryFile());
    }
    // reverse order resource collection
    Sort sort = new Sort();
    sort.setProject(scpTask.getProject());
    Reverse reverse = new Reverse();
    reverse.add(new Name());
    sort.add(reverse);
    FilenameSelector selector = new FilenameSelector();
    selector.setName("scp*");
    FileSet fileset = new FileSet();
    fileset.setProject(scpTask.getProject());
    fileset.setDir(tempDir);
    fileset.addFilename(selector);
    sort.add(fileset);
    scpTask.add(sort);
    scpTask.setTodir(sshHostUri);
    scpTask.execute();
}
Also used : FilenameSelector(org.apache.tools.ant.types.selectors.FilenameSelector) Reverse(org.apache.tools.ant.types.resources.comparators.Reverse) FileSet(org.apache.tools.ant.types.FileSet) ArrayList(java.util.ArrayList) Sort(org.apache.tools.ant.types.resources.Sort) File(java.io.File) Name(org.apache.tools.ant.types.resources.comparators.Name) Test(org.junit.Test)

Aggregations

File (java.io.File)2 FileSet (org.apache.tools.ant.types.FileSet)2 Sort (org.apache.tools.ant.types.resources.Sort)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 BuildException (org.apache.tools.ant.BuildException)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 Resource (org.apache.tools.ant.types.Resource)1 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)1 FileProvider (org.apache.tools.ant.types.resources.FileProvider)1 FileResourceIterator (org.apache.tools.ant.types.resources.FileResourceIterator)1 Resources (org.apache.tools.ant.types.resources.Resources)1 Restrict (org.apache.tools.ant.types.resources.Restrict)1 Name (org.apache.tools.ant.types.resources.comparators.Name)1 Reverse (org.apache.tools.ant.types.resources.comparators.Reverse)1 FilenameSelector (org.apache.tools.ant.types.selectors.FilenameSelector)1 Test (org.junit.Test)1