View Javadoc
1   /*
2    * Copyright 2020-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.demo;
6   
7   import java.io.File;
8   import java.net.URISyntaxException;
9   import java.util.List;
10  
11  import oshi.SystemInfo;
12  import oshi.annotation.SuppressForbidden;
13  import oshi.hardware.HWDiskStore;
14  import oshi.hardware.HWPartition;
15  import oshi.hardware.HardwareAbstractionLayer;
16  import oshi.software.os.OSFileStore;
17  import oshi.software.os.OperatingSystem;
18  import oshi.util.tuples.Pair;
19  
20  /**
21   * Uses OSHI to attempt to identify which OSFileStore, HWDiskStore, and HWPartition a file resides on. Intended as a
22   * demonstration, not intended to be used in production code.
23   * <p>
24   * In pariticular, this won't work in all cases, particularly with logical partitions.
25   */
26  public class DiskStoreForPath {
27      /**
28       * Main method
29       *
30       * @param args Optional file path
31       * @throws URISyntaxException on invalid path
32       */
33      @SuppressForbidden(reason = "Using System.out in a demo class")
34      public static void main(String[] args) throws URISyntaxException {
35          // Use the arg as a file path or get this class's path
36          String filePath = args.length > 0 ? args[0]
37                  : new File(DiskStoreForPath.class.getProtectionDomain().getCodeSource().getLocation().toURI())
38                          .getPath();
39          System.out.println("Searching stores for path: " + filePath);
40  
41          SystemInfo si = new SystemInfo();
42          HardwareAbstractionLayer hal = si.getHardware();
43          List<HWDiskStore> diskStores = hal.getDiskStores();
44          Pair<Integer, Integer> dsPartIdx = getDiskStoreAndPartitionForPath(filePath, diskStores);
45          int dsIndex = dsPartIdx.getA();
46          int partIndex = dsPartIdx.getB();
47  
48          System.out.println();
49          System.out.println("DiskStore index " + dsIndex + " and Partition index " + partIndex);
50          if (dsIndex >= 0 && partIndex >= 0) {
51              System.out.println(diskStores.get(dsIndex));
52              System.out.println(" |-- " + diskStores.get(dsIndex).getPartitions().get(partIndex));
53          } else {
54              System.out.println("Couldn't find that path on a partition.");
55          }
56  
57          OperatingSystem os = si.getOperatingSystem();
58          List<OSFileStore> fileStores = os.getFileSystem().getFileStores();
59          int fsIndex = getFileStoreForPath(filePath, fileStores);
60  
61          System.out.println();
62          System.out.println("FileStore index " + fsIndex);
63          if (fsIndex >= 0) {
64              System.out.println(fileStores.get(fsIndex));
65          } else {
66              System.out.println("Couldn't find that path on a filestore.");
67          }
68      }
69  
70      private static Pair<Integer, Integer> getDiskStoreAndPartitionForPath(String path, List<HWDiskStore> diskStores) {
71          for (int ds = 0; ds < diskStores.size(); ds++) {
72              HWDiskStore store = diskStores.get(ds);
73              List<HWPartition> parts = store.getPartitions();
74              for (int part = 0; part < parts.size(); part++) {
75                  String mount = parts.get(part).getMountPoint();
76                  if (!mount.isEmpty() && path.substring(0, mount.length()).equalsIgnoreCase(mount)) {
77                      return new Pair<>(ds, part);
78                  }
79              }
80          }
81          return new Pair<>(-1, -1);
82      }
83  
84      private static int getFileStoreForPath(String path, List<OSFileStore> fileStores) {
85          for (int fs = 0; fs < fileStores.size(); fs++) {
86              String mount = fileStores.get(fs).getMount();
87              if (!mount.isEmpty() && path.substring(0, mount.length()).equalsIgnoreCase(mount)) {
88                  return fs;
89              }
90          }
91          return -1;
92      }
93  }