-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuse-native.c
201 lines (142 loc) · 4.32 KB
/
fuse-native.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#define FUSE_USE_VERSION 29
#include "semaphore.h"
#include <uv.h>
#include <node_api.h>
#include <napi-macros.h>
#include <stdio.h>
#include <stdlib.h>
#include <fuse.h>
#include <fuse_opt.h>
#include <fuse_lowlevel.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>
typedef struct {
napi_env env;
pthread_t thread;
pthread_attr_t attr;
napi_ref ctx;
napi_ref on_op;
struct fuse *fuse;
uv_async_t async;
} fuse_thread_t;
typedef struct {
int hello;
fuse_thread_t *fuse;
fuse_native_semaphore_t sem;
uv_async_t async;
// char *reply;
} fuse_thread_locals_t;
static pthread_key_t thread_locals_key;
// static fuse_thread_locals_t * thread_local_map[1024];
// static int thread_local_map_length = 0;
// static int get_free_thread_id () {
// for (int i = 0; i < thread_local_map_length; i++) {
// if (thread_local_map[i] == NULL) return i;
// }
// return thread_local_map_length++;
// }
static void fin (napi_env env, void *fin_data, void* fin_hint) {
printf("finaliser is run\n");
// exit(0);
}
static void fuse_native_dispatch (uv_async_t* handle, int status) {
fuse_thread_locals_t *l = (fuse_thread_locals_t *) handle->data;
fuse_thread_t *ft = l->fuse;
napi_env env = ft->env;
napi_handle_scope scope;
napi_open_handle_scope(env, &scope);
napi_value ctx;
napi_get_reference_value(env, ft->ctx, &ctx);
napi_value callback;
napi_get_reference_value(env, ft->on_op, &callback);
// int ptr = get_free_thread_id()
// thread_local_map[ptr] = l
napi_value argv[1];
napi_create_external_buffer(env, sizeof(fuse_thread_locals_t), l, &fin, NULL, &(argv[0]));
NAPI_MAKE_CALLBACK(env, NULL, ctx, callback, 1, argv, NULL)
napi_close_handle_scope(env, scope);
}
static fuse_thread_locals_t* get_thread_locals () {
void *data = pthread_getspecific(thread_locals_key);
if (data != NULL) {
return (fuse_thread_locals_t *) data;
}
fuse_thread_locals_t* l = (fuse_thread_locals_t *) malloc(sizeof(fuse_thread_locals_t));
l->hello = 42;
// TODO: mutex me??
int err = uv_async_init(uv_default_loop(), &(l->async), (uv_async_cb) fuse_native_dispatch);
l->async.data = l;
if (err < 0) {
printf("uv_async failed: %i\n", err);
return NULL;
}
fuse_native_semaphore_init(&(l->sem));
pthread_setspecific(thread_locals_key, (void *) l);
return l;
}
static void* start_fuse_thread (void *data) {
fuse_thread_t *ft = (fuse_thread_t *) data;
fuse_loop_mt(ft->fuse);
// printf("her nu\n");
// fuse_unmount(mnt, ch);
// fuse_session_remove_chan(ch);
// fuse_destroy(fuse);
return NULL;
}
static int fuse_native_getattr (const char *path, struct stat *stat) {
printf("hi\n");
struct fuse_context *ctx = fuse_get_context();
fuse_thread_t *ft = (fuse_thread_t *) ctx->private_data;
fuse_thread_locals_t *l = get_thread_locals();
l->fuse = ft;
// b->op = OP_GETATTR;
// b->path = (char *) path;
// b->data = stat;
uv_async_send(&(l->async));
fuse_native_semaphore_wait(&(l->sem));
return -1;
}
NAPI_METHOD(fuse_native_signal) {
NAPI_ARGV(5)
NAPI_ARGV_BUFFER_CAST(fuse_thread_locals_t *, l, 0);
fuse_native_semaphore_signal(&(l->sem));
}
NAPI_METHOD(fuse_native_mount) {
NAPI_ARGV(5)
NAPI_ARGV_UTF8(mnt, 1024, 0);
NAPI_ARGV_UTF8(mntopts, 1024, 1);
NAPI_ARGV_BUFFER_CAST(fuse_thread_t *, ft, 2);
napi_create_reference(env, argv[3], 1, &(ft->ctx));
napi_create_reference(env, argv[4], 1, &(ft->on_op));
ft->env = env;
struct fuse_operations ops = {
.getattr = fuse_native_getattr
};
int _argc = 2;
char *_argv[] = {
(char *) "fuse_bindings_dummy",
(char *) mntopts
};
struct fuse_args args = FUSE_ARGS_INIT(_argc, _argv);
struct fuse_chan *ch = fuse_mount(mnt, &args);
if (ch == NULL) {
napi_throw_error(env, "fuse failed", "fuse failed");
return NULL;
}
struct fuse *fuse = fuse_new(ch, &args, &ops, sizeof(struct fuse_operations), ft);
ft->fuse = fuse;
if (fuse == NULL) {
napi_throw_error(env, "fuse failed", "fuse failed");
return NULL;
}
pthread_attr_init(&(ft->attr));
pthread_create(&(ft->thread), &(ft->attr), start_fuse_thread, ft);
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_signal)
NAPI_EXPORT_SIZEOF(fuse_thread_t)
}