#define FUSE_USE_VERSION 34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <assert.h>
#include <stddef.h>
#include <unistd.h>
#include <pthread.h>
#define NO_TIMEOUT 500000
#define MAX_STR_LEN 128
#define TIME_FILE_NAME "current_time"
#define TIME_FILE_INO 2
#define GROW_FILE_NAME "growing"
#define GROW_FILE_INO 3
static char time_file_contents[MAX_STR_LEN];
static size_t grow_file_size;
struct options {
int no_notify;
int update_interval;
};
static struct options options = {
.no_notify = 0,
.update_interval = 1,
};
#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),
};
{
(void) conn;
return NULL;
}
static int xmp_getattr(const char *path,
(void) fi;
if (strcmp(path, "/") == 0) {
stbuf->st_ino = 1;
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1;
} else if (strcmp(path, "/" TIME_FILE_NAME) == 0) {
stbuf->st_ino = TIME_FILE_INO;
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = strlen(time_file_contents);
} else if (strcmp(path, "/" GROW_FILE_NAME) == 0) {
stbuf->st_ino = GROW_FILE_INO;
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = grow_file_size;
} else {
return -ENOENT;
}
return 0;
}
static int xmp_readdir(
const char *path,
void *buf,
fuse_fill_dir_t filler,
(void) fi;
(void) flags;
if (strcmp(path, "/") != 0) {
return -ENOTDIR;
} else {
(void) filler;
(void) buf;
struct stat file_stat;
xmp_getattr("/" TIME_FILE_NAME, &file_stat, NULL);
filler(buf, TIME_FILE_NAME, &file_stat, 0, 0);
xmp_getattr("/" GROW_FILE_NAME, &file_stat, NULL);
filler(buf, GROW_FILE_NAME, &file_stat, 0, 0);
return 0;
}
}
(void) path;
return 0;
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
(void) fi;
(void) offset;
if (strcmp(path, "/" TIME_FILE_NAME) == 0) {
int file_length = strlen(time_file_contents);
int to_copy = offset + size <= file_length
? size
: file_length - offset;
memcpy(buf, time_file_contents, to_copy);
return to_copy;
} else {
assert(strcmp(path, "/" GROW_FILE_NAME) == 0);
int to_copy = offset + size <= grow_file_size
? size
: grow_file_size - offset;
memset(buf, 'x', to_copy);
return to_copy;
}
}
.getattr = xmp_getattr,
.readdir = xmp_readdir,
.open = xmp_open,
.read = xmp_read,
};
static void update_fs(void) {
static int count = 0;
struct tm *now;
time_t t;
t = time(NULL);
now = localtime(&t);
assert(now != NULL);
int time_file_size = strftime(time_file_contents, MAX_STR_LEN,
"The current time is %H:%M:%S\n", now);
assert(time_file_size != 0);
grow_file_size = count++;
}
static int invalidate(struct fuse *fuse, const char *path) {
if (status == -ENOENT) {
return 0;
} else {
return status;
}
}
static void* update_fs_loop(void *data) {
struct fuse *fuse = (struct fuse*) data;
while (1) {
update_fs();
if (!options.no_notify) {
assert(invalidate(fuse, "/" TIME_FILE_NAME) == 0);
assert(invalidate(fuse, "/" GROW_FILE_NAME) == 0);
}
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"
" --update-interval=<secs> Update-rate of file system contents\n"
" --no-notify Disable kernel notifications\n"
"\n");
}
int main(int argc, char *argv[]) {
struct fuse *fuse;
int res;
update_fs();
return 1;
if (fuse_parse_cmdline(&args, &opts) != 0)
return 1;
if (opts.show_version) {
res = 0;
goto out1;
} else if (opts.show_help) {
show_help(argv[0]);
res = 0;
goto out1;
} else if (!opts.mountpoint) {
fprintf(stderr, "error: no mountpoint specified\n");
res = 1;
goto out1;
}
fuse = fuse_new(&args, &xmp_oper, sizeof(xmp_oper), NULL);
if (fuse == NULL) {
res = 1;
goto out1;
}
res = 1;
goto out2;
}
res = 1;
goto out3;
}
pthread_t updater;
int ret = pthread_create(&updater, NULL, update_fs_loop, (void *) fuse);
if (ret != 0) {
fprintf(stderr, "pthread_create failed with %s\n", strerror(ret));
return 1;
};
res = 1;
goto out3;
}
if (opts.singlethread)
else {
config.clone_fd = opts.clone_fd;
config.max_idle_threads = opts.max_idle_threads;
res = fuse_loop_mt(fuse, &config);
}
if (res)
res = 1;
out3:
out2:
out1:
free(opts.mountpoint);
return res;
}
int fuse_mount(struct fuse *f, const char *mountpoint)
void fuse_destroy(struct fuse *f)
int fuse_invalidate_path(struct fuse *f, const char *path)
int fuse_loop(struct fuse *f)
int(* fuse_fill_dir_t)(void *buf, const char *name, const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
void fuse_lib_help(struct fuse_args *args)
struct fuse_session * fuse_get_session(struct fuse *f)
void fuse_unmount(struct fuse *f)
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_cmdline_help(void)
void fuse_lowlevel_version(void)
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)(struct fuse_conn_info *conn, struct fuse_config *cfg)