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