libfuse: fuse-3.18.2/example/hello_ll.c Source File

libfuse

hello_ll.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file GPL2.txt.
7*/
8
25#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
26
27#include <fuse_lowlevel.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <unistd.h>
34#include <assert.h>
35
36static const char *hello_str = "Hello World!\n";
37static const char *hello_name = "hello";
38
39static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
40{
41 stbuf->st_ino = ino;
42 switch (ino) {
43 case 1:
44 stbuf->st_mode = S_IFDIR | 0755;
45 stbuf->st_nlink = 2;
46 break;
47
48 case 2:
49 stbuf->st_mode = S_IFREG | 0444;
50 stbuf->st_nlink = 1;
51 stbuf->st_size = strlen(hello_str);
52 break;
53
54 default:
55 return -1;
56 }
57 return 0;
58}
59
60static void hello_ll_init(void *userdata, struct fuse_conn_info *conn)
61{
62 (void)userdata;
63
64 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
65 conn->no_interrupt = 1;
66
67 /* Test setting flags the old way */
68 conn->want = FUSE_CAP_ASYNC_READ;
69 conn->want &= ~FUSE_CAP_ASYNC_READ;
70}
71
72static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
73 struct fuse_file_info *fi)
74{
75 struct stat stbuf;
76
77 (void) fi;
78
79 memset(&stbuf, 0, sizeof(stbuf));
80 if (hello_stat(ino, &stbuf) == -1)
81 fuse_reply_err(req, ENOENT);
82 else
83 fuse_reply_attr(req, &stbuf, 1.0);
84}
85
86static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
87{
88 struct fuse_entry_param e;
89
90 if (parent != 1 || strcmp(name, hello_name) != 0)
91 fuse_reply_err(req, ENOENT);
92 else {
93 memset(&e, 0, sizeof(e));
94 e.ino = 2;
95 e.attr_timeout = 1.0;
96 e.entry_timeout = 1.0;
97 hello_stat(e.ino, &e.attr);
98
99 fuse_reply_entry(req, &e);
100 }
101}
102
103struct dirbuf {
104 char *p;
105 size_t size;
106};
107
108static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
109 fuse_ino_t ino)
110{
111 struct stat stbuf;
112 size_t oldsize = b->size;
113 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
114 b->p = (char *) realloc(b->p, b->size);
115 memset(&stbuf, 0, sizeof(stbuf));
116 stbuf.st_ino = ino;
117 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
118 b->size);
119}
120
121#define min(x, y) ((x) < (y) ? (x) : (y))
122
123static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
124 off_t off, size_t maxsize)
125{
126 if (off < bufsize)
127 return fuse_reply_buf(req, buf + off,
128 min(bufsize - off, maxsize));
129 else
130 return fuse_reply_buf(req, NULL, 0);
131}
132
133static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
134 off_t off, struct fuse_file_info *fi)
135{
136 (void) fi;
137
138 if (ino != 1)
139 fuse_reply_err(req, ENOTDIR);
140 else {
141 struct dirbuf b;
142
143 memset(&b, 0, sizeof(b));
144 dirbuf_add(req, &b, ".", 1);
145 dirbuf_add(req, &b, "..", 1);
146 dirbuf_add(req, &b, hello_name, 2);
147 reply_buf_limited(req, b.p, b.size, off, size);
148 free(b.p);
149 }
150}
151
152static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
153 struct fuse_file_info *fi)
154{
155 if (ino != 2)
156 fuse_reply_err(req, EISDIR);
157 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
158 fuse_reply_err(req, EACCES);
159 else
160 fuse_reply_open(req, fi);
161}
162
163static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
164 off_t off, struct fuse_file_info *fi)
165{
166 (void) fi;
167
168 assert(ino == 2);
169 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
170}
171
172static void hello_ll_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
173 size_t size)
174{
175 (void)size;
176 assert(ino == 1 || ino == 2);
177 if (strcmp(name, "hello_ll_getxattr_name") == 0)
178 {
179 const char *buf = "hello_ll_getxattr_value";
180 fuse_reply_buf(req, buf, strlen(buf));
181 }
182 else
183 {
184 fuse_reply_err(req, ENOTSUP);
185 }
186}
187
188static void hello_ll_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
189 const char *value, size_t size, int flags)
190{
191 (void)flags;
192 (void)size;
193 assert(ino == 1 || ino == 2);
194 const char* exp_val = "hello_ll_setxattr_value";
195 if (strcmp(name, "hello_ll_setxattr_name") == 0 &&
196 strlen(exp_val) == size &&
197 strncmp(value, exp_val, size) == 0)
198 {
199 fuse_reply_err(req, 0);
200 }
201 else
202 {
203 fuse_reply_err(req, ENOTSUP);
204 }
205}
206
207static void hello_ll_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
208{
209 assert(ino == 1 || ino == 2);
210 if (strcmp(name, "hello_ll_removexattr_name") == 0)
211 {
212 fuse_reply_err(req, 0);
213 }
214 else
215 {
216 fuse_reply_err(req, ENOTSUP);
217 }
218}
219
220static const struct fuse_lowlevel_ops hello_ll_oper = {
221 .init = hello_ll_init,
222 .lookup = hello_ll_lookup,
223 .getattr = hello_ll_getattr,
224 .readdir = hello_ll_readdir,
225 .open = hello_ll_open,
226 .read = hello_ll_read,
227 .setxattr = hello_ll_setxattr,
228 .getxattr = hello_ll_getxattr,
229 .removexattr = hello_ll_removexattr,
230};
231
232int main(int argc, char *argv[])
233{
234 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
235 struct fuse_session *se;
236 struct fuse_cmdline_opts opts;
237 struct fuse_loop_config *config;
238 int ret = -1;
239
240 if (fuse_parse_cmdline(&args, &opts) != 0)
241 return 1;
242 if (opts.show_help) {
243 printf("usage: %s [options] <mountpoint>\n\n", argv[0]);
244 fuse_cmdline_help();
245 fuse_lowlevel_help();
246 ret = 0;
247 goto err_out1;
248 } else if (opts.show_version) {
249 printf("FUSE library version %s\n", fuse_pkgversion());
250 fuse_lowlevel_version();
251 ret = 0;
252 goto err_out1;
253 }
254
255 if(opts.mountpoint == NULL) {
256 printf("usage: %s [options] <mountpoint>\n", argv[0]);
257 printf(" %s --help\n", argv[0]);
258 ret = 1;
259 goto err_out1;
260 }
261
262 se = fuse_session_new(&args, &hello_ll_oper,
263 sizeof(hello_ll_oper), NULL);
264 if (se == NULL)
265 goto err_out1;
266
267 if (fuse_set_signal_handlers(se) != 0)
268 goto err_out2;
269
270 if (fuse_session_mount(se, opts.mountpoint) != 0)
271 goto err_out3;
272
273 fuse_daemonize(opts.foreground);
274
275 /* Block until ctrl+c or fusermount -u */
276 if (opts.singlethread)
277 ret = fuse_session_loop(se);
278 else {
279 config = fuse_loop_cfg_create();
280 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
281 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
282 ret = fuse_session_loop_mt(se, config);
283 fuse_loop_cfg_destroy(config);
284 config = NULL;
285 }
286
287 fuse_session_unmount(se);
288err_out3:
289 fuse_remove_signal_handlers(se);
290err_out2:
291 fuse_session_destroy(se);
292err_out1:
293 free(opts.mountpoint);
294 fuse_opt_free_args(&args);
295
296 return ret ? 1 : 0;
297}
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:140
FUSE_CAP_ASYNC_READ
#define FUSE_CAP_ASYNC_READ
Definition fuse_common.h:177
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition fuse.c:5211
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:187
fuse_daemonize
int fuse_daemonize(int foreground)
Definition helper.c:253
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:3010
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition fuse_lowlevel.c:505
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:331
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition fuse_lowlevel.c:524
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition fuse_lowlevel.c:428
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3539
fuse_cmdline_help
void fuse_cmdline_help(void)
Definition helper.c:130
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition fuse_lowlevel.c:3000
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3473
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition fuse_lowlevel.c:2993
fuse_ino_t
uint64_t fuse_ino_t
Definition fuse_lowlevel.h:47
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:286
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition fuse_lowlevel.c:460
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
fuse_args
Definition fuse_opt.h:109
fuse_args::argc
int argc
Definition fuse_opt.h:111
fuse_args::argv
char ** argv
Definition fuse_opt.h:114
fuse_cmdline_opts
Definition fuse_lowlevel.h:2003
fuse_conn_info
Definition fuse_common.h:538
fuse_conn_info::want
uint32_t want
Definition fuse_common.h:590
fuse_conn_info::no_interrupt
uint32_t no_interrupt
Definition fuse_common.h:677
fuse_entry_param
Definition fuse_lowlevel.h:60
fuse_file_info
Definition fuse_common.h:50
fuse_file_info::flags
int32_t flags
Definition fuse_common.h:52
fuse_loop_config
Definition fuse_common.h:139
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_lowlevel_ops::init
void(* init)(void *userdata, struct fuse_conn_info *conn)
Definition fuse_lowlevel.h:223