Skip to content

Commit

Permalink
added unmount + better example
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewosh committed Aug 1, 2019
1 parent d5747dd commit 9121e88
Show file tree
Hide file tree
Showing 3 changed files with 352 additions and 55 deletions.
84 changes: 84 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const Fuse = require('./')

const ops = {
readdir: function (path, cb) {
console.log('readdir(%s)', path)
if (path === '/') return process.nextTick(cb, 0, ['test'])
return process.nextTick(cb, 0)
},
/*
access: function (path, cb) {
return process.nextTick(cb, 0)
},
*/
getattr: function (path, cb) {
console.log('getattr(%s)', path)
if (path === '/') {
return process.nextTick(cb, 0, {
mtime: new Date(),
atime: new Date(),
ctime: new Date(),
nlink: 1,
size: 100,
mode: 16877,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0
})
}

if (path === '/test') {
return process.nextTick(cb, 0, {
mtime: new Date(),
atime: new Date(),
ctime: new Date(),
nlink: 1,
size: 12,
mode: 33188,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0
})
}

return process.nextTick(cb, Fuse.ENOENT)
},
open: function (path, flags, cb) {
console.log('open(%s, %d)', path, flags)
return process.nextTick(cb, 0, 42) // 42 is an fd
},
read: function (path, fd, buf, len, pos, cb) {
console.log('read(%s, %d, %d, %d)', path, fd, len, pos)
var str = 'hello world\n'.slice(pos)
if (!str) return process.nextTick(cb, 0)
buf.write(str)
return process.nextTick(cb, str.length)
}
}

const fuse = new Fuse('./mnt', ops, { debug: true })
fuse.mount(err => {
if (err) throw err
console.log('filesystem mounted on ' + fuse.mnt)
})

process.on('SIGINT', function () {
fuse.unmount(err => {
if (err) {
console.log('filesystem at ' + fuse.mnt + ' not unmounted', err)
} else {
console.log('filesystem at ' + fuse.mnt + ' unmounted')
}
})
})

function emptyStat (mode) {
return {
mtime: new Date(),
atime: new Date(),
ctime: new Date(),
nlink: 1,
size: 100,
mode: mode,
uid: process.getuid ? process.getuid() : 0,
gid: process.getgid ? process.getgid() : 0
}
}
23 changes: 22 additions & 1 deletion fuse-native.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ typedef struct {
napi_ref on_symlink;

struct fuse *fuse;
struct fuse_chan *ch;
bool mounted;
uv_async_t async;
} fuse_thread_t;

Expand Down Expand Up @@ -637,7 +639,7 @@ NAPI_METHOD(fuse_native_signal_readdir) {
}

NAPI_METHOD(fuse_native_mount) {
NAPI_ARGV(10)
NAPI_ARGV(11)

NAPI_ARGV_UTF8(mnt, 1024, 0);
NAPI_ARGV_UTF8(mntopts, 1024, 1);
Expand Down Expand Up @@ -711,6 +713,8 @@ NAPI_METHOD(fuse_native_mount) {
struct fuse *fuse = fuse_new(ch, &args, &ops, sizeof(struct fuse_operations), ft);

ft->fuse = fuse;
ft->ch = ch;
ft->mounted = true;

if (fuse == NULL) {
napi_throw_error(env, "fuse failed", "fuse failed");
Expand All @@ -723,10 +727,27 @@ NAPI_METHOD(fuse_native_mount) {
return NULL;
}

NAPI_METHOD(fuse_native_unmount) {
NAPI_ARGV(2)
NAPI_ARGV_UTF8(mnt, 1024, 0);
NAPI_ARGV_BUFFER_CAST(fuse_thread_t *, ft, 1);

if (ft != NULL && ft->mounted) {
fuse_unmount(mnt, ft->ch);
printf("joining\n");
pthread_join(ft->thread, NULL);
printf("joined\n");
}
ft->mounted = false;

return NULL;
}

NAPI_INIT() {
pthread_key_create(&(thread_locals_key), NULL); // TODO: add destructor

NAPI_EXPORT_FUNCTION(fuse_native_mount)
NAPI_EXPORT_FUNCTION(fuse_native_unmount)
NAPI_EXPORT_FUNCTION(fuse_native_signal_path)
NAPI_EXPORT_FUNCTION(fuse_native_signal_stat)
NAPI_EXPORT_FUNCTION(fuse_native_signal_statfs)
Expand Down
Loading

0 comments on commit 9121e88

Please sign in to comment.