61#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12) 
   63#include <fuse_lowlevel.h> 
   77#define NO_TIMEOUT 500000 
   79#define MAX_STR_LEN 128 
   81#define FILE_NAME "current_time" 
   82static char file_contents[MAX_STR_LEN];
 
   83static int lookup_cnt = 0;
 
   84static int open_cnt = 0;
 
   85static size_t file_size;
 
   86static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 
   90static int retrieve_status = 0;
 
   92static bool is_umount = 
false;
 
   95static pthread_t updater;
 
  103static struct options options = {
 
  105    .update_interval = 1,
 
  108#define OPTION(t, p)                           \ 
  109    { t, offsetof(struct options, p), 1 } 
  110static const struct fuse_opt option_spec[] = {
 
  111    OPTION(
"--no-notify", no_notify),
 
  112    OPTION(
"--update-interval=%d", update_interval),
 
  116static int tfs_stat(
fuse_ino_t ino, 
struct stat *stbuf) {
 
  118    if (ino == FUSE_ROOT_ID) {
 
  119        stbuf->st_mode = S_IFDIR | 0755;
 
  123    else if (ino == FILE_INO) {
 
  124        stbuf->st_mode = S_IFREG | 0444;
 
  126        stbuf->st_size = file_size;
 
  135static void tfs_init(
void *userdata, 
struct fuse_conn_info *conn) {
 
  145    memset(&e, 0, 
sizeof(e));
 
  149    else if (strcmp(name, FILE_NAME) == 0) {
 
  154    e.attr_timeout = NO_TIMEOUT;
 
  155    e.entry_timeout = NO_TIMEOUT;
 
  156    if (tfs_stat(e.ino, &e.attr) != 0)
 
  165    if (e.ino == FILE_INO) {
 
  166        pthread_mutex_lock(&lock);
 
  168        pthread_mutex_unlock(&lock);
 
  180    if(
ino == FILE_INO) {
 
  181        pthread_mutex_lock(&lock);
 
  182        lookup_cnt -= nlookup;
 
  183        pthread_mutex_unlock(&lock);
 
  195    memset(&stbuf, 0, 
sizeof(stbuf));
 
  196    if (tfs_stat(ino, &stbuf) != 0)
 
  207static void dirbuf_add(
fuse_req_t req, 
struct dirbuf *b, 
const char *name,
 
  210    size_t oldsize = b->size;
 
  212    b->p = (
char *) realloc(b->p, b->size);
 
  213    memset(&stbuf, 0, 
sizeof(stbuf));
 
  219#define min(x, y) ((x) < (y) ? (x) : (y)) 
  221static int reply_buf_limited(
fuse_req_t req, 
const char *buf, 
size_t bufsize,
 
  222                             off_t off, 
size_t maxsize) {
 
  225                              min(bufsize - off, maxsize));
 
  234    if (ino != FUSE_ROOT_ID)
 
  239        memset(&b, 0, 
sizeof(b));
 
  240        dirbuf_add(req, &b, FILE_NAME, FILE_INO);
 
  241        reply_buf_limited(req, b.p, b.size, off, size);
 
  253    if (ino == FUSE_ROOT_ID)
 
  255    else if ((fi->
flags & O_ACCMODE) != O_RDONLY)
 
  257    else if (ino == FILE_INO) {
 
  259        pthread_mutex_lock(&lock);
 
  261        pthread_mutex_unlock(&lock);
 
  264        fprintf(stderr, 
"Got open for non-existing inode!\n");
 
  273    assert(ino == FILE_INO);
 
  274    reply_buf_limited(req, file_contents, file_size, off, size);
 
  280    char buf[MAX_STR_LEN];
 
  284    assert(ino == FILE_INO);
 
  286    expected = (
char*) cookie;
 
  291    bufv.buf[0].size = MAX_STR_LEN;
 
  292    bufv.buf[0].mem = 
buf;
 
  293    bufv.buf[0].
flags = 0;
 
  297    assert(strncmp(
buf, expected, ret) == 0);
 
  303static void tfs_destroy(
void *userdata)
 
  309        pthread_join(updater, NULL);
 
  315    .lookup     = tfs_lookup,
 
  316    .getattr    = tfs_getattr,
 
  317    .readdir    = tfs_readdir,
 
  320    .forget     = tfs_forget,
 
  321    .retrieve_reply = tfs_retrieve_reply,
 
  322    .destroy    = tfs_destroy,
 
  325static void update_fs(
void) {
 
  332    file_size = strftime(file_contents, MAX_STR_LEN,
 
  333                         "The current time is %H:%M:%S\n", now);
 
  334    assert(file_size != 0);
 
  337static void* update_fs_loop(
void *data) {
 
  338    struct fuse_session *se = (
struct fuse_session*) data;
 
  344        pthread_mutex_lock(&lock);
 
  345        if (!options.no_notify && open_cnt && lookup_cnt) {
 
  351            bufv.buf[0].size = file_size;
 
  352            bufv.buf[0].mem = file_contents;
 
  353            bufv.buf[0].flags = 0;
 
  362            if ((ret != 0 && !is_umount) &&
 
  363                ret != -ENOENT && ret != -EBADF && ret != -ENODEV) {
 
  365                        "ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n",
 
  366                        strerror(-ret), -ret);
 
  373                                                0, (
void*) strdup(file_contents));
 
  374            assert((ret == 0 || is_umount) || ret == -ENOENT || ret == -EBADF ||
 
  376            if(retrieve_status == 0)
 
  379        pthread_mutex_unlock(&lock);
 
  380        sleep(options.update_interval);
 
  385static void show_help(
const char *progname)
 
  387    printf(
"usage: %s [options] <mountpoint>\n\n", progname);
 
  388    printf(
"File-system specific options:\n" 
  389               "    --update-interval=<secs>  Update-rate of file system contents\n" 
  390               "    --no-notify            Disable kernel notifications\n" 
  394int main(
int argc, 
char *argv[]) {
 
  396    struct fuse_session *se;
 
  404    if (fuse_parse_cmdline(&args, &opts) != 0)
 
  406    if (opts.show_help) {
 
  412    } 
else if (opts.show_version) {
 
  422    se = fuse_session_new(&args, &tfs_oper,
 
  423                          sizeof(tfs_oper), NULL);
 
  436    ret = pthread_create(&updater, NULL, update_fs_loop, (
void *)se);
 
  438        fprintf(stderr, 
"pthread_create failed with %s\n",
 
  444    if (opts.singlethread)
 
  447        config = fuse_loop_cfg_create();
 
  448        fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
 
  449        fuse_loop_cfg_set_max_threads(config, opts.max_threads);
 
  450        ret = fuse_session_loop_mt(se, config);
 
  451        fuse_loop_cfg_destroy(config);
 
  455    assert(retrieve_status != 1);
 
  462    free(opts.mountpoint);
 
int fuse_set_signal_handlers(struct fuse_session *se)
ssize_t fuse_buf_copy(struct fuse_bufvec *dst, struct fuse_bufvec *src, enum fuse_buf_copy_flags flags)
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_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino, size_t size, off_t offset, void *cookie)
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_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)
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)
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)
enum fuse_buf_flags flags
void(* init)(void *userdata, struct fuse_conn_info *conn)