• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4 
5   This program can be distributed under the terms of the GNU GPLv2.
6   See the file COPYING.
7 */
8 
9 
10 #define FUSE_USE_VERSION 30
11 
12 #include <fuse_config.h>
13 #include <fuse_lowlevel.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <assert.h>
20 #include <stddef.h>
21 #include <unistd.h>
22 #include <pthread.h>
23 
24 #ifndef __linux__
25 #include <limits.h>
26 #else
27 #include <linux/limits.h>
28 #endif
29 
30 #define FILE_INO 2
31 #define FILE_NAME "truncate_me"
32 
33 static int got_fh;
34 static mode_t file_mode = S_IFREG | 0644;
35 
tfs_stat(fuse_ino_t ino,struct stat * stbuf)36 static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
37     stbuf->st_ino = ino;
38     if (ino == FUSE_ROOT_ID) {
39         stbuf->st_mode = S_IFDIR | 0755;
40         stbuf->st_nlink = 1;
41     }
42 
43     else if (ino == FILE_INO) {
44         stbuf->st_mode = file_mode;
45         stbuf->st_nlink = 1;
46         stbuf->st_size = 0;
47     }
48 
49     else
50         return -1;
51 
52     return 0;
53 }
54 
tfs_lookup(fuse_req_t req,fuse_ino_t parent,const char * name)55 static void tfs_lookup(fuse_req_t req, fuse_ino_t parent,
56                        const char *name) {
57     struct fuse_entry_param e;
58     memset(&e, 0, sizeof(e));
59 
60     if (parent != FUSE_ROOT_ID)
61         goto err_out;
62     else if (strcmp(name, FILE_NAME) == 0)
63         e.ino = FILE_INO;
64     else
65         goto err_out;
66 
67     if (tfs_stat(e.ino, &e.attr) != 0)
68         goto err_out;
69     fuse_reply_entry(req, &e);
70     return;
71 
72 err_out:
73     fuse_reply_err(req, ENOENT);
74 }
75 
tfs_getattr(fuse_req_t req,fuse_ino_t ino,struct fuse_file_info * fi)76 static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
77                         struct fuse_file_info *fi) {
78     struct stat stbuf;
79 
80     (void) fi;
81 
82     memset(&stbuf, 0, sizeof(stbuf));
83     if (tfs_stat(ino, &stbuf) != 0)
84         fuse_reply_err(req, ENOENT);
85     else
86         fuse_reply_attr(req, &stbuf, 5);
87 }
88 
tfs_open(fuse_req_t req,fuse_ino_t ino,struct fuse_file_info * fi)89 static void tfs_open(fuse_req_t req, fuse_ino_t ino,
90                      struct fuse_file_info *fi) {
91     if (ino == FUSE_ROOT_ID)
92         fuse_reply_err(req, EISDIR);
93     else {
94         assert(ino == FILE_INO);
95         fi->fh = FILE_INO;
96         fuse_reply_open(req, fi);
97     }
98 }
99 
tfs_setattr(fuse_req_t req,fuse_ino_t ino,struct stat * attr,int to_set,struct fuse_file_info * fi)100 static void tfs_setattr (fuse_req_t req, fuse_ino_t ino, struct stat *attr,
101                          int to_set, struct fuse_file_info *fi) {
102     if(ino != FILE_INO ||
103        !(to_set & FUSE_SET_ATTR_MODE)) {
104         fuse_reply_err(req, EINVAL);
105         return;
106     }
107 
108     if(fi == NULL)
109         fprintf(stderr, "setattr with fi == NULL\n");
110     else if (fi->fh != FILE_INO)
111         fprintf(stderr, "setattr with wrong fi->fh\n");
112     else {
113         fprintf(stderr, "setattr ok\n");
114         got_fh = 1;
115         file_mode = attr->st_mode;
116     }
117 
118     tfs_getattr(req, ino, fi);
119 }
120 
121 static struct fuse_lowlevel_ops tfs_oper = {
122     .lookup	= tfs_lookup,
123     .getattr	= tfs_getattr,
124     .open	= tfs_open,
125     .setattr	= tfs_setattr,
126 };
127 
run_fs(void * data)128 static void* run_fs(void *data) {
129     struct fuse_session *se = (struct fuse_session*) data;
130     assert(fuse_session_loop(se) == 0);
131     return NULL;
132 }
133 
test_fs(char * mountpoint)134 static void test_fs(char *mountpoint) {
135     char fname[PATH_MAX];
136     int fd;
137 
138     assert(snprintf(fname, PATH_MAX, "%s/" FILE_NAME,
139                      mountpoint) > 0);
140     fd = open(fname, O_WRONLY);
141     if (fd == -1) {
142         perror(fname);
143         assert(0);
144     }
145 
146     assert(fchmod(fd, 0600) == 0);
147     close(fd);
148 }
149 
main(int argc,char * argv[])150 int main(int argc, char *argv[]) {
151     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
152     struct fuse_session *se;
153     struct fuse_cmdline_opts fuse_opts;
154     pthread_t fs_thread;
155 
156     assert(fuse_parse_cmdline(&args, &fuse_opts) == 0);
157 #ifndef __FreeBSD__
158     assert(fuse_opt_add_arg(&args, "-oauto_unmount") == 0);
159 #endif
160     se = fuse_session_new(&args, &tfs_oper,
161                           sizeof(tfs_oper), NULL);
162     assert (se != NULL);
163     assert(fuse_set_signal_handlers(se) == 0);
164     assert(fuse_session_mount(se, fuse_opts.mountpoint) == 0);
165 
166     /* Start file-system thread */
167     assert(pthread_create(&fs_thread, NULL, run_fs, (void *)se) == 0);
168 
169     /* Do test */
170     test_fs(fuse_opts.mountpoint);
171 
172     /* Stop file system */
173     assert(pthread_cancel(fs_thread) == 0);
174 
175     fuse_session_unmount(se);
176     assert(got_fh == 1);
177     fuse_remove_signal_handlers(se);
178     fuse_session_destroy(se);
179 
180     printf("Test completed successfully.\n");
181     return 0;
182 }
183 
184 
185 /**
186  * Local Variables:
187  * mode: c
188  * indent-tabs-mode: nil
189  * c-basic-offset: 4
190  * End:
191  */
192