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