Skip to content

Commit

Permalink
closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Schrenk committed Oct 5, 2018
1 parent 8fde791 commit 03d92cf
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions src/main/java/org/cryptomator/frontend/fuse/mount/MacMounter.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package org.cryptomator.frontend.fuse.mount;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

class MacMounter implements Mounter {

private static final Logger LOG = LoggerFactory.getLogger(MacMounter.class);
private static final boolean IS_MAC = System.getProperty("os.name").toLowerCase().contains("mac");
private static final int[] OSXFUSE_MINIMUM_SUPPORTED_VERSION = new int[]{3, 8, 2};
private static final Path OSXFUSE_VERSIONFILE_LOCATION = Paths.get("/Library/Filesystems/osxfuse.fs/Contents/version.plist");
private static final String OSXFUSE_XML_VERSION_TEXT = "CFBundleShortVersionString";

@Override
public Mount mount(Path directory, EnvironmentVariables envVars, String... additionalMountParams) throws CommandFailedException {
Expand All @@ -20,11 +34,56 @@ public Mount mount(Path directory, EnvironmentVariables envVars, String... addit
}

/**
* @return <code>true</code> if on OS X and osxfuse is installed.
* @return <code>true</code> if on OS X and osxfuse with a higher version than the minimum supported one is installed.
*/
@Override
public boolean isApplicable() {
return IS_MAC && Files.exists(Paths.get("/usr/local/lib/libosxfuse.2.dylib"));
return IS_MAC && Files.exists(Paths.get("/usr/local/lib/libosxfuse.2.dylib")) && installedVersionSupported();
}

public boolean installedVersionSupported() {
String versionString = getVersionString();
if (versionString == null) {
LOG.error("Did not find {} in document {}.", OSXFUSE_XML_VERSION_TEXT, OSXFUSE_VERSIONFILE_LOCATION);
return false;
}

Integer[] parsedVersion = Arrays.stream(versionString.split("\\.")).map(s -> Integer.valueOf(s)).toArray(Integer[]::new);
for (int i = 0; i < OSXFUSE_MINIMUM_SUPPORTED_VERSION.length && i < parsedVersion.length; i++) {
if (parsedVersion[i] < OSXFUSE_MINIMUM_SUPPORTED_VERSION[i]) {
return false;
} else if (parsedVersion[i] > OSXFUSE_MINIMUM_SUPPORTED_VERSION[i]) {
return true;
}
}

if (OSXFUSE_MINIMUM_SUPPORTED_VERSION.length <= parsedVersion.length) {
return true;
} else {
return false;
}
}


private String getVersionString() {
String version = null;
try (InputStream in = Files.newInputStream(OSXFUSE_VERSIONFILE_LOCATION, StandardOpenOption.READ)) {
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(in);
while (reader.hasNext()) {
reader.next();
if (reader.getEventType() == XMLStreamReader.CHARACTERS && OSXFUSE_XML_VERSION_TEXT.equalsIgnoreCase(reader.getText())) {
reader.next();
reader.next();
reader.next();
version = reader.getElementText();
}
}
} catch (XMLStreamException | FactoryConfigurationError e) {
LOG.error("Could not parse file {} to detect version of OSXFUSE.", OSXFUSE_VERSIONFILE_LOCATION);
} catch (IOException e1) {
LOG.error("Could not read file {} to detect version of OSXFUSE.", OSXFUSE_VERSIONFILE_LOCATION);
}
return version;
}

private static class MacMount extends AbstractMount {
Expand Down

0 comments on commit 03d92cf

Please sign in to comment.