2
|
1 |
#include <string.h>
|
|
2 |
#include <errno.h>
|
|
3 |
#include <stdlib.h>
|
|
4 |
|
1
|
5 |
#include <event2/event.h>
|
|
6 |
#include <fuse/fuse_opt.h>
|
|
7 |
|
|
8 |
#include "lib/common.h"
|
2
|
9 |
#include "lib/math.h"
|
3
|
10 |
#include "lib/signals.h"
|
1
|
11 |
#include "evfuse.h"
|
|
12 |
|
2
|
13 |
const char *file_name = "hello";
|
|
14 |
const char *file_data = "Hello World\n";
|
|
15 |
|
|
16 |
static struct hello {
|
1
|
17 |
struct event_base *ev_base;
|
|
18 |
|
3
|
19 |
struct signals *signals;
|
|
20 |
|
1
|
21 |
struct evfuse *ev_fuse;
|
2
|
22 |
|
1
|
23 |
} ctx;
|
|
24 |
|
|
25 |
void hello_init (void *userdata, struct fuse_conn_info *conn) {
|
|
26 |
INFO("[hello.init] userdata=%p, conn=%p", userdata, conn);
|
|
27 |
}
|
|
28 |
|
|
29 |
void hello_destroy (void *userdata) {
|
|
30 |
INFO("[hello.destroy] userdata=%p", userdata);
|
|
31 |
}
|
|
32 |
|
2
|
33 |
void hello_lookup (fuse_req_t req, fuse_ino_t parent, const char *name) {
|
|
34 |
struct fuse_entry_param e;
|
|
35 |
|
|
36 |
INFO("[hello.lookup] (uid=%d, pid=%d) parent=%lu name=%s", fuse_req_ctx(req)->uid, fuse_req_ctx(req)->pid, parent, name);
|
|
37 |
|
|
38 |
// the world is flat
|
|
39 |
if (parent != 1 || strcmp(name, file_name)) {
|
|
40 |
fuse_reply_err(req, ENOENT);
|
|
41 |
|
|
42 |
return;
|
|
43 |
}
|
|
44 |
|
|
45 |
// set up the entry
|
|
46 |
memset(&e, 0, sizeof(e));
|
|
47 |
e.ino = 2;
|
|
48 |
e.attr_timeout = 1.0;
|
|
49 |
e.entry_timeout = 1.0;
|
|
50 |
e.attr.st_mode = S_IFREG | 0444;
|
|
51 |
e.attr.st_nlink = 1;
|
|
52 |
e.attr.st_size = strlen(file_data);
|
|
53 |
|
|
54 |
// reply
|
|
55 |
fuse_reply_entry(req, &e);
|
|
56 |
}
|
|
57 |
|
|
58 |
void hello_getattr (fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {
|
|
59 |
struct stat stbuf;
|
|
60 |
|
|
61 |
INFO("[hello.getattr] (uid=%d, pid=%d) ino=%lu, fi=%p", fuse_req_ctx(req)->uid, fuse_req_ctx(req)->pid, ino, fi);
|
|
62 |
|
|
63 |
memset(&stbuf, 0, sizeof(stbuf));
|
|
64 |
|
|
65 |
// the root dir, or the file?
|
|
66 |
if (ino == 1) {
|
|
67 |
stbuf.st_mode = S_IFDIR | 0555;
|
|
68 |
stbuf.st_nlink = 2;
|
|
69 |
|
|
70 |
} else if (ino == 2) {
|
|
71 |
stbuf.st_mode = S_IFREG | 0444;
|
|
72 |
stbuf.st_nlink = 1;
|
|
73 |
stbuf.st_size = strlen(file_data);
|
|
74 |
|
|
75 |
} else {
|
|
76 |
fuse_reply_err(req, ENOENT);
|
|
77 |
return;
|
|
78 |
}
|
|
79 |
|
|
80 |
// reply
|
|
81 |
fuse_reply_attr(req, &stbuf, 1.0);
|
|
82 |
}
|
|
83 |
|
|
84 |
struct dirbuf {
|
|
85 |
char *buf;
|
|
86 |
size_t len;
|
|
87 |
size_t off;
|
|
88 |
};
|
|
89 |
|
|
90 |
#define DIRBUF_INITIAL_SIZE 1024
|
|
91 |
|
|
92 |
static int dirbuf_init (struct dirbuf *buf) {
|
|
93 |
buf->len = DIRBUF_INITIAL_SIZE;
|
|
94 |
buf->off = 0;
|
|
95 |
|
|
96 |
// allocate the mem
|
|
97 |
if ((buf->buf = malloc(buf->len)) == NULL)
|
|
98 |
ERROR("malloc");
|
|
99 |
|
|
100 |
// ok
|
|
101 |
return 0;
|
|
102 |
|
|
103 |
error:
|
|
104 |
return -1;
|
|
105 |
}
|
|
106 |
|
|
107 |
/*
|
|
108 |
* Ensure that `new` bytes fit into the buf. If they already fit, update offset and set *retry = 0. If they don't fit,
|
|
109 |
* grow buf and set *retry = 1.
|
|
110 |
*
|
|
111 |
* Returns 0 on success, -1 on failure (don't retry).
|
|
112 |
*/
|
|
113 |
static int dirbuf_update (struct dirbuf *buf, size_t new, int *retry) {
|
|
114 |
if (buf->off + new <= buf->len) {
|
|
115 |
INFO("\thello.dirbuf_update: update offset by %zu from %zu -> %zu", new, buf->off, buf->off + new);
|
|
116 |
|
|
117 |
// great, it fit, update offset and return
|
|
118 |
buf->off += new;
|
|
119 |
|
|
120 |
*retry = 0;
|
|
121 |
|
|
122 |
} else {
|
|
123 |
size_t old_len = buf->len;
|
|
124 |
|
|
125 |
// calc new size
|
|
126 |
do {
|
|
127 |
buf->len *= 2;
|
|
128 |
|
|
129 |
} while (buf->off + new > buf->len);
|
|
130 |
|
|
131 |
INFO("\thello.dirbuf_update: grow size for %zu from %zu -> %zu", new, old_len, buf->len);
|
|
132 |
|
|
133 |
// realloc
|
|
134 |
if ((buf->buf = realloc(buf->buf, buf->len)) == NULL)
|
|
135 |
ERROR("realloc");
|
|
136 |
|
|
137 |
// done, just retry
|
|
138 |
*retry = 1;
|
|
139 |
}
|
|
140 |
|
|
141 |
// success
|
|
142 |
return 0;
|
|
143 |
|
|
144 |
error:
|
|
145 |
return -1;
|
|
146 |
}
|
|
147 |
|
|
148 |
static int dirbuf_add (fuse_req_t req, size_t req_size, off_t req_off, struct dirbuf *buf, off_t ent_off, off_t next_off, const char *ent_name, fuse_ino_t ent_ino) {
|
|
149 |
struct stat stbuf;
|
|
150 |
size_t ent_size;
|
|
151 |
int err, retry;
|
|
152 |
|
|
153 |
INFO("\thello.dirbuf_add: req_size=%zu, req_off=%zu, buf->len=%zu, buf->off=%zu, ent_off=%zu, next_off=%zu, ent_name=%s, ent_ino=%lu",
|
|
154 |
req_size, req_off, buf->len, buf->off, ent_off, next_off, ent_name, ent_ino);
|
|
155 |
|
|
156 |
// skip entries as needed
|
|
157 |
if (buf->len >= req_size || ent_off < req_off)
|
|
158 |
return 0;
|
|
159 |
|
|
160 |
// set ino
|
|
161 |
stbuf.st_ino = ent_ino;
|
|
162 |
|
|
163 |
// add the dirent and update dirbuf until it fits
|
|
164 |
do {
|
|
165 |
ent_size = fuse_add_direntry(req, buf->buf + buf->off, buf->len - buf->off, ent_name, &stbuf, next_off);
|
|
166 |
|
|
167 |
} while (!(err = dirbuf_update(buf, ent_size, &retry)) && retry);
|
|
168 |
|
|
169 |
if (err)
|
|
170 |
return err;
|
|
171 |
|
|
172 |
// success
|
|
173 |
return 0;
|
|
174 |
}
|
|
175 |
|
|
176 |
static int dirbuf_done (fuse_req_t req, struct dirbuf *buf, size_t req_size) {
|
|
177 |
int err;
|
|
178 |
|
|
179 |
// send the reply, return the error later
|
|
180 |
err = fuse_reply_buf(req, buf->buf, MIN(buf->off, req_size));
|
|
181 |
|
|
182 |
INFO("\thello.dirbuf_done: MIN(%zu, %zu)=%zu, err=%d", buf->off, req_size, MIN(buf->off, req_size), err);
|
|
183 |
|
|
184 |
// free the dirbuf
|
|
185 |
free(buf->buf);
|
|
186 |
|
|
187 |
// return the error code
|
|
188 |
return err;
|
|
189 |
}
|
|
190 |
|
|
191 |
void hello_readdir (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi) {
|
|
192 |
int err = 0;
|
|
193 |
struct dirbuf buf;
|
|
194 |
|
|
195 |
INFO("[hello.readdir] ino=%lu, size=%zu, off=%zu, fi=%p", ino, size, off, fi);
|
|
196 |
|
|
197 |
// there exists only one dir
|
|
198 |
if (ino != 1) {
|
|
199 |
fuse_reply_err(req, ENOTDIR);
|
|
200 |
return;
|
|
201 |
}
|
|
202 |
|
|
203 |
// fill in the dirbuf
|
|
204 |
if (dirbuf_init(&buf))
|
|
205 |
ERROR("failed to init dirbuf");
|
|
206 |
|
|
207 |
if ( dirbuf_add(req, size, off, &buf, 0, 1, ".", 1)
|
|
208 |
|| dirbuf_add(req, size, off, &buf, 1, 2, "..", 1)
|
|
209 |
|| dirbuf_add(req, size, off, &buf, 2, 3, file_name, 2)
|
|
210 |
) ERROR("failed to add dirents to buf");
|
|
211 |
|
|
212 |
// send it
|
|
213 |
if ((err = -dirbuf_done(req, &buf, size)))
|
|
214 |
EERROR(-err, "failed to send buf");
|
|
215 |
|
|
216 |
// success
|
|
217 |
return;
|
|
218 |
|
|
219 |
error:
|
|
220 |
if ((err = fuse_reply_err(req, err ? err : EIO)))
|
|
221 |
EWARNING(err, "failed to send error reply");
|
|
222 |
}
|
|
223 |
|
4
|
224 |
void hello_open (fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi) {
|
|
225 |
int err = 0;
|
|
226 |
|
|
227 |
INFO("[hello.open] ino=%lu, fi=%p, fi->flags=%08X", ino, fi, fi->flags);
|
|
228 |
|
|
229 |
if (ino != 2) {
|
|
230 |
// must open our only file, not the dir
|
|
231 |
fuse_reply_err(req, ino == 1 ? EISDIR : ENOENT);
|
|
232 |
return;
|
|
233 |
|
|
234 |
} else if ((fi->flags & 0x03) != O_RDONLY) {
|
|
235 |
// "permission denied"
|
|
236 |
fuse_reply_err(req, EACCES);
|
|
237 |
return;
|
|
238 |
}
|
|
239 |
|
|
240 |
// XXX: update fi stuff?
|
|
241 |
|
|
242 |
// open it!
|
|
243 |
if ((err = fuse_reply_open(req, fi)))
|
|
244 |
EERROR(err, "fuse_reply_open");
|
|
245 |
|
|
246 |
// success
|
|
247 |
return;
|
|
248 |
|
|
249 |
error:
|
|
250 |
if ((err = fuse_reply_err(req, err ? err : EIO)))
|
|
251 |
EWARNING(err, "failed to send error reply");
|
|
252 |
}
|
|
253 |
|
|
254 |
void hello_read (fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, struct fuse_file_info *fi) {
|
|
255 |
int err = 0;
|
|
256 |
|
|
257 |
// fi is unused
|
|
258 |
(void) fi;
|
|
259 |
|
|
260 |
INFO("[hello.read] ino=%lu, size=%zu, off=%zu, fi=%p", ino, size, off, fi);
|
|
261 |
|
|
262 |
if (ino != 2) {
|
|
263 |
// EEK!
|
|
264 |
FATAL("wrong inode");
|
|
265 |
}
|
|
266 |
|
|
267 |
// validate off
|
|
268 |
if (off >= strlen(file_data) && (err = EIO))
|
|
269 |
ERROR("offset is out-of-bounds (%zu >= %zu)", off, strlen(file_data));
|
|
270 |
|
|
271 |
// reply
|
|
272 |
if ((err = fuse_reply_buf(req, file_data + off, MIN(strlen(file_data) - off, size))))
|
|
273 |
PERROR("fuse_reply_buf");
|
|
274 |
|
|
275 |
// success
|
|
276 |
return;
|
|
277 |
|
|
278 |
error:
|
|
279 |
if ((err = fuse_reply_err(req, err ? err : EIO)))
|
|
280 |
EWARNING(err, "failed to send error reply");
|
|
281 |
}
|
|
282 |
|
1
|
283 |
struct fuse_lowlevel_ops hello_llops = {
|
|
284 |
.init = &hello_init,
|
|
285 |
.destroy = &hello_destroy,
|
2
|
286 |
|
|
287 |
.lookup = &hello_lookup,
|
|
288 |
.getattr = &hello_getattr,
|
|
289 |
|
4
|
290 |
.open = &hello_open,
|
|
291 |
|
|
292 |
.read = &hello_read,
|
|
293 |
|
2
|
294 |
.readdir = &hello_readdir,
|
1
|
295 |
};
|
|
296 |
|
|
297 |
|
|
298 |
int main (int argc, char **argv) {
|
|
299 |
struct fuse_args fuse_args = FUSE_ARGS_INIT(argc, argv);
|
3
|
300 |
|
|
301 |
// zero
|
|
302 |
memset(&ctx, 0, sizeof(ctx));
|
1
|
303 |
|
|
304 |
// init libevent
|
|
305 |
if ((ctx.ev_base = event_base_new()) == NULL)
|
3
|
306 |
ERROR("event_base_new");
|
1
|
307 |
|
3
|
308 |
// setup signals
|
|
309 |
if ((ctx.signals = signals_default(ctx.ev_base)) == NULL)
|
|
310 |
ERROR("signals_default");
|
|
311 |
|
1
|
312 |
// open fuse
|
|
313 |
if ((ctx.ev_fuse = evfuse_new(ctx.ev_base, &fuse_args, &hello_llops, &ctx)) == NULL)
|
3
|
314 |
ERROR("evfuse_new");
|
1
|
315 |
|
|
316 |
// run libevent
|
|
317 |
INFO("running libevent loop");
|
|
318 |
|
|
319 |
if (event_base_dispatch(ctx.ev_base))
|
3
|
320 |
PERROR("event_base_dispatch");
|
|
321 |
|
|
322 |
// clean shutdown
|
1
|
323 |
|
3
|
324 |
error :
|
1
|
325 |
// cleanup
|
3
|
326 |
if (ctx.ev_fuse)
|
|
327 |
evfuse_close(ctx.ev_fuse);
|
|
328 |
|
|
329 |
if (ctx.signals)
|
|
330 |
signals_free(ctx.signals);
|
|
331 |
|
|
332 |
if (ctx.ev_base)
|
|
333 |
event_base_free(ctx.ev_base);
|
|
334 |
|
|
335 |
fuse_opt_free_args(&fuse_args);
|
1
|
336 |
}
|
|
337 |
|