28#define FUSE_USE_VERSION 34
45#define NO_TIMEOUT 500000
47#define MAX_STR_LEN 128
48#define TIME_FILE_NAME "current_time"
49#define TIME_FILE_INO 2
50#define GROW_FILE_NAME "growing"
51#define GROW_FILE_INO 3
53static char time_file_contents[MAX_STR_LEN];
54static size_t grow_file_size;
61static struct options options = {
66#define OPTION(t, p) { t, offsetof(struct options, p), 1 }
67static const struct fuse_opt option_spec[] = {
68 OPTION(
"--no-notify", no_notify),
69 OPTION(
"--update-interval=%d", update_interval),
83static int xmp_getattr(
const char *path,
86 if (strcmp(path,
"/") == 0) {
88 stbuf->st_mode = S_IFDIR | 0755;
90 }
else if (strcmp(path,
"/" TIME_FILE_NAME) == 0) {
91 stbuf->st_ino = TIME_FILE_INO;
92 stbuf->st_mode = S_IFREG | 0444;
94 stbuf->st_size = strlen(time_file_contents);
95 }
else if (strcmp(path,
"/" GROW_FILE_NAME) == 0) {
96 stbuf->st_ino = GROW_FILE_INO;
97 stbuf->st_mode = S_IFREG | 0444;
99 stbuf->st_size = grow_file_size;
107static int xmp_readdir(
const char *path,
void *buf,
fuse_fill_dir_t filler,
113 if (strcmp(path,
"/") != 0) {
118 struct stat file_stat;
119 xmp_getattr(
"/" TIME_FILE_NAME, &file_stat, NULL);
120 filler(buf, TIME_FILE_NAME, &file_stat, 0, 0);
121 xmp_getattr(
"/" GROW_FILE_NAME, &file_stat, NULL);
122 filler(buf, GROW_FILE_NAME, &file_stat, 0, 0);
135static int xmp_read(
const char *path,
char *buf,
size_t size, off_t offset,
139 if (strcmp(path,
"/" TIME_FILE_NAME) == 0) {
140 int file_length = strlen(time_file_contents);
141 int to_copy = offset + size <= file_length
143 : file_length - offset;
144 memcpy(buf, time_file_contents, to_copy);
147 assert(strcmp(path,
"/" GROW_FILE_NAME) == 0);
148 int to_copy = offset + size <= grow_file_size
150 : grow_file_size - offset;
151 memset(buf,
'x', to_copy);
158 .getattr = xmp_getattr,
159 .readdir = xmp_readdir,
164static void update_fs(
void) {
165 static int count = 0;
172 int time_file_size = strftime(time_file_contents, MAX_STR_LEN,
173 "The current time is %H:%M:%S\n", now);
174 assert(time_file_size != 0);
176 grow_file_size = count++;
179static int invalidate(
struct fuse *fuse,
const char *path) {
181 if (status == -ENOENT) {
188static void* update_fs_loop(
void *data) {
189 struct fuse *fuse = (
struct fuse*) data;
193 if (!options.no_notify) {
194 assert(invalidate(fuse,
"/" TIME_FILE_NAME) == 0);
195 assert(invalidate(fuse,
"/" GROW_FILE_NAME) == 0);
197 sleep(options.update_interval);
202static void show_help(
const char *progname)
204 printf(
"usage: %s [options] <mountpoint>\n\n", progname);
205 printf(
"File-system specific options:\n"
206 " --update-interval=<secs> Update-rate of file system contents\n"
207 " --no-notify Disable kernel notifications\n"
211int main(
int argc,
char *argv[]) {
224 if (fuse_parse_cmdline(&args, &opts) != 0)
227 if (opts.show_version) {
232 }
else if (opts.show_help) {
238 }
else if (!opts.mountpoint) {
239 fprintf(stderr,
"error: no mountpoint specified\n");
244 fuse = fuse_new(&args, &xmp_oper,
sizeof(xmp_oper), NULL);
261 int ret = pthread_create(&updater, NULL, update_fs_loop, (
void *) fuse);
263 fprintf(stderr,
"pthread_create failed with %s\n", strerror(ret));
273 if (opts.singlethread)
276 config.clone_fd = opts.clone_fd;
277 config.max_idle_threads = opts.max_idle_threads;
278 res = fuse_loop_mt(fuse, &config);
289 free(opts.mountpoint);
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)