View Javadoc
1   /*
2    * Copyright 2021-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
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   * The FreeBSD File System contains {@link oshi.software.os.OSFileStore}s which are a storage pool, device, partition,
24   * volume, concrete file system or other implementation specific means of file storage.
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      // Called by OpenBsdOSFileStore
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          // Get inode usage data
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              /*- Sample Output:
62               $ df -i
63              Filesystem  512-blocks      Used     Avail Capacity iused   ifree  %iused  Mounted on
64              /dev/wd0a      2149212    908676   1133076    45%    8355  147163     5%   /
65              /dev/wd0e      4050876        36   3848300     0%      10  285108     0%   /home
66              /dev/wd0d      6082908   3343172   2435592    58%   27813  386905     7%   /usr
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          // Get mount table
78          for (String fs : ExecutingCommand.runNative("mount -v")) { // NOSONAR squid:S135
79              /*-
80               Sample Output:
81               /dev/wd0a (d1c342b6965d372c.a) on / type ffs (rw, local, ctime=Sun Jan  3 18:03:00 2021)
82               /dev/wd0e (d1c342b6965d372c.e) on /home type ffs (rw, local, nodevl, nosuid, ctime=Sun Jan  3 18:02:56 2021)
83               /dev/wd0d (d1c342b6965d372c.d) on /usr type ffs (rw, local, nodev, wxallowed, ctime=Sun Jan  3 18:02:56 2021)
84               */
85              String[] split = ParseUtil.whitespaces.split(fs, 7);
86              if (split.length == 7) {
87                  // 1st field is volume name [0-index] + partition letter
88                  // 2nd field is disklabel UUID (DUID) + partition letter after the dot
89                  // 4th field is mount point
90                  // 6rd field is fs type
91                  // 7th field is options
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                  // Skip non-local drives if requested, and exclude pseudo file systems
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                 // Special case for /, pull last element of volume instead
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                     // dynamic size in memory FS
124                     description = "Ram Disk (dynamic)";
125                 } else if (volume.equals("mfs")) {
126                     // fixed size in memory FS
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 }