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