forked from adlerosn/cicpoffs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cicpoffs.cpp
371 lines (320 loc) · 11.6 KB
/
cicpoffs.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) 2020 Adler Neves <[email protected]>
* Copyright (c) 2023 Bruno Gazotti <[email protected]>
*
* CICPOFFS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*/
#include "cicpoffs.hpp"
#include "cicpps.hpp"
extern "C"{
#include <ulockmgr.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <fcntl.h>
#include <sys/time.h>
}
#define VERSION "0.0.2"
static struct fuse_operations operations = {
.getattr = fuse_fn_getattr,
.readlink = fuse_fn_readlink,
// .getdir = <unimplemented> - deprecated,
.mknod = fuse_fn_mknod,
.mkdir = fuse_fn_mkdir,
.unlink = fuse_fn_unlink,
.rmdir = fuse_fn_rmdir,
.symlink = fuse_fn_symlink,
.rename = fuse_fn_rename,
.link = fuse_fn_link,
.chmod = fuse_fn_chmod,
.chown = fuse_fn_chown,
.truncate = fuse_fn_truncate,
//.utime = fuse_fn_utime,
.open = fuse_fn_open,
.read = fuse_fn_read,
.write = fuse_fn_write,
.statfs = fuse_fn_statfs,
.flush = fuse_fn_flush,
.release = fuse_fn_release,
.fsync = fuse_fn_fsync,
.setxattr = fuse_fn_setxattr,
.getxattr = fuse_fn_getxattr,
.listxattr = fuse_fn_listxattr,
.removexattr = fuse_fn_removexattr,
.opendir = fuse_fn_opendir,
.readdir = fuse_fn_readdir,
.releasedir = fuse_fn_releasedir,
// .fsyncdir = fuse_fn_fsyncdir,
.init = fuse_fn_init,
.destroy = fuse_fn_destroy,
.access = fuse_fn_access,
.create = fuse_fn_create,
//.ftruncate = fuse_fn_ftruncate,
//.fgetattr = fuse_fn_fgetattr,
.lock = fuse_fn_lock,
.utimens = fuse_fn_utimens,
//.bmap = <unimplemented> - not for block devices,
//.ioctl = fuse_fn_ioctl,
//.poll = fuse_fn_poll,
//.flock = fuse_fn_flock,
.fallocate = fuse_fn_fallocate,
};
static char* read_source_directory = NULL;
static char* argv0 = NULL;
static void stderr_print(const char* msg){
fprintf(stderr, "[%s] %s\n", argv0, msg);
};
static void ignore_print(const char* msg){};
static void (*logmsg) (const char* msg) = ignore_print;
void* (fuse_fn_init) (struct fuse_conn_info* conn, struct fuse_config* cfg){
struct stat st;
if(stat(read_source_directory, &st)){
logmsg("Source does not exist.");
exit(ENOENT);
}
if(read_source_directory[0]!='/'){
logmsg("Path must be absolute.");
exit(ENOENT);
}
return NULL;
};
void (fuse_fn_destroy) (void* private_data){
return;
};
int (fuse_fn_getattr) (const char* path, struct stat* stbuf, struct fuse_file_info* fi){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = stat(correctpath, stbuf);
free((void*) correctpath);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_readlink) (const char* path, char* buf, size_t size){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = readlink(correctpath, buf, size);
free((void*) correctpath);
return retval;
};
int (fuse_fn_mknod) (const char* path, mode_t mode, dev_t rdev){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = mknod(correctpath, mode, rdev);
free((void*) correctpath);
return retval;
};
int (fuse_fn_mkdir) (const char* path, mode_t mode){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = mkdir(correctpath, mode);
free((void*) correctpath);
return retval;
};
int (fuse_fn_unlink) (const char* path){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = unlink(correctpath);
free((void*) correctpath);
return retval;
};
int (fuse_fn_rmdir) (const char* path){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = rmdir(correctpath);
free((void*) correctpath);
return retval;
};
int (fuse_fn_symlink) (const char* to, const char* from){
const char* correctfrom = correct_case_sensitivity_for(read_source_directory, from);
int retval = symlink(to, correctfrom);
free((void*) correctfrom);
return retval;
};
// flag behavior is absolutely ignored
int (fuse_fn_rename) (const char* from, const char* to, unsigned int flags){
const char* correctfrom = correct_case_sensitivity_for(read_source_directory, from);
const char* correctto = correct_case_sensitivity_for(read_source_directory, to);
int retval = rename(correctfrom, correctto);
free((void*) correctfrom);
free((void*) correctto);
return retval;
};
int (fuse_fn_link) (const char* from, const char* to){
const char* correctfrom = correct_case_sensitivity_for(read_source_directory, from);
const char* correctto = correct_case_sensitivity_for(read_source_directory, to);
int retval = link(correctfrom, correctto);
free((void*) correctfrom);
free((void*) correctto);
return retval;
};
int (fuse_fn_chmod) (const char* path, mode_t mode, struct fuse_file_info* fi){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = chmod(correctpath, mode);
free((void*) correctpath);
return retval;
};
int (fuse_fn_chown) (const char* path, uid_t uid, gid_t gid, struct fuse_file_info*){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = chown(correctpath, uid, gid);
free((void*) correctpath);
return retval;
};
int (fuse_fn_truncate) (const char* path, off_t off, struct fuse_file_info*){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = truncate(correctpath, off);
free((void*) correctpath);
return retval;
};
/** int (fuse_fn_utime) (const char* path, struct utimbuf* buf){
* const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
* int retval = utime(correctpath, buf);
* free((void*) correctpath);
* return retval;
};*/
int (fuse_fn_open) (const char* path, struct fuse_file_info* ffi){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int fd = open(correctpath, ffi->flags);
free((void*) correctpath);
ffi->fh = fd;
return (fd==-1) ? -errno : 0;
};
int (fuse_fn_read) (const char* path, char* buf, size_t size, off_t off, struct fuse_file_info* ffi){
return pread(ffi->fh, buf, size, off);
};
int (fuse_fn_write) (const char* path, const char* buf, size_t size, off_t off, struct fuse_file_info* ffi){
return pwrite(ffi->fh, buf, size, off);
};
int (fuse_fn_statfs) (const char* path, struct statvfs* svfs){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = statvfs(correctpath, svfs);
free((void*) correctpath);
return retval;
};
int (fuse_fn_flush) (const char* path, struct fuse_file_info* ffi){
return close(dup(ffi->fh));
};
int (fuse_fn_release) (const char* path, struct fuse_file_info* ffi){
return close(ffi->fh);
};
int (fuse_fn_fsync) (const char* path, int data_sync, struct fuse_file_info* ffi){
#ifdef HAVE_FDATASYNC
if(data_sync)
return fdatasync(ffi->fh);
else
#endif
return fsync(ffi->fh);
};
int (fuse_fn_setxattr) (const char* path, const char* key, const char* value, size_t size, int flags){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = lsetxattr(correctpath, key, value, size, flags);
free((void*) correctpath);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_getxattr) (const char* path, const char* key, char* value, size_t size){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = lgetxattr(correctpath, key, value, size);
free((void*) correctpath);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_listxattr) (const char* path, char* list, size_t size){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = llistxattr(correctpath, list, size);
free((void*) correctpath);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_removexattr) (const char* path, const char* key){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = lremovexattr(correctpath, key);
free((void*) correctpath);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_opendir) (const char* path, struct fuse_file_info* ffi){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
DIR* dp = opendir(correctpath);
free((void*) correctpath);
ffi->fh = (uintptr_t) dp;
return dp==NULL ? -errno : 0;
};
int (fuse_fn_readdir) (const char* path, void* buf, fuse_fill_dir_t filler, off_t off, struct fuse_file_info* ffi, enum fuse_readdir_flags flags){
DIR* dp = (DIR*) ffi->fh;
if(!dp) return -EBADF;
seekdir(dp, off);
struct dirent *de;
while((de = readdir(dp)) != NULL){
struct stat st;
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, telldir(dp), FUSE_FILL_DIR_PLUS)) break;
}
return 0;
};
int (fuse_fn_releasedir) (const char* path, struct fuse_file_info* ffi){
if(ffi->fh)
closedir((DIR*)(uintptr_t) ffi->fh);
ffi->fh = 0;
return 0;
};
// int (fuse_fn_fsyncdir) (const char* path, int isdatasync, struct fuse_file_info* ffi){
// return ENOSYS;
// };
int (fuse_fn_access) (const char* path, int mode){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = access(correctpath, mode);
free((void*) correctpath);
return retval;
};
int (fuse_fn_create) (const char* path, mode_t mode, struct fuse_file_info* ffi){
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int fd = open(correctpath, ffi->flags, mode);
free((void*) correctpath);
if(fd==-1) return -errno;
ffi->fh = fd;
return 0;
};
int (fuse_fn_ftruncate) (const char* path, off_t off, struct fuse_file_info* ffi){
return ftruncate(ffi->fh, off);
};
int (fuse_fn_fgetattr) (const char* path, struct stat* st, struct fuse_file_info* ffi){
int retval = fstat(ffi->fh, st);
if(retval==-1) return -errno;
return retval;
};
int (fuse_fn_lock) (const char* path, struct fuse_file_info* ffi, int cmd, struct flock* lock){
return ulockmgr_op(ffi->fh, cmd, lock, &ffi->lock_owner, sizeof(ffi->lock_owner));
};
int (fuse_fn_utimens) (const char* path, const struct timespec ts[2], struct fuse_file_info* ffi){
struct timeval tv[2];
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = ts[1].tv_nsec / 1000;
const char* correctpath = correct_case_sensitivity_for(read_source_directory, path);
int retval = utimes(correctpath, tv);
free((void*) correctpath);
return retval;
};
// int (fuse_fn_ioctl) (const char* path, int cmd, void* arg, struct fuse_file_info* ffi, unsigned int flags, void* data){
// return ENOSYS;
// };
// int (fuse_fn_poll) (const char* path, struct fuse_file_info* ffi, struct fuse_pollhandle* ph, unsigned* reventsp){
// return ENOSYS;
// };
// int (fuse_fn_write_buf) (const char* path, struct fuse_bufvec* buf, off_t off, struct fuse_file_info* ffi){
// return ENOSYS;
// };
// int (fuse_fn_read_buf) (const char* path, struct fuse_bufvec** buf, size_t size, off_t off, struct fuse_file_info* ffi){
// return ENOSYS;
// };
// int (fuse_fn_flock) (const char* path, struct fuse_file_info*, int){
// return ENOSYS;
// };
int (fuse_fn_fallocate) (const char* path, int mode, off_t off, off_t len, struct fuse_file_info* ffi){
return fallocate(ffi->fh, mode, off, len);
};