Skip to content

Commit

Permalink
Merge pull request #7 from btiernay/master
Browse files Browse the repository at this point in the history
Added basic support for FUSE mount options.
  • Loading branch information
pron committed Oct 13, 2015

Verified

This commit was signed with the committer’s verified signature. The key has expired.
sbihel Simon Bihel
2 parents 44dedac + 6a11dc3 commit 2ff07d0
Showing 3 changed files with 63 additions and 9 deletions.
29 changes: 25 additions & 4 deletions src/main/java/co/paralleluniverse/fuse/Fuse.java
Original file line number Diff line number Diff line change
@@ -5,12 +5,15 @@
import java.nio.file.Files;
import java.nio.file.NotDirectoryException;
import java.nio.file.Path;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

import jnr.ffi.Struct;

public final class Fuse {
@@ -112,7 +115,7 @@ static LibFuse init() throws UnsatisfiedLinkError {
}
}

public static void mount(FuseFilesystem filesystem, Path mountPoint, boolean blocking, boolean debug) throws IOException {
public static void mount(FuseFilesystem filesystem, Path mountPoint, boolean blocking, boolean debug, Map<String, String> mountOptions) throws IOException {
mountPoint = mountPoint.toAbsolutePath().normalize().toRealPath();
if (!Files.isDirectory(mountPoint))
throw new NotDirectoryException(mountPoint.toString());
@@ -127,19 +130,21 @@ public static void mount(FuseFilesystem filesystem, Path mountPoint, boolean blo
filesystem.mount(mountPoint, blocking);

final String filesystemName = filesystem.getFuseName();
final String[] options = filesystem.getOptions();
final String[] options = toOptionsArray(mountOptions);
final String[] argv;
if (options == null)
argv = new String[debug ? 4 : 3];
else {
argv = new String[3 + options.length];
System.arraycopy(options, 0, argv, 2, options.length);
argv = new String[(debug ? 4 : 3) + options.length];
System.arraycopy(options, 0, argv, (debug ? 3 : 2), options.length);
}

argv[0] = filesystemName;
argv[1] = "-f";
if (debug)
argv[2] = "-d";
argv[argv.length - 1] = mountPoint.toString();

final LibFuse fuse = init();
final StructFuseOperations operations = new StructFuseOperations(jnr.ffi.Runtime.getRuntime(fuse), filesystem);

@@ -207,4 +212,20 @@ public static void unmount(Path mountPoint) throws IOException {
if (res != 0)
throw new FuseException(res);
}

private static String[] toOptionsArray(Map<String, String> options) {
if (options == null) {
return null;
}

int i = 0;
String[] values = new String[options.size() * 2];
for (Entry<String, String> entry : options.entrySet()) {
values[i++] = "-o";
values[i++] = entry.getKey()
+ (entry.getValue() == null ? "" : "=" + entry.getValue());
}

return values;
}
}
35 changes: 31 additions & 4 deletions src/main/java/co/paralleluniverse/javafs/JavaFS.java
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.util.Map;

import co.paralleluniverse.fuse.Fuse;

/**
@@ -11,6 +13,22 @@
* @author pron
*/
public final class JavaFS {

/**
* Mounts a filesystem.
*
* @param fs the filesystem
* @param mountPoint the path of the mount point
* @param readonly if {@code true}, mounts the filesystem as read-only
* @param log if {@code true}, all filesystem calls will be logged with juc logging.
* @param mountOptions the platform specific mount options (e.g. {@code ro}, {@code rw}, etc.). {@code null} for value-less options.
*/
public static void mount(FileSystem fs, Path mountPoint, boolean readonly, boolean log, Map<String, String> mountOptions) throws IOException {
if (readonly)
fs = new ReadOnlyFileSystem(fs);
Fuse.mount(new FuseFileSystemProvider(fs, log).log(log), mountPoint, false, log, mountOptions);
}

/**
* Mounts a filesystem.
*
@@ -20,19 +38,28 @@ public final class JavaFS {
* @param log if {@code true}, all filesystem calls will be logged with juc logging.
*/
public static void mount(FileSystem fs, Path mountPoint, boolean readonly, boolean log) throws IOException {
if (readonly)
fs = new ReadOnlyFileSystem(fs);
Fuse.mount(new FuseFileSystemProvider(fs, log).log(log), mountPoint, false, log);
mount(fs,mountPoint, readonly, log, null);
}

/**
* Mounts a filesystem.
*
* @param fs the filesystem
* @param mountPoint the path of the mount point
* @param mountOptions the platform specific mount options (e.g. {@code ro}, {@code rw}, etc.). {@code null} for value-less options.
*/
public static void mount(FileSystem fs, Path mountPoint, Map<String, String> mountOptions) throws IOException {
mount(fs, mountPoint, false, false, mountOptions);
}

/**
* Mounts a filesystem.
*
* @param fs the filesystem
* @param mountPoint the path of the mount point
*/
public static void mount(FileSystem fs, Path mountPoint) throws IOException {
mount(fs, mountPoint, false, false);
mount(fs, mountPoint, false, false, null);
}

/**
8 changes: 7 additions & 1 deletion src/test/java/co/paralleluniverse/javafs/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package co.paralleluniverse.javafs;

import com.google.common.jimfs.Jimfs;

import java.nio.file.FileSystem;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

public class Main {
public static void main(final String... args) throws Exception {
@@ -23,7 +26,10 @@ public static void main(final String... args) throws Exception {
System.out.println("Mounting filesystem " + fs + " at " + mountPoint + (readonly ? " READONLY" : ""));
System.out.println("========================");

JavaFS.mount(fs, Paths.get(mountPoint), readonly, true);
Map<String, String> options = new HashMap<>();
options.put("fsname", fs.getClass().getSimpleName() + "@" + System.currentTimeMillis());

JavaFS.mount(fs, Paths.get(mountPoint), readonly, true, options);
Thread.sleep(Long.MAX_VALUE);
} catch (IllegalArgumentException e) {
System.err.println("Usage: JavaFS [-r] <mountpoint> [<zipfile>]");

0 comments on commit 2ff07d0

Please sign in to comment.