CleaningPathVisitor.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.file;

  18. import java.io.IOException;
  19. import java.nio.file.FileVisitResult;
  20. import java.nio.file.Files;
  21. import java.nio.file.LinkOption;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;
  24. import java.util.Arrays;
  25. import java.util.Objects;

  26. import org.apache.commons.io.file.Counters.PathCounters;

  27. /**
  28.  * Deletes files but not directories as a visit proceeds.
  29.  *
  30.  * @since 2.7
  31.  */
  32. public class CleaningPathVisitor extends CountingPathVisitor {

  33.     /**
  34.      * Constructs a new instance configured with a BigInteger {@link PathCounters}.
  35.      *
  36.      * @return a new instance configured with a BigInteger {@link PathCounters}.
  37.      */
  38.     public static CountingPathVisitor withBigIntegerCounters() {
  39.         return new CleaningPathVisitor(Counters.bigIntegerPathCounters());
  40.     }

  41.     /**
  42.      * Constructs a new instance configured with a long {@link PathCounters}.
  43.      *
  44.      * @return a new instance configured with a long {@link PathCounters}.
  45.      */
  46.     public static CountingPathVisitor withLongCounters() {
  47.         return new CleaningPathVisitor(Counters.longPathCounters());
  48.     }

  49.     private final String[] skip;
  50.     private final boolean overrideReadOnly;

  51.     /**
  52.      * Constructs a instance that deletes files except for the files and directories explicitly given.
  53.      *
  54.      * @param pathCounter How to count visits.
  55.      * @param deleteOption How deletion is handled.
  56.      * @param skip The files to skip deleting.
  57.      * @since 2.8.0
  58.      */
  59.     public CleaningPathVisitor(final PathCounters pathCounter, final DeleteOption[] deleteOption, final String... skip) {
  60.         super(pathCounter);
  61.         final String[] temp = skip != null ? skip.clone() : EMPTY_STRING_ARRAY;
  62.         Arrays.sort(temp);
  63.         this.skip = temp;
  64.         this.overrideReadOnly = StandardDeleteOption.overrideReadOnly(deleteOption);
  65.     }

  66.     /**
  67.      * Constructs a new instance that deletes files except for the files and directories explicitly given.
  68.      *
  69.      * @param pathCounter How to count visits.
  70.      * @param skip The files to skip deleting.
  71.      */
  72.     public CleaningPathVisitor(final PathCounters pathCounter, final String... skip) {
  73.         this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip);
  74.     }

  75.     /**
  76.      * Returns true to process the given path, false if not.
  77.      *
  78.      * @param path the path to test.
  79.      * @return true to process the given path, false if not.
  80.      */
  81.     private boolean accept(final Path path) {
  82.         return Arrays.binarySearch(skip, PathUtils.getFileNameString(path)) < 0;
  83.     }

  84.     @Override
  85.     public boolean equals(final Object obj) {
  86.         if (this == obj) {
  87.             return true;
  88.         }
  89.         if (!super.equals(obj)) {
  90.             return false;
  91.         }
  92.         if (getClass() != obj.getClass()) {
  93.             return false;
  94.         }
  95.         final CleaningPathVisitor other = (CleaningPathVisitor) obj;
  96.         return overrideReadOnly == other.overrideReadOnly && Arrays.equals(skip, other.skip);
  97.     }

  98.     @Override
  99.     public int hashCode() {
  100.         final int prime = 31;
  101.         int result = super.hashCode();
  102.         result = prime * result + Arrays.hashCode(skip);
  103.         result = prime * result + Objects.hash(overrideReadOnly);
  104.         return result;
  105.     }

  106.     @Override
  107.     public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException {
  108.         super.preVisitDirectory(dir, attributes);
  109.         return accept(dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
  110.     }

  111.     @Override
  112.     public FileVisitResult visitFile(final Path file, final BasicFileAttributes attributes) throws IOException {
  113.         // Files.deleteIfExists() never follows links, so use LinkOption.NOFOLLOW_LINKS in other calls to Files.
  114.         if (accept(file) && Files.exists(file, LinkOption.NOFOLLOW_LINKS)) {
  115.             if (overrideReadOnly) {
  116.                 PathUtils.setReadOnly(file, false, LinkOption.NOFOLLOW_LINKS);
  117.             }
  118.             Files.deleteIfExists(file);
  119.         }
  120.         updateFileCounters(file, attributes);
  121.         return FileVisitResult.CONTINUE;
  122.     }
  123. }