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
18 package org.apache.logging.log4j.core.appender.rolling.action;
19
20 import java.io.IOException;
21 import java.nio.file.FileVisitResult;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.SimpleFileVisitor;
25 import java.nio.file.attribute.BasicFileAttributes;
26 import java.util.List;
27 import java.util.Objects;
28
29 import org.apache.logging.log4j.Logger;
30 import org.apache.logging.log4j.status.StatusLogger;
31
32 /**
33 * FileVisitor that deletes files that are accepted by all PathFilters. Directories are ignored.
34 */
35 public class DeletingVisitor extends SimpleFileVisitor<Path> {
36 private static final Logger LOGGER = StatusLogger.getLogger();
37
38 private final Path basePath;
39 private final boolean testMode;
40 private final List<? extends PathCondition> pathConditions;
41
42 /**
43 * Constructs a new DeletingVisitor.
44 *
45 * @param basePath used to relativize paths
46 * @param pathConditions objects that need to confirm whether a file can be deleted
47 * @param testMode if true, files are not deleted but instead a message is printed to the <a
48 * href="http://logging.apache.org/log4j/2.x/manual/configuration.html#StatusMessages">status logger</a>
49 * at INFO level. Users can use this to do a dry run to test if their configuration works as expected.
50 */
51 public DeletingVisitor(final Path basePath, final List<? extends PathCondition> pathConditions,
52 final boolean testMode) {
53 this.testMode = testMode;
54 this.basePath = Objects.requireNonNull(basePath, "basePath");
55 this.pathConditions = Objects.requireNonNull(pathConditions, "pathConditions");
56 for (final PathCondition condition : pathConditions) {
57 condition.beforeFileTreeWalk();
58 }
59 }
60
61 @Override
62 public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
63 for (final PathCondition pathFilter : pathConditions) {
64 final Path relative = basePath.relativize(file);
65 if (!pathFilter.accept(basePath, relative, attrs)) {
66 LOGGER.trace("Not deleting base={}, relative={}", basePath, relative);
67 return FileVisitResult.CONTINUE;
68 }
69 }
70 if (isTestMode()) {
71 LOGGER.info("Deleting {} (TEST MODE: file not actually deleted)", file);
72 } else {
73 delete(file);
74 }
75 return FileVisitResult.CONTINUE;
76 }
77
78 /**
79 * Deletes the specified file.
80 *
81 * @param file the file to delete
82 * @throws IOException if a problem occurred deleting the file
83 */
84 protected void delete(final Path file) throws IOException {
85 LOGGER.trace("Deleting {}", file);
86 Files.deleteIfExists(file);
87 }
88
89 /**
90 * Returns {@code true} if files are not deleted even when all conditions accept a path, {@code false} otherwise.
91 *
92 * @return {@code true} if files are not deleted even when all conditions accept a path, {@code false} otherwise
93 */
94 public boolean isTestMode() {
95 return testMode;
96 }
97 }