View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix.aix;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import oshi.annotation.concurrent.ThreadSafe;
11  import oshi.util.ExecutingCommand;
12  import oshi.util.ParseUtil;
13  import oshi.util.tuples.Pair;
14  
15  /**
16   * Utility to query ls
17   */
18  @ThreadSafe
19  public final class Ls {
20  
21      private Ls() {
22      }
23  
24      /**
25       * Query {@code ls} to get parition info
26       *
27       * @return A map of device name to a major-minor pair
28       */
29      public static Map<String, Pair<Integer, Integer>> queryDeviceMajorMinor() {
30          // Map major and minor from ls
31          /*-
32           $ ls -l /dev
33          brw-rw----  1 root system 10,  5 Sep 12  2017 hd2
34          brw-------  1 root system 20,  0 Jun 28  1970 hdisk0
35           */
36          Map<String, Pair<Integer, Integer>> majMinMap = new HashMap<>();
37          for (String s : ExecutingCommand.runNative("ls -l /dev")) {
38              // Filter to block devices
39              if (!s.isEmpty() && s.charAt(0) == 'b') {
40                  // Device name is last space-delim string
41                  int idx = s.lastIndexOf(' ');
42                  if (idx > 0 && idx < s.length()) {
43                      String device = s.substring(idx + 1);
44                      int major = ParseUtil.getNthIntValue(s, 2);
45                      int minor = ParseUtil.getNthIntValue(s, 3);
46                      majMinMap.put(device, new Pair<>(major, minor));
47                  }
48              }
49          }
50          return majMinMap;
51      }
52  }