예제 #1
0
  public static List<DiskDeviceInfo> getDefaultDiskDeviceInfos() {
    DiskDeviceInfo diskDeviceInfo = new DiskDeviceInfo(0);
    diskDeviceInfo.setName("default");

    List<DiskDeviceInfo> infos = new ArrayList<DiskDeviceInfo>();

    infos.add(diskDeviceInfo);

    return infos;
  }
예제 #2
0
  private static List<DiskDeviceInfo> getUnixDiskDeviceInfos() {
    List<DiskDeviceInfo> infos = new ArrayList<DiskDeviceInfo>();

    File file = new File(UNIX_DISK_DEVICE_PATH);
    if (!file.exists()) {
      System.out.println("No partition file:" + file.getAbsolutePath());
      return getDefaultDiskDeviceInfos();
    }

    BufferedReader reader = null;
    try {
      reader =
          new BufferedReader(new InputStreamReader(new FileInputStream(UNIX_DISK_DEVICE_PATH)));
      String line = null;

      int count = 0;
      Set<String> deviceNames = new TreeSet<String>();
      while ((line = reader.readLine()) != null) {
        if (count > 0 && !line.trim().isEmpty()) {
          String[] tokens = line.trim().split(" +");
          if (tokens.length == 4) {
            String deviceName = getDiskDeviceName(tokens[3]);
            deviceNames.add(deviceName);
          }
        }
        count++;
      }

      int id = 0;
      for (String eachDeviceName : deviceNames) {
        DiskDeviceInfo diskDeviceInfo = new DiskDeviceInfo(id++);
        diskDeviceInfo.setName(eachDeviceName);

        // TODO set additional info
        // /sys/block/sda/queue
        infos.add(diskDeviceInfo);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
        }
      }
    }

    return infos;
  }
예제 #3
0
  private static void setDeviceMountInfo(List<DiskDeviceInfo> deviceInfos) throws IOException {
    Map<String, DiskDeviceInfo> deviceMap = new HashMap<String, DiskDeviceInfo>();
    for (DiskDeviceInfo eachDevice : deviceInfos) {
      deviceMap.put(eachDevice.getName(), eachDevice);
    }

    BufferedReader mountOutput = null;
    try {
      Process mountProcess = Runtime.getRuntime().exec("mount");
      mountOutput = new BufferedReader(new InputStreamReader(mountProcess.getInputStream()));
      while (true) {
        String line = mountOutput.readLine();
        if (line == null) {
          break;
        }

        int indexStart = line.indexOf(" on /");
        int indexEnd = line.indexOf(" ", indexStart + 4);

        String deviceName = line.substring(0, indexStart).trim();
        String[] deviceNameTokens = deviceName.split("/");
        if (deviceNameTokens.length == 3) {
          if ("dev".equals(deviceNameTokens[1])) {
            String realDeviceName = getDiskDeviceName(deviceNameTokens[2]);
            String mountPath = new File(line.substring(indexStart + 4, indexEnd)).getAbsolutePath();

            DiskDeviceInfo diskDeviceInfo = deviceMap.get(realDeviceName);
            if (diskDeviceInfo != null) {
              diskDeviceInfo.addMountPath(new DiskMountInfo(diskDeviceInfo.getId(), mountPath));
            }
          }
        }
      }
    } catch (IOException e) {
      throw e;
    } finally {
      if (mountOutput != null) {
        mountOutput.close();
      }
    }
  }