View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.rolling.helper;
15  
16  import java.io.File;
17  
18  import ch.qos.logback.core.CoreConstants;
19  import ch.qos.logback.core.rolling.RollingFileAppender;
20  import ch.qos.logback.core.rolling.RolloverFailure;
21  import ch.qos.logback.core.spi.ContextAwareBase;
22  import ch.qos.logback.core.util.EnvUtil;
23  import ch.qos.logback.core.util.FileUtil;
24  
25  /**
26   * Utility class to help solving problems encountered while renaming files.
27   *
28   * @author Ceki Gulcu
29   */
30  public class RenameUtil extends ContextAwareBase {
31  
32      static String RENAMING_ERROR_URL = CoreConstants.CODES_URL + "#renamingError";
33  
34      /**
35       * A relatively robust file renaming method which in case of failure due to
36       * src and target being on different volumes, falls back onto
37       * renaming by copying.
38       *
39       * @param src
40       * @param target
41       * @throws RolloverFailure
42       */
43      public void rename(String src, String target) throws RolloverFailure {
44          if (src.equals(target)) {
45              addWarn("Source and target files are the same [" + src + "]. Skipping.");
46              return;
47          }
48          File srcFile = new File(src);
49  
50          if (srcFile.exists()) {
51              File targetFile = new File(target);
52              createMissingTargetDirsIfNecessary(targetFile);
53  
54              addInfo("Renaming file [" + srcFile + "] to [" + targetFile + "]");
55  
56              boolean result = srcFile.renameTo(targetFile);
57  
58              if (!result) {
59                  addWarn("Failed to rename file [" + srcFile + "] as [" + targetFile + "].");
60                  Boolean areOnDifferentVolumes = areOnDifferentVolumes(srcFile, targetFile);
61                  if (Boolean.TRUE.equals(areOnDifferentVolumes)) {
62                      addWarn("Detected different file systems for source [" + src + "] and target [" + target + "]. Attempting rename by copying.");
63                      renameByCopying(src, target);
64                      return;
65                  } else {
66                      addWarn("Please consider leaving the [file] option of " + RollingFileAppender.class.getSimpleName() + " empty.");
67                      addWarn("See also " + RENAMING_ERROR_URL);
68                  }
69              }
70          } else {
71              throw new RolloverFailure("File [" + src + "] does not exist.");
72          }
73      }
74  
75      
76      
77      /**
78       * Attempts to determine whether both files are on different volumes. Returns true if we could determine that
79       * the files are on different volumes. Returns false otherwise or if an error occurred while doing the check.
80       *
81       * @param srcFile
82       * @param targetFile
83       * @return true if on different volumes, false otherwise or if an error occurred
84       */
85      Boolean areOnDifferentVolumes(File srcFile, File targetFile) throws RolloverFailure {
86          if (!EnvUtil.isJDK7OrHigher())
87              return false;
88  
89          // target file is not certain to exist but its parent has to exist given the call hierarchy of this method
90          File parentOfTarget = targetFile.getAbsoluteFile().getParentFile();
91          
92          if(parentOfTarget == null) {
93              addWarn("Parent of target file ["+targetFile+"] is null");
94              return null;
95          }
96          if(!parentOfTarget.exists()) {
97              addWarn("Parent of target file ["+targetFile+"] does not exist");
98              return null;
99          }
100         
101         try {
102             boolean onSameFileStore = FileStoreUtil.areOnSameFileStore(srcFile, parentOfTarget);
103             return !onSameFileStore;
104         } catch (RolloverFailure rf) {
105             addWarn("Error while checking file store equality", rf);
106             return null;
107         }
108     }
109 
110     public void renameByCopying(String src, String target) throws RolloverFailure {
111 
112         FileUtil fileUtil = new FileUtil(getContext());
113         fileUtil.copy(src, target);
114 
115         File srcFile = new File(src);
116         if (!srcFile.delete()) {
117             addWarn("Could not delete " + src);
118         }
119 
120     }
121 
122     void createMissingTargetDirsIfNecessary(File toFile) throws RolloverFailure {
123         boolean result = FileUtil.createMissingParentDirectories(toFile);
124         if (!result) {
125             throw new RolloverFailure("Failed to create parent directories for [" + toFile.getAbsolutePath() + "]");
126         }
127     }
128 
129     @Override
130     public String toString() {
131         return "c.q.l.co.rolling.helper.RenameUtil";
132     }
133 }