Repository navigation
Expand file tree
/
Copy pathnull.c
More file actions
525 lines (465 loc) · 11.9 KB
/
Copy pathnull.c
File metadata and controls
525 lines (465 loc) · 11.9 KB
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#define FUSE_USE_VERSION 30
#include <fuse3/fuse.h>
#include <stdio.h> /* fprintf (). */
#include <stdlib.h> /* malloc (). */
#include <errno.h> /* ENOMEM. */
typedef struct {
struct fuse_fs *next;
} null_t;
/* null file info. */
typedef struct {
uint64_t fh_below;
} nfi_t;
static const struct fuse_opt null_opts[] = {
FUSE_OPT_KEY("-h", 0),
FUSE_OPT_KEY("--help", 0),
FUSE_OPT_END
};
static nfi_t *
nfi_of_fh (uint64_t fh)
{
return (nfi_t *) fh;
}
static uint64_t
fh_of_nfi (nfi_t *nfi)
{
return (uint64_t) nfi;
}
static void
nfi_destroy (nfi_t *nfi)
{
free (nfi);
}
static void
null_help (void)
{
fprintf(stderr, "(no option for the null fusemod)\n");
}
static int
null_opt_proc (void *data, const char *arg, int key,
struct fuse_args *outargs)
{
if (key == 0) {
null_help ();
return -1;
}
return 1;
}
static null_t *
null_get (void)
{
return fuse_get_context()->private_data;
}
static int
null_getattr (const char *path, struct stat *st)
{
null_t *null = null_get ();
return fuse_fs_getattr (null->next, path, st);
}
static int
null_readlink (const char *path, char *buf, size_t capacity)
{
null_t *null = null_get ();
return fuse_fs_readlink (null->next, path, buf, capacity);
}
static int
null_mknod (const char *path, mode_t mode, dev_t dev)
{
null_t *null = null_get ();
return fuse_fs_mknod (null->next, path, mode, dev);
}
static int
null_mkdir (const char *path, mode_t mode)
{
null_t *null = null_get ();
return fuse_fs_mkdir (null->next, path, mode);
}
static int
null_unlink (const char *path)
{
null_t *null = null_get ();
return fuse_fs_unlink (null->next, path);
}
static int
null_rmdir (const char *path)
{
null_t *null = null_get ();
return fuse_fs_rmdir (null->next, path);
}
static int
null_symlink (const char *contents, const char *path)
{
null_t *null = null_get ();
return fuse_fs_symlink (null->next, contents, path);
}
static int
null_rename (const char *oldpath, const char *newpath)
{
null_t *null = null_get ();
return fuse_fs_rename (null->next, oldpath, newpath);
}
static int
null_link (const char *oldpath, const char *newpath)
{
null_t *null = null_get ();
return fuse_fs_link (null->next, oldpath, newpath);
}
static int
null_chmod (const char *path, mode_t mode)
{
null_t *null = null_get ();
return fuse_fs_chmod (null->next, path, mode);
}
static int
null_chown (const char *path, uid_t uid, gid_t gid)
{
null_t *null = null_get ();
return fuse_fs_chown (null->next, path, uid, gid);
}
static int
null_truncate (const char *path, off_t off)
{
null_t *null = null_get ();
return fuse_fs_truncate (null->next, path, off);
}
static int
null_open (const char *path, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = malloc (sizeof *nfi);
if (nfi == NULL)
return -ENOMEM;
int ret = fuse_fs_open (null->next, path, ffi);
nfi->fh_below = ffi->fh;
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_read (const char *path, char *buf, size_t capacity, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_read (null->next, path, buf, capacity, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_write (const char *path, const char *buf, size_t len, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_write (null->next, path, buf, len, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_statfs (const char *path, struct statvfs *st)
{
null_t *null = null_get ();
return fuse_fs_statfs (null->next, path, st);
}
static int
null_flush (const char *path, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_flush (null->next, path, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_release (const char *path, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_release (null->next, path, ffi);
nfi_destroy (nfi);
return ret;
}
static int
null_fsync (const char *path, int datasync, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_fsync (null->next, path, datasync, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_setxattr (const char *path, const char *key, const char *value, size_t size, int flags)
{
null_t *null = null_get ();
return fuse_fs_setxattr (null->next, path, key, value, size, flags);
}
static int
null_getxattr (const char *path, const char *key, char *buffer, size_t capacity)
{
null_t *null = null_get ();
return fuse_fs_getxattr (null->next, path, key, buffer, capacity);
}
static int
null_listxattr (const char *path, char *buffer, size_t capacity)
{
null_t *null = null_get ();
return fuse_fs_listxattr (null->next, path, buffer, capacity);
}
static int
null_removexattr (const char *path, const char *key)
{
null_t *null = null_get ();
return fuse_fs_removexattr (null->next, path, key);
}
static int
null_opendir (const char *path, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = malloc (sizeof *nfi);
if (nfi == NULL)
return -ENOMEM;
int ret = fuse_fs_opendir (null->next, path, ffi);
nfi->fh_below = ffi->fh;
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_readdir (const char *path, void *buffer, fuse_fill_dir_t filler, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_readdir (null->next, path, buffer, filler, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_releasedir (const char *path, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_releasedir (null->next, path, ffi);
nfi_destroy (nfi);
return ret;
}
static int
null_fsyncdir (const char *path, int datasync, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_fsyncdir (null->next, path, datasync, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static void *
null_init (struct fuse_conn_info *fci)
{
null_t *null = null_get ();
fuse_fs_init (null->next, fci);
return null;
}
static void
null_destroy (void *data)
{
null_t *null = data;
fuse_fs_destroy (null->next);
}
static int
null_access (const char *path, int how)
{
null_t *null = null_get ();
return fuse_fs_access (null->next, path, how);
}
static int
null_create (const char *path, mode_t mode, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = malloc (sizeof *nfi);
if (nfi == NULL)
return -ENOMEM;
int ret = fuse_fs_create (null->next, path, mode, ffi);
nfi->fh_below = ffi->fh;
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_ftruncate (const char *path, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_ftruncate (null->next, path, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_fgetattr (const char *path, struct stat *st, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_fgetattr (null->next, path, st, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_lock (const char *path, struct fuse_file_info *ffi, int cmd, struct flock *fl)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_lock (null->next, path, ffi, cmd, fl);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_utimens (const char *path, const struct timespec tv[2])
{
null_t *null = null_get ();
return fuse_fs_utimens (null->next, path, tv);
}
static int
null_bmap (const char *path, size_t blocksize, uint64_t *idx)
{
null_t *null = null_get ();
return fuse_fs_bmap (null->next, path, blocksize, idx);
}
static int
null_ioctl (const char *path, int cmd, void *arg, struct fuse_file_info *ffi, unsigned int flags, void *data)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_ioctl (null->next, path, cmd, arg, ffi, flags, data);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_poll (const char *path, struct fuse_file_info *ffi, struct fuse_pollhandle *ph, unsigned *reventsp)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_poll (null->next, path, ffi, ph, reventsp);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_write_buf (const char *path, struct fuse_bufvec *buf, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_write_buf (null->next, path, buf, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_read_buf (const char *path, struct fuse_bufvec **bufp, size_t size, off_t off, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_read_buf (null->next, path, bufp, size, off, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_flock (const char *path, struct fuse_file_info *ffi, int op)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_flock (null->next, path, ffi, op);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static int
null_fallocate (const char *path, int mode, off_t off, off_t length, struct fuse_file_info *ffi)
{
null_t *null = null_get ();
nfi_t *nfi = nfi_of_fh (ffi->fh);
ffi->fh = nfi->fh_below;
int ret = fuse_fs_fallocate (null->next, path, mode, off, length, ffi);
ffi->fh = fh_of_nfi (nfi);
return ret;
}
static const struct fuse_operations null_oper = {
.getattr = null_getattr,
.readlink = null_readlink,
.mknod = null_mknod,
.mkdir = null_mkdir,
.unlink = null_unlink,
.rmdir = null_rmdir,
.symlink = null_symlink,
.rename = null_rename,
.link = null_link,
.chmod = null_chmod,
.chown = null_chown,
.truncate = null_truncate,
.open = null_open,
.read = null_read,
.write = null_write,
.statfs = null_statfs,
.flush = null_flush,
.release = null_release,
.fsync = null_fsync,
.setxattr = null_setxattr,
.getxattr = null_getxattr,
.listxattr = null_listxattr,
.removexattr = null_removexattr,
.opendir = null_opendir,
.readdir = null_readdir,
.releasedir = null_releasedir,
.fsyncdir = null_fsyncdir,
.init = null_init,
.destroy = null_destroy,
.access = null_access,
.create = null_create,
.ftruncate = null_ftruncate,
.fgetattr = null_fgetattr,
.lock = null_lock,
.utimens = null_utimens,
.bmap = null_bmap,
.ioctl = null_ioctl,
.poll = null_poll,
.write_buf = null_write_buf,
.read_buf = null_read_buf,
.flock = null_flock,
.fallocate = null_fallocate,
};
static struct fuse_fs *
null_new (struct fuse_args *args,
struct fuse_fs *next[])
{
struct fuse_fs *fs;
null_t *null = malloc (sizeof *null);
if (null == NULL)
{
fprintf (stderr, "null_new: Cannot allocate memory\n");
goto fail;
}
if (fuse_opt_parse (args, null, null_opts, null_opt_proc))
{
fprintf (stderr, "fuse_opt_parse() failed\n");
goto fail_malloc;
}
null->next = next[0];
fs = fuse_fs_new (&null_oper, sizeof null_oper, null);
if (fs == NULL)
{
fprintf (stderr, "fuse_fs_new() failed\n");
goto fail_malloc;
}
return fs;
fail_malloc:
free (null);
fail:
return NULL;
}
FUSE_REGISTER_MODULE(null, null_new);