SymbolicLinkFileFilter.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.filefilter;

  18. import java.io.File;
  19. import java.io.Serializable;
  20. import java.nio.file.FileVisitResult;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.attribute.BasicFileAttributes;

  24. /**
  25.  * This filter accepts {@link File}s that are symbolic links.
  26.  * <p>
  27.  * For example, here is how to print out a list of the real files
  28.  * within the current directory:
  29.  * </p>
  30.  * <h2>Using Classic IO</h2>
  31.  * <pre>
  32.  * File dir = FileUtils.current();
  33.  * String[] files = dir.list(SymbolicLinkFileFilter.INSTANCE);
  34.  * for (String file : files) {
  35.  *     System.out.println(file);
  36.  * }
  37.  * </pre>
  38.  *
  39.  * <h2>Using NIO</h2>
  40.  * <pre>
  41.  * final Path dir = PathUtils.current();
  42.  * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(SymbolicLinkFileFilter.INSTANCE);
  43.  * //
  44.  * // Walk one directory
  45.  * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
  46.  * System.out.println(visitor.getPathCounters());
  47.  * System.out.println(visitor.getFileList());
  48.  * //
  49.  * visitor.getPathCounters().reset();
  50.  * //
  51.  * // Walk directory tree
  52.  * Files.<strong>walkFileTree</strong>(dir, visitor);
  53.  * System.out.println(visitor.getPathCounters());
  54.  * System.out.println(visitor.getDirList());
  55.  * System.out.println(visitor.getFileList());
  56.  * </pre>
  57.  * <h2>Deprecating Serialization</h2>
  58.  * <p>
  59.  * <em>Serialization is deprecated and will be removed in 3.0.</em>
  60.  * </p>
  61.  *
  62.  * @since 2.11.0
  63.  * @see FileFilterUtils#fileFileFilter()
  64.  */
  65. public class SymbolicLinkFileFilter extends AbstractFileFilter implements Serializable {
  66.     /*
  67.      * Note to developers: The unit test needs to create symbolic links to files. However, on
  68.      * Windows, this can't be done without admin privileges. This class is designed to allow a
  69.      * unit test to works around this by doing two things: 1) This separates the class logic from
  70.      * the call to identify a symbolic link, and 2) It allows the unit test to override that
  71.      * symbolic link call on Windows only.
  72.      * This means we can write unit tests that will run on all machines. On Windows, the unit test
  73.      * can't create a symbolic link without admin privileges, so the unit tests won't
  74.      * completely test all the necessary behavior on Windows, but they will still test the class
  75.      * logic. Be careful not to break this, but be aware of it when writing unit tests. You can
  76.      * still maintain this class and its unit tests on Windows. The one method that won't get
  77.      * tested on Windows is not likely to change, and will be tested properly when it gets run
  78.      * on Apache servers.
  79.      */

  80.     /**
  81.      * Singleton instance of file filter.
  82.      */
  83.     public static final SymbolicLinkFileFilter INSTANCE = new SymbolicLinkFileFilter();

  84.     private static final long serialVersionUID = 1L;

  85.     /**
  86.      * Restrictive constructor.
  87.      */
  88.     protected SymbolicLinkFileFilter() {
  89.     }

  90.     /**
  91.      * Constructs a new instance.
  92.      *
  93.      * @param onAccept What to do on acceptance.
  94.      * @param onReject What to do on rejection.
  95.      * @since 2.12.0.
  96.      */
  97.     public SymbolicLinkFileFilter(final FileVisitResult onAccept, final FileVisitResult onReject) {
  98.         super(onAccept, onReject);
  99.     }

  100.     /**
  101.      * Checks to see if the file is a symbolic link.
  102.      *
  103.      * @param file  the File to check
  104.      * @return true if the file exists and is a symbolic link to either another file or a directory,
  105.      *         false otherwise.
  106.      */
  107.     @Override
  108.     public boolean accept(final File file) {
  109.         return isSymbolicLink(file.toPath());
  110.     }

  111.     /**
  112.      * Checks to see if the file is a symbolic link.
  113.      *
  114.      * @param path the File Path to check
  115.      * @param attributes the path's basic attributes (may be null).
  116.      * @return {@code onAccept} from {@link #SymbolicLinkFileFilter(FileVisitResult, FileVisitResult)} if the file exists and is a symbolic link to either
  117.      *         another file or a directory; returns {@code onReject} otherwise.
  118.      */
  119.     @Override
  120.     public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
  121.         return toFileVisitResult(isSymbolicLink(path));
  122.     }

  123.     /**
  124.      * Delegates to {@link Files#isSymbolicLink(Path)} for testing.
  125.      * <p>
  126.      * Using package access for unit tests. To facilitate unit testing, all calls to test if the file is a symbolic should go through this method. (See the unit
  127.      * test for why.)
  128.      * </p>
  129.      *
  130.      * @param filePath The filePath to test
  131.      * @return true if the file exists and is a symbolic link to either a file or directory, false otherwise.
  132.      */
  133.     boolean isSymbolicLink(final Path filePath) {
  134.         return Files.isSymbolicLink(filePath);
  135.     }
  136. }