1
0
mirror of https://github.com/fuse-friends/fuse-native synced 2024-10-27 18:34:01 +00:00

Merge pull request #7 from raymond-h/master

Windows support using Dokany (Dokan fork)
This commit is contained in:
Mathias Buus 2015-08-08 22:48:46 +02:00
commit e7f5308ee5
7 changed files with 339 additions and 108 deletions

View File

@ -11,10 +11,18 @@ any buffer copys in read/write. It also supports unmount and mouting of multiple
## Requirements
You need to have FUSE installed
You need to have FUSE installed (or Dokany on Windows)
* On Linux/Ubuntu `sudo apt-get install libfuse-dev`
* On OSX install [OSXFuse](http://osxfuse.github.com/) and pkg-config, `brew install pkg-config`
* On Windows see `Windows` down below...
### Windows
**WARNING**: Dokany is still not quite stable. It can cause BSODs. Be careful.
Using this on Windows is slightly more complicated. You need to install [Dokany](https://github.com/dokan-dev/dokany) (for `dokanfuse.lib`, `dokanctl.exe`, driver and service) **and** clone its repo (for the headers).
Once the Dokany repo is cloned, you also need to set environment variable `DOKAN_INSTALL_DIR` to the path to `DokenLibrary` of your Dokany installaton, and `DOKAN_FUSE_INCLUDE` to the path to `*dokany repo*\dokan_fuse\include`.
## Usage

85
abstractions.cc Normal file
View File

@ -0,0 +1,85 @@
#include "abstractions.h"
#ifdef __APPLE__
#include <unistd.h>
#include <sys/wait.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void thread_create (abstr_thread_t* thread, thread_fn fn, void* data) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(thread, &attr, fn, data);
}
void thread_join (abstr_thread_t thread) {
pthread_join(thread, NULL);
}
void fusermount (char *path) {
char *argv[] = {(char *) "umount", path, NULL};
pid_t cpid = vfork();
if (cpid > 0) waitpid(cpid, NULL, 0);
else execvp(argv[0], argv);
}
#elif defined(_WIN32)
HANDLE mutex = CreateMutex(NULL, false, NULL);
void thread_create (HANDLE* thread, thread_fn fn, void* data) {
*thread = CreateThread(NULL, 0, fn, data, 0, NULL);
}
void thread_join (HANDLE thread) {
WaitForSingleObject(thread, INFINITE);
}
void fusermount (char *path) {
char* dokanPath = getenv("DOKAN_INSTALL_DIR");
char cmdLine[MAX_PATH];
if(dokanPath) sprintf(cmdLine, "\"%s/dokanctl.exe\" /u %s", dokanPath, path);
else sprintf(cmdLine, "dokanctl.exe /u %s", path);
STARTUPINFO info = {sizeof(info)};
PROCESS_INFORMATION procInfo;
CreateProcess(NULL, cmdLine, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &info, &procInfo);
WaitForSingleObject(procInfo.hProcess, INFINITE);
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
// dokanctl.exe requires admin permissions for some reason, so if node is not run as admin,
// it'll fail to create the process for unmounting. The path will be unmounted once
// the process is killed, however, so there's that!
}
#else
#include <unistd.h>
#include <sys/wait.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void thread_create (abstr_thread_t* thread, thread_fn fn, void* data) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(thread, &attr, fn, data);
}
void thread_join (abstr_thread_t thread) {
pthread_join(thread, NULL);
}
void fusermount (char *path) {
char *argv[] = {(char *) "fusermount", (char *) "-q", (char *) "-u", path, NULL};
pid_t cpid = vfork();
if (cpid > 0) waitpid(cpid, NULL, 0);
else execvp(argv[0], argv);
}
#endif

126
abstractions.h Normal file
View File

@ -0,0 +1,126 @@
#include <nan.h>
#define FUSE_USE_VERSION 29
#ifdef __APPLE__
// OS X
#include <sys/mount.h>
#include <semaphore.h>
#include <dispatch/dispatch.h>
#include <fuse_lowlevel.h>
#define FUSE_OFF_T off_t
typedef dispatch_semaphore_t bindings_sem_t;
NAN_INLINE static int semaphore_init (dispatch_semaphore_t *sem) {
*sem = dispatch_semaphore_create(0);
return *sem == NULL ? -1 : 0;
}
NAN_INLINE static void semaphore_wait (dispatch_semaphore_t *sem) {
dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
}
NAN_INLINE static void semaphore_signal (dispatch_semaphore_t *sem) {
dispatch_semaphore_signal(*sem);
}
extern pthread_mutex_t mutex;
NAN_INLINE static void mutex_lock (pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);
}
NAN_INLINE static void mutex_unlock (pthread_mutex_t *mutex) {
pthread_mutex_unlock(mutex);
}
typedef pthread_t abstr_thread_t;
typedef void* thread_fn_rtn_t;
#elif defined(_WIN32)
#include <windows.h>
#include <io.h>
#include <winsock2.h>
typedef HANDLE bindings_sem_t;
NAN_INLINE static int semaphore_init (HANDLE *sem) {
*sem = CreateSemaphore(NULL, 0, 10, NULL);
return *sem == NULL ? -1 : 0;
}
NAN_INLINE static void semaphore_wait (HANDLE *sem) {
WaitForSingleObject(*sem, INFINITE);
}
NAN_INLINE static void semaphore_signal (HANDLE *sem) {
ReleaseSemaphore(*sem, 1, NULL);
}
extern HANDLE mutex;
NAN_INLINE static void mutex_lock (HANDLE *mutex) {
WaitForSingleObject(*mutex, INFINITE);
}
NAN_INLINE static void mutex_unlock (HANDLE *mutex) {
ReleaseMutex(*mutex);
}
typedef HANDLE abstr_thread_t;
typedef DWORD thread_fn_rtn_t;
#define fuse_session_remove_chan(x)
#define stat _stati64
#else
// Linux and whatnot
#include <sys/mount.h>
#include <semaphore.h>
#include <fuse_lowlevel.h>
#define FUSE_OFF_T off_t
typedef sem_t bindings_sem_t;
NAN_INLINE static int semaphore_init (sem_t *sem) {
return sem_init(sem, 0, 0);
}
NAN_INLINE static void semaphore_wait (sem_t *sem) {
sem_wait(sem);
}
NAN_INLINE static void semaphore_signal (sem_t *sem) {
sem_post(sem);
}
extern pthread_mutex_t mutex;
NAN_INLINE static void mutex_lock (pthread_mutex_t *mutex) {
pthread_mutex_lock(mutex);
}
NAN_INLINE static void mutex_unlock (pthread_mutex_t *mutex) {
pthread_mutex_unlock(mutex);
}
typedef pthread_t abstr_thread_t;
typedef void* thread_fn_rtn_t;
#endif
typedef thread_fn_rtn_t(*thread_fn)(void*);
void thread_create (abstr_thread_t*, thread_fn, void*);
void thread_join (abstr_thread_t);
void fusermount (char*);

View File

@ -1,17 +1,48 @@
{
"targets": [
{
"target_name": "fuse_bindings",
"sources": [ "fuse-bindings.cc" ],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"<!@(pkg-config fuse --cflags-only-I | sed s/-I//g)"
],
"link_settings": {
"libraries": [
"<!@(pkg-config --libs-only-L --libs-only-l fuse)"
]
}
}
]
"targets": [{
"target_name": "fuse_bindings",
"sources": ["fuse-bindings.cc", "abstractions.cc"],
"include_dirs": [
"<!(node -e \"require('nan')\")"
],
"conditions": [
['OS!="win"', {
"include_dirs": [
"<!@(pkg-config fuse --cflags-only-I | sed s/-I//g)"
],
"link_settings": {
"libraries": [
"<!@(pkg-config --libs-only-L --libs-only-l fuse)"
]
}
}],
['OS=="win"', {
"include_dirs": [
"$(DOKAN_FUSE_INCLUDE)",
"$(INCLUDE)"
],
"link_settings": {
"libraries": [
"<!(echo %DOKAN_INSTALL_DIR%)/dokanfuse.lib"
]
}
}]
],
"configurations": {
"Debug": {
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": 2
}
}
},
"Release": {
"msvs_settings": {
"VCCLCompilerTool": {
"RuntimeLibrary": 2
}
}
}
}
}]
}

View File

@ -1,6 +1,8 @@
var fuse = require('./')
fuse.mount('./mnt', {
var mountPath = process.platform !== 'win32' ? './mnt' : 'M:\\';
fuse.mount(mountPath, {
readdir: function (path, cb) {
console.log('readdir(%s)', path)
if (path === '/') return cb(0, ['test'])
@ -15,8 +17,8 @@ fuse.mount('./mnt', {
ctime: new Date(),
size: 100,
mode: 16877,
uid: process.getuid(),
gid: process.getgid()
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0
})
return
}
@ -28,8 +30,8 @@ fuse.mount('./mnt', {
ctime: new Date(),
size: 12,
mode: 33188,
uid: process.getuid(),
gid: process.getgid()
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0
})
return
}
@ -49,11 +51,12 @@ fuse.mount('./mnt', {
}
}, function (err) {
if (err) throw err
console.log('filesystem mounted on ./mnt')
console.log('filesystem mounted on ' + mountPath)
})
process.on('SIGINT', function () {
fuse.unmount('./mnt', function () {
fuse.unmount(mountPath, function () {
console.log('filesystem at ' + mountPath + ' unmounted')
process.exit()
})
})

View File

@ -4,19 +4,14 @@
#include <fuse.h>
#include <fuse_opt.h>
#include <fuse_lowlevel.h>
#include <semaphore.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef __APPLE__
#include <dispatch/dispatch.h>
#endif
#include "abstractions.h"
using namespace v8;
@ -56,7 +51,6 @@ enum bindings_ops_t {
OP_DESTROY
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static Persistent<Function> buffer_constructor;
static NanCallback *callback_constructor;
static struct stat empty_stat;
@ -73,12 +67,8 @@ struct bindings_t {
// fuse data
char mnt[1024];
char mntopts[1024];
pthread_t thread;
#ifdef __APPLE__
dispatch_semaphore_t semaphore;
#else
sem_t semaphore;
#endif
abstr_thread_t thread;
bindings_sem_t semaphore;
uv_async_t async;
// methods
@ -124,8 +114,8 @@ struct bindings_t {
struct fuse_file_info *info;
char *path;
char *name;
off_t offset;
off_t length;
FUSE_OFF_T offset;
FUSE_OFF_T length;
void *data; // various structs
int mode;
int dev;
@ -138,33 +128,6 @@ static bindings_t *bindings_mounted[1024];
static int bindings_mounted_count = 0;
static bindings_t *bindings_current = NULL;
#ifdef __APPLE__
NAN_INLINE static int semaphore_init (dispatch_semaphore_t *sem) {
*sem = dispatch_semaphore_create(0);
return *sem == NULL ? -1 : 0;
}
NAN_INLINE static void semaphore_wait (dispatch_semaphore_t *sem) {
dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
}
NAN_INLINE static void semaphore_signal (dispatch_semaphore_t *sem) {
dispatch_semaphore_signal(*sem);
}
#else
NAN_INLINE static int semaphore_init (sem_t *sem) {
return sem_init(sem, 0, 0);
}
NAN_INLINE static void semaphore_wait (sem_t *sem) {
sem_wait(sem);
}
NAN_INLINE static void semaphore_signal (sem_t *sem) {
sem_post(sem);
}
#endif
static bindings_t *bindings_find_mounted (char *path) {
for (int i = 0; i < bindings_mounted_count; i++) {
bindings_t *b = bindings_mounted[i];
@ -176,23 +139,16 @@ static bindings_t *bindings_find_mounted (char *path) {
}
static void bindings_fusermount (char *path) {
#ifdef __APPLE__
char *argv[] = {(char *) "umount", path, NULL};
#else
char *argv[] = {(char *) "fusermount", (char *) "-q", (char *) "-u", path, NULL};
#endif
pid_t cpid = vfork();
if (cpid > 0) waitpid(cpid, NULL, 0);
else execvp(argv[0], argv);
fusermount(path);
}
static void bindings_unmount (char *path) {
pthread_mutex_lock(&mutex);
mutex_lock(&mutex);
bindings_t *b = bindings_find_mounted(path);
if (b != NULL) b->gc = 1;
bindings_fusermount(path);
if (b != NULL) pthread_join(b->thread, NULL);
pthread_mutex_unlock(&mutex);
if (b != NULL) thread_join(b->thread);
mutex_unlock(&mutex);
}
@ -236,7 +192,7 @@ static int bindings_mknod (const char *path, mode_t mode, dev_t dev) {
return bindings_call(b);
}
static int bindings_truncate (const char *path, off_t size) {
static int bindings_truncate (const char *path, FUSE_OFF_T size) {
bindings_t *b = bindings_get_context();
b->op = OP_TRUNCATE;
@ -246,7 +202,7 @@ static int bindings_truncate (const char *path, off_t size) {
return bindings_call(b);
}
static int bindings_ftruncate (const char *path, off_t size, struct fuse_file_info *info) {
static int bindings_ftruncate (const char *path, FUSE_OFF_T size, struct fuse_file_info *info) {
bindings_t *b = bindings_get_context();
b->op = OP_FTRUNCATE;
@ -310,7 +266,7 @@ static int bindings_fsyncdir (const char *path, int datasync, struct fuse_file_i
return bindings_call(b);
}
static int bindings_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *info) {
static int bindings_readdir (const char *path, void *buf, fuse_fill_dir_t filler, FUSE_OFF_T offset, struct fuse_file_info *info) {
bindings_t *b = bindings_get_context();
b->op = OP_READDIR;
@ -441,7 +397,7 @@ static int bindings_opendir (const char *path, struct fuse_file_info *info) {
return bindings_call(b);
}
static int bindings_read (const char *path, char *buf, size_t len, off_t offset, struct fuse_file_info *info) {
static int bindings_read (const char *path, char *buf, size_t len, FUSE_OFF_T offset, struct fuse_file_info *info) {
bindings_t *b = bindings_get_context();
b->op = OP_READ;
@ -454,7 +410,7 @@ static int bindings_read (const char *path, char *buf, size_t len, off_t offset,
return bindings_call(b);
}
static int bindings_write (const char *path, const char *buf, size_t len, off_t offset, struct fuse_file_info * info) {
static int bindings_write (const char *path, const char *buf, size_t len, FUSE_OFF_T offset, struct fuse_file_info * info) {
bindings_t *b = bindings_get_context();
b->op = OP_WRITE;
@ -637,12 +593,12 @@ static void bindings_free (bindings_t *b) {
}
static void bindings_on_close (uv_handle_t *handle) {
pthread_mutex_lock(&mutex);
mutex_lock(&mutex);
bindings_free((bindings_t *) handle->data);
pthread_mutex_unlock(&mutex);
mutex_unlock(&mutex);
}
static void *bindings_thread (void *data) {
static thread_fn_rtn_t bindings_thread (void *data) {
bindings_t *b = (bindings_t *) data;
struct fuse_operations ops = { };
@ -713,7 +669,7 @@ static void *bindings_thread (void *data) {
uv_close((uv_handle_t*) &(b->async), &bindings_on_close);
return NULL;
return 0;
}
NAN_INLINE static Local<Date> bindings_get_date (struct timespec *out) {
@ -730,6 +686,16 @@ NAN_INLINE static void bindings_set_date (struct timespec *out, Local<Date> date
out->tv_nsec = ns;
}
NAN_INLINE static Local<Date> bindings_get_date (time_t *out) {
return NanNew<Date>(*out * 1000.0);
}
NAN_INLINE static void bindings_set_date (time_t *out, Local<Date> date) {
double ms = date->NumberValue();
time_t secs = (time_t)(ms / 1000.0);
*out = secs;
}
NAN_INLINE static void bindings_set_stat (struct stat *stat, Local<Object> obj) {
if (obj->Has(NanNew<String>("dev"))) stat->st_dev = obj->Get(NanNew<String>("dev"))->NumberValue();
if (obj->Has(NanNew<String>("ino"))) stat->st_ino = obj->Get(NanNew<String>("ino"))->NumberValue();
@ -739,12 +705,18 @@ NAN_INLINE static void bindings_set_stat (struct stat *stat, Local<Object> obj)
if (obj->Has(NanNew<String>("gid"))) stat->st_gid = obj->Get(NanNew<String>("gid"))->NumberValue();
if (obj->Has(NanNew<String>("rdev"))) stat->st_rdev = obj->Get(NanNew<String>("rdev"))->NumberValue();
if (obj->Has(NanNew<String>("size"))) stat->st_size = obj->Get(NanNew<String>("size"))->NumberValue();
if (obj->Has(NanNew<String>("blksize"))) stat->st_blksize = obj->Get(NanNew<String>("blksize"))->NumberValue();
#ifndef _WIN32
if (obj->Has(NanNew<String>("blocks"))) stat->st_blocks = obj->Get(NanNew<String>("blocks"))->NumberValue();
if (obj->Has(NanNew<String>("blksize"))) stat->st_blksize = obj->Get(NanNew<String>("blksize"))->NumberValue();
#endif
#ifdef __APPLE__
if (obj->Has(NanNew<String>("mtime"))) bindings_set_date(&stat->st_mtimespec, obj->Get(NanNew("mtime")).As<Date>());
if (obj->Has(NanNew<String>("ctime"))) bindings_set_date(&stat->st_ctimespec, obj->Get(NanNew("ctime")).As<Date>());
if (obj->Has(NanNew<String>("atime"))) bindings_set_date(&stat->st_atimespec, obj->Get(NanNew("atime")).As<Date>());
#elif defined(_WIN32)
if (obj->Has(NanNew<String>("mtime"))) bindings_set_date(&stat->st_mtime, obj->Get(NanNew("mtime")).As<Date>());
if (obj->Has(NanNew<String>("ctime"))) bindings_set_date(&stat->st_ctime, obj->Get(NanNew("ctime")).As<Date>());
if (obj->Has(NanNew<String>("atime"))) bindings_set_date(&stat->st_atime, obj->Get(NanNew("atime")).As<Date>());
#else
if (obj->Has(NanNew<String>("mtime"))) bindings_set_date(&stat->st_mtim, obj->Get(NanNew("mtime")).As<Date>());
if (obj->Has(NanNew<String>("ctime"))) bindings_set_date(&stat->st_ctim, obj->Get(NanNew("ctime")).As<Date>());
@ -810,7 +782,7 @@ NAN_METHOD(OpCallback) {
case OP_READLINK: {
if (args.Length() > 2 && args[2]->IsString()) {
NanUtf8String path(args[2]);
stpcpy((char *) b->data, *path);
strcpy((char *) b->data, *path);
}
}
break;
@ -1065,7 +1037,11 @@ static void bindings_dispatch (uv_async_t* handle, int status) {
return;
case OP_UTIMENS: {
#ifdef _WIN32
time_t *tv = (time_t *) b->data;
#else
struct timespec *tv = (struct timespec *) b->data;
#endif
Local<Value> tmp[] = {NanNew<String>(b->path), bindings_get_date(tv), bindings_get_date(tv + 1), callback};
bindings_call_op(b, b->ops_utimens, 4, tmp);
}
@ -1119,15 +1095,15 @@ NAN_METHOD(Mount) {
if (!args[0]->IsString()) return NanThrowError("mnt must be a string");
pthread_mutex_lock(&mutex);
mutex_lock(&mutex);
int index = bindings_alloc();
pthread_mutex_unlock(&mutex);
mutex_unlock(&mutex);
if (index == -1) return NanThrowError("You cannot mount more than 1024 filesystem in one process");
pthread_mutex_lock(&mutex);
mutex_lock(&mutex);
bindings_t *b = bindings_mounted[index];
pthread_mutex_unlock(&mutex);
mutex_unlock(&mutex);
memset(&empty_stat, 0, sizeof(empty_stat));
memset(b, 0, sizeof(bindings_t));
@ -1172,8 +1148,8 @@ NAN_METHOD(Mount) {
Local<Value> tmp[] = {NanNew<Number>(index), NanNew<FunctionTemplate>(OpCallback)->GetFunction()};
b->callback = new NanCallback(callback_constructor->Call(2, tmp).As<Function>());
stpcpy(b->mnt, *path);
stpcpy(b->mntopts, "-o");
strcpy(b->mnt, *path);
strcpy(b->mntopts, "-o");
Local<Array> options = ops->Get(NanNew<String>("options")).As<Array>();
if (options->IsArray()) {
@ -1188,9 +1164,7 @@ NAN_METHOD(Mount) {
uv_async_init(uv_default_loop(), &(b->async), (uv_async_cb) bindings_dispatch);
b->async.data = b;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&(b->thread), &attr, bindings_thread, b);
thread_create(&(b->thread), bindings_thread, b);
NanReturnUndefined();
}
@ -1247,7 +1221,7 @@ NAN_METHOD(Unmount) {
Local<Function> callback = args[1].As<Function>();
char *path_alloc = (char *) malloc(1024);
stpcpy(path_alloc, *path);
strcpy(path_alloc, *path);
NanAsyncQueueWorker(new UnmountWorker(new NanCallback(callback), path_alloc));
NanReturnUndefined();
@ -1261,4 +1235,4 @@ void Init(Handle<Object> exports) {
exports->Set(NanNew("populateContext"), NanNew<FunctionTemplate>(PopulateContext)->GetFunction());
}
NODE_MODULE(fuse_bindings, Init)
NODE_MODULE(fuse_bindings, Init)

View File

@ -68,14 +68,18 @@ exports.mount = function (mnt, ops, cb) {
}
var mount = function () {
fs.stat(mnt, function (err, stat) {
if (err) return cb(new Error('Mountpoint does not exist'))
if (!stat.isDirectory()) return cb(new Error('Mountpoint is not a directory'))
fs.stat(path.join(mnt, '..'), function (_, parent) {
if (parent && parent.dev !== stat.dev) return cb(new Error('Mountpoint in use'))
fuse.mount(mnt, ops)
})
})
// TODO: I got a feeling this can be done better
if(os.platform() !== 'win32') {
fs.stat(mnt, function (err, stat) {
if (err) return cb(new Error('Mountpoint does not exist'))
if (!stat.isDirectory()) return cb(new Error('Mountpoint is not a directory'))
fs.stat(path.join(mnt, '..'), function (_, parent) {
if (parent && parent.dev !== stat.dev) return cb(new Error('Mountpoint in use'))
fuse.mount(mnt, ops)
})
})
}
else fuse.mount(mnt, ops)
}
if (!ops.force) return mount()