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
64static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
65 struct fuse_file_info *fi)
66{
67 struct stat stbuf;
68
69 (void) fi;
70
71 memset(&stbuf, 0, sizeof(stbuf));
72 if (hello_stat(ino, &stbuf) == -1)
73 fuse_reply_err(req, ENOENT);
74 else
75 fuse_reply_attr(req, &stbuf, 1.0);
76}
77
78static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
79{
80 struct fuse_entry_param e;
81
82 if (parent != 1 || strcmp(name, hello_name) != 0)
83 fuse_reply_err(req, ENOENT);
84 else {
85 memset(&e, 0, sizeof(e));
86 e.ino = 2;
87 e.attr_timeout = 1.0;
88 e.entry_timeout = 1.0;
89 hello_stat(e.ino, &e.attr);
90
91 fuse_reply_entry(req, &e);
92 }
93}
94
95struct dirbuf {
96 char *p;
97 size_t size;
98};
99
100static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
101 fuse_ino_t ino)
102{
103 struct stat stbuf;
104 size_t oldsize = b->size;
105 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
106 b->p = (char *) realloc(b->p, b->size);
107 memset(&stbuf, 0, sizeof(stbuf));
108 stbuf.st_ino = ino;
109 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
110 b->size);
111}
112
113#define min(x, y) ((x) < (y) ? (x) : (y))
114
115static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
116 off_t off, size_t maxsize)
117{
118 if (off < bufsize)
119 return fuse_reply_buf(req, buf + off,
120 min(bufsize - off, maxsize));
121 else
122 return fuse_reply_buf(req, NULL, 0);
123}
124
125static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
126 off_t off, struct fuse_file_info *fi)
127{
128 (void) fi;
129
130 if (ino != 1)
131 fuse_reply_err(req, ENOTDIR);
132 else {
133 struct dirbuf b;
134
135 memset(&b, 0, sizeof(b));
136 dirbuf_add(req, &b, ".", 1);
137 dirbuf_add(req, &b, "..", 1);
138 dirbuf_add(req, &b, hello_name, 2);
139 reply_buf_limited(req, b.p, b.size, off, size);
140 free(b.p);
141 }
142}
143
144static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
145 struct fuse_file_info *fi)
146{
147 if (ino != 2)
148 fuse_reply_err(req, EISDIR);
149 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
150 fuse_reply_err(req, EACCES);
151 else
152 fuse_reply_open(req, fi);
153}
154
155static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
156 off_t off, struct fuse_file_info *fi)
157{
158 (void) fi;
159
160 assert(ino == 2);
161 reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
162}
163
164static void hello_ll_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
165 size_t size)
166{
167 (void)size;
168 assert(ino == 1 || ino == 2);
169 if (strcmp(name, "hello_ll_getxattr_name") == 0)
170 {
171 const char *buf = "hello_ll_getxattr_value";
172 fuse_reply_buf(req, buf, strlen(buf));
173 }
174 else
175 {
176 fuse_reply_err(req, ENOTSUP);
177 }
178}
179
180static void hello_ll_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name,
181 const char *value, size_t size, int flags)
182{
183 (void)flags;
184 (void)size;
185 assert(ino == 1 || ino == 2);
186 const char* exp_val = "hello_ll_setxattr_value";
187 if (strcmp(name, "hello_ll_setxattr_name") == 0 &&
188 strlen(exp_val) == size &&
189 strncmp(value, exp_val, size) == 0)
190 {
191 fuse_reply_err(req, 0);
192 }
193 else
194 {
195 fuse_reply_err(req, ENOTSUP);
196 }
197}
198
199static void hello_ll_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name)
200{
201 assert(ino == 1 || ino == 2);
202 if (strcmp(name, "hello_ll_removexattr_name") == 0)
203 {
204 fuse_reply_err(req, 0);
205 }
206 else
207 {
208 fuse_reply_err(req, ENOTSUP);
209 }
210}
211
212static const struct fuse_lowlevel_ops hello_ll_oper = {
213 .init = hello_ll_init,
214 .lookup = hello_ll_lookup,
215 .getattr = hello_ll_getattr,
216 .readdir = hello_ll_readdir,
217 .open = hello_ll_open,
218 .read = hello_ll_read,
219 .setxattr = hello_ll_setxattr,
220 .getxattr = hello_ll_getxattr,
221 .removexattr = hello_ll_removexattr,
222};
223
224int main(int argc, char *argv[])
225{
226 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
227 struct fuse_session *se;
228 struct fuse_cmdline_opts opts;
229 struct fuse_loop_config *config;
230 int ret = -1;
231
232 if (fuse_parse_cmdline(&args, &opts) != 0)
233 return 1;
234 if (opts.show_help) {
235 printf("usage: %s [options] <mountpoint>\n\n", argv[0]);
238 ret = 0;
239 goto err_out1;
240 } else if (opts.show_version) {
241 printf("FUSE library version %s\n", fuse_pkgversion());
243 ret = 0;
244 goto err_out1;
245 }
246
247 if(opts.mountpoint == NULL) {
248 printf("usage: %s [options] <mountpoint>\n", argv[0]);
249 printf(" %s --help\n", argv[0]);
250 ret = 1;
251 goto err_out1;
252 }
253
254 se = fuse_session_new(&args, &hello_ll_oper,
255 sizeof(hello_ll_oper), NULL);
256 if (se == NULL)
257 goto err_out1;
258
259 if (fuse_set_signal_handlers(se) != 0)
260 goto err_out2;
261
262 if (fuse_session_mount(se, opts.mountpoint) != 0)
263 goto err_out3;
264
265 fuse_daemonize(opts.foreground);
266
267 /* Block until ctrl+c or fusermount -u */
268 if (opts.singlethread)
269 ret = fuse_session_loop(se);
270 else {
271 config = fuse_loop_cfg_create();
272 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
273 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
274 ret = fuse_session_loop_mt(se, config);
275 fuse_loop_cfg_destroy(config);
276 config = NULL;
277 }
278
280err_out3:
282err_out2:
284err_out1:
285 free(opts.mountpoint);
286 fuse_opt_free_args(&args);
287
288 return ret ? 1 : 0;
289}
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
Definition fuse.c:5218
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)