61 #define FUSE_USE_VERSION 31 63 #include <fuse_lowlevel.h> 76 #define NO_TIMEOUT 500000 80 #define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR) 82 #define MAX_STR_LEN 128 84 #define FILE_NAME "current_time" 85 static char file_contents[MAX_STR_LEN];
86 static int lookup_cnt = 0;
87 static size_t file_size;
91 static int retrieve_status = 0;
98 static struct options options = {
100 .update_interval = 1,
103 #define OPTION(t, p) \ 104 { t, offsetof(struct options, p), 1 } 105 static const struct fuse_opt option_spec[] = {
106 OPTION(
"--no-notify", no_notify),
107 OPTION(
"--update-interval=%d", update_interval),
111 static int tfs_stat(
fuse_ino_t ino,
struct stat *stbuf) {
114 stbuf->st_mode = S_IFDIR | 0755;
118 else if (ino == FILE_INO) {
119 stbuf->st_mode = S_IFREG | 0444;
121 stbuf->st_size = file_size;
133 memset(&e, 0,
sizeof(e));
137 else if (strcmp(name, FILE_NAME) == 0) {
143 e.attr_timeout = NO_TIMEOUT;
144 e.entry_timeout = NO_TIMEOUT;
145 if (tfs_stat(e.ino, &e.attr) != 0)
158 lookup_cnt -= nlookup;
170 memset(&stbuf, 0,
sizeof(stbuf));
171 if (tfs_stat(ino, &stbuf) != 0)
182 static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
185 size_t oldsize = b->size;
187 b->p = (
char *) realloc(b->p, b->size);
188 memset(&stbuf, 0,
sizeof(stbuf));
194 #define min(x, y) ((x) < (y) ? (x) : (y)) 196 static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
197 off_t off,
size_t maxsize) {
200 min(bufsize - off, maxsize));
214 memset(&b, 0,
sizeof(b));
215 dirbuf_add(req, &b, FILE_NAME, FILE_INO);
216 reply_buf_limited(req, b.p, b.size, off, size);
230 else if ((fi->
flags & ACCESS_MASK) != O_RDONLY)
232 else if (ino == FILE_INO)
236 fprintf(stderr,
"Got open for non-existing inode!\n");
245 assert(ino == FILE_INO);
246 reply_buf_limited(req, file_contents, file_size, off, size);
252 char buf[MAX_STR_LEN];
256 assert(ino == FILE_INO);
258 expected = (
char*) cookie;
263 bufv.buf[0].size = MAX_STR_LEN;
264 bufv.buf[0].mem =
buf;
265 bufv.buf[0].
flags = 0;
269 assert(strncmp(buf, expected, ret) == 0);
278 .getattr = tfs_getattr,
279 .readdir = tfs_readdir,
282 .forget = tfs_forget,
283 .retrieve_reply = tfs_retrieve_reply,
286 static void update_fs(
void) {
293 file_size = strftime(file_contents, MAX_STR_LEN,
294 "The current time is %H:%M:%S\n", now);
295 assert(file_size != 0);
298 static void* update_fs_loop(
void *data) {
299 struct fuse_session *se = (
struct fuse_session*) data;
305 if (!options.no_notify && lookup_cnt) {
311 bufv.buf[0].size = file_size;
312 bufv.buf[0].mem = file_contents;
313 bufv.buf[0].flags = 0;
318 if (-ret == ENODEV) {
323 fprintf(stderr,
"ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n",
324 strerror(-ret), -ret);
331 0, (
void*) strdup(file_contents));
332 if (-ret == ENODEV) {
336 if(retrieve_status == 0)
339 sleep(options.update_interval);
344 static void show_help(
const char *progname)
346 printf(
"usage: %s [options] <mountpoint>\n\n", progname);
347 printf(
"File-system specific options:\n" 348 " --update-interval=<secs> Update-rate of file system contents\n" 349 " --no-notify Disable kernel notifications\n" 353 int main(
int argc,
char *argv[]) {
355 struct fuse_session *se;
356 struct fuse_cmdline_opts opts;
365 if (opts.show_help) {
371 }
else if (opts.show_version) {
382 sizeof(tfs_oper), NULL);
395 ret = pthread_create(&updater, NULL, update_fs_loop, (
void *)se);
397 fprintf(stderr,
"pthread_create failed with %s\n",
403 if (opts.singlethread)
406 ret = fuse_session_loop_mt(se, opts.clone_fd);
408 assert(retrieve_status != 1);
415 free(opts.mountpoint);
int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)
struct fuse_req * fuse_req_t
const char * fuse_pkgversion(void)
void fuse_session_unmount(struct fuse_session *se)
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_daemonize(int foreground)
void fuse_lowlevel_help(void)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
#define FUSE_ARGS_INIT(argc, argv)
int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, off_t offset, struct fuse_bufvec *bufv, enum fuse_buf_copy_flags flags)
int fuse_reply_err(fuse_req_t req, int err)
void fuse_cmdline_help(void)
enum fuse_buf_flags flags
void fuse_reply_none(fuse_req_t req)
int fuse_set_signal_handlers(struct fuse_session *se)
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
void fuse_opt_free_args(struct fuse_args *args)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino, size_t size, off_t offset, void *cookie)
void fuse_lowlevel_version(void)
void fuse_remove_signal_handlers(struct fuse_session *se)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)