oshi.software.os.mac.MacFileSystem Maven / Gradle / Ivy
/**
* Oshi (https://github.com/dblock/oshi)
*
* Copyright (c) 2010 - 2016 The Oshi Project Team
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Maintainers:
* dblock[at]dblock[dot]org
* widdis[at]gmail[dot]com
* enrico.bianchi[at]gmail[dot]com
*
* Contributors:
* https://github.com/dblock/oshi/graphs/contributors
*/
package oshi.software.os.mac;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.swing.filechooser.FileSystemView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jna.ptr.IntByReference;
import oshi.jna.platform.mac.CoreFoundation.CFMutableDictionaryRef;
import oshi.jna.platform.mac.IOKit;
import oshi.jna.platform.mac.SystemB;
import oshi.jna.platform.mac.SystemB.Statfs;
import oshi.software.common.AbstractFileSystem;
import oshi.software.os.OSFileStore;
import oshi.util.platform.mac.IOKitUtil;
import oshi.util.platform.mac.SysctlUtil;
/**
* The Mac File System contains {@link OSFileStore}s which are a storage pool,
* device, partition, volume, concrete file system or other implementation
* specific means of file storage. In Mac OS X, these are found in the /Volumes
* directory.
*
* @author widdis[at]gmail[dot]com
*/
public class MacFileSystem extends AbstractFileSystem {
private static final long serialVersionUID = 1L;
private static final Logger LOG = LoggerFactory.getLogger(MacFileSystem.class);
// Regexp matcher for /dev/disk1 etc.
private static final Pattern LOCAL_DISK = Pattern.compile("/dev/disk\\d");
/**
* Gets File System Information.
*
* @return An array of {@link OSFileStore} objects representing mounted
* volumes. May return disconnected volumes with
* {@link OSFileStore#getTotalSpace()} = 0.
*/
public OSFileStore[] getFileStores() {
// Use getfsstat to map filesystem paths to types
Map fstype = new HashMap<>();
// Query with null to get total # required
int numfs = SystemB.INSTANCE.getfsstat64(null, 0, 0);
if (numfs > 0) {
// Create array to hold results
Statfs[] fs = new Statfs[numfs];
// Fill array with results
numfs = SystemB.INSTANCE.getfsstat64(fs, numfs * (new Statfs()).size(), SystemB.MNT_NOWAIT);
for (int f = 0; f < numfs; f++) {
// Mount to name will match canonical path.
// Byte arrays are null-terminated strings
fstype.put(new String(fs[f].f_mntonname).trim(), new String(fs[f].f_fstypename).trim());
}
}
// Now list file systems
List fsList = new ArrayList<>();
FileSystemView fsv = FileSystemView.getFileSystemView();
// Mac file systems are mounted in /Volumes
File volumes = new File("/Volumes");
if (volumes != null && volumes.listFiles() != null) {
for (File f : volumes.listFiles()) {
// Everyone hates DS Store
if (f.getName().endsWith(".DS_Store")) {
continue;
}
String name = fsv.getSystemDisplayName(f);
String description = "Volume";
String type = "unknown";
String path = "unknown";
String volume = "";
String uuid = "";
try {
path = f.getCanonicalPath();
if (path.equals("/"))
name = name + " (/)";
FileStore fs = Files.getFileStore(f.toPath());
volume = fs.name();
if (LOCAL_DISK.matcher(volume).matches()) {
description = "Local Disk";
}
if (volume.startsWith("localhost:") || volume.startsWith("//")) {
description = "Network Drive";
}
if (fstype.containsKey(path)) {
type = fstype.get(path);
}
// Use volume to find registry entry and get UUID
String bsdName = volume.replace("/dev/disk", "disk");
if (bsdName.startsWith("disk")) {
CFMutableDictionaryRef matchingDict = IOKitUtil.getBSDNameMatchingDict(bsdName);
if (matchingDict != null) {
// search for all IOservices that match the bsd name
IntByReference fsIter = new IntByReference();
IOKitUtil.getMatchingServices(matchingDict, fsIter);
// getMatchingServices releases matchingDict
// Should only match one logical drive
int fsEntry = IOKit.INSTANCE.IOIteratorNext(fsIter.getValue());
if (fsEntry != 0 && IOKit.INSTANCE.IOObjectConformsTo(fsEntry, "IOMedia")) {
// Now get the UUID
uuid = IOKitUtil.getIORegistryStringProperty(fsEntry, "UUID");
if (uuid == null) {
uuid = "";
}
IOKit.INSTANCE.IOObjectRelease(fsEntry);
}
IOKit.INSTANCE.IOObjectRelease(fsIter.getValue());
}
}
} catch (IOException e) {
LOG.trace("", e);
continue;
}
fsList.add(new OSFileStore(name, volume, path, description, type, uuid, f.getUsableSpace(),
f.getTotalSpace()));
}
}
return fsList.toArray(new OSFileStore[fsList.size()]);
}
@Override
public long getOpenFileDescriptors() {
return SysctlUtil.sysctl("kern.num_files", 0);
}
@Override
public long getMaxFileDescriptors() {
return SysctlUtil.sysctl("kern.maxfiles", 0);
}
}