Pushing FuseEnvironment to other project, using fuseMount now in FuseNioAdapter

This commit is contained in:
infeo
2018-02-27 15:38:25 +01:00
parent d0d83c6833
commit f774829fb1
10 changed files with 57 additions and 389 deletions

View File

@@ -28,7 +28,7 @@
<cryptomator.cryptofs.version>1.4.5</cryptomator.cryptofs.version>
<cryptomator.webdav.version>1.0.3</cryptomator.webdav.version>
<cryptomator.jni.version>1.0.2</cryptomator.jni.version>
<cryptomator.fuse.version>0.1.0-SNAPSHOT</cryptomator.fuse.version>
<cryptomator.fuse.version>0.1.1-SNAPSHOT</cryptomator.fuse.version>
<commons-io.version>2.5</commons-io.version>
<commons-lang3.version>3.6</commons-lang3.version>

View File

@@ -1,28 +0,0 @@
package org.cryptomator.ui.model;
import java.nio.file.Path;
public interface FuseEnvironment {
void prepare() throws CommandFailedException;
String[] getMountParameters() throws CommandFailedException;
Path getFsRootPath();
/**
* TODO: implement it in subclasses!
* @throws CommandFailedException
*/
default void revealFsRootInFilesystemManager() throws CommandFailedException {
throw new CommandFailedException("Not implemented.");
}
void cleanUp();
default boolean supportsFuse(){
return false;
}
}

View File

@@ -1,46 +1,70 @@
package org.cryptomator.ui.model;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.common.settings.VaultSettings;
import org.cryptomator.cryptofs.CryptoFileSystem;
import org.cryptomator.frontend.fuse.AdapterFactory;
import org.cryptomator.frontend.fuse.mount.EnvironmentVariables;
import org.cryptomator.frontend.fuse.mount.FuseMount;
import org.cryptomator.frontend.fuse.mount.MountFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import java.nio.file.Paths;
@VaultModule.PerVault
public class FuseNioAdapter implements NioAdapter {
private static final Logger LOG = LoggerFactory.getLogger(FuseNioAdapter.class);
private final FuseEnvironment fuseEnv;
private org.cryptomator.frontend.fuse.FuseNioAdapter ffs;
private final FuseMount fuseMnt;
private final VaultSettings vaultSettings;
private final WindowsDriveLetters windowsDriveLetters;
private CryptoFileSystem cfs;
@Inject
public FuseNioAdapter(FuseEnvironment fuseEnv) {
this.fuseEnv = fuseEnv;
public FuseNioAdapter(VaultSettings vaultSettings, WindowsDriveLetters windowsDriveLetters) {
this.vaultSettings = vaultSettings;
this.windowsDriveLetters = windowsDriveLetters;
this.fuseMnt = MountFactory.createMountObject();
}
@Override
public void prepare(CryptoFileSystem fs) {
this.cfs = fs;
ffs = AdapterFactory.createReadWriteAdapter(fs.getPath("/"));
if (!(vaultSettings.mountPath().isNotNull().get() || SystemUtils.IS_OS_WINDOWS)) {
fuseMnt.useExtraMountDir();
}
}
@Override
public void mount() throws CommandFailedException {
try {
fuseEnv.prepare();
ffs.mount(fuseEnv.getFsRootPath(), false, false, fuseEnv.getMountParameters());
EnvironmentVariables envVars = EnvironmentVariables.create()
.withMountName(vaultSettings.mountName().getValue() + vaultSettings.getId())
.withMountPath(
SystemUtils.IS_OS_WINDOWS? computeWinDriveLetter() : vaultSettings.mountPath().getValue())
.build();
fuseMnt.mount(cfs.getPath("/"), envVars);
} catch (Exception e) {
throw new CommandFailedException("Unable to mount Filesystem", e);
}
}
private String computeWinDriveLetter(){
if(vaultSettings.winDriveLetter().get() != null){
return vaultSettings.winDriveLetter().getValue()+":\\";
}
else{
return windowsDriveLetters.getAvailableDriveLetters().iterator().next()+":\\";
}
}
@Override
public void reveal() throws CommandFailedException {
fuseEnv.revealFsRootInFilesystemManager();
//fuseMnt.reveal();
}
@Override
@@ -57,23 +81,36 @@ public class FuseNioAdapter implements NioAdapter {
this.unmountRaw();
}
private synchronized void unmountRaw() {
ffs.umount();
private synchronized void unmountRaw() throws CommandFailedException {
try {
fuseMnt.unmount();
} catch (org.cryptomator.frontend.fuse.mount.CommandFailedException e) {
throw new CommandFailedException(e);
}
}
@Override
public void stop() {
fuseEnv.cleanUp();
try {
fuseMnt.cleanUp();
} catch (org.cryptomator.frontend.fuse.mount.CommandFailedException e) {
LOG.warn(e.getMessage());
}
}
@Override
public String getMountUrl() {
return fuseEnv.getFsRootPath().toUri().toString();
public String getMountUri() {
return Paths.get(fuseMnt.getMountPath()).toUri().toString();
}
/**
* TODO: chang this to a real implementation
*
* @return
*/
@Override
public boolean isSupported() {
return fuseEnv.supportsFuse();
return true;
}
@Override

View File

@@ -1,118 +0,0 @@
package org.cryptomator.ui.model;
import org.cryptomator.common.settings.VaultSettings;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class LinuxFuseEnvironment implements FuseEnvironment{
private final VaultSettings vaultSettings;
private Path root;
@Inject
public LinuxFuseEnvironment(VaultSettings vaultSettings){
this.vaultSettings = vaultSettings;
}
@Override
public void prepare() throws CommandFailedException {
this.root = Paths.get(vaultSettings.mountPath().get() + vaultSettings.mountName().get()).toAbsolutePath();
try {
createVaultDirIfNotExist(root);
} catch (IOException e) {
e.printStackTrace();
throw new CommandFailedException(e);
}
}
private void createVaultDirIfNotExist(Path p) throws IOException {
try {
if (Files.exists(p)) {
if (Files.isDirectory(p)) {
if (Files.newDirectoryStream(p).iterator().hasNext()) {
return;
} else {
throw new DirectoryNotEmptyException("Directory not empty.");
}
}
} else {
Files.createDirectory(p);
}
} catch (IOException e) {
throw e;
}
}
@Override
public String[] getMountParameters() throws CommandFailedException {
ArrayList<String> mountOptions = new ArrayList<>(8);
mountOptions.add(("-oatomic_o_trunc"));
try {
mountOptions.add("-ouid=" + getUIdOrGID("uid"));
mountOptions.add("-ogid=" + getUIdOrGID("gid"));
} catch (IOException e) {
e.printStackTrace();
throw new CommandFailedException(e);
}
mountOptions.add("-oauto_unmount");
mountOptions.add("-ofsname=CryptoFs");
return mountOptions.toArray(new String [mountOptions.size()]);
}
private String getUIdOrGID(String idtype) throws IOException {
String id;
String parameter;
switch (idtype) {
case "uid":
parameter = "-u";
break;
case "gid":
parameter = "-g";
break;
default:
throw new IllegalArgumentException("Unkown ID type");
}
Process getId = new ProcessBuilder("sh", "-c", "id " + parameter).start();
Scanner s = new Scanner(getId.getInputStream()).useDelimiter("\\A");
try {
getId.waitFor(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
id = s.nextLine();
return id;
}
@Override
public Path getFsRootPath() {
return this.root;
}
@Override
public void revealFsRootInFilesystemManager() throws CommandFailedException {
throw new CommandFailedException("Not implemented.");
}
@Override
public void cleanUp() {
try {
Files.deleteIfExists(Paths.get(vaultSettings.mountPath().get() + vaultSettings.mountName().get()));
} catch (IOException e) {
//LOG.warn("Could not delete mount directory of vault " + vaultSettings.mountName().get());
e.printStackTrace();
}
}
@Override
public boolean supportsFuse() {
return true;
}
}

View File

@@ -1,118 +0,0 @@
package org.cryptomator.ui.model;
import org.cryptomator.common.settings.VaultSettings;
import javax.inject.Inject;
import java.io.IOException;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class MacFuseEnvironment implements FuseEnvironment {
private final VaultSettings vaultSettings;
private Path root;
@Inject
public MacFuseEnvironment(VaultSettings vaultSettings){
this.vaultSettings = vaultSettings;
}
@Override
public void prepare() throws CommandFailedException {
this.root = Paths.get(vaultSettings.mountPath().get() + vaultSettings.mountName().get()).toAbsolutePath();
try {
createVaultDirIfNotExist(root);
} catch (IOException e) {
throw new CommandFailedException(e);
}
}
private void createVaultDirIfNotExist(Path p) throws IOException {
try {
if (Files.exists(p)) {
if (Files.isDirectory(p)) {
if (Files.newDirectoryStream(p).iterator().hasNext()) {
return;
} else {
throw new DirectoryNotEmptyException("Directory not empty.");
}
}
} else {
Files.createDirectory(p);
}
} catch (IOException e) {
throw e;
}
}
@Override
public String[] getMountParameters() throws CommandFailedException {
ArrayList<String> mountOptions = new ArrayList<>(8);
mountOptions.add(("-oatomic_o_trunc"));
try {
mountOptions.add("-ouid=" + getUIdOrGID("uid"));
mountOptions.add("-ogid=" + getUIdOrGID("gid"));
} catch (IOException e) {
e.printStackTrace();
throw new CommandFailedException(e);
}
mountOptions.add("-ovolname=" + vaultSettings.mountName().get());
mountOptions.add("-oauto_xattr");
return mountOptions.toArray(new String [mountOptions.size()]);
}
private String getUIdOrGID(String idtype) throws IOException {
String id;
String parameter;
switch (idtype) {
case "uid":
parameter = "-u";
break;
case "gid":
parameter = "-g";
break;
default:
throw new IllegalArgumentException("Unkown ID type");
}
Process getId = new ProcessBuilder("sh", "-c", "id " + parameter).start();
Scanner s = new Scanner(getId.getInputStream()).useDelimiter("\\A");
try {
getId.waitFor(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
id = s.nextLine();
return id;
}
@Override
public Path getFsRootPath() {
return root;
}
@Override
public void revealFsRootInFilesystemManager() throws CommandFailedException {
throw new CommandFailedException("Not implemented.");
}
@Override
public void cleanUp() {
try {
Files.deleteIfExists(Paths.get(vaultSettings.mountPath().get() + vaultSettings.mountName().get()));
} catch (IOException e) {
//LOG.warn("Could not delete mount directory of vault " + vaultSettings.mountName().get());
e.printStackTrace();
}
}
@Override
public boolean supportsFuse() {
return false;
}
}

View File

@@ -20,7 +20,7 @@ public interface NioAdapter {
void stop();
String getMountUrl();
String getMountUri();
default boolean isSupported() {
return false;

View File

@@ -290,7 +290,7 @@ public class Vault {
}
public String getFilesystemRootUrl() {
return nioAdapter.getMountUrl();
return nioAdapter.getMountUri();
}
public String getId() {

View File

@@ -18,7 +18,6 @@ import org.cryptomator.common.settings.VaultSettings;
import dagger.Module;
import dagger.Provides;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
@Module
public class VaultModule {
@@ -57,40 +56,4 @@ public class VaultModule {
}
}
//TODO: ask sebi if this should be here
private final OS os = OS.getCurrentOS();
private enum OS {
WINDOWS,
LINUX,
MAC;
public static OS getCurrentOS() {
if (SystemUtils.IS_OS_WINDOWS) {
return WINDOWS;
} else if (SystemUtils.IS_OS_MAC) {
return MAC;
} else {
return LINUX;
}
}
}
@Provides
@VaultModule.PerVault
FuseEnvironment providesFuseEnvironment(WindowsFuseEnvironment windowsFuseEnvironment, LinuxFuseEnvironment linuxFuseEnvironment, MacFuseEnvironment macFuseEnvironment){
switch (os){
case LINUX:
return linuxFuseEnvironment;
case WINDOWS:
return windowsFuseEnvironment;
case MAC:
return macFuseEnvironment;
default:
//TODO: should be better something else returned?
return null;
}
}
}

View File

@@ -104,12 +104,13 @@ public class WebDavNioAdapter implements NioAdapter {
}
public synchronized String getMountUrl() {
public synchronized String getMountUri() {
return servlet.getServletRootUri().toString();
}
/**
* TODO: what to check wether it is implemented?
*
* @return
*/
@Override

View File

@@ -1,69 +0,0 @@
package org.cryptomator.ui.model;
import org.cryptomator.common.settings.VaultSettings;
import javax.inject.Inject;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
@VaultModule.PerVault
public class WindowsFuseEnvironment implements FuseEnvironment{
private static final String AUTOASSIGN_DRRIVE_LETTER = "*";
private final VaultSettings vaultSettings;
private final WindowsDriveLetters windowsDriveLetters;
private Path root;
@Inject
public WindowsFuseEnvironment(VaultSettings vaultSettings, WindowsDriveLetters windowsDriveLetters){
this.vaultSettings = vaultSettings;
this.windowsDriveLetters = windowsDriveLetters;
}
@Override
public void prepare() throws CommandFailedException {
if (vaultSettings.winDriveLetter().get().equals(AUTOASSIGN_DRRIVE_LETTER)) {
if (!windowsDriveLetters.getAvailableDriveLetters().isEmpty()) {
root= Paths.get(windowsDriveLetters.getAvailableDriveLetters().iterator().next() + ":\\").toAbsolutePath();
} else {
throw new CommandFailedException("No free drive letter to mount.");
}
} else {
root = Paths.get(vaultSettings.winDriveLetter().get() + ":\\").toAbsolutePath();
}
}
@Override
public String[] getMountParameters() throws CommandFailedException {
ArrayList<String> mountOptions = new ArrayList<>(8);
mountOptions.add(("-oatomic_o_trunc"));
mountOptions.add("-ouid=-1");
mountOptions.add("-ogid=-1");
mountOptions.add("-ovolname=" + vaultSettings.mountName().get());
mountOptions.add("-oFileInfoTimeout=-1");
return mountOptions.toArray(new String [mountOptions.size()]);
}
@Override
public Path getFsRootPath() {
return root;
}
@Override
public void revealFsRootInFilesystemManager() throws CommandFailedException {
throw new CommandFailedException("Not Implemented");
}
@Override
public void cleanUp() {
}
@Override
public boolean supportsFuse() {
return false;
}
}