libfuse
test_api_30.c
1/*
2 * FUSE: Filesystem in Userspace
3 *
4 * Minimal low level filesystem built against the FUSE_USE_VERSION 30 API,
5 * so that the old API keeps being compiled and exercised.
6 *
7 * This program can be distributed under the terms of the GNU GPLv2.
8 * See the file GPL2.txt.
9 */
10
11#define FUSE_USE_VERSION 30
12
13/* the high level header has to keep building at this version as well */
14#include <fuse.h>
15
16#include <fuse_config.h>
17#include <fuse_lowlevel.h>
18#include <assert.h>
19#include <dirent.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <pthread.h>
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
29#ifndef __linux__
30#include <limits.h>
31#else
32#include <linux/limits.h>
33#endif
34
35#define FILE_INO 2
36
37static const char file_name[] = "hello";
38static const char file_contents[] = "Hello World!\n";
39
40static int tfs_stat(fuse_ino_t ino, struct stat *stbuf)
41{
42 stbuf->st_ino = ino;
43 switch (ino) {
44 case FUSE_ROOT_ID:
45 stbuf->st_mode = S_IFDIR | 0755;
46 stbuf->st_nlink = 2;
47 break;
48
49 case FILE_INO:
50 stbuf->st_mode = S_IFREG | 0444;
51 stbuf->st_nlink = 1;
52 stbuf->st_size = strlen(file_contents);
53 break;
54
55 default:
56 return -1;
57 }
58 return 0;
59}
60
61static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
62{
63 struct fuse_entry_param e;
64
65 if (parent != FUSE_ROOT_ID || strcmp(name, file_name) != 0) {
66 fuse_reply_err(req, ENOENT);
67 return;
68 }
69
70 memset(&e, 0, sizeof(e));
71 e.ino = FILE_INO;
72 e.attr_timeout = 1.0;
73 e.entry_timeout = 1.0;
74 tfs_stat(e.ino, &e.attr);
75
76 fuse_reply_entry(req, &e);
77}
78
79static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
80 struct fuse_file_info *fi)
81{
82 struct stat stbuf;
83
84 (void) fi;
85
86 memset(&stbuf, 0, sizeof(stbuf));
87 if (tfs_stat(ino, &stbuf) == -1)
88 fuse_reply_err(req, ENOENT);
89 else
90 fuse_reply_attr(req, &stbuf, 1.0);
91}
92
93struct dirbuf {
94 char *p;
95 size_t size;
96};
97
98static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
99 fuse_ino_t ino)
100{
101 struct stat stbuf;
102 size_t oldsize = b->size;
103
104 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
105 b->p = realloc(b->p, b->size);
106 assert(b->p != NULL);
107
108 memset(&stbuf, 0, sizeof(stbuf));
109 stbuf.st_ino = ino;
110 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
111 b->size);
112}
113
114static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
115 off_t off, size_t maxsize)
116{
117 size_t offset = (size_t) off;
118
119 if (off < 0 || offset >= bufsize)
120 return fuse_reply_buf(req, NULL, 0);
121
122 if (bufsize - offset < maxsize)
123 maxsize = bufsize - offset;
124
125 return fuse_reply_buf(req, buf + offset, maxsize);
126}
127
128static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
129 off_t off, struct fuse_file_info *fi)
130{
131 struct dirbuf b;
132
133 (void) fi;
134
135 if (ino != FUSE_ROOT_ID) {
136 fuse_reply_err(req, ENOTDIR);
137 return;
138 }
139
140 memset(&b, 0, sizeof(b));
141 dirbuf_add(req, &b, ".", FUSE_ROOT_ID);
142 dirbuf_add(req, &b, "..", FUSE_ROOT_ID);
143 dirbuf_add(req, &b, file_name, FILE_INO);
144 reply_buf_limited(req, b.p, b.size, off, size);
145 free(b.p);
146}
147
148static void tfs_open(fuse_req_t req, fuse_ino_t ino,
149 struct fuse_file_info *fi)
150{
151 if (ino != FILE_INO)
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 tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
160 struct fuse_file_info *fi)
161{
162 (void) fi;
163
164 assert(ino == FILE_INO);
165 reply_buf_limited(req, file_contents, strlen(file_contents), off, size);
166}
167
168static const struct fuse_lowlevel_ops tfs_oper = {
169 .lookup = tfs_lookup,
170 .getattr = tfs_getattr,
171 .readdir = tfs_readdir,
172 .open = tfs_open,
173 .read = tfs_read,
174};
175
176static void *run_fs(void *data)
177{
178 struct fuse_session *se = (struct fuse_session *)data;
179
180 assert(fuse_session_loop(se) == 0);
181 return NULL;
182}
183
184static void check_readdir(const char *mountpoint)
185{
186 DIR *dir = opendir(mountpoint);
187 const struct dirent *ent;
188 int found = 0;
189
190 assert(dir != NULL);
191 while ((ent = readdir(dir)) != NULL) {
192 if (strcmp(ent->d_name, file_name) == 0)
193 found = 1;
194 }
195 assert(closedir(dir) == 0);
196 assert(found == 1);
197}
198
199static void check_read(const char *mountpoint)
200{
201 char path[PATH_MAX];
202 char buf[sizeof(file_contents)];
203 int fd;
204 ssize_t len;
205
206 assert(snprintf(path, PATH_MAX, "%s/%s", mountpoint, file_name) > 0);
207 fd = open(path, O_RDONLY);
208 if (fd == -1) {
209 perror(path);
210 assert(0);
211 }
212
213 len = read(fd, buf, sizeof(buf));
214 assert(len == (ssize_t) strlen(file_contents));
215 assert(memcmp(buf, file_contents, len) == 0);
216 assert(close(fd) == 0);
217}
218
219int main(int argc, char *argv[])
220{
221 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
222 struct fuse_session *se;
223 struct fuse_cmdline_opts fuse_opts;
224 pthread_t fs_thread;
225
226 assert(fuse_parse_cmdline(&args, &fuse_opts) == 0);
227#ifndef __FreeBSD__
228 assert(fuse_opt_add_arg(&args, "-oauto_unmount") == 0);
229#endif
230 se = fuse_session_new(&args, &tfs_oper, sizeof(tfs_oper), NULL);
231 assert(se != NULL);
232 assert(fuse_set_signal_handlers(se) == 0);
233 assert(fuse_session_mount(se, fuse_opts.mountpoint) == 0);
234
235 assert(pthread_create(&fs_thread, NULL, run_fs, (void *)se) == 0);
236
237 check_readdir(fuse_opts.mountpoint);
238 check_read(fuse_opts.mountpoint);
239
240 /*
241 * fuse_session_exit() does not interrupt read(), so nudge this thread
242 * with the ignored signal to let the loop free its receive buffer.
243 */
245 assert(pthread_kill(fs_thread, SIGPIPE) == 0);
246 assert(pthread_join(fs_thread, NULL) == 0);
247
251 free(fuse_opts.mountpoint);
252 fuse_opt_free_args(&args);
253
254 printf("Test completed successfully.\n");
255 return 0;
256}
int fuse_set_signal_handlers(struct fuse_session *se)
void fuse_remove_signal_handlers(struct fuse_session *se)
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_exit(struct fuse_session *se)
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)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
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)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
Definition fuse_opt.c:55
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
fuse_ino_t ino