62#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
79#define NO_TIMEOUT 500000
81#define MAX_STR_LEN 128
83#define FILE_NAME "current_time"
84static char file_contents[MAX_STR_LEN];
85static int lookup_cnt = 0;
86static size_t file_size;
87static _Atomic
bool is_stop =
false;
94static struct options options = {
100 { t, offsetof(struct options, p), 1 }
101static const struct fuse_opt option_spec[] = {
102 OPTION(
"--no-notify", no_notify),
103 OPTION(
"--update-interval=%d", update_interval),
107static int tfs_stat(
fuse_ino_t ino,
struct stat *stbuf) {
109 if (ino == FUSE_ROOT_ID) {
110 stbuf->st_mode = S_IFDIR | 0755;
114 else if (ino == FILE_INO) {
115 stbuf->st_mode = S_IFREG | 0444;
117 stbuf->st_size = file_size;
126static void tfs_init(
void *userdata,
struct fuse_conn_info *conn) {
133static void tfs_destroy(
void *userarg)
143 memset(&e, 0,
sizeof(e));
147 else if (strcmp(name, FILE_NAME) == 0) {
153 e.attr_timeout = NO_TIMEOUT;
154 e.entry_timeout = NO_TIMEOUT;
155 if (tfs_stat(e.ino, &e.attr) != 0)
168 lookup_cnt -= nlookup;
180 memset(&stbuf, 0,
sizeof(stbuf));
181 if (tfs_stat(ino, &stbuf) != 0)
192static void dirbuf_add(
fuse_req_t req,
struct dirbuf *b,
const char *name,
195 size_t oldsize = b->size;
197 b->p = (
char *) realloc(b->p, b->size);
198 memset(&stbuf, 0,
sizeof(stbuf));
204#define min(x, y) ((x) < (y) ? (x) : (y))
206static int reply_buf_limited(
fuse_req_t req,
const char *buf,
size_t bufsize,
207 off_t off,
size_t maxsize) {
210 min(bufsize - off, maxsize));
219 if (ino != FUSE_ROOT_ID)
224 memset(&b, 0,
sizeof(b));
225 dirbuf_add(req, &b, FILE_NAME, FILE_INO);
226 reply_buf_limited(req, b.p, b.size, off, size);
238 if (ino == FUSE_ROOT_ID)
240 else if ((fi->
flags & O_ACCMODE) != O_RDONLY)
242 else if (ino == FILE_INO)
246 fprintf(stderr,
"Got open for non-existing inode!\n");
255 assert(ino == FILE_INO);
256 reply_buf_limited(req, file_contents, file_size, off, size);
261 .destroy = tfs_destroy,
262 .lookup = tfs_lookup,
263 .getattr = tfs_getattr,
264 .readdir = tfs_readdir,
267 .forget = tfs_forget,
270static void update_fs(
void) {
277 file_size = strftime(file_contents, MAX_STR_LEN,
278 "The current time is %H:%M:%S\n", now);
279 assert(file_size != 0);
282static void* update_fs_loop(
void *data) {
283 struct fuse_session *se = (
struct fuse_session*) data;
287 if (!options.no_notify && lookup_cnt) {
296 if ((ret != 0 && !is_stop) &&
297 ret != -ENOENT && ret != -EBADF && ret != -ENODEV) {
299 "ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n",
300 strerror(-ret), -ret);
304 sleep(options.update_interval);
309static void show_help(
const char *progname)
311 printf(
"usage: %s [options] <mountpoint>\n\n", progname);
312 printf(
"File-system specific options:\n"
313 " --update-interval=<secs> Update-rate of file system contents\n"
314 " --no-notify Disable kernel notifications\n"
318int main(
int argc,
char *argv[]) {
320 struct fuse_session *se;
329 if (fuse_parse_cmdline(&args, &opts) != 0) {
334 if (opts.show_help) {
340 }
else if (opts.show_version) {
350 se = fuse_session_new(&args, &tfs_oper,
351 sizeof(tfs_oper), NULL);
364 ret = pthread_create(&updater, NULL, update_fs_loop, (
void *)se);
366 fprintf(stderr,
"pthread_create failed with %s\n",
372 if (opts.singlethread)
375 config = fuse_loop_cfg_create();
376 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
377 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
378 ret = fuse_session_loop_mt(se, config);
379 fuse_loop_cfg_destroy(config);
390 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)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
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_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)
void fuse_lowlevel_help(void)
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
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(* init)(void *userdata, struct fuse_conn_info *conn)