Skip to content

Commit

Permalink
Demo class matching filestore to partition (oshi#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis authored Jul 7, 2020
1 parent 75df799 commit af016d6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
4.9.1 / 5.2.1 (coming soon)
================
* [#1282](https://github.com/oshi/oshi/pull/1282): (5.x) AIX Port - [@tausiflife](https://github.com/tausiflife), [@dbwiddis](https://github.com/dbwiddis).
* [#1285](https://github.com/oshi/oshi/pull/1247): Fallback to read properties file from classloader of the class - [@ngyukman](https://github.com/ngyukman).
* [#1285](https://github.com/oshi/oshi/pull/1285): Fallback to read properties file from classloader of the class - [@ngyukman](https://github.com/ngyukman).
* [#1290](https://github.com/oshi/oshi/pull/1290): Demo class matching filestore to partition - [@dbwiddis](https://github.com/dbwiddis).
* Your contribution here

4.9.0 / 5.2.0 (2020-06-25)
Expand Down
88 changes: 88 additions & 0 deletions oshi-demo/src/main/java/oshi/demo/DiskStoreForPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package oshi.demo;

import java.io.File;
import java.net.URISyntaxException;
import java.util.List;

import oshi.SystemInfo;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HWPartition;
import oshi.hardware.HardwareAbstractionLayer;
import oshi.software.os.OSFileStore;
import oshi.software.os.OperatingSystem;
import oshi.util.tuples.Pair;

/**
* Uses OSHI to attempt to identify which OSFileStore, HWDiskStore, and
* HWPartition a file resides on. Intended as a demonstration, not intended to
* be used in production code.
*/
public class DiskStoreForPath {
/**
* Main method
*
* @param args
* Optional file path
* @throws URISyntaxException
*/
public static void main(String[] args) throws URISyntaxException {
// Use the arg as a file path or get this class's path
String filePath = args.length > 0 ? args[0]
: new File(
DiskStoreForPath.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.getPath();
System.out.println("Searching stores for path: " + filePath);

SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
List<HWDiskStore> diskStores = hal.getDiskStores();
Pair<Integer, Integer> dsPartIdx = getDiskStoreAndPartitionForPath(filePath, diskStores);
int dsIndex = dsPartIdx.getA();
int partIndex = dsPartIdx.getB();

System.out.println();
System.out.println("DiskStore index " + dsIndex + " and Partition index " + partIndex);
if (dsIndex >= 0 && partIndex >= 0) {
System.out.println(diskStores.get(dsIndex));
System.out.println(" |-- " + diskStores.get(dsIndex).getPartitions().get(partIndex));
} else {
System.out.println("Couldn't find that path on a partition.");
}

OperatingSystem os = si.getOperatingSystem();
List<OSFileStore> fileStores = os.getFileSystem().getFileStores();
int fsIndex = getFileStoreForPath(filePath, fileStores);

System.out.println();
System.out.println("FileStore index " + fsIndex);
if (fsIndex >= 0) {
System.out.println(fileStores.get(fsIndex));
} else {
System.out.println("Couldn't find that path on a filestore.");
}
}

private static Pair<Integer, Integer> getDiskStoreAndPartitionForPath(String path, List<HWDiskStore> diskStores) {
for (int ds = 0; ds < diskStores.size(); ds++) {
HWDiskStore store = diskStores.get(0);
List<HWPartition> parts = store.getPartitions();
for (int part = 0; part < parts.size(); part++) {
String mount = parts.get(part).getMountPoint();
if (!mount.isEmpty() && path.substring(0, mount.length()).equalsIgnoreCase(mount)) {
return new Pair<>(ds, part);
}
}
}
return new Pair<>(-1, -1);
}

private static int getFileStoreForPath(String path, List<OSFileStore> fileStores) {
for (int fs = 0; fs < fileStores.size(); fs++) {
String mount = fileStores.get(fs).getMount();
if (!mount.isEmpty() && path.substring(0, mount.length()).equalsIgnoreCase(mount)) {
return fs;
}
}
return -1;
}
}

0 comments on commit af016d6

Please sign in to comment.