Skip to content

Commit

Permalink
Added Linux mount script (issue #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
overheadhunter committed Feb 6, 2017
1 parent f5f1903 commit 5f7a0a2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,40 @@

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
import javax.inject.Singleton;

import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
class LinuxGvfsMounter implements MounterStrategy {

private static final Logger LOG = LoggerFactory.getLogger(LinuxGvfsMounter.class);
private static final String DEFAULT_GVFS_SCHEME = "dav";

@Inject
LinuxGvfsMounter() {
}

@Override
public boolean isApplicable() {
if (!SystemUtils.IS_OS_LINUX) {
// fail fast (non-blocking)
return false;
}

// check if gvfs is installed:
assert SystemUtils.IS_OS_LINUX;
try {
ProcessBuilder checkDependencies = new ProcessBuilder("which", "gvfs-mount", "xdg-open");
Process checkDependenciesProcess = checkDependencies.start();
ProcessBuilder checkDependenciesCmd = new ProcessBuilder("which", "gvfs-mount", "xdg-open");
Process checkDependenciesProcess = checkDependenciesCmd.start();
ProcessUtil.waitFor(checkDependenciesProcess, 500, TimeUnit.MILLISECONDS);
ProcessUtil.assertExitValue(checkDependenciesProcess, 0);
return true;
} catch (CommandFailedException | IOException e) {
Expand All @@ -36,8 +45,61 @@ public boolean isApplicable() {

@Override
public Mount mount(URI uri, Map<MountParam, String> mountParams) throws CommandFailedException {
// TODO Auto-generated method stub
throw new CommandFailedException("not yet implemented.");
try {
URI schemeCorrectedUri = new URI(mountParams.getOrDefault(MountParam.PREFERRED_GVFS_SCHEME, DEFAULT_GVFS_SCHEME), uri.getSchemeSpecificPart(), null);
ProcessBuilder mountCmd = new ProcessBuilder("sh", "-c", "gvfs-mount \"" + schemeCorrectedUri.toASCIIString() + "\"");
Process mountProcess = mountCmd.start();
ProcessUtil.waitFor(mountProcess, 5, TimeUnit.SECONDS);
ProcessUtil.assertExitValue(mountProcess, 0);
LOG.debug("Mounted {}", schemeCorrectedUri.toASCIIString());
return new MountImpl(schemeCorrectedUri);
} catch (IOException e) {
throw new CommandFailedException(e);
} catch (URISyntaxException e) {
throw new IllegalStateException("URI constructed from elements known to be valid.", e);
}
}

private static class MountImpl implements Mount {

private final ProcessBuilder revealCmd;
private ProcessBuilder isMountedCmd;
private final ProcessBuilder unmountCmd;

private MountImpl(URI uri) {
this.revealCmd = new ProcessBuilder("sh", "-c", "gvfs-open \"" + uri.toASCIIString() + "\"");
this.isMountedCmd = new ProcessBuilder("sh", "-c", "test `gvfs-mount --list | grep \"" + uri.toASCIIString() + "\" | wc -l` -eq 1");
this.unmountCmd = new ProcessBuilder("sh", "-c", "gvfs-mount -u \"" + uri.toASCIIString() + "\"");
}

@Override
public void unmount() throws CommandFailedException {
try {
Process isMountedProcess = isMountedCmd.start();
ProcessUtil.waitFor(isMountedProcess, 1, TimeUnit.SECONDS);

if (isMountedProcess.exitValue() == 0) {
// only unmount if volume is still mounted, noop otherwise
Process proc = unmountCmd.start();
ProcessUtil.waitFor(proc, 1, TimeUnit.SECONDS);
ProcessUtil.assertExitValue(proc, 0);
}
} catch (IOException e) {
throw new CommandFailedException(e);
}
}

@Override
public void reveal() throws CommandFailedException {
try {
Process proc = revealCmd.start();
ProcessUtil.waitFor(proc, 2, TimeUnit.SECONDS);
ProcessUtil.assertExitValue(proc, 0);
} catch (IOException e) {
throw new CommandFailedException(e);
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void main(String[] args) throws IOException, CommandFailedExceptio

Map<MountParam, String> mountOptions = new HashMap<>();
mountOptions.put(MountParam.WIN_DRIVE_LETTER, "X:");
mountOptions.put(MountParam.PREFERRED_GVFS_SCHEME, "dav");
Mount mount = servlet.mount(mountOptions);
mount.reveal();

Expand Down

0 comments on commit 5f7a0a2

Please sign in to comment.