author | Tero Marttila <terom@fixme.fi> |
Sat, 13 Dec 2008 19:55:50 +0200 | |
branch | new-evsql |
changeset 53 | 0d6e07f4c9a1 |
parent 29 | 5de62ca9a5aa |
permissions | -rw-r--r-- |
1 | 1 |
|
2 |
#include <errno.h> |
|
3 |
#include <string.h> |
|
4 |
#include <stdlib.h> |
|
0 | 5 |
|
6 |
#include "evfuse.h" |
|
6
d2036d7799fd
new 'simple' module, plus hello_simple
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
7 |
#include "lib/log.h" |
0 | 8 |
|
9 |
struct evfuse { |
|
3 | 10 |
// our mountpoint |
11 |
char *mountpoint; |
|
12 |
||
0 | 13 |
// the /dev/fuse fd/channel that we get from fuse_mount |
14 |
struct fuse_chan *chan; |
|
15 |
||
16 |
// the session that we use to process the fuse stuff |
|
17 |
struct fuse_session *session; |
|
18 |
||
19 |
// the event that we use to receive requests |
|
20 |
struct event *ev; |
|
21 |
||
22 |
// what our receive-message length is |
|
23 |
size_t recv_size; |
|
24 |
||
25 |
// the buffer that we use to receive events |
|
26 |
char *recv_buf; |
|
27 |
}; |
|
28 |
||
27 | 29 |
// prototypes |
30 |
void evfuse_close (struct evfuse *ctx); |
|
31 |
||
0 | 32 |
static void _evfuse_ev_read (evutil_socket_t fd, short what, void *arg) { |
33 |
struct evfuse *ctx = arg; |
|
1 | 34 |
struct fuse_chan *ch = ctx->chan; |
0 | 35 |
int res; |
36 |
||
37 |
// loop until we complete a recv |
|
38 |
do { |
|
39 |
// a new fuse_req is available |
|
40 |
res = fuse_chan_recv(&ch, ctx->recv_buf, ctx->recv_size); |
|
41 |
} while (res == -EINTR); |
|
42 |
||
43 |
if (res == 0) |
|
44 |
ERROR("fuse_chan_recv gave EOF"); |
|
45 |
||
46 |
if (res < 0 && res != -EAGAIN) |
|
47 |
ERROR("fuse_chan_recv failed: %s", strerror(-res)); |
|
48 |
||
49 |
if (res > 0) { |
|
29
5de62ca9a5aa
reorganize evsql into a separate dir, rename dbfs slightly, and split dbfs_op out from dirop (in prep for fileop)
Tero Marttila <terom@fixme.fi>
parents:
27
diff
changeset
|
50 |
DEBUG("got %d bytes from /dev/fuse", res); |
1 | 51 |
|
0 | 52 |
// received a fuse_req, so process it |
53 |
fuse_session_process(ctx->session, ctx->recv_buf, res, ch); |
|
54 |
} |
|
55 |
||
56 |
// reschedule |
|
57 |
if (event_add(ctx->ev, NULL)) |
|
58 |
PERROR("event_add"); |
|
59 |
||
60 |
// ok, wait for the next event |
|
61 |
return; |
|
62 |
||
63 |
error: |
|
27 | 64 |
// close, but don't free |
6
d2036d7799fd
new 'simple' module, plus hello_simple
Tero Marttila <terom@fixme.fi>
parents:
3
diff
changeset
|
65 |
evfuse_close(ctx); |
0 | 66 |
} |
67 |
||
68 |
struct evfuse *evfuse_new (struct event_base *evbase, struct fuse_args *args, struct fuse_lowlevel_ops *llops, void *cb_data) { |
|
69 |
struct evfuse *ctx = NULL; |
|
70 |
int multithreaded, foreground; |
|
71 |
||
72 |
// allocate our context |
|
1 | 73 |
if ((ctx = calloc(1, sizeof(*ctx))) == NULL) |
0 | 74 |
ERROR("calloc"); |
75 |
||
76 |
// parse the commandline for the mountpoint |
|
3 | 77 |
if (fuse_parse_cmdline(args, &ctx->mountpoint, &multithreaded, &foreground) == -1) |
0 | 78 |
ERROR("fuse_parse_cmdline"); |
79 |
||
80 |
// mount it |
|
3 | 81 |
if ((ctx->chan = fuse_mount(ctx->mountpoint, args)) == NULL) |
0 | 82 |
PERROR("fuse_mount_common"); |
83 |
||
84 |
// the receive buffer stufff |
|
85 |
ctx->recv_size = fuse_chan_bufsize(ctx->chan); |
|
86 |
||
87 |
// allocate the recv buffer |
|
88 |
if ((ctx->recv_buf = malloc(ctx->recv_size)) == NULL) |
|
89 |
ERROR("malloc"); |
|
90 |
||
91 |
// allocate a low-level session |
|
92 |
if ((ctx->session = fuse_lowlevel_new(args, llops, sizeof(*llops), cb_data)) == NULL) |
|
93 |
PERROR("fuse_lowlevel_new"); |
|
94 |
||
95 |
// add the channel to the session |
|
96 |
// this isn't strictly necessary, but let's do it anyways |
|
97 |
fuse_session_add_chan(ctx->session, ctx->chan); |
|
98 |
||
99 |
// now, we can start listening for events on the channel |
|
100 |
if ((ctx->ev = event_new(evbase, fuse_chan_fd(ctx->chan), EV_READ, &_evfuse_ev_read, ctx)) == NULL) |
|
101 |
ERROR("event_new"); |
|
102 |
||
103 |
if (event_add(ctx->ev, NULL)) |
|
104 |
PERROR("event_add"); |
|
105 |
||
106 |
// and then we wait |
|
1 | 107 |
return ctx; |
0 | 108 |
|
109 |
error: |
|
27 | 110 |
evfuse_free(ctx); |
0 | 111 |
|
112 |
return NULL; |
|
113 |
} |
|
3 | 114 |
|
115 |
void evfuse_close (struct evfuse *ctx) { |
|
27 | 116 |
if (ctx->ev) { |
117 |
// remove our event |
|
118 |
if (event_del(ctx->ev)) |
|
119 |
PWARNING("event_del"); |
|
3 | 120 |
|
27 | 121 |
ctx->ev = NULL; |
122 |
} |
|
3 | 123 |
|
27 | 124 |
if (ctx->session) { |
125 |
// remove the chan |
|
126 |
fuse_session_remove_chan(ctx->chan); |
|
127 |
||
128 |
// destroy the session |
|
129 |
fuse_session_destroy(ctx->session); |
|
130 |
||
131 |
ctx->session = NULL; |
|
132 |
} |
|
133 |
||
134 |
if (ctx->chan) { |
|
135 |
// unmount |
|
136 |
fuse_unmount(ctx->mountpoint, ctx->chan); |
|
137 |
||
138 |
ctx->chan = NULL; |
|
139 |
} |
|
140 |
||
3 | 141 |
// free |
27 | 142 |
free(ctx->recv_buf); ctx->recv_buf = NULL; |
143 |
free(ctx->mountpoint); ctx->mountpoint = NULL; |
|
3 | 144 |
} |
145 |
||
27 | 146 |
void evfuse_free (struct evfuse *ctx) { |
147 |
if (ctx) { |
|
148 |
evfuse_close(ctx); |
|
149 |
||
150 |
free(ctx); |
|
151 |
} |
|
152 |
} |
|
153 |