View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.mac.disk;
6   
7   import java.nio.charset.StandardCharsets;
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import com.sun.jna.Native;
12  import com.sun.jna.platform.mac.SystemB;
13  import com.sun.jna.platform.mac.SystemB.Statfs;
14  
15  import oshi.annotation.concurrent.ThreadSafe;
16  
17  /**
18   * Utility to query fsstat
19   */
20  @ThreadSafe
21  public final class Fsstat {
22  
23      private Fsstat() {
24      }
25  
26      /**
27       * Query fsstat to map partitions to mount points
28       *
29       * @return A map with partitions as the key and mount points as the value
30       */
31      public static Map<String, String> queryPartitionToMountMap() {
32          Map<String, String> mountPointMap = new HashMap<>();
33  
34          // Use statfs to get size of mounted file systems
35          int numfs = queryFsstat(null, 0, 0);
36          // Get data on file system
37          Statfs s = new Statfs();
38          // Create array to hold results
39          Statfs[] fs = (Statfs[]) s.toArray(numfs);
40          // Write file system data to array
41          queryFsstat(fs, numfs * fs[0].size(), SystemB.MNT_NOWAIT);
42  
43          // Iterate all mounted file systems
44          for (Statfs f : fs) {
45              String mntFrom = Native.toString(f.f_mntfromname, StandardCharsets.UTF_8);
46              mountPointMap.put(mntFrom.replace("/dev/", ""), Native.toString(f.f_mntonname, StandardCharsets.UTF_8));
47          }
48          return mountPointMap;
49      }
50  
51      private static int queryFsstat(Statfs[] buf, int bufsize, int flags) {
52          return SystemB.INSTANCE.getfsstat64(buf, bufsize, flags);
53      }
54  
55  }