libfuse: fuse-3.6.0/test/test_setattr.c Source File

libfuse

test_setattr.c
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 GPL.
6 See the file COPYING.
7 */
8
9
10 #define FUSE_USE_VERSION 30
11
12 #include <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
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
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
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
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
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
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
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
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
fuse_lowlevel_ops::lookup
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)
Definition: fuse_lowlevel.h:215
fuse_session_new
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
Definition: fuse_lowlevel.c:2826
fuse_file_info::fh
uint64_t fh
Definition: fuse_common.h:91
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition: fuse_lowlevel.c:2976
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition: fuse_loop.c:19
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition: fuse_lowlevel.c:401
FUSE_ROOT_ID
#define FUSE_ROOT_ID
Definition: fuse_lowlevel.h:43
fuse_parse_cmdline
int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
Definition: helper.c:202
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:62
fuse_entry_param
Definition: fuse_lowlevel.h:59
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition: fuse_lowlevel.c:312
fuse_req_t
struct fuse_req * fuse_req_t
Definition: fuse_lowlevel.h:49
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:79
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition: fuse_opt.h:123
fuse_ino_t
uint64_t fuse_ino_t
Definition: fuse_lowlevel.h:46
fuse_file_info
Definition: fuse_common.h:43
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition: fuse_lowlevel.c:453
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition: fuse_lowlevel.c:2643
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition: fuse_lowlevel.c:2921
fuse_opt_add_arg
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
Definition: fuse_opt.c:54
fuse_lowlevel_ops
Definition: fuse_lowlevel.h:172
fuse_args
Definition: fuse_opt.h:109
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition: fuse_lowlevel.c:433