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.logging.log4j.core.appender.rolling.action;
019
020import org.apache.logging.log4j.Logger;
021import org.apache.logging.log4j.status.StatusLogger;
022
023import java.io.IOException;
024import java.nio.file.FileVisitResult;
025import java.nio.file.NoSuchFileException;
026import java.nio.file.Path;
027import java.nio.file.SimpleFileVisitor;
028import java.nio.file.attribute.BasicFileAttributes;
029import java.util.ArrayList;
030import java.util.Collections;
031import java.util.List;
032import java.util.Objects;
033
034/**
035 * FileVisitor that sorts files.
036 */
037public class SortingVisitor extends SimpleFileVisitor<Path> {
038
039    private static final Logger LOGGER = StatusLogger.getLogger();
040    private final PathSorter sorter;
041    private final List<PathWithAttributes> collected = new ArrayList<>();
042
043    /**
044     * Constructs a new DeletingVisitor.
045     *
046     * @param basePath used to relativize paths
047     * @param pathFilters objects that need to confirm whether a file can be deleted
048     */
049    public SortingVisitor(final PathSorter sorter) {
050        this.sorter = Objects.requireNonNull(sorter, "sorter");
051    }
052
053    @Override
054    public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException {
055        collected.add(new PathWithAttributes(path, attrs));
056        return FileVisitResult.CONTINUE;
057    }
058
059    @Override
060    public FileVisitResult visitFileFailed(Path file, IOException ioException) throws IOException {
061        // LOG4J2-2677: Appenders may rollover and purge in parallel. SimpleVisitor rethrows exceptions from
062        // failed attempts to load file attributes.
063        if (ioException instanceof NoSuchFileException) {
064            LOGGER.info("File {} could not be accessed, it has likely already been deleted", file, ioException);
065            return FileVisitResult.CONTINUE;
066        } else {
067            return super.visitFileFailed(file, ioException);
068        }
069    }
070
071    public List<PathWithAttributes> getSortedPaths() {
072        Collections.sort(collected, sorter);
073        return collected;
074    }
075}