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  import java.nio.file.FileStore;
18  import java.nio.file.Files;
19  import java.nio.file.Path;
20  
21  import ch.qos.logback.core.rolling.RolloverFailure;
22  
23  /**
24   * A utility class using functionality available since JDK 1.7.
25   *
26   * @author ceki
27   * @since 1.0.10
28   */
29  public class FileStoreUtil {
30  
31      static final String PATH_CLASS_STR = "java.nio.file.Path";
32      static final String FILES_CLASS_STR = "java.nio.file.Files";
33  
34      /**
35       * This method assumes that both files a and b exists.
36       *
37       * @param a
38       * @param b
39       * @return
40       * @throws RolloverFailure
41       */
42      static public boolean areOnSameFileStore(File a, File b) throws RolloverFailure {
43          if (!a.exists()) {
44              throw new IllegalArgumentException("File [" + a + "] does not exist.");
45          }
46          if (!b.exists()) {
47              throw new IllegalArgumentException("File [" + b + "] does not exist.");
48          }
49  
50          // Implements the following by reflection
51  
52          try {
53              Path pathA = a.toPath();
54              Path pathB = b.toPath();
55  
56              FileStore fileStoreA = Files.getFileStore(pathA);
57              FileStore fileStoreB = Files.getFileStore(pathB);
58  
59              return fileStoreA.equals(fileStoreB);
60          } catch (Exception e) {
61              throw new RolloverFailure("Failed to check file store equality for [" + a + "] and [" + b + "]", e);
62          }
63      }
64  }