-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuse.c
863 lines (697 loc) · 18.3 KB
/
fuse.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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
/*
FUSE: Filesystem in Userspace lua binding
Copyright 2007 (C) gary ng <linux@garyng.com>
This program can be distributed under the terms of the GNU LGPL.
*/
#define _GNU_SOURCE
#include <fuse.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
#include <pthread.h>
#include <signal.h>
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif
static pthread_rwlock_t vm_lock;
static lua_State *L_VM = NULL;
static int dispatch_table = LUA_REFNIL;
static int pulse_freq = 5;
#define min(x,y) (x > y ? y : x)
#define LOAD_FUNC(x) \
pthread_rwlock_rdlock(&vm_lock);\
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, dispatch_table); \
lua_pushstring(L_VM, (x));\
lua_gettable(L_VM, -2);\
if (!lua_isfunction(L_VM, -1)) {\
fprintf(stderr,"load function %s failed\n", x);\
lua_pop(L_VM, 2);\
pthread_rwlock_unlock(&vm_lock);\
return -ENOSYS;\
}\
lua_pushvalue(L_VM, -2);\
#define EPILOGUE(x) \
pthread_rwlock_unlock(&vm_lock);\
lua_pop(L_VM, x+1);\
#define obj_pcall(x, y, z) \
res = lua_pcall(L_VM, x+1, y, z);\
#define err_pcall(x) \
if (x) {\
const char *err_msg = lua_tostring(L_VM, -1);\
fprintf(stderr, "%s\n", err_msg);\
EPILOGUE(1)\
return -1;\
}\
static void l_signal(int i)
{ /* assert(i==SIGALRM); */
int res;
signal(i,SIG_DFL); /* reset */
LOAD_FUNC("pulse")
obj_pcall(0, 1, 0);
err_pcall(res);
EPILOGUE(1)
}
static int xmp_getattr(const char *path, struct stat *st)
{
int res;
LOAD_FUNC("getattr")
lua_pushstring(L_VM, path);
obj_pcall(1, 11, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -11);
st->st_mode = lua_tointeger(L_VM, -10);
st->st_ino = lua_tointeger(L_VM, -9);
st->st_rdev = lua_tointeger(L_VM, -8);
st->st_dev = lua_tointeger(L_VM, -8);
st->st_nlink= lua_tointeger(L_VM, -7);
st->st_uid = lua_tointeger(L_VM, -6);
st->st_gid = lua_tointeger(L_VM, -5);
st->st_size = lua_tointeger(L_VM, -4);
st->st_atime= lua_tointeger(L_VM, -3);
st->st_mtime= lua_tointeger(L_VM, -2);
st->st_ctime= lua_tointeger(L_VM, -1);
/* Fill in fields not provided by Python lstat() */
st->st_blksize= 4096;
st->st_blocks= (st->st_size + 511)/512;
EPILOGUE(11)
return res;
}
static int xmp_fgetattr(const char *path, struct stat *st,
struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("fgetattr")
lua_pushstring(L_VM, path);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(2, 11, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -11);
st->st_mode = lua_tointeger(L_VM, -10);
st->st_ino = lua_tointeger(L_VM, -9);
st->st_rdev = lua_tointeger(L_VM, -8);
st->st_dev = lua_tointeger(L_VM, -8);
st->st_nlink= lua_tointeger(L_VM, -7);
st->st_uid = lua_tointeger(L_VM, -6);
st->st_gid = lua_tointeger(L_VM, -5);
st->st_size = lua_tointeger(L_VM, -4);
st->st_atime= lua_tointeger(L_VM, -3);
st->st_mtime= lua_tointeger(L_VM, -2);
st->st_ctime= lua_tointeger(L_VM, -1);
/* Fill in fields not provided by Python lstat() */
st->st_blksize= 4096;
st->st_blocks= (st->st_size + 511)/512;
EPILOGUE(11);
return res;
}
static int xmp_access(const char *path, int mask)
{
int res;
LOAD_FUNC("access")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, mask);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_readlink(const char *path, char *buf, size_t size)
{
int res;
size_t l;
const char *str;
LOAD_FUNC("readlink")
lua_pushstring(L_VM, path);
obj_pcall(1, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
str = lua_tolstring(L_VM, -1, &l);
memcpy(buf, str, min(l,size-1));
buf[min(l,size-1)]='\0';
EPILOGUE(2);
return res;
}
static int xmp_opendir(const char *path, struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("opendir")
lua_pushstring(L_VM, path);
obj_pcall(1, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
/* save the return as reference */
fi->fh = luaL_ref(L_VM, LUA_REGISTRYINDEX);
EPILOGUE(1);
return res;
}
static inline DIR *get_dirp(struct fuse_file_info *fi)
{
return (DIR *) (uintptr_t) fi->fh;
}
static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
int res;
struct dirent *de;
int cnt;
int done = FALSE;
int i;
LOAD_FUNC("readdir")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, offset);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(3, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
cnt = lua_objlen(L_VM, -1);
for (i =0; i < cnt && !done; i++) {
struct stat st;
off_t o = 0;
const char *name = NULL;
lua_rawgeti(L_VM, -1, i+1);
if (lua_istable(L_VM, -1)) {
memset(&st, 0, sizeof(st));
lua_pushstring(L_VM, "ino");
lua_gettable(L_VM, -2);
st.st_ino = lua_tointeger(L_VM, -1);
lua_pushstring(L_VM, "d_type");
lua_gettable(L_VM, -3);
st.st_mode = lua_tointeger(L_VM, -1);
lua_pushstring(L_VM, "offset");
lua_gettable(L_VM, -4);
o = lua_tointeger(L_VM, -1);
lua_pushstring(L_VM, "d_name");
lua_gettable(L_VM, -5);
name = lua_tostring(L_VM, -1);
if (!name || filler(buf, name, &st, o)) done = TRUE;
lua_pop(L_VM, 5);
} else if (lua_isstring(L_VM, -1)) {
name = lua_tostring(L_VM, -1);
if (!name || filler(buf, name, NULL, 0)) done = TRUE;
lua_pop(L_VM, 1);
} else {
lua_pop(L_VM, 1);
}
}
EPILOGUE(2);
return res;
}
static int xmp_fsyncdir(const char *path, int isdatasync, struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("syncdir")
lua_pushstring(L_VM, path);
lua_pushboolean(L_VM, isdatasync);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_releasedir(const char *path, struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("releasedir")
lua_pushstring(L_VM, path);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(2, 1, 0);
luaL_unref(L_VM, LUA_REGISTRYINDEX, fi->fh);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
{
int res;
int fifo = S_ISFIFO(mode);
LOAD_FUNC("mknod")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, mode);
lua_pushnumber(L_VM, rdev);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_mkdir(const char *path, mode_t mode)
{
int res;
LOAD_FUNC("mkdir")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, mode);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_unlink(const char *path)
{
int res;
LOAD_FUNC("unlink")
lua_pushstring(L_VM, path);
obj_pcall(1, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_rmdir(const char *path)
{
int res;
LOAD_FUNC("rmdir")
lua_pushstring(L_VM, path);
obj_pcall(1, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_symlink(const char *from, const char *to)
{
int res;
LOAD_FUNC("symlink")
lua_pushstring(L_VM, from);
lua_pushstring(L_VM, to);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_rename(const char *from, const char *to)
{
int res;
LOAD_FUNC("rename")
lua_pushstring(L_VM, from);
lua_pushstring(L_VM, to);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_link(const char *from, const char *to)
{
int res;
LOAD_FUNC("link")
lua_pushstring(L_VM, from);
lua_pushstring(L_VM, to);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_chmod(const char *path, mode_t mode)
{
int res;
LOAD_FUNC("chmod")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, mode);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_chown(const char *path, uid_t uid, gid_t gid)
{
int res;
LOAD_FUNC("chown")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, uid);
lua_pushnumber(L_VM, gid);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_truncate(const char *path, off_t size)
{
int res;
LOAD_FUNC("truncate")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, size);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_ftruncate(const char *path, off_t size,
struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("ftruncate")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, size);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_utime(const char *path, struct utimbuf *buf)
{
int res;
LOAD_FUNC("utime")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, buf->actime);
lua_pushnumber(L_VM, buf->modtime);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
int res;
int fd;
LOAD_FUNC("create")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, mode);
lua_pushnumber(L_VM, fi->flags);
obj_pcall(3, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
fi->fh = luaL_ref(L_VM, LUA_REGISTRYINDEX);
EPILOGUE(1);
return res;
}
static int xmp_open(const char *path, struct fuse_file_info *fi)
{
int res;
int fd;
LOAD_FUNC("open")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, fi->flags);
obj_pcall(2, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
fi->fh = luaL_ref(L_VM, LUA_REGISTRYINDEX);
EPILOGUE(1);
return res;
}
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int res;
const char* o;
size_t l;
LOAD_FUNC("read")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, size);
lua_pushnumber(L_VM, offset);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(4, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
o = lua_tolstring(L_VM, -1, &l);
if (o) memcpy(buf, o, min(l,size));
EPILOGUE(2)
return o ? min(l,size) : res;
}
static int xmp_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int res;
int written;
LOAD_FUNC("write")
lua_pushstring(L_VM, path);
lua_pushlstring(L_VM, buf, size);
lua_pushnumber(L_VM, offset);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(4, 1, 0);
err_pcall(res);
written = lua_tointeger(L_VM, -1);
EPILOGUE(1)
return written;
}
static int xmp_statfs(const char *path, struct statvfs *st)
{
int res;
LOAD_FUNC("statfs")
lua_pushstring(L_VM, path);
obj_pcall(1, 7, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -7);
st->f_bsize = lua_tointeger(L_VM, -6);
st->f_blocks = lua_tointeger(L_VM, -5);
st->f_bfree = lua_tointeger(L_VM, -4);
st->f_bavail = lua_tointeger(L_VM, -3);
st->f_files = lua_tointeger(L_VM, -2);
st->f_ffree = lua_tointeger(L_VM, -1);
EPILOGUE(7);
return res;
}
static int xmp_flush(const char *path, struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("flush")
lua_pushstring(L_VM, path);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_release(const char *path, struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("release")
lua_pushstring(L_VM, path);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
luaL_unref(L_VM, LUA_REGISTRYINDEX, fi->fh);
EPILOGUE(1);
return res;
}
static int xmp_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
int res;
LOAD_FUNC("fsync")
lua_pushstring(L_VM, path);
lua_pushboolean(L_VM, isdatasync);
lua_rawgeti(L_VM, LUA_REGISTRYINDEX, fi->fh);
obj_pcall(3, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
#ifdef HAVE_SETXATTR
/* xattr operations are optional and can safely be left unimplemented */
static int xmp_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags)
{
int res;
LOAD_FUNC("setxattr")
lua_pushstring(L_VM, path);
lua_pushstring(L_VM, name);
lua_pushlstring(L_VM, value, size);
lua_pushnumber(L_VM, flags);
obj_pcall(4, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
static int xmp_getxattr(const char *path, const char *name, char *value,
size_t size)
{
int res;
size_t l;
const char *o;
LOAD_FUNC("getxattr")
lua_pushstring(L_VM, path);
lua_pushstring(L_VM, name);
lua_pushnumber(L_VM, size);
obj_pcall(3, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
o = lua_tolstring(L_VM, -1, &l);
if (o) memcpy(value, o, min(l,size));
EPILOGUE(2);
return o ? (size > 0 ? min(l,size) : l) : res;
}
static int xmp_listxattr(const char *path, char *list, size_t size)
{
int res;
size_t l;
const char *o;
LOAD_FUNC("listxattr")
lua_pushstring(L_VM, path);
lua_pushnumber(L_VM, size);
obj_pcall(2, 2, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -2);
o = lua_tolstring(L_VM, -1, &l);
if (o) memcpy(list, o, min(l,size));
EPILOGUE(2);
return o ? (size > 0 ? min(l,size) : l) : res;
}
static int xmp_removexattr(const char *path, const char *name)
{
int res;
LOAD_FUNC("removexattr")
lua_pushstring(L_VM, path);
lua_pushstring(L_VM, name);
obj_pcall(2, 1, 0);
err_pcall(res);
res = lua_tointeger(L_VM, -1);
EPILOGUE(1);
return res;
}
#endif /* HAVE_SETXATTR */
static struct fuse_operations xmp_oper = {
.getattr = xmp_getattr,
.fgetattr = xmp_fgetattr,
.access = xmp_access,
.readlink = xmp_readlink,
.readdir = xmp_readdir,
.opendir = xmp_opendir,
.fsyncdir = xmp_fsyncdir,
.releasedir = xmp_releasedir,
#if 0
#endif
.mknod = xmp_mknod,
.mkdir = xmp_mkdir,
.symlink = xmp_symlink,
.unlink = xmp_unlink,
.rmdir = xmp_rmdir,
.rename = xmp_rename,
.link = xmp_link,
.chmod = xmp_chmod,
.chown = xmp_chown,
.truncate = xmp_truncate,
.ftruncate = xmp_ftruncate,
.utime = xmp_utime,
.create = xmp_create,
.open = xmp_open,
.read = xmp_read,
.write = xmp_write,
.statfs = xmp_statfs,
.release = xmp_release,
.flush = xmp_flush,
.fsync = xmp_fsync,
#ifdef HAVE_SETXATTR
.setxattr = xmp_setxattr,
.getxattr = xmp_getxattr,
.listxattr = xmp_listxattr,
.removexattr= xmp_removexattr,
#endif
};
static xmp_alarm(lua_State *L)
{
int freq = lua_tointeger(L, -1);
signal(SIGALRM,l_signal);
alarm(freq);
return 0;
}
static xmp_main(lua_State *L)
{
/*
* called in lua as fuse.main(dispatch_table, fuse_table)
*/
L_VM = L;
int argc = lua_objlen(L, 2);
char *argv[256];
int i;
if (argc < 2) luaL_error(L, "check parameter fusemount parameter table");
#if 0
lua_pushstring(L, "pulse_freq");
lua_gettable(L, 1);
pulse_freq = lua_tointeger(L,-1);
lua_pop(L,1);
if (pulse_freq == 0) pulse_freq = 10;
#endif
/*
* save dispatch table for future reference by C program
*/
lua_pushvalue(L, 1);
dispatch_table = luaL_ref(L_VM, LUA_REGISTRYINDEX);
/*
* turn fuse option table(effectively cmdline) to argv style
*/
for (i=0; i < argc && i < 255; i++) {
lua_rawgeti(L, 2, i + 1);
argv[i]=lua_tostring(L, -1);
}
argv[i] = NULL;
#if 0
if (pulse_freq > 0) {
signal(SIGALRM,l_signal);
alarm(pulse_freq);
}
#endif
/*
* hand over to fuse loop
*/
fuse_main(argc, argv, &xmp_oper);
/*
* remove argv strings from L stack
*/
lua_pop(L, i);
/*
* discard reference to dispatch_table
*/
luaL_unref(L, LUA_REGISTRYINDEX, dispatch_table);
#if 0
signal(SIGALRM, SIG_DFL);
#endif
return 0;
}
static int xmp_get_context(lua_State *L)
{
struct fuse_context *fc;
fc = fuse_get_context();
lua_pushnumber(L, fc->uid);
lua_pushnumber(L, fc->gid);
lua_pushnumber(L, fc->pid);
lua_pushnumber(L, geteuid());
lua_pushnumber(L, getegid());
return 5;
}
static const luaL_reg Rm[] = {
{ "main", xmp_main},
{ "alarm", xmp_alarm},
{ "context",xmp_get_context},
{ NULL, NULL }
};
LUA_API int luaopen_fuse(lua_State *L)
{
luaL_openlib( L, "fuse", Rm, 0 );
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2007 gary ng <linux@garyng.com>");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "Binding to linux fuse file system");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "LuaFuse 0.1");
lua_settable (L, -3);
pthread_rwlock_init(&vm_lock, NULL);
return 1;
}