1
2
3
4
5 package oshi.software.os.unix.openbsd;
6
7 import java.io.File;
8 import java.nio.file.PathMatcher;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 import oshi.annotation.concurrent.ThreadSafe;
15 import oshi.software.common.AbstractFileSystem;
16 import oshi.software.os.OSFileStore;
17 import oshi.util.ExecutingCommand;
18 import oshi.util.FileSystemUtil;
19 import oshi.util.ParseUtil;
20 import oshi.util.platform.unix.openbsd.OpenBsdSysctlUtil;
21
22
23
24
25
26 @ThreadSafe
27 public class OpenBsdFileSystem extends AbstractFileSystem {
28
29 public static final String OSHI_OPENBSD_FS_PATH_EXCLUDES = "oshi.os.openbsd.filesystem.path.excludes";
30 public static final String OSHI_OPENBSD_FS_PATH_INCLUDES = "oshi.os.openbsd.filesystem.path.includes";
31 public static final String OSHI_OPENBSD_FS_VOLUME_EXCLUDES = "oshi.os.openbsd.filesystem.volume.excludes";
32 public static final String OSHI_OPENBSD_FS_VOLUME_INCLUDES = "oshi.os.openbsd.filesystem.volume.includes";
33
34 private static final List<PathMatcher> FS_PATH_EXCLUDES = FileSystemUtil
35 .loadAndParseFileSystemConfig(OSHI_OPENBSD_FS_PATH_EXCLUDES);
36 private static final List<PathMatcher> FS_PATH_INCLUDES = FileSystemUtil
37 .loadAndParseFileSystemConfig(OSHI_OPENBSD_FS_PATH_INCLUDES);
38 private static final List<PathMatcher> FS_VOLUME_EXCLUDES = FileSystemUtil
39 .loadAndParseFileSystemConfig(OSHI_OPENBSD_FS_VOLUME_EXCLUDES);
40 private static final List<PathMatcher> FS_VOLUME_INCLUDES = FileSystemUtil
41 .loadAndParseFileSystemConfig(OSHI_OPENBSD_FS_VOLUME_INCLUDES);
42
43 @Override
44 public List<OSFileStore> getFileStores(boolean localOnly) {
45 return getFileStoreMatching(null, localOnly);
46 }
47
48
49 static List<OSFileStore> getFileStoreMatching(String nameToMatch) {
50 return getFileStoreMatching(nameToMatch, false);
51 }
52
53 private static List<OSFileStore> getFileStoreMatching(String nameToMatch, boolean localOnly) {
54 List<OSFileStore> fsList = new ArrayList<>();
55
56
57 Map<String, Long> inodeFreeMap = new HashMap<>();
58 Map<String, Long> inodeUsedlMap = new HashMap<>();
59 String command = "df -i" + (localOnly ? " -l" : "");
60 for (String line : ExecutingCommand.runNative(command)) {
61
62
63
64
65
66
67
68 if (line.startsWith("/")) {
69 String[] split = ParseUtil.whitespaces.split(line);
70 if (split.length > 6) {
71 inodeUsedlMap.put(split[0], ParseUtil.parseLongOrDefault(split[5], 0L));
72 inodeFreeMap.put(split[0], ParseUtil.parseLongOrDefault(split[6], 0L));
73 }
74 }
75 }
76
77
78 for (String fs : ExecutingCommand.runNative("mount -v")) {
79
80
81
82
83
84
85 String[] split = ParseUtil.whitespaces.split(fs, 7);
86 if (split.length == 7) {
87
88
89
90
91
92 String volume = split[0];
93 String uuid = split[1];
94 String path = split[3];
95 String type = split[5];
96 String options = split[6];
97
98
99 if ((localOnly && NETWORK_FS_TYPES.contains(type)) || !path.equals("/")
100 && (PSEUDO_FS_TYPES.contains(type) || FileSystemUtil.isFileStoreExcluded(path, volume,
101 FS_PATH_INCLUDES, FS_PATH_EXCLUDES, FS_VOLUME_INCLUDES, FS_VOLUME_EXCLUDES))) {
102 continue;
103 }
104
105 String name = path.substring(path.lastIndexOf('/') + 1);
106
107 if (name.isEmpty()) {
108 name = volume.substring(volume.lastIndexOf('/') + 1);
109 }
110
111 if (nameToMatch != null && !nameToMatch.equals(name)) {
112 continue;
113 }
114 File f = new File(path);
115 long totalSpace = f.getTotalSpace();
116 long usableSpace = f.getUsableSpace();
117 long freeSpace = f.getFreeSpace();
118
119 String description;
120 if (volume.startsWith("/dev") || path.equals("/")) {
121 description = "Local Disk";
122 } else if (volume.equals("tmpfs")) {
123
124 description = "Ram Disk (dynamic)";
125 } else if (volume.equals("mfs")) {
126
127 description = "Ram Disk (fixed)";
128 } else if (NETWORK_FS_TYPES.contains(type)) {
129 description = "Network Disk";
130 } else {
131 description = "Mount Point";
132 }
133
134 fsList.add(new OpenBsdOSFileStore(name, volume, name, path, options, uuid, "", description, type,
135 freeSpace, usableSpace, totalSpace, inodeFreeMap.getOrDefault(volume, 0L),
136 inodeUsedlMap.getOrDefault(volume, 0L) + inodeFreeMap.getOrDefault(volume, 0L)));
137 }
138 }
139 return fsList;
140 }
141
142 @Override
143 public long getOpenFileDescriptors() {
144 return OpenBsdSysctlUtil.sysctl("kern.nfiles", 0);
145 }
146
147 @Override
148 public long getMaxFileDescriptors() {
149 return OpenBsdSysctlUtil.sysctl("kern.maxfiles", 0);
150 }
151
152 @Override
153 public long getMaxFileDescriptorsPerProcess() {
154 return OpenBsdSysctlUtil.sysctl("kern.maxfilesperproc", 0);
155 }
156 }