85#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
87#include <fuse_lowlevel.h>
100#define MAX_STR_LEN 128
101static char file_name[MAX_STR_LEN];
103static int lookup_cnt = 0;
104static pthread_t main_thread;
114static struct options options = {
117 .update_interval = 1,
122#define OPTION(t, p) \
123 { t, offsetof(struct options, p), 1 }
124static const struct fuse_opt option_spec[] = {
125 OPTION(
"--no-notify", no_notify),
126 OPTION(
"--update-interval=%d", update_interval),
127 OPTION(
"--timeout=%f", timeout),
128 OPTION(
"--only-expire", only_expire),
129 OPTION(
"--inc-epoch", inc_epoch),
133static int tfs_stat(
fuse_ino_t ino,
struct stat *stbuf) {
135 if (ino == FUSE_ROOT_ID) {
136 stbuf->st_mode = S_IFDIR | 0755;
140 else if (ino == file_ino) {
141 stbuf->st_mode = S_IFREG | 0000;
152static void tfs_init(
void *userdata,
struct fuse_conn_info *conn) {
162 memset(&e, 0,
sizeof(e));
166 else if (strcmp(name, file_name) == 0) {
172 e.attr_timeout = options.timeout;
173 e.entry_timeout = options.timeout;
174 if (tfs_stat(e.ino, &e.attr) != 0)
187 lookup_cnt -= nlookup;
199 memset(&stbuf, 0,
sizeof(stbuf));
200 if (tfs_stat(ino, &stbuf) != 0)
211static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
214 size_t oldsize = b->size;
216 b->p = (
char *) realloc(b->p, b->size);
217 memset(&stbuf, 0,
sizeof(stbuf));
223#define min(x, y) ((x) < (y) ? (x) : (y))
225static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
226 off_t off,
size_t maxsize) {
229 min(bufsize - off, maxsize));
238 if (ino != FUSE_ROOT_ID)
243 memset(&b, 0,
sizeof(b));
244 dirbuf_add(req, &b, file_name, file_ino);
245 reply_buf_limited(req, b.p, b.size, off, size);
252 .lookup = tfs_lookup,
253 .getattr = tfs_getattr,
254 .readdir = tfs_readdir,
255 .forget = tfs_forget,
258static void update_fs(
void) {
267 ret = strftime(file_name, MAX_STR_LEN,
268 "Time_is_%Hh_%Mm_%Ss", now);
272static void* update_fs_loop(
void *data) {
273 struct fuse_session *se = (
struct fuse_session*) data;
278 old_name = strdup(file_name);
281 if (!options.no_notify && lookup_cnt) {
282 if(options.only_expire) {
284 (se, FUSE_ROOT_ID, old_name, strlen(old_name));
287 if (ret == -ENOSYS) {
288 printf(
"fuse_lowlevel_notify_expire_entry not supported by kernel\n");
295 assert(ret == 0 || ret == -ENOENT);
296 }
else if (options.inc_epoch) {
299 if (ret == -ENOSYS) {
300 printf(
"fuse_lowlevel_notify_increment_epoch not supported by kernel\n");
306 (se, FUSE_ROOT_ID, old_name, strlen(old_name)) == 0);
310 sleep(options.update_interval);
313 if (ret == -ENOSYS) {
314 printf(
"Exiting...\n");
318 pthread_kill(main_thread, SIGPIPE);
324static void show_help(
const char *progname)
326 printf(
"usage: %s [options] <mountpoint>\n\n", progname);
327 printf(
"File-system specific options:\n"
328 " --timeout=<secs> Timeout for kernel caches\n"
329 " --update-interval=<secs> Update-rate of file system contents\n"
330 " --no-notify Disable kernel notifications\n"
331 " --only-expire Expire entries instead of invalidating them\n"
332 " --inc-epoch Increment epoch, invalidating all dentries\n"
336int main(
int argc,
char *argv[]) {
338 struct fuse_session *se;
347 if (fuse_parse_cmdline(&args, &opts) != 0)
349 if (opts.show_help) {
355 }
else if (opts.show_version) {
361 if (options.only_expire && options.inc_epoch) {
362 printf(
"'only-expire' and 'inc-epoch' options are exclusive\n");
370 se = fuse_session_new(&args, &tfs_oper,
371 sizeof(tfs_oper), &se);
386 main_thread = pthread_self();
389 ret = pthread_create(&updater, NULL, update_fs_loop, (
void *)se);
391 fprintf(stderr,
"pthread_create failed with %s\n",
397 if (opts.singlethread) {
400 config = fuse_loop_cfg_create();
401 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
402 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
403 ret = fuse_session_loop_mt(se, config);
404 fuse_loop_cfg_destroy(config);
414 free(opts.mountpoint);
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)
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)
int fuse_lowlevel_notify_increment_epoch(struct fuse_session *se)
void(* init)(void *userdata, struct fuse_conn_info *conn)