View Javadoc
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.logging.log4j.core.appender.rolling.action;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.PrintWriter;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.nio.file.StandardCopyOption;
26  
27  /**
28   * File rename action.
29   */
30  public class FileRenameAction extends AbstractAction {
31  
32      /**
33       * Source.
34       */
35      private final File source;
36  
37      /**
38       * Destination.
39       */
40      private final File destination;
41  
42      /**
43       * If true, rename empty files, otherwise delete empty files.
44       */
45      private final boolean renameEmptyFiles;
46  
47      /**
48       * Creates an FileRenameAction.
49       *
50       * @param src current file name.
51       * @param dst new file name.
52       * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
53       */
54      public FileRenameAction(final File src, final File dst, final boolean renameEmptyFiles) {
55          source = src;
56          destination = dst;
57          this.renameEmptyFiles = renameEmptyFiles;
58      }
59  
60      /**
61       * Rename file.
62       *
63       * @return true if successfully renamed.
64       */
65      @Override
66      public boolean execute() {
67          return execute(source, destination, renameEmptyFiles);
68      }
69  
70      /**
71       * Gets the destination.
72       *
73       * @return the destination.
74       */
75      public File getDestination() {
76          return this.destination;
77      }
78  
79      /**
80       * Gets the source.
81       *
82       * @return the source.
83       */
84      public File getSource() {
85          return this.source;
86      }
87  
88      /**
89       * Whether to rename empty files. If true, rename empty files, otherwise delete empty files.
90       *
91       * @return Whether to rename empty files.
92       */
93      public boolean isRenameEmptyFiles() {
94          return renameEmptyFiles;
95      }
96  
97      /**
98       * Rename file.
99       *
100      * @param source current file name.
101      * @param destination new file name.
102      * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
103      * @return true if successfully renamed.
104      */
105     public static boolean execute(final File source, final File destination, final boolean renameEmptyFiles) {
106         if (renameEmptyFiles || (source.length() > 0)) {
107             final File parent = destination.getParentFile();
108             if ((parent != null) && !parent.exists()) {
109                 // LOG4J2-679: ignore mkdirs() result: in multithreaded scenarios,
110                 // if one thread succeeds the other thread returns false
111                 // even though directories have been created. Check if dir exists instead.
112                 parent.mkdirs();
113                 if (!parent.exists()) {
114                     LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
115                     return false;
116                 }
117             }
118             try {
119                 try {
120                     Files.move(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
121                             StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
122                     LOGGER.trace("Renamed file {} to {} with Files.move", source.getAbsolutePath(),
123                             destination.getAbsolutePath());
124                     return true;
125                 } catch (final IOException exMove) {
126                     LOGGER.error("Unable to move file {} to {}: {} {}", source.getAbsolutePath(),
127                             destination.getAbsolutePath(), exMove.getClass().getName(), exMove.getMessage());
128                     final boolean result = source.renameTo(destination);
129                     if (!result) {
130                         try {
131                             Files.copy(Paths.get(source.getAbsolutePath()), Paths.get(destination.getAbsolutePath()),
132                                     StandardCopyOption.REPLACE_EXISTING);
133                             try {
134                                 Files.delete(Paths.get(source.getAbsolutePath()));
135                                 LOGGER.trace("Renamed file {} to {} using copy and delete",
136                                         source.getAbsolutePath(), destination.getAbsolutePath());
137                             } catch (final IOException exDelete) {
138                                 LOGGER.error("Unable to delete file {}: {} {}", source.getAbsolutePath(),
139                                         exDelete.getClass().getName(), exDelete.getMessage());
140                                 try {
141                                     new PrintWriter(source.getAbsolutePath()).close();
142                                     LOGGER.trace("Renamed file {} to {} with copy and truncation",
143                                             source.getAbsolutePath(), destination.getAbsolutePath());
144                                 } catch (final IOException exOwerwrite) {
145                                     LOGGER.error("Unable to overwrite file {}: {} {}",
146                                             source.getAbsolutePath(), exOwerwrite.getClass().getName(),
147                                             exOwerwrite.getMessage());
148                                 }
149                             }
150                         } catch (final IOException exCopy) {
151                             LOGGER.error("Unable to copy file {} to {}: {} {}", source.getAbsolutePath(),
152                                     destination.getAbsolutePath(), exCopy.getClass().getName(), exCopy.getMessage());
153                         }
154                     } else {
155                         LOGGER.trace("Renamed file {} to {} with source.renameTo",
156                                 source.getAbsolutePath(), destination.getAbsolutePath());
157                     }
158                     return result;
159                 }
160             } catch (final RuntimeException ex) {
161                 LOGGER.error("Unable to rename file {} to {}: {} {}", source.getAbsolutePath(),
162                         destination.getAbsolutePath(), ex.getClass().getName(), ex.getMessage());
163             }
164         } else {
165             try {
166                 source.delete();
167             } catch (final Exception exDelete) {
168                 LOGGER.error("Unable to delete empty file {}: {} {}", source.getAbsolutePath(),
169                         exDelete.getClass().getName(), exDelete.getMessage());
170             }
171         }
172 
173         return false;
174     }
175 
176     @Override
177     public String toString() {
178         return FileRenameAction.class.getSimpleName() + '[' + source + " to " + destination //
179                 + ", renameEmptyFiles=" + renameEmptyFiles + ']';
180     }
181 
182 }