This example implements a file system with a single file whose file name changes dynamically to reflect the current time.
However, if you try to access a file by name the kernel will report that it still exists:
In contrast, if you enable notifications you will be unable to stat the file as soon as the file system updates its name:
#define FUSE_USE_VERSION 34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <signal.h>
#include <stddef.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_STR_LEN 128
static char file_name[MAX_STR_LEN];
static int lookup_cnt = 0;
static pthread_t main_thread;
struct options {
int no_notify;
float timeout;
int update_interval;
int only_expire;
};
static struct options options = {
.timeout = 5,
.no_notify = 0,
.update_interval = 1,
.only_expire = 0,
};
#define OPTION(t, p) \
{ t, offsetof(struct options, p), 1 }
static const struct fuse_opt option_spec[] = {
OPTION("--no-notify", no_notify),
OPTION("--update-interval=%d", update_interval),
OPTION("--timeout=%f", timeout),
OPTION("--only-expire", only_expire),
};
static int tfs_stat(
fuse_ino_t ino,
struct stat *stbuf) {
stbuf->st_ino = ino;
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1;
}
else if (ino == file_ino) {
stbuf->st_mode = S_IFREG | 0000;
stbuf->st_nlink = 1;
stbuf->st_size = 0;
}
else
return -1;
return 0;
}
const char *name) {
memset(&e, 0, sizeof(e));
goto err_out;
else if (strcmp(name, file_name) == 0) {
e.ino = file_ino;
lookup_cnt++;
} else
goto err_out;
e.attr_timeout = options.timeout;
e.entry_timeout = options.timeout;
if (tfs_stat(e.ino, &e.attr) != 0)
goto err_out;
return;
err_out:
}
uint64_t nlookup) {
(void) req;
lookup_cnt -= nlookup;
else
}
struct stat stbuf;
(void) fi;
memset(&stbuf, 0, sizeof(stbuf));
if (tfs_stat(ino, &stbuf) != 0)
else
}
struct dirbuf {
char *p;
size_t size;
};
static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
struct stat stbuf;
size_t oldsize = b->size;
b->p = (char *) realloc(b->p, b->size);
memset(&stbuf, 0, sizeof(stbuf));
stbuf.st_ino = ino;
b->size);
}
#define min(x, y) ((x) < (y) ? (x) : (y))
static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
off_t off, size_t maxsize) {
if (off < bufsize)
min(bufsize - off, maxsize));
else
}
(void) fi;
else {
struct dirbuf b;
memset(&b, 0, sizeof(b));
dirbuf_add(req, &b, file_name, file_ino);
reply_buf_limited(req, b.p, b.size, off, size);
free(b.p);
}
}
.getattr = tfs_getattr,
.readdir = tfs_readdir,
.forget = tfs_forget,
};
static void update_fs(void) {
time_t t;
struct tm *now;
ssize_t ret;
t = time(NULL);
now = localtime(&t);
assert(now != NULL);
ret = strftime(file_name, MAX_STR_LEN,
"Time_is_%Hh_%Mm_%Ss", now);
assert(ret != 0);
}
static void* update_fs_loop(void *data) {
struct fuse_session *se = (struct fuse_session*) data;
char *old_name;
old_name = strdup(file_name);
update_fs();
if (!options.no_notify && lookup_cnt) {
if(options.only_expire) {
if (ret == -ENOSYS) {
printf("fuse_lowlevel_notify_expire_entry not supported by kernel\n");
printf("Exiting...\n");
pthread_kill(main_thread, SIGPIPE);
break;
}
assert(ret == 0 || ret == -ENOENT);
} else {
}
}
free(old_name);
sleep(options.update_interval);
}
return NULL;
}
static void show_help(const char *progname)
{
printf("usage: %s [options] <mountpoint>\n\n", progname);
printf("File-system specific options:\n"
" --timeout=<secs> Timeout for kernel caches\n"
" --update-interval=<secs> Update-rate of file system contents\n"
" --no-notify Disable kernel notifications\n"
" --only-expire Expire entries instead of invalidating them\n"
"\n");
}
int main(int argc, char *argv[]) {
struct fuse_session *se;
pthread_t updater;
int ret = -1;
return 1;
if (fuse_parse_cmdline(&args, &opts) != 0)
return 1;
if (opts.show_help) {
show_help(argv[0]);
ret = 0;
goto err_out1;
} else if (opts.show_version) {
ret = 0;
goto err_out1;
}
update_fs();
sizeof(tfs_oper), &se);
if (se == NULL)
goto err_out1;
goto err_out2;
goto err_out3;
main_thread = pthread_self();
ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
if (ret != 0) {
fprintf(stderr, "pthread_create failed with %s\n",
strerror(ret));
goto err_out3;
}
if (opts.singlethread) {
} else {
config.clone_fd = opts.clone_fd;
config.max_idle_threads = opts.max_idle_threads;
ret = fuse_session_loop_mt(se, &config);
}
err_out3:
err_out2:
err_out1:
free(opts.mountpoint);
return ret ? 1 : 0;
}
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
void fuse_remove_signal_handlers(struct fuse_session *se)
int fuse_daemonize(int foreground)
void fuse_session_destroy(struct fuse_session *se)
void fuse_session_exit(struct fuse_session *se)
int fuse_reply_err(fuse_req_t req, int err)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
struct fuse_req * fuse_req_t
int fuse_session_exited(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)
void fuse_session_unmount(struct fuse_session *se)
void fuse_cmdline_help(void)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
void fuse_reply_none(fuse_req_t req)
int fuse_lowlevel_notify_expire_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
void fuse_lowlevel_help(void)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
void fuse_lowlevel_version(void)
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_ARGS_INIT(argc, argv)
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)