View Javadoc
1   /*
2    * Copyright 2024 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.util.platform.linux;
6   
7   import java.io.File;
8   
9   import oshi.annotation.concurrent.ThreadSafe;
10  import oshi.util.GlobalConfig;
11  
12  /**
13   * Provides constants for paths in the {@code /dev} filesystem on Linux.
14   * <p>
15   * If the user desires to configure a custom {@code /dev} path, it must be declared in the OSHI configuration file or
16   * updated in the {@link GlobalConfig} class prior to initializing this class.
17   */
18  @ThreadSafe
19  public final class DevPath {
20  
21      /**
22       * The /dev filesystem location.
23       */
24      public static final String DEV = queryDevConfig() + "/";
25  
26      public static final String DISK_BY_UUID = DEV + "disk/by-uuid";
27      public static final String DM = DEV + "dm";
28      public static final String LOOP = DEV + "loop";
29      public static final String MAPPER = DEV + "mapper/";
30      public static final String RAM = DEV + "ram";
31  
32      private DevPath() {
33      }
34  
35      private static String queryDevConfig() {
36          String devPath = GlobalConfig.get(GlobalConfig.OSHI_UTIL_DEV_PATH, "/dev");
37          // Ensure prefix begins with path separator, but doesn't end with one
38          devPath = '/' + devPath.replaceAll("/$|^/", "");
39          if (!new File(devPath).exists()) {
40              throw new GlobalConfig.PropertyException(GlobalConfig.OSHI_UTIL_DEV_PATH, "The path does not exist");
41          }
42          return devPath;
43      }
44  }