libfuse

notify_inval_entry.c
Go to the documentation of this file.
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file COPYING.
7 */
8
76 #define FUSE_USE_VERSION 31
77
78 #include <fuse_lowlevel.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <errno.h>
83 #include <fcntl.h>
84 #include <assert.h>
85 #include <stddef.h>
86 #include <unistd.h>
87 #include <pthread.h>
88
89 #define MAX_STR_LEN 128
90 static char file_name[MAX_STR_LEN];
91 static fuse_ino_t file_ino = 2;
92 static int lookup_cnt = 0;
93
94 /* Command line parsing */
95 struct options {
96 int no_notify;
97 float timeout;
98 int update_interval;
99 };
100 static struct options options = {
101 .timeout = 5,
102 .no_notify = 0,
103 .update_interval = 1,
104 };
105
106 #define OPTION(t, p) \
107 { t, offsetof(struct options, p), 1 }
108 static const struct fuse_opt option_spec[] = {
109 OPTION("--no-notify", no_notify),
110 OPTION("--update-interval=%d", update_interval),
111 OPTION("--timeout=%f", timeout),
112 FUSE_OPT_END
113 };
114
115 static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
116 stbuf->st_ino = ino;
117 if (ino == FUSE_ROOT_ID) {
118 stbuf->st_mode = S_IFDIR | 0755;
119 stbuf->st_nlink = 1;
120 }
121
122 else if (ino == file_ino) {
123 stbuf->st_mode = S_IFREG | 0000;
124 stbuf->st_nlink = 1;
125 stbuf->st_size = 0;
126 }
127
128 else
129 return -1;
130
131 return 0;
132 }
133
134 static void tfs_lookup(fuse_req_t req, fuse_ino_t parent,
135 const char *name) {
136 struct fuse_entry_param e;
137 memset(&e, 0, sizeof(e));
138
139 if (parent != FUSE_ROOT_ID)
140 goto err_out;
141 else if (strcmp(name, file_name) == 0) {
142 e.ino = file_ino;
143 lookup_cnt++;
144 } else
145 goto err_out;
146
147 e.attr_timeout = options.timeout;
148 e.entry_timeout = options.timeout;
149 if (tfs_stat(e.ino, &e.attr) != 0)
150 goto err_out;
151 fuse_reply_entry(req, &e);
152 return;
153
154 err_out:
155 fuse_reply_err(req, ENOENT);
156 }
157
158 static void tfs_forget (fuse_req_t req, fuse_ino_t ino,
159 uint64_t nlookup) {
160 (void) req;
161 if(ino == file_ino)
162 lookup_cnt -= nlookup;
163 else
164 assert(ino == FUSE_ROOT_ID);
165 fuse_reply_none(req);
166 }
167
168 static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
169 struct fuse_file_info *fi) {
170 struct stat stbuf;
171
172 (void) fi;
173
174 memset(&stbuf, 0, sizeof(stbuf));
175 if (tfs_stat(ino, &stbuf) != 0)
176 fuse_reply_err(req, ENOENT);
177 else
178 fuse_reply_attr(req, &stbuf, options.timeout);
179 }
180
181 struct dirbuf {
182 char *p;
183 size_t size;
184 };
185
186 static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
187 fuse_ino_t ino) {
188 struct stat stbuf;
189 size_t oldsize = b->size;
190 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
191 b->p = (char *) realloc(b->p, b->size);
192 memset(&stbuf, 0, sizeof(stbuf));
193 stbuf.st_ino = ino;
194 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
195 b->size);
196 }
197
198 #define min(x, y) ((x) < (y) ? (x) : (y))
199
200 static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
201 off_t off, size_t maxsize) {
202 if (off < bufsize)
203 return fuse_reply_buf(req, buf + off,
204 min(bufsize - off, maxsize));
205 else
206 return fuse_reply_buf(req, NULL, 0);
207 }
208
209 static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
210 off_t off, struct fuse_file_info *fi) {
211 (void) fi;
212
213 if (ino != FUSE_ROOT_ID)
214 fuse_reply_err(req, ENOTDIR);
215 else {
216 struct dirbuf b;
217
218 memset(&b, 0, sizeof(b));
219 dirbuf_add(req, &b, file_name, file_ino);
220 reply_buf_limited(req, b.p, b.size, off, size);
221 free(b.p);
222 }
223 }
224
225 static const struct fuse_lowlevel_ops tfs_oper = {
226 .lookup = tfs_lookup,
227 .getattr = tfs_getattr,
228 .readdir = tfs_readdir,
229 .forget = tfs_forget,
230 };
231
232 static void update_fs(void) {
233 time_t t;
234 struct tm *now;
235 ssize_t ret;
236
237 t = time(NULL);
238 now = localtime(&t);
239 assert(now != NULL);
240
241 ret = strftime(file_name, MAX_STR_LEN,
242 "Time_is_%Hh_%Mm_%Ss", now);
243 assert(ret != 0);
244 }
245
246 static void* update_fs_loop(void *data) {
247 struct fuse_session *se = (struct fuse_session*) data;
248 char *old_name;
249
250 while(1) {
251 old_name = strdup(file_name);
252 update_fs();
253 if (!options.no_notify && lookup_cnt)
254 assert(fuse_lowlevel_notify_inval_entry
255 (se, FUSE_ROOT_ID, old_name, strlen(old_name)) == 0);
256 free(old_name);
257 sleep(options.update_interval);
258 }
259 return NULL;
260 }
261
262 static void show_help(const char *progname)
263 {
264 printf("usage: %s [options] <mountpoint>\n\n", progname);
265 printf("File-system specific options:\n"
266 " --timeout=<secs> Timeout for kernel caches\n"
267 " --update-interval=<secs> Update-rate of file system contents\n"
268 " --no-notify Disable kernel notifications\n"
269 "\n");
270 }
271
272 int main(int argc, char *argv[]) {
273 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
274 struct fuse_session *se;
275 struct fuse_cmdline_opts opts;
276 pthread_t updater;
277 int ret = -1;
278
279 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
280 return 1;
281
282 if (fuse_parse_cmdline(&args, &opts) != 0)
283 return 1;
284 if (opts.show_help) {
285 show_help(argv[0]);
286 fuse_cmdline_help();
287 fuse_lowlevel_help();
288 ret = 0;
289 goto err_out1;
290 } else if (opts.show_version) {
291 printf("FUSE library version %s\n", fuse_pkgversion());
292 fuse_lowlevel_version();
293 ret = 0;
294 goto err_out1;
295 }
296
297 /* Initial contents */
298 update_fs();
299
300 se = fuse_session_new(&args, &tfs_oper,
301 sizeof(tfs_oper), NULL);
302 if (se == NULL)
303 goto err_out1;
304
305 if (fuse_set_signal_handlers(se) != 0)
306 goto err_out2;
307
308 if (fuse_session_mount(se, opts.mountpoint) != 0)
309 goto err_out3;
310
311 fuse_daemonize(opts.foreground);
312
313 /* Start thread to update file contents */
314 ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
315 if (ret != 0) {
316 fprintf(stderr, "pthread_create failed with %s\n",
317 strerror(ret));
318 goto err_out3;
319 }
320
321 /* Block until ctrl+c or fusermount -u */
322 if (opts.singlethread)
323 ret = fuse_session_loop(se);
324 else
325 ret = fuse_session_loop_mt(se, opts.clone_fd);
326
327 fuse_session_unmount(se);
328 err_out3:
329 fuse_remove_signal_handlers(se);
330 err_out2:
331 fuse_session_destroy(se);
332 err_out1:
333 free(opts.mountpoint);
334 fuse_opt_free_args(&args);
335
336 return ret ? 1 : 0;
337 }
338
339
fuse_cmdline_help
void fuse_cmdline_help(void)
Definition: helper.c:129
fuse_lowlevel_ops::lookup
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)
Definition: fuse_lowlevel.h:215
fuse_opt_parse
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
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition: fuse_lowlevel.c:2981
fuse_lowlevel_notify_inval_entry
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
Definition: fuse_lowlevel.c:2241
fuse_req_t
struct fuse_req * fuse_req_t
Definition: fuse_lowlevel.h:49
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:62
fuse_reply_none
void fuse_reply_none(fuse_req_t req)
Definition: fuse_lowlevel.c:317
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition: fuse_lowlevel.c:433
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition: fuse_opt.h:123
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition: fuse_lowlevel.c:472
fuse_entry_param
Definition: fuse_lowlevel.h:59
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition: fuse_lowlevel.c:2683
fuse_file_info
Definition: fuse_common.h:44
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition: fuse.c:5117
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition: fuse_loop.c:19
FUSE_ROOT_ID
#define FUSE_ROOT_ID
Definition: fuse_lowlevel.h:43
fuse_parse_cmdline
int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
Definition: helper.c:202
fuse_session_new
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
Definition: fuse_lowlevel.c:2886
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:79
FUSE_OPT_END
#define FUSE_OPT_END
Definition: fuse_opt.h:104
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition: fuse_lowlevel.c:3036
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition: fuse_lowlevel.c:312
fuse_add_direntry
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
Definition: fuse_lowlevel.c:267
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition: fuse_opt.c:34
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition: fuse_lowlevel.c:401
fuse_daemonize
int fuse_daemonize(int foreground)
Definition: helper.c:225
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition: fuse_lowlevel.c:2700
fuse_lowlevel_ops
Definition: fuse_lowlevel.h:172
fuse_opt
Definition: fuse_opt.h:77
fuse_args
Definition: fuse_opt.h:109
fuse_ino_t
uint64_t fuse_ino_t
Definition: fuse_lowlevel.h:46
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition: fuse_lowlevel.c:2690