View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.freebsd.disk;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   import java.util.regex.Matcher;
10  import java.util.regex.Pattern;
11  
12  import oshi.annotation.concurrent.ThreadSafe;
13  import oshi.util.ExecutingCommand;
14  
15  /**
16   * Utility to query mount
17   */
18  @ThreadSafe
19  public final class Mount {
20  
21      private static final String MOUNT_CMD = "mount";
22      private static final Pattern MOUNT_PATTERN = Pattern.compile("/dev/(\\S+p\\d+) on (\\S+) .*");
23  
24      private Mount() {
25      }
26  
27      /**
28       * Query mount to map partitions to mount points
29       *
30       * @return A map with partitions as the key and mount points as the value
31       */
32      public static Map<String, String> queryPartitionToMountMap() {
33          // Parse 'mount' to map partitions to mount point
34          Map<String, String> mountMap = new HashMap<>();
35          for (String mnt : ExecutingCommand.runNative(MOUNT_CMD)) {
36              Matcher m = MOUNT_PATTERN.matcher(mnt);
37              if (m.matches()) {
38                  mountMap.put(m.group(1), m.group(2));
39              }
40          }
41          return mountMap;
42      }
43  }