    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 */
    017package org.apache.commons.io.filefilter;
    018
    019import java.io.File;
    020import java.io.Serializable;
    021import java.nio.file.FileVisitResult;
    022import java.nio.file.Files;
    023import java.nio.file.Path;
    024import java.nio.file.attribute.BasicFileAttributes;
    025
    026/**
    027 * This filter accepts {@link File}s that are symbolic links.
    028 * <p>
    029 * For example, here is how to print out a list of the real files
    030 * within the current directory:
    031 * </p>
    032 * <h2>Using Classic IO</h2>
    033 * <pre>
    034 * File dir = FileUtils.current();
    035 * String[] files = dir.list(SymbolicLinkFileFilter.INSTANCE);
    036 * for (String file : files) {
    037 *     System.out.println(file);
    038 * }
    039 * </pre>
    040 *
    041 * <h2>Using NIO</h2>
    042 * <pre>
    043 * final Path dir = PathUtils.current();
    044 * final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(SymbolicLinkFileFilter.INSTANCE);
    045 * //
    046 * // Walk one directory
    047 * Files.<strong>walkFileTree</strong>(dir, Collections.emptySet(), 1, visitor);
    048 * System.out.println(visitor.getPathCounters());
    049 * System.out.println(visitor.getFileList());
    050 * //
    051 * visitor.getPathCounters().reset();
    052 * //
    053 * // Walk directory tree
    054 * Files.<strong>walkFileTree</strong>(dir, visitor);
    055 * System.out.println(visitor.getPathCounters());
    056 * System.out.println(visitor.getDirList());
    057 * System.out.println(visitor.getFileList());
    058 * </pre>
    059 * <h2>Deprecating Serialization</h2>
    060 * <p>
    061 * <em>Serialization is deprecated and will be removed in 3.0.</em>
    062 * </p>
    063 *
    064 * @since 2.11.0
    065 * @see FileFilterUtils#fileFileFilter()
    066 */
    067public class SymbolicLinkFileFilter extends AbstractFileFilter implements Serializable {
    068    /*
    069     * Note to developers: The unit test needs to create symbolic links to files. However, on
    070     * Windows, this can't be done without admin privileges. This class is designed to allow a
    071     * unit test to works around this by doing two things: 1) This separates the class logic from
    072     * the call to identify a symbolic link, and 2) It allows the unit test to override that
    073     * symbolic link call on Windows only.
    074     * This means we can write unit tests that will run on all machines. On Windows, the unit test
    075     * can't create a symbolic link without admin privileges, so the unit tests won't
    076     * completely test all the necessary behavior on Windows, but they will still test the class
    077     * logic. Be careful not to break this, but be aware of it when writing unit tests. You can
    078     * still maintain this class and its unit tests on Windows. The one method that won't get
    079     * tested on Windows is not likely to change, and will be tested properly when it gets run
    080     * on Apache servers.
    081     */
    082
    083    /**
    084     * Singleton instance of file filter.
    085     */
    086    public static final SymbolicLinkFileFilter INSTANCE = new SymbolicLinkFileFilter();
    087
    088    private static final long serialVersionUID = 1L;
    089
    090    /**
    091     * Restrictive constructor.
    092     */
    093    protected SymbolicLinkFileFilter() {
    094    }
    095
    096    /**
    097     * Constructs a new instance.
    098     *
    099     * @param onAccept What to do on acceptance.
    100     * @param onReject What to do on rejection.
    101     * @since 2.12.0.
    102     */
    103    public SymbolicLinkFileFilter(final FileVisitResult onAccept, final FileVisitResult onReject) {
    104        super(onAccept, onReject);
    105    }
    106
    107    /**
    108     * Checks to see if the file is a symbolic link.
    109     *
    110     * @param file  the File to check
    111     * @return true if the file exists and is a symbolic link to either another file or a directory,
    112     *         false otherwise.
    113     */
    114    @Override
    115    public boolean accept(final File file) {
    116        return isSymbolicLink(file.toPath());
    117    }
    118
    119    /**
    120     * Checks to see if the file is a symbolic link.
    121     *
    122     * @param path the File Path to check
    123     * @param attributes the path's basic attributes (may be null).
    124     * @return {@code onAccept} from {@link #SymbolicLinkFileFilter(FileVisitResult, FileVisitResult)} if the file exists and is a symbolic link to either
    125     *         another file or a directory; returns {@code onReject} otherwise.
    126     */
    127    @Override
    128    public FileVisitResult accept(final Path path, final BasicFileAttributes attributes) {
    129        return toFileVisitResult(isSymbolicLink(path));
    130    }
    131
    132    /**
    133     * Delegates to {@link Files#isSymbolicLink(Path)} for testing.
    134     * <p>
    135     * 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
    136     * test for why.)
    137     * </p>
    138     *
    139     * @param filePath The filePath to test
    140     * @return true if the file exists and is a symbolic link to either a file or directory, false otherwise.
    141     */
    142    boolean isSymbolicLink(final Path filePath) {
    143        return Files.isSymbolicLink(filePath);
    144    }
    145}