This repository has been archived by the owner on May 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_fuse3.c
263 lines (226 loc) · 7.72 KB
/
main_fuse3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fuse.h>
#include <fuse_lowlevel.h>
#include "context.h"
#include "unionfs.h"
static int f3_ufs_getattr(const char* path, struct stat* stbuf, struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return fi && fi->fh > 0 ? ufs_fgetattr(fs, (ufs_fd_t)fi->fh, stbuf) : ufs_getattr(fs, path, stbuf);
}
static int f3_ufs_readlink(const char* path, char* buf, size_t bufsize)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_readlink(fs, path, buf, bufsize);
}
static int f3_ufs_mkdir(const char* path, mode_t mode)
{
struct fuse_context* fctx = fuse_get_context();
struct unionfs* fs = get_unionfs(fctx);
struct process_context pctx;
change_process_context(fctx, &pctx);
int res = ufs_mkdir(fs, path, mode);
restore_process_context(fctx, &pctx);
return res;
}
static int f3_ufs_unlink(const char* path)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_remove(fs, path);
}
static int f3_ufs_rmdir(const char* path)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_rmdir(fs, path);
}
static int f3_ufs_symlink(const char* target, const char* link_path)
{
struct fuse_context* fctx = fuse_get_context();
struct unionfs* fs = get_unionfs(fctx);
struct process_context pctx;
change_process_context(fctx, &pctx);
int res = ufs_symlink(fs, target, link_path);
restore_process_context(fctx, &pctx);
return res;
}
static int f3_ufs_chmod(const char* path, mode_t mode, struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return fi && fi->fh > 0 ? ufs_fchmod(fs, (ufs_fd_t)fi->fh, mode) : ufs_chmod(fs, path, mode);
}
static int f3_ufs_chown(const char* path, uid_t uid, gid_t gid, struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return fi && fi->fh > 0 ? ufs_fchown(fs, (ufs_fd_t)fi->fh, uid, gid) : ufs_chown(fs, path, uid, gid);
}
static int f3_ufs_truncate(const char* path, off_t offset, struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return fi && fi->fh > 0 ? ufs_ftruncate(fs, (ufs_fd_t)fi->fh, offset) : ufs_truncate(fs, path, offset);
}
static int f3_ufs_open(const char* path, struct fuse_file_info* fi)
{
struct fuse_context* fctx = fuse_get_context();
struct unionfs* fs = get_unionfs(fctx);
struct process_context pctx;
change_process_context(fctx, &pctx);
int res = ufs_open(fs, path, fi->flags, (ufs_fd_t*)&fi->fh);
restore_process_context(fctx, &pctx);
return res;
}
static int f3_ufs_read(const char* path, char* buffer, size_t size, off_t offset,
struct fuse_file_info* fi)
{
(void) path;
struct unionfs* fs = get_unionfs(fuse_get_context());
return (int)ufs_read(fs, (ufs_fd_t)fi->fh, offset, size, buffer);
}
static int f3_ufs_write(const char* path, const char* buffer, size_t size, off_t offset,
struct fuse_file_info* fi)
{
(void) path;
struct unionfs* fs = get_unionfs(fuse_get_context());
return (int)ufs_write(fs, (ufs_fd_t)fi->fh, offset, size, buffer);
}
static int f3_ufs_statfs(const char* path, struct statvfs* stbuf)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_statfs(fs, path, stbuf);
}
static int f3_ufs_release(const char* path, struct fuse_file_info* fi)
{
(void) path;
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_close(fs, (ufs_fd_t)fi->fh);
}
static int f3_ufs_fsync(const char* path, int datasync, struct fuse_file_info* fi)
{
(void) path;
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_fsync(fs, (ufs_fd_t)fi->fh, !datasync);
}
static int f3_ufs_setxattr(const char* path, const char* name, const char* value, size_t size, int flags)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_setxattr(fs, path, name, value, size, flags);
}
static int f3_ufs_getxattr(const char* path, const char* name, char* value, size_t size)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return (int)ufs_getxattr(fs, path, name, value, size);
}
static int f3_ufs_listxattr(const char* path, char* list, size_t size)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return (int)ufs_listxattr(fs, path, list, size);
}
static int f3_ufs_removexattr(const char* path, const char* name)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_removexattr(fs, path, name);
}
static int f3_ufs_opendir(const char* path, struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_opendir(fs, path, (ufs_dir_t**)&fi->fh);
}
static int f3_ufs_readdir(const char* path, void* buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info* fi,
enum fuse_readdir_flags flags)
{
(void) path;
(void) offset;
struct unionfs* fs = get_unionfs(fuse_get_context());
struct dirent* entry;
int res = 0;
if (flags & FUSE_READDIR_PLUS) {
struct stat stbuf;
while ((res = ufs_readdir_plus(fs, (ufs_dir_t*)fi->fh, &entry, &stbuf)) == 0 && entry) {
filler(buf, entry->d_name, &stbuf, 0, FUSE_FILL_DIR_PLUS);
}
} else {
while ((res = ufs_readdir(fs, (ufs_dir_t*)fi->fh, &entry)) == 0 && entry) {
filler(buf, entry->d_name, NULL, 0, 0);
}
}
return res;
}
static int f3_ufs_releasedir(const char* path, struct fuse_file_info* fi)
{
(void) path;
struct unionfs* fs = get_unionfs(fuse_get_context());
return ufs_closedir(fs, (ufs_dir_t*)fi->fh);
}
static void* f3_ufs_init(struct fuse_conn_info* conn, struct fuse_config* cfg)
{
(void) conn;
(void) cfg;
return global_data_create(fuse_get_context()->private_data);
}
static void f3_ufs_destroy(void* private_data)
{
global_data_destroy(private_data);
}
static int f3_ufs_create(const char* path, mode_t mode, struct fuse_file_info* fi)
{
struct fuse_context* fctx = fuse_get_context();
struct unionfs* fs = get_unionfs(fctx);
struct process_context pctx;
change_process_context(fctx, &pctx);
int res = ufs_open3(fs, path, fi->flags, mode, (ufs_fd_t*)&fi->fh);
restore_process_context(fctx, &pctx);
return res;
}
static int f3_ufs_utimens(const char* path, const struct timespec tv[2],
struct fuse_file_info* fi)
{
struct unionfs* fs = get_unionfs(fuse_get_context());
return fi && fi->fh > 0 ? ufs_futimens(fs, (ufs_fd_t)fi->fh, tv) : ufs_utimens(fs, path, tv);
}
static struct fuse_operations f3_ufs_oper = {
.getattr = f3_ufs_getattr,
.readlink = f3_ufs_readlink,
/* .mknod = f3_ufs_mknod, */
.mkdir = f3_ufs_mkdir,
.unlink = f3_ufs_unlink,
.rmdir = f3_ufs_rmdir,
.symlink = f3_ufs_symlink,
/* .rename = f3_ufs_rename, */
/* .link = f3_ufs_link, */
.chmod = f3_ufs_chmod,
.chown = f3_ufs_chown,
.truncate = f3_ufs_truncate,
.open = f3_ufs_open,
.read = f3_ufs_read,
.write = f3_ufs_write,
.statfs = f3_ufs_statfs,
.release = f3_ufs_release,
.fsync = f3_ufs_fsync,
.setxattr = f3_ufs_setxattr,
.getxattr = f3_ufs_getxattr,
.listxattr = f3_ufs_listxattr,
.removexattr= f3_ufs_removexattr,
.opendir = f3_ufs_opendir,
.readdir = f3_ufs_readdir,
.releasedir = f3_ufs_releasedir,
.init = f3_ufs_init,
.destroy = f3_ufs_destroy,
.create = f3_ufs_create,
.utimens = f3_ufs_utimens,
};
int main(int argc, char* argv[])
{
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
struct fuse_cmdline_opts opts;
if (fuse_parse_cmdline(&args, &opts) != 0)
return 1;
umask(0);
struct init_data data;
data.mountpoint = opts.mountpoint;
int res = fuse_main(argc, argv, &f3_ufs_oper, &data);
free(opts.mountpoint);
fuse_opt_free_args(&args);
return res;
}