1
2
3
4
5 package oshi.driver.unix.openbsd.disk;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import oshi.annotation.concurrent.ThreadSafe;
11 import oshi.hardware.HWPartition;
12 import oshi.util.Constants;
13 import oshi.util.ExecutingCommand;
14 import oshi.util.ParseUtil;
15 import oshi.util.tuples.Pair;
16 import oshi.util.tuples.Quartet;
17
18
19
20
21 @ThreadSafe
22 public final class Disklabel {
23
24 private Disklabel() {
25 }
26
27
28
29
30
31
32
33 public static Quartet<String, String, Long, List<HWPartition>> getDiskParams(String diskName) {
34
35
36
37
38
39
40
41
42 List<HWPartition> partitions = new ArrayList<>();
43
44 String totalMarker = "total sectors:";
45 long totalSectors = 1L;
46 String bpsMarker = "bytes/sector:";
47 int bytesPerSector = 1;
48 String labelMarker = "label:";
49 String label = "";
50 String duidMarker = "duid:";
51 String duid = "";
52 for (String line : ExecutingCommand.runNative("disklabel -n " + diskName)) {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 if (line.contains(totalMarker)) {
70 totalSectors = ParseUtil.getFirstIntValue(line);
71 } else if (line.contains(bpsMarker)) {
72 bytesPerSector = ParseUtil.getFirstIntValue(line);
73 } else if (line.contains(labelMarker)) {
74 label = line.split(labelMarker)[1].trim();
75 } else if (line.contains(duidMarker)) {
76 duid = line.split(duidMarker)[1].trim();
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 if (line.trim().indexOf(':') == 1) {
96
97 String[] split = ParseUtil.whitespaces.split(line.trim(), 9);
98 String name = split[0].substring(0, 1);
99
100 Pair<Integer, Integer> majorMinor = getMajorMinor(diskName, name);
101
102 if (split.length > 4) {
103 partitions.add(new HWPartition(diskName + name, name, split[3], duid + "." + name,
104 ParseUtil.parseLongOrDefault(split[1], 0L) * bytesPerSector, majorMinor.getA(),
105 majorMinor.getB(), split.length > 5 ? split[split.length - 1] : ""));
106 }
107 }
108 }
109 if (partitions.isEmpty()) {
110 return getDiskParamsNoRoot(diskName);
111 }
112 return new Quartet<>(label, duid, totalSectors * bytesPerSector, partitions);
113 }
114
115 private static Quartet<String, String, Long, List<HWPartition>> getDiskParamsNoRoot(String diskName) {
116 List<HWPartition> partitions = new ArrayList<>();
117 for (String line : ExecutingCommand.runNative("df")) {
118 if (line.startsWith("/dev/" + diskName)) {
119 String[] split = ParseUtil.whitespaces.split(line);
120 String name = split[0].substring(5 + diskName.length());
121 Pair<Integer, Integer> majorMinor = getMajorMinor(diskName, name);
122 if (split.length > 5) {
123 long partSize = ParseUtil.parseLongOrDefault(split[1], 1L) * 512L;
124 partitions.add(new HWPartition(split[0], split[0].substring(5), Constants.UNKNOWN,
125 Constants.UNKNOWN, partSize, majorMinor.getA(), majorMinor.getB(), split[5]));
126 }
127 }
128 }
129 return new Quartet<>(Constants.UNKNOWN, Constants.UNKNOWN, 0L, partitions);
130 }
131
132 private static Pair<Integer, Integer> getMajorMinor(String diskName, String name) {
133 int major = 0;
134 int minor = 0;
135 String majorMinor = ExecutingCommand.getFirstAnswer("stat -f %Hr,%Lr /dev/" + diskName + name);
136 int comma = majorMinor.indexOf(',');
137 if (comma > 0 && comma < majorMinor.length()) {
138 major = ParseUtil.parseIntOrDefault(majorMinor.substring(0, comma), 0);
139 minor = ParseUtil.parseIntOrDefault(majorMinor.substring(comma + 1), 0);
140 }
141 return new Pair<>(major, minor);
142 }
143 }