001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.file;
019
020import java.io.IOException;
021import java.nio.file.FileVisitResult;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.nio.file.attribute.BasicFileAttributes;
025import java.util.Arrays;
026import java.util.Objects;
027
028import org.apache.commons.io.file.Counters.PathCounters;
029
030/**
031 * Deletes files but not directories as a visit proceeds.
032 *
033 * @since 2.7
034 */
035public class CleaningPathVisitor extends CountingPathVisitor {
036
037    /**
038     * Creates a new instance configured with a BigInteger {@link PathCounters}.
039     *
040     * @return a new instance configured with a BigInteger {@link PathCounters}.
041     */
042    public static CountingPathVisitor withBigIntegerCounters() {
043        return new CleaningPathVisitor(Counters.bigIntegerPathCounters());
044    }
045
046    /**
047     * Creates a new instance configured with a long {@link PathCounters}.
048     *
049     * @return a new instance configured with a long {@link PathCounters}.
050     */
051    public static CountingPathVisitor withLongCounters() {
052        return new CleaningPathVisitor(Counters.longPathCounters());
053    }
054
055    private final String[] skip;
056
057    /**
058     * Constructs a new visitor that deletes files except for the files and directories explicitly given.
059     *
060     * @param pathCounter How to count visits.
061     * @param skip The files to skip deleting.
062     */
063    public CleaningPathVisitor(final PathCounters pathCounter, final String... skip) {
064        super(pathCounter);
065        final String[] temp = skip != null ? skip.clone() : EMPTY_STRING_ARRAY;
066        Arrays.sort(temp);
067        this.skip = temp;
068    }
069
070    /**
071     * Returns true to process the given path, false if not.
072     *
073     * @param path the path to test.
074     * @return true to process the given path, false if not.
075     */
076    private boolean accept(final Path path) {
077        return Arrays.binarySearch(skip, Objects.toString(path.getFileName(), null)) < 0;
078    }
079
080    @Override
081    public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException {
082        super.preVisitDirectory(dir, attributes);
083        return accept(dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
084    }
085
086    @Override
087    public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
088        if (accept(file) && Files.exists(file)) {
089            Files.deleteIfExists(file);
090        }
091        updateFileCounters(file, attributes);
092        return FileVisitResult.CONTINUE;
093    }
094}