1
|
1 |
|
|
2 |
#include <errno.h>
|
|
3 |
#include <string.h>
|
|
4 |
#include <stdlib.h>
|
0
|
5 |
|
|
6 |
#include "evfuse.h"
|
1
|
7 |
#include "lib/common.h"
|
0
|
8 |
|
|
9 |
struct evfuse {
|
|
10 |
// the /dev/fuse fd/channel that we get from fuse_mount
|
|
11 |
struct fuse_chan *chan;
|
|
12 |
|
|
13 |
// the session that we use to process the fuse stuff
|
|
14 |
struct fuse_session *session;
|
|
15 |
|
|
16 |
// the event that we use to receive requests
|
|
17 |
struct event *ev;
|
|
18 |
|
|
19 |
// what our receive-message length is
|
|
20 |
size_t recv_size;
|
|
21 |
|
|
22 |
// the buffer that we use to receive events
|
|
23 |
char *recv_buf;
|
|
24 |
};
|
|
25 |
|
|
26 |
static void _evfuse_ev_read (evutil_socket_t fd, short what, void *arg) {
|
|
27 |
struct evfuse *ctx = arg;
|
1
|
28 |
struct fuse_chan *ch = ctx->chan;
|
0
|
29 |
int res;
|
|
30 |
|
|
31 |
// loop until we complete a recv
|
|
32 |
do {
|
|
33 |
// a new fuse_req is available
|
|
34 |
res = fuse_chan_recv(&ch, ctx->recv_buf, ctx->recv_size);
|
|
35 |
} while (res == -EINTR);
|
|
36 |
|
|
37 |
if (res == 0)
|
|
38 |
ERROR("fuse_chan_recv gave EOF");
|
|
39 |
|
|
40 |
if (res < 0 && res != -EAGAIN)
|
|
41 |
ERROR("fuse_chan_recv failed: %s", strerror(-res));
|
|
42 |
|
|
43 |
if (res > 0) {
|
1
|
44 |
INFO("[evfuse] got %d bytes from /dev/fuse", res);
|
|
45 |
|
0
|
46 |
// received a fuse_req, so process it
|
|
47 |
fuse_session_process(ctx->session, ctx->recv_buf, res, ch);
|
|
48 |
}
|
|
49 |
|
|
50 |
// reschedule
|
|
51 |
if (event_add(ctx->ev, NULL))
|
|
52 |
PERROR("event_add");
|
|
53 |
|
|
54 |
// ok, wait for the next event
|
|
55 |
return;
|
|
56 |
|
|
57 |
error:
|
|
58 |
// XXX: fail
|
|
59 |
FATAL("no error handling yet...");
|
|
60 |
}
|
|
61 |
|
|
62 |
struct evfuse *evfuse_new (struct event_base *evbase, struct fuse_args *args, struct fuse_lowlevel_ops *llops, void *cb_data) {
|
|
63 |
struct evfuse *ctx = NULL;
|
|
64 |
char *mountpoint;
|
|
65 |
int multithreaded, foreground;
|
|
66 |
|
|
67 |
// allocate our context
|
1
|
68 |
if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
|
0
|
69 |
ERROR("calloc");
|
|
70 |
|
|
71 |
// parse the commandline for the mountpoint
|
|
72 |
if (fuse_parse_cmdline(args, &mountpoint, &multithreaded, &foreground) == -1)
|
|
73 |
ERROR("fuse_parse_cmdline");
|
|
74 |
|
|
75 |
// mount it
|
|
76 |
if ((ctx->chan = fuse_mount(mountpoint, args)) == NULL)
|
|
77 |
PERROR("fuse_mount_common");
|
|
78 |
|
|
79 |
// the receive buffer stufff
|
|
80 |
ctx->recv_size = fuse_chan_bufsize(ctx->chan);
|
|
81 |
|
|
82 |
// allocate the recv buffer
|
|
83 |
if ((ctx->recv_buf = malloc(ctx->recv_size)) == NULL)
|
|
84 |
ERROR("malloc");
|
|
85 |
|
|
86 |
// allocate a low-level session
|
|
87 |
if ((ctx->session = fuse_lowlevel_new(args, llops, sizeof(*llops), cb_data)) == NULL)
|
|
88 |
PERROR("fuse_lowlevel_new");
|
|
89 |
|
|
90 |
// add the channel to the session
|
|
91 |
// this isn't strictly necessary, but let's do it anyways
|
|
92 |
fuse_session_add_chan(ctx->session, ctx->chan);
|
|
93 |
|
|
94 |
// now, we can start listening for events on the channel
|
|
95 |
if ((ctx->ev = event_new(evbase, fuse_chan_fd(ctx->chan), EV_READ, &_evfuse_ev_read, ctx)) == NULL)
|
|
96 |
ERROR("event_new");
|
|
97 |
|
|
98 |
if (event_add(ctx->ev, NULL))
|
|
99 |
PERROR("event_add");
|
|
100 |
|
|
101 |
// and then we wait
|
1
|
102 |
return ctx;
|
0
|
103 |
|
|
104 |
error:
|
|
105 |
free(ctx);
|
|
106 |
|
|
107 |
return NULL;
|
|
108 |
}
|