Skip to content

Commit

Permalink
try to read Winfsp sxsDir and only on second attempt use InstallDir
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Oct 16, 2023
1 parent 070a921 commit b29ce3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ protected Set<String> combinedMountFlags() {
@Override
public Mount mount() throws MountFailedException {
var builder = Fuse.builder();
var libPath = WinfspUtil.getWinFspInstallDir() + "bin\\" + (OS_ARCH.contains("aarch64") ? "winfsp-a64.dll" : "winfsp-x64.dll");
var libPath = WinfspUtil.getWinFspSharedLibraryDir() + (OS_ARCH.contains("aarch64") ? "winfsp-a64.dll" : "winfsp-x64.dll");
builder.setLibraryPath(libPath);
var fuseAdapter = ReadWriteAdapter.create(builder.errno(), vfsRoot, FuseNioAdapter.DEFAULT_MAX_FILENAMELENGTH, FileNameTranscoder.transcoder());
try {
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/org/cryptomator/frontend/fuse/mount/WinfspUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -23,35 +24,47 @@ private WinfspUtil() {

private static final String REGSTR_TOKEN = "REG_SZ";
private static final String REG_WINFSP_KEY = "HKLM\\SOFTWARE\\WOW6432Node\\WinFsp";
private static final String REG_WINFSP_VALUE = "InstallDir";
private static final String REG_WINFSP_INSTALLDIR_VALUE = "InstallDir";
private static final String REG_WINFSP_SXSDIR_VALUE = "SxsDir";

private static final AtomicReference<String> cache = new AtomicReference<>(null);

static String getWinFspInstallDir() throws WinFspNotFoundException {
static String getWinFspSharedLibraryDir() throws WinFspNotFoundException {
if (cache.get() == null) {
cache.set(readWinFspInstallDirFromRegistry());
cache.set(readWinFspDirFromRegistry() + "bin\\");
}
return cache.get();
}

static String readWinFspInstallDirFromRegistry() {

static String readWinFspDirFromRegistry() {
try {
ProcessBuilder command = new ProcessBuilder("reg", "query", REG_WINFSP_KEY, "/v", REG_WINFSP_VALUE);
Process p = command.start();
ProcessHelper.waitForSuccess(p, 3, "`reg query`");
String result = p.inputReader(StandardCharsets.UTF_8).lines().filter(l -> l.contains(REG_WINFSP_VALUE)).findFirst().orElseThrow();
return result.substring(result.indexOf(REGSTR_TOKEN) + REGSTR_TOKEN.length()).trim();
} catch (TimeoutException | IOException | ProcessHelper.CommandFailedException e) {
try {
return readDataOfRegValue(REG_WINFSP_SXSDIR_VALUE);
} catch (NoSuchElementException e) {
return readDataOfRegValue(REG_WINFSP_INSTALLDIR_VALUE);
}
} catch (TimeoutException | IOException | ProcessHelper.CommandFailedException | NoSuchElementException e) {
throw new WinFspNotFoundException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new WinFspNotFoundException(e);
}
}

static String readDataOfRegValue(String regValue) throws IOException, InterruptedException, ProcessHelper.CommandFailedException, TimeoutException {
ProcessBuilder command = new ProcessBuilder("reg", "query", REG_WINFSP_KEY, "/v", regValue);
Process p = command.start();
ProcessHelper.waitForSuccess(p, 3, "`reg query`");
String result = p.inputReader(StandardCharsets.UTF_8).lines() //
.filter(l -> l.contains(regValue)) //
.findFirst().orElseThrow();
return result.substring(result.indexOf(REGSTR_TOKEN) + REGSTR_TOKEN.length()).trim();
}

static boolean isWinFspInstalled() {
try {
return Files.exists(Path.of(getWinFspInstallDir()));
return Files.exists(Path.of(getWinFspSharedLibraryDir()));
} catch (WinFspNotFoundException e) {
return false;
}
Expand All @@ -62,6 +75,10 @@ static class WinFspNotFoundException extends RuntimeException {
public WinFspNotFoundException(Exception e) {
super(e);
}

public WinFspNotFoundException(String msg) {
super(msg);
}
}

}

0 comments on commit b29ce3c

Please sign in to comment.