View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.wmi;
6   
7   import java.util.Objects;
8   
9   import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
10  import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
11  
12  import oshi.annotation.concurrent.ThreadSafe;
13  import oshi.util.platform.windows.WmiQueryHandler;
14  
15  /**
16   * Utility to query WMI class {@code Win32_LogicalDisk}
17   */
18  @ThreadSafe
19  public final class Win32LogicalDisk {
20  
21      private static final String WIN32_LOGICAL_DISK = "Win32_LogicalDisk";
22  
23      /**
24       * Logical disk properties.
25       */
26      public enum LogicalDiskProperty {
27          ACCESS, DESCRIPTION, DRIVETYPE, FILESYSTEM, FREESPACE, NAME, PROVIDERNAME, SIZE, VOLUMENAME;
28      }
29  
30      private Win32LogicalDisk() {
31      }
32  
33      /**
34       * Queries logical disk information
35       *
36       * @param nameToMatch an optional string to filter match, null otherwise
37       * @param localOnly   Whether to only search local drives
38       * @return Logical Disk Information
39       */
40      public static WmiResult<LogicalDiskProperty> queryLogicalDisk(String nameToMatch, boolean localOnly) {
41          StringBuilder wmiClassName = new StringBuilder(WIN32_LOGICAL_DISK);
42          boolean where = false;
43          if (localOnly) {
44              wmiClassName.append(" WHERE DriveType != 4");
45              where = true;
46          }
47          if (nameToMatch != null) {
48              wmiClassName.append(where ? " AND" : " WHERE").append(" Name=\"").append(nameToMatch).append('\"');
49          }
50          WmiQuery<LogicalDiskProperty> logicalDiskQuery = new WmiQuery<>(wmiClassName.toString(),
51                  LogicalDiskProperty.class);
52          return Objects.requireNonNull(WmiQueryHandler.createInstance()).queryWMI(logicalDiskQuery);
53      }
54  }