-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.c
445 lines (378 loc) · 9.47 KB
/
operations.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <fcntl.h>
#include "defs.h"
#include "fs.h"
#define ZERO_FILLER_OFFSET 0
extern char *filedisk;
extern filesystem_t fs;
/*
┌───────────────────┐
│ OPERATIONS │
└───────────────────┘
*/
int
fisopfs_getattr(const char *path, struct stat *st)
{
printf("[debug] getattr, path: %s\n", path);
return fs_get_stat_info(&fs, path, st);
}
int
fisopfs_readlink(const char *path, char *buf, size_t len)
{
printf("[debug] [error] readlink called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_getdir(const char *path, fuse_dirh_t fuse_dirh, fuse_dirfil_t fuse_dirfil)
{
printf("[debug] [error] getdir called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_mknod(const char *path, mode_t mode, dev_t rdev)
{
printf("[debug] [error] mknod called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_mkdir(const char *path, mode_t mode)
{
printf("[debug] mkdir\n");
struct fuse_context *context = fuse_get_context();
return fs_create_directory(&fs, path, mode, context->uid, context->gid);
}
int
fisopfs_unlink(const char *path)
{
printf("[debug] unlink, path: %s\n", path);
return fs_remove_file(&fs, path);
}
int
fisopfs_rmdir(const char *path)
{
printf("[debug] rmdir, path: %s\n", path);
return fs_remove_dir(&fs, path);
}
int
fisopfs_symlink(const char *linkname, const char *path)
{
printf("[debug] [error] symlink called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_rename(const char *oldpath, const char *newpath)
{
printf("[debug] [error] rename called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_link(const char *oldpath, const char *newpath)
{
printf("[debug] [error] link called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_chmod(const char *path, mode_t mode)
{
printf("[debug] [error] chmod called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_chown(const char *path, uid_t uid, gid_t gid)
{
printf("[debug] [error] chown called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_truncate(const char *path, off_t size)
{
printf("[debug] truncate\n");
return fs_truncate_regular_file(&fs, path, size);
}
int
fisopfs_utime(const char *path, struct utimbuf *utimbuf)
{
printf("[debug] utime, deprecated, use utimens instead\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_open(const char *path, struct fuse_file_info *fi)
{
printf("[debug] open, path: %s\n", path);
return fs_open_regular_file(&fs, path);
}
int
fisopfs_read(const char *path,
char *buf,
size_t size,
off_t offset,
struct fuse_file_info *fi)
{
printf("[debug] read\n");
return fs_read_regular_file(&fs, path, buf, size, offset);
}
int
fisopfs_write(const char *path,
const char *buf,
size_t size,
off_t off,
struct fuse_file_info *fi)
{
printf("[debug] write\n");
return fs_write_regular_file(&fs, path, buf, size, off);
}
int
fisopfs_statfs(const char *path, struct statvfs *buf)
{
printf("[debug] [error] statfs called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_flush(const char *path, struct fuse_file_info *fi)
{
printf("[debug] flush, saving filesystem to disk\n");
int res = fs_save_to_disk(&fs, filedisk);
return res;
}
int
fisopfs_release(const char *path, struct fuse_file_info *fi)
{
printf("[debug] release\n");
return SUCCESS;
}
int
fisopfs_fsync(const char *path, int datasync, struct fuse_file_info *fi)
{
printf("[debug] [error] fsync called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_setxattr(const char *path,
const char *name,
const char *value,
size_t size,
int flags)
{
printf("[debug] setxattr, no support for extra attributes\n");
errno = ENOTSUP;
return -ENOTSUP;
}
int
fisopfs_getxattr(const char *path, const char *name, char *value, size_t size)
{
printf("[debug] getxattr, no support for extra attributes\n");
errno = ENOTSUP;
return -ENOTSUP;
}
int
fisopfs_listxattr(const char *path, char *list, size_t size)
{
printf("[debug] listxattr, no support for extra attributes\n");
errno = ENOTSUP;
return -ENOTSUP;
}
int
fisopfs_removexattr(const char *path, const char *name)
{
printf("[debug] removexattr, no support for extra attributes\n");
errno = ENOTSUP;
return -ENOTSUP;
}
int
fisopfs_opendir(const char *path, struct fuse_file_info *fi)
{
printf("[debug] opendir, path: %s\n", path);
int res = fs_does_dir_exist(&fs, path);
if (res != SUCCESS) {
return res;
}
return SUCCESS;
}
int
fisopfs_readdir(const char *path,
void *buf,
fuse_fill_dir_t filler,
off_t off,
struct fuse_file_info *fi)
{
printf("[debug] readdir, path: %s\n", path);
char *name = NULL;
struct stat *st = NULL;
int index_offset = 0;
int res = fs_readdir(&fs, path, &name, &st, &index_offset);
while (res == SUCCESS && index_offset != FINISHED_READDIR) {
printf("[debug] filler passing over '%s'\n", name);
res = filler(buf, name, st, ZERO_FILLER_OFFSET);
if (res != SUCCESS) {
printf("[debug] [error] Error while iterating over "
"dir\n");
break;
}
res = fs_readdir(&fs, path, &name, &st, &index_offset);
}
return res;
}
int
fisopfs_releasedir(const char *path, struct fuse_file_info *fi)
{
printf("[debug] releasedir\n");
return SUCCESS;
}
int
fisopfs_fsyncdir(const char *path, int datasync, struct fuse_file_info *fi)
{
printf("[debug] [error] fsyncdir called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
void *
fisopfs_init(struct fuse_conn_info *conn)
{
printf("[debug] init\n");
struct fuse_context *context = fuse_get_context();
fs_init(&fs, filedisk, context->uid, context->gid);
printf("[debug] fisopfs_init completed\n");
return &fs;
}
void
fisopfs_destroy(void *private_data)
{
printf("[debug] destroy\n");
filesystem_t *fs_data = (filesystem_t *) private_data;
fs_save_to_disk(fs_data, filedisk);
return;
}
int
fisopfs_access(const char *path, int mask)
{
printf("[debug] access, path: %s\n", path);
struct stat stat_inf;
int res = fs_get_stat_info(&fs, path, &stat_inf);
if (res != SUCCESS) {
return res;
}
if (mask & F_OK) {
return SUCCESS;
}
if ((mask & R_OK) && !(stat_inf.st_mode & PERM_OWNER_READ)) {
errno = EACCES;
return -EACCES;
}
if ((mask & W_OK) && !(stat_inf.st_mode & PERM_OWNER_WRITE)) {
errno = EACCES;
return -EACCES;
}
if ((mask & X_OK) && !(stat_inf.st_mode & PERM_OWNER_EXECUTE)) {
errno = EACCES;
return -EACCES;
}
return SUCCESS;
}
int
fisopfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
printf("[debug] create\n");
struct fuse_context *context = fuse_get_context();
int res =
fs_create_regular_file(&fs, path, mode, context->uid, context->gid);
if (res != SUCCESS) {
return res;
}
return SUCCESS;
}
int
fisopfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
{
printf("[debug] [error] ftruncate called,\033[1;31m not "
"implemented\033[0mn");
return GENERIC_ERROR_CODE;
}
int
fisopfs_fgetattr(const char *path, struct stat *st, struct fuse_file_info *fi)
{
printf("[debug] fgetattr, path: %s\n", path);
return fs_get_stat_info(&fs, path, st);
}
int
fisopfs_lock(const char *path, struct fuse_file_info *fi, int cmd, struct flock *lock)
{
printf("[debug] lock, empty but ok\n");
/*
* As noted in the FUSE documentation:
* if this method is not implemented, the kernel will still
* allow file locking to work locally. Hence it is only
* interesting for network filesystems and similar
* */
return SUCCESS;
}
int
fisopfs_utimens(const char *path, const struct timespec tv[2])
{
printf("[debug] utimens\n");
struct stat stat_inf;
int res = fs_get_stat_info(&fs, path, &stat_inf);
if (res != SUCCESS) {
return res;
}
stat_inf.st_atim = *tv;
stat_inf.st_mtim = *tv;
stat_inf.st_ctim = *tv;
return SUCCESS;
}
int
fisopfs_bmap(const char *path, size_t blocksize, uint64_t *idx)
{
printf("[debug] [error] bmap called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_ioctl(const char *path,
int cmd,
void *arg,
struct fuse_file_info *fi,
unsigned int flags,
void *data)
{
printf("[debug] [error] ioctl called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_poll(const char *path,
struct fuse_file_info *fi,
struct fuse_pollhandle *ph,
unsigned *reventsp)
{
printf("[debug] [error] poll called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_flock(const char *path, struct fuse_file_info *fi, int op)
{
printf("[debug] [error] flock called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}
int
fisopfs_fallocate(const char *path,
int mode,
off_t offset,
off_t length,
struct fuse_file_info *fi)
{
printf("[debug] [error] fallocate called,\033[1;31m not "
"implemented\033[0m\n");
return GENERIC_ERROR_CODE;
}