libfuse
invalidate_path.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4 (C) 2017 EditShare LLC <slawek.rudnicki@editshare.com>
5
6 This program can be distributed under the terms of the GNU GPLv2.
7 See the file GPL2.txt.
8 */
9
27
28#define FUSE_USE_VERSION 34
29
30#include <fuse.h>
31#include <fuse_lowlevel.h> /* for fuse_cmdline_opts */
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <assert.h>
39#include <stddef.h>
40#include <unistd.h>
41#include <pthread.h>
42
43/* We can't actually tell the kernel that there is no
44 timeout, so we just send a big value */
45#define NO_TIMEOUT 500000
46
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
52
53static char time_file_contents[MAX_STR_LEN];
54static size_t grow_file_size;
55
56/* Command line parsing */
57struct options {
58 int no_notify;
59 int update_interval;
60};
61static struct options options = {
62 .no_notify = 0,
63 .update_interval = 1,
64};
65
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),
71};
72
73static void *xmp_init(struct fuse_conn_info *conn, struct fuse_config *cfg)
74{
75 (void) conn;
76 cfg->entry_timeout = NO_TIMEOUT;
77 cfg->attr_timeout = NO_TIMEOUT;
78 cfg->negative_timeout = 0;
79
80 return NULL;
81}
82
83static int xmp_getattr(const char *path,
84 struct stat *stbuf, struct fuse_file_info* fi) {
85 (void) fi;
86 if (strcmp(path, "/") == 0) {
87 stbuf->st_ino = 1;
88 stbuf->st_mode = S_IFDIR | 0755;
89 stbuf->st_nlink = 1;
90 } else if (strcmp(path, "/" TIME_FILE_NAME) == 0) {
91 stbuf->st_ino = TIME_FILE_INO;
92 stbuf->st_mode = S_IFREG | 0444;
93 stbuf->st_nlink = 1;
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;
98 stbuf->st_nlink = 1;
99 stbuf->st_size = grow_file_size;
100 } else {
101 return -ENOENT;
102 }
103
104 return 0;
105}
106
107static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
108 off_t offset, struct fuse_file_info *fi,
109 enum fuse_readdir_flags flags) {
110 (void) fi;
111 (void) offset;
112 (void) flags;
113 if (strcmp(path, "/") != 0) {
114 return -ENOTDIR;
115 } else {
116 (void) filler;
117 (void) buf;
118 struct stat file_stat;
119 xmp_getattr("/" TIME_FILE_NAME, &file_stat, NULL);
120 filler(buf, TIME_FILE_NAME, &file_stat, 0, FUSE_FILL_DIR_DEFAULTS);
121 xmp_getattr("/" GROW_FILE_NAME, &file_stat, NULL);
122 filler(buf, GROW_FILE_NAME, &file_stat, 0, FUSE_FILL_DIR_DEFAULTS);
123 return 0;
124 }
125}
126
127static int xmp_open(const char *path, struct fuse_file_info *fi) {
128 (void) path;
129 /* Make cache persistent even if file is closed,
130 this makes it easier to see the effects */
131 fi->keep_cache = 1;
132 return 0;
133}
134
135static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
136 struct fuse_file_info *fi) {
137 (void) fi;
138 (void) 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
142 ? size
143 : file_length - offset;
144 memcpy(buf, time_file_contents, to_copy);
145 return to_copy;
146 } else {
147 assert(strcmp(path, "/" GROW_FILE_NAME) == 0);
148 int to_copy = offset + size <= grow_file_size
149 ? size
150 : grow_file_size - offset;
151 memset(buf, 'x', to_copy);
152 return to_copy;
153 }
154}
155
156static const struct fuse_operations xmp_oper = {
157 .init = xmp_init,
158 .getattr = xmp_getattr,
159 .readdir = xmp_readdir,
160 .open = xmp_open,
161 .read = xmp_read,
162};
163
164static void update_fs(void) {
165 static int count = 0;
166 struct tm tmbuf;
167 struct tm *now;
168 time_t t;
169 t = time(NULL);
170 now = localtime_r(&t, &tmbuf);
171 assert(now != NULL);
172
173 int time_file_size = strftime(time_file_contents, MAX_STR_LEN,
174 "The current time is %H:%M:%S\n", now);
175 assert(time_file_size != 0);
176
177 grow_file_size = count++;
178}
179
180static int invalidate(struct fuse *fuse, const char *path) {
181 int status = fuse_invalidate_path(fuse, path);
182 if (status == -ENOENT) {
183 return 0;
184 } else {
185 return status;
186 }
187}
188
189static void* update_fs_loop(void *data) {
190 struct fuse *fuse = (struct fuse*) data;
191
192 while (1) {
193 update_fs();
194 if (!options.no_notify) {
195 assert(invalidate(fuse, "/" TIME_FILE_NAME) == 0);
196 assert(invalidate(fuse, "/" GROW_FILE_NAME) == 0);
197 }
198 sleep(options.update_interval);
199 }
200 return NULL;
201}
202
203static void show_help(const char *progname)
204{
205 printf("usage: %s [options] <mountpoint>\n\n", progname);
206 printf("File-system specific options:\n"
207 " --update-interval=<secs> Update-rate of file system contents\n"
208 " --no-notify Disable kernel notifications\n"
209 "\n");
210}
211
212int main(int argc, char *argv[]) {
213 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
214 struct fuse *fuse;
215 struct fuse_cmdline_opts opts;
216 struct fuse_loop_config config;
217 int res;
218
219 /* Initialize the files */
220 update_fs();
221
222 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
223 return 1;
224
225 if (fuse_parse_cmdline(&args, &opts) != 0)
226 return 1;
227
228 if (opts.show_version) {
229 printf("FUSE library version %s\n", fuse_pkgversion());
231 res = 0;
232 goto out1;
233 } else if (opts.show_help) {
234 show_help(argv[0]);
236 fuse_lib_help(&args);
237 res = 0;
238 goto out1;
239 } else if (!opts.mountpoint) {
240 fprintf(stderr, "error: no mountpoint specified\n");
241 res = 1;
242 goto out1;
243 }
244
245 fuse = fuse_new(&args, &xmp_oper, sizeof(xmp_oper), NULL);
246 if (fuse == NULL) {
247 res = 1;
248 goto out1;
249 }
250
251 if (fuse_mount(fuse,opts.mountpoint) != 0) {
252 res = 1;
253 goto out2;
254 }
255
256 if (fuse_daemonize(opts.foreground) != 0) {
257 res = 1;
258 goto out3;
259 }
260
261 pthread_t updater; /* Start thread to update file contents */
262 int ret = pthread_create(&updater, NULL, update_fs_loop, (void *) fuse);
263 if (ret != 0) {
264 fprintf(stderr, "pthread_create failed with %s\n", strerror(ret));
265 return 1;
266 };
267
268 struct fuse_session *se = fuse_get_session(fuse);
269 if (fuse_set_signal_handlers(se) != 0) {
270 res = 1;
271 goto out3;
272 }
273
274 if (opts.singlethread)
275 res = fuse_loop(fuse);
276 else {
277 config.clone_fd = opts.clone_fd;
278 config.max_idle_threads = opts.max_idle_threads;
279 res = fuse_loop_mt(fuse, &config);
280 }
281 if (res)
282 res = 1;
283
285out3:
286 fuse_unmount(fuse);
287out2:
288 fuse_destroy(fuse);
289out1:
290 free(opts.mountpoint);
291 fuse_opt_free_args(&args);
292 return res;
293}
int fuse_mount(struct fuse *f, const char *mountpoint)
Definition fuse.c:5260
void fuse_destroy(struct fuse *f)
Definition fuse.c:5209
int fuse_invalidate_path(struct fuse *f, const char *path)
Definition fuse.c:4740
int fuse_loop(struct fuse *f)
Definition fuse.c:4644
int(* fuse_fill_dir_t)(void *buf, const char *name, const struct stat *stbuf, off_t off, enum fuse_fill_dir_flags flags)
Definition fuse.h:93
void fuse_lib_help(struct fuse_args *args)
Definition fuse.c:4813
struct fuse_session * fuse_get_session(struct fuse *f)
Definition fuse.c:4588
void fuse_unmount(struct fuse *f)
Definition fuse.c:5265
@ FUSE_FILL_DIR_DEFAULTS
Definition fuse.h:74
fuse_readdir_flags
Definition fuse.h:45
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
Definition fuse.c:5274
void fuse_remove_signal_handlers(struct fuse_session *se)
int fuse_daemonize(int foreground)
Definition helper.c:253
void fuse_cmdline_help(void)
Definition helper.c:130
void fuse_lowlevel_version(void)
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition fuse_opt.c:398
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
#define FUSE_OPT_END
Definition fuse_opt.h:104
char ** argv
Definition fuse_opt.h:114
double entry_timeout
Definition fuse.h:133
double negative_timeout
Definition fuse.h:143
double attr_timeout
Definition fuse.h:149
uint32_t keep_cache
Definition fuse_common.h:69
unsigned long offset
Definition fuse_opt.h:85