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.List;
9   import java.util.Map;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  import oshi.util.Constants;
13  import oshi.util.ExecutingCommand;
14  import oshi.util.ParseUtil;
15  import oshi.util.tuples.Triplet;
16  
17  /**
18   * Utility to query geom part list
19   */
20  @ThreadSafe
21  public final class GeomDiskList {
22  
23      private static final String GEOM_DISK_LIST = "geom disk list";
24  
25      private GeomDiskList() {
26      }
27  
28      /**
29       * Queries disk data using geom
30       *
31       * @return A map with disk name as the key and a Triplet of model, serial, and size as the value
32       */
33      public static Map<String, Triplet<String, String, Long>> queryDisks() {
34          // Map of device name to disk, to be returned
35          Map<String, Triplet<String, String, Long>> diskMap = new HashMap<>();
36          // Parameters needed.
37          String diskName = null; // Non-null identifies a valid partition
38          String descr = Constants.UNKNOWN;
39          String ident = Constants.UNKNOWN;
40          long mediaSize = 0L;
41  
42          List<String> geom = ExecutingCommand.runNative(GEOM_DISK_LIST);
43          for (String line : geom) {
44              line = line.trim();
45              // Marks the DiskStore device
46              if (line.startsWith("Geom name:")) {
47                  // Save any previous disk in the map
48                  if (diskName != null) {
49                      diskMap.put(diskName, new Triplet<>(descr, ident, mediaSize));
50                      descr = Constants.UNKNOWN;
51                      ident = Constants.UNKNOWN;
52                      mediaSize = 0L;
53                  }
54                  // Now use new diskName
55                  diskName = line.substring(line.lastIndexOf(' ') + 1);
56              }
57              // If we don't have a valid store, don't bother parsing anything
58              if (diskName != null) {
59                  line = line.trim();
60                  if (line.startsWith("Mediasize:")) {
61                      String[] split = ParseUtil.whitespaces.split(line);
62                      if (split.length > 1) {
63                          mediaSize = ParseUtil.parseLongOrDefault(split[1], 0L);
64                      }
65                  }
66                  if (line.startsWith("descr:")) {
67                      descr = line.replace("descr:", "").trim();
68                  }
69                  if (line.startsWith("ident:")) {
70                      ident = line.replace("ident:", "").replace("(null)", "").trim();
71                  }
72              }
73          }
74          if (diskName != null) {
75              diskMap.put(diskName, new Triplet<>(descr, ident, mediaSize));
76          }
77          return diskMap;
78      }
79  }