-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflu.c
991 lines (919 loc) · 27.7 KB
/
flu.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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
#ifndef FUSE_USE_VERSION
#error FUSE_USE_VERSION is not defined.
#endif
#if FUSE_USE_VERSION > 26
//#error Versions newer than FUSE 2.6 (26) are not yet supported (FUSE 2.7.x and 2.8.x use APIv26).
#endif
#if FUSE_USE_VERSION < 25
#error Versions older than FUSE 2.5 (25) are not yet supported.
#endif
#define _GNU_SOURCE 1
#define _FILE_OFFSET_BITS 64
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fuse.h>
#include <sys/xattr.h>
#include <lua.h>
#include <lauxlib.h>
#include "compat.h"
#include "posix_structs.h"
/****************************************************************************/
int lua_iserrno(lua_State* L, int index);
int lua_toerrno(lua_State* L, int index);
void push_errno_table(lua_State* L);
// PROLOGUE does the following:
// 1. Gets the fuse_context in variable context and gets the Lua state from it in L
// 2. Pushes the lua handler function from the finctions table TABLEB - as documented in the main function TABLEB[#method]
// 3. Initializes top to stack top
#define PROLOGUE(method) \
struct fuse_context* context; \
lua_State* L; \
int top; \
int result = -ENOSYS; \
\
context = fuse_get_context(); \
L = (lua_State*)context->private_data; \
\
top = lua_gettop(L); \
lua_getfield(L, 2, #method);
// CALL does the following:
// 1. pcalls the function at the top of the stack and checks for any error
// - lua_pcall nargs = gettop(L)-top-1 because 1 entry has to be removed for the function that is being called
// 2. If there was an error then it checks the error object (lua_iserrno)
// 3. If not the error object userdata from errno then simply generates an error using lua_error otherwise
// gets the error number and signals it as an error by returning the negative error code as instructed in fuse.h fuse_operations structure documentation
// If code does not return by now then code execution successful and you can return 0
#define CALL(nresult) \
if (lua_pcall(L, lua_gettop(L)-top-1, nresult, 0)) \
{ \
/* if error is a small integer, return it */ \
/* otherwise propagate the value */ \
/* Error object is pushed to the stack */ \
int err; \
/* :KLUDGE: luaB_error is calling lua_isstring, and numbers get converted to strings, so we need to use userdata */ \
if (!lua_iserrno(L, -1)) \
lua_error(L); \
err = lua_toerrno(L, -1); \
return -err; \
}
#define EPILOGUE() \
lua_settop(L, top); \
return result;
#define EPILOGUE1(method, resultname) \
/* if result is invalid throw an error */ \
if (result == -ENOSYS) \
{ \
lua_pushliteral(L, "bad result #1 from '"); \
lua_pushliteral(L, #method); \
lua_pushliteral(L, "' ("); \
lua_pushliteral(L, resultname); \
lua_pushliteral(L, " expected, got "); \
lua_pushstring(L, luaL_typename(L, top+1)); \
lua_pushliteral(L, ")"); \
lua_concat(L, 7); \
luaL_error(L, lua_tostring(L, -1)); \
} \
EPILOGUE()
#define EPILOGUE_I(method) \
if (lua_type(L, -1)==LUA_TNUMBER) \
{ \
lua_Number n; \
int i; \
n = lua_tonumber(L, -1); \
i = (int)n; \
if (n==(lua_Number)i && i >= 0) \
result = i; \
} \
EPILOGUE1(method, "positive integer")
#define EPILOGUE_S(method, buf, size) \
if (lua_type(L, -1)==LUA_TSTRING) \
{ \
size_t len; \
const char* str; \
str = lua_tolstring(L, -1, &len); \
if (len > size-1) \
len = size-1; \
memcpy(buf, str, len); \
buf[len] = '\0'; \
result = 0; \
} \
EPILOGUE1(method, "string")
#define EPILOGUE_BIN(method, buf, size) \
if (lua_type(L, -1)==LUA_TSTRING) \
{ \
size_t len; \
const char* str; \
str = lua_tolstring(L, -1, &len); \
if (buf) \
{ \
if (len > size) \
len = size; \
memcpy(buf, str, len); \
} \
result = len; \
} \
EPILOGUE1(method, "string")
#define DEFINE_STUB(method)
/****************************************************************************/
static int lua__fuse_file_info__index(lua_State* L)
{
struct fuse_file_info** pfi;
pfi = lua_touserdata(L, 1);
if (*pfi)
{
struct fuse_file_info* fi = *pfi;
if (lua_type(L, 2)==LUA_TSTRING)
{
const char* k = lua_tostring(L, 2);
if (!strcmp(k, "flags")) lua__push__open_flags(L, fi->flags);
else if (!strcmp(k, "writepage")) lua_pushnumber(L, fi->writepage);
else if (!strcmp(k, "direct_io")) lua_pushboolean(L, fi->direct_io);
else if (!strcmp(k, "keep_cache")) lua_pushboolean(L, fi->keep_cache);
else if (!strcmp(k, "flush")) lua_pushboolean(L, fi->flush);
else if (!strcmp(k, "fh")) lua_pushnumber(L, fi->fh);
else if (!strcmp(k, "lock_owner")) lua_pushnumber(L, fi->lock_owner);
else lua_pushnil(L);
return 1;
}
}
lua_pushnil(L);
return 1;
}
static int lua__fuse_file_info__newindex(lua_State* L)
{
struct fuse_file_info** pfi;
pfi = lua_touserdata(L, 1);
if (*pfi)
{
struct fuse_file_info* fi = *pfi;
if (lua_type(L, 2)==LUA_TSTRING)
{
const char* k = lua_tostring(L, 2);
if (!strcmp(k, "flags")) { if (lua_istable(L, 3)) { fi->flags = lua__to__open_flags(L, 3); } }
else if (!strcmp(k, "writepage")) { if (lua_isnumber(L, 3)) fi->writepage = lua_tonumber(L, 3); }
else if (!strcmp(k, "direct_io")) fi->direct_io = lua_toboolean(L, 3);
else if (!strcmp(k, "keep_cache")) fi->keep_cache = lua_toboolean(L, 3);
else if (!strcmp(k, "flush")) fi->flush = lua_toboolean(L, 3);
else if (!strcmp(k, "fh")) { if (lua_isnumber(L, 3)) fi->fh = lua_tonumber(L, 3); }
else if (!strcmp(k, "lock_owner")) { if (lua_isnumber(L, 3)) fi->lock_owner = lua_tonumber(L, 3); }
}
}
return 0;
}
// Function to link the fuse_file_info structure fi to a userdata and set its metatable to read and write values to it
// function lua_fuse_file_info__index is used to read data from it
// function lua_fuse_file_info__newindex is used to write data to it
static void lua__push__fuse_file_info(lua_State* L, struct fuse_file_info* fi)
{
struct fuse_file_info** pfi;
pfi = (struct fuse_file_info**)lua_newuserdata(L, sizeof(struct fuse_file_info*));
*pfi = fi;
lua_newtable(L);
lua_pushcfunction(L, lua__fuse_file_info__index);
lua_setfield(L, -2, "__index");
lua_pushcfunction(L, lua__fuse_file_info__newindex);
lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
}
/****************************************************************************/
DEFINE_STUB(getattr)
static int fuse__getattr(const char* path, struct stat* st)
{
PROLOGUE(getattr)
lua_pushstring(L, path);
CALL(1)
if (lua_type(L, -1)==LUA_TTABLE)
{
lua_pushnil(L);
while (lua_next(L, -2))
{
if (lua_type(L, -2)==LUA_TSTRING)
{
const char* k = lua_tostring(L, -2);
if (!strcmp(k, "dev")) { if (lua_isnumber(L, -1)) st->st_dev = lua_tonumber(L, -1); }
else if (!strcmp(k, "ino")) { if (lua_isnumber(L, -1)) st->st_ino = lua_tonumber(L, -1); }
else if (!strcmp(k, "mode")) { if (lua_istable(L, -1) || lua_isstring(L, -1)) st->st_mode = lua__to__stat_mode(L, -1); }
else if (!strcmp(k, "nlink")) { if (lua_isnumber(L, -1)) st->st_nlink = lua_tonumber(L, -1); }
else if (!strcmp(k, "uid")) { if (lua_isnumber(L, -1)) st->st_uid = lua_tonumber(L, -1); }
else if (!strcmp(k, "gid")) { if (lua_isnumber(L, -1)) st->st_gid = lua_tonumber(L, -1); }
else if (!strcmp(k, "rdev")) { if (lua_isnumber(L, -1)) st->st_rdev = lua_tonumber(L, -1); }
else if (!strcmp(k, "access")) { if (lua_isnumber(L, -1)) st->st_atime = lua_tonumber(L, -1); }
else if (!strcmp(k, "modification")) { if (lua_isnumber(L, -1)) st->st_mtime = lua_tonumber(L, -1); }
else if (!strcmp(k, "change")) { if (lua_isnumber(L, -1)) st->st_ctime = lua_tonumber(L, -1); }
else if (!strcmp(k, "size")) { if (lua_isnumber(L, -1)) st->st_size = lua_tonumber(L, -1); }
else if (!strcmp(k, "blocks")) { if (lua_isnumber(L, -1)) st->st_blocks = lua_tonumber(L, -1); }
else if (!strcmp(k, "blksize")) { if (lua_isnumber(L, -1)) st->st_blksize = lua_tonumber(L, -1); }
}
lua_pop(L, 1);
}
}
result = 0;
EPILOGUE()
}
DEFINE_STUB(readlink)
static int fuse__readlink(const char* path, char* buf, size_t size)
{
PROLOGUE(readlink)
lua_pushstring(L, path);
lua_pushnumber(L, size);
CALL(1)
EPILOGUE_S(readlink, buf, size)
}
DEFINE_STUB(mknod)
static int fuse__mknod(const char* path, mode_t mode, dev_t rdev)
{
PROLOGUE(mknod)
lua_pushstring(L, path);
lua__push__stat_mode(L, mode);
lua_pushnumber(L, rdev);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(mkdir)
static int fuse__mkdir(const char* path, mode_t mode)
{
PROLOGUE(mkdir)
lua_pushstring(L, path);
lua__push__stat_mode(L, mode);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(unlink)
static int fuse__unlink(const char* path)
{
PROLOGUE(unlink)
lua_pushstring(L, path);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(rmdir)
static int fuse__rmdir(const char* path)
{
PROLOGUE(rmdir)
lua_pushstring(L, path);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(symlink)
static int fuse__symlink(const char* linkname, const char* path)
{
PROLOGUE(symlink)
lua_pushstring(L, linkname);
lua_pushstring(L, path);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(rename)
static int fuse__rename(const char* oldpath, const char* newpath)
{
PROLOGUE(rename)
lua_pushstring(L, oldpath);
lua_pushstring(L, newpath);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(link)
static int fuse__link(const char* oldpath, const char* newpath)
{
PROLOGUE(link)
lua_pushstring(L, oldpath);
lua_pushstring(L, newpath);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(chmod)
static int fuse__chmod(const char* path, mode_t mode)
{
PROLOGUE(chmod)
lua_pushstring(L, path);
lua__push__stat_mode(L, mode);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(chown)
static int fuse__chown(const char* path, uid_t uid, gid_t gid)
{
PROLOGUE(chown)
lua_pushstring(L, path);
lua_pushnumber(L, uid);
lua_pushnumber(L, gid);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(truncate)
static int fuse__truncate(const char* path, off_t size)
{
PROLOGUE(truncate)
lua_pushstring(L, path);
lua_pushnumber(L, size);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(open)
static int fuse__open(const char* path, struct fuse_file_info* fi)
{
PROLOGUE(open)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(read)
static int fuse__read(const char* path, char* buf, size_t size, off_t offset, struct fuse_file_info* fi)
{
PROLOGUE(read)
lua_pushstring(L, path);
lua_pushnumber(L, size);
lua_pushnumber(L, offset);
lua__push__fuse_file_info(L, fi);
CALL(1)
if (lua_type(L, -1)==LUA_TSTRING)
{
size_t len;
const char* str;
str = lua_tolstring(L, -1, &len);
if (len > size)
len = size;
memcpy(buf, str, len);
result = len;
}
EPILOGUE1(read, "string")
}
DEFINE_STUB(write)
static int fuse__write(const char* path, const char* buf, size_t size, off_t offset, struct fuse_file_info* fi)
{
PROLOGUE(write)
lua_pushstring(L, path);
lua_pushlstring(L, (const char*)buf, size);
lua_pushnumber(L, offset);
lua__push__fuse_file_info(L, fi);
CALL(1)
EPILOGUE_I(write)
}
DEFINE_STUB(statfs)
static int fuse__statfs(const char* path, struct statvfs* st)
{
PROLOGUE(statfs)
lua_pushstring(L, path);
CALL(1)
if (lua_type(L, -1)==LUA_TTABLE)
{
lua_pushnil(L);
while (lua_next(L, -2))
{
if (lua_type(L, -2)==LUA_TSTRING)
{
const char* k = lua_tostring(L, -2);
if (!strcmp(k, "bsize")) { if (lua_isnumber(L, -1)) st->f_bsize = lua_tonumber(L, -1); }
else if (!strcmp(k, "frsize")) { if (lua_isnumber(L, -1)) st->f_frsize = lua_tonumber(L, -1); }
else if (!strcmp(k, "blocks")) { if (lua_isnumber(L, -1)) st->f_blocks = lua_tonumber(L, -1); }
else if (!strcmp(k, "bfree")) { if (lua_isnumber(L, -1)) st->f_bfree = lua_tonumber(L, -1); }
else if (!strcmp(k, "bavail")) { if (lua_isnumber(L, -1)) st->f_bavail = lua_tonumber(L, -1); }
else if (!strcmp(k, "files")) { if (lua_isnumber(L, -1)) st->f_files = lua_tonumber(L, -1); }
else if (!strcmp(k, "ffree")) { if (lua_isnumber(L, -1)) st->f_ffree = lua_tonumber(L, -1); }
else if (!strcmp(k, "favail")) { if (lua_isnumber(L, -1)) st->f_favail = lua_tonumber(L, -1); }
else if (!strcmp(k, "fsid")) { if (lua_isnumber(L, -1)) st->f_fsid = lua_tonumber(L, -1); }
else if (!strcmp(k, "flag")) { if (lua_istable(L, -1)) st->f_flag = lua__to__statvfs_flag(L, -1); }
else if (!strcmp(k, "namemax")) { if (lua_isnumber(L, -1)) st->f_namemax = lua_tonumber(L, -1); }
}
lua_pop(L, 1);
}
}
result = 0;
EPILOGUE()
}
DEFINE_STUB(flush)
static int fuse__flush(const char* path, struct fuse_file_info* fi)
{
PROLOGUE(flush)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(release)
static int fuse__release(const char* path, struct fuse_file_info* fi)
{
PROLOGUE(release)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(fsync)
static int fuse__fsync(const char* path, int isdatasync, struct fuse_file_info* fi)
{
PROLOGUE(fsync)
lua_pushstring(L, path);
lua_pushboolean(L, isdatasync!=0?1:0);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
static void push_xattr_flags(lua_State* L, int flags)
{
lua_newtable(L);
#define ADD_FLAG(cflag, lflag) \
if(flags & cflag) \
{ \
lua_pushboolean(L, 1); \
lua_setfield(L, -2, #lflag); \
}
ADD_FLAG(XATTR_CREATE, create)
ADD_FLAG(XATTR_REPLACE, replace)
#undef ADD_FLAG
}
DEFINE_STUB(setxattr)
static int fuse__setxattr(const char* path, const char* name, const char* value, size_t size, int flags)
{
PROLOGUE(setxattr)
lua_pushstring(L, path);
lua_pushstring(L, name);
lua_pushlstring(L, value, size);
push_xattr_flags(L, flags);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(getxattr)
static int fuse__getxattr(const char* path, const char* name, char* value, size_t size)
{
PROLOGUE(getxattr)
lua_pushstring(L, path);
lua_pushstring(L, name);
// lua_pushnumber(L, size);
CALL(1)
EPILOGUE_BIN(getxattr, value, size)
}
DEFINE_STUB(listxattr)
static int fuse__listxattr(const char* path, char* list, size_t size)
{
PROLOGUE(listxattr)
lua_pushstring(L, path);
// lua_pushnumber(L, size);
CALL(1)
if (lua_istable(L, -1))
{
size_t len, i;
int ok;
ok = 1;
len = rawlen(L, -1);
for (i=1; i<=len; ++i)
{
lua_rawgeti(L, -1, i);
if (lua_type(L, -1)!=LUA_TSTRING)
ok = 0;
lua_pop(L, 1);
}
if (ok)
{
size_t len;
const char* str;
lua_getglobal(L, "table");
lua_getfield(L, -1, "concat");
lua_replace(L, -2);
lua_insert(L, -2);
lua_pushlstring(L, "\0", 1);
lua_call(L, 2, 1);
str = lua_tolstring(L, -1, &len);
if(len > 0)
++len; // add the trailing 0
if (list)
{
if (len > size)
len = size;
memcpy(list, str, len);
}
result = len;
}
}
EPILOGUE1(listxattr, "array of strings")
}
DEFINE_STUB(removexattr)
static int fuse__removexattr(const char* path, const char* name)
{
PROLOGUE(removexattr)
lua_pushstring(L, path);
lua_pushstring(L, name);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(opendir)
static int fuse__opendir(const char* path, struct fuse_file_info* fi)
{
PROLOGUE(opendir)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
static int lua__fuse_fill_dir(lua_State* L)
{
void* buf;
int result;
fuse_fill_dir_t filler;
buf = lua_touserdata(L, lua_upvalueindex(1));
filler = lua_touserdata(L, lua_upvalueindex(2));
luaL_checktype(L, 1, LUA_TSTRING);
result = filler(buf, lua_tostring(L, 1), NULL,0, 0);
lua_pushboolean(L, result==0);
return 1;
}
DEFINE_STUB(readdir)
static int fuse__readdir(const char* path, void* buf, fuse_fill_dir_t filler, off_t off, struct fuse_file_info* fi)
{
PROLOGUE(readdir)
lua_pushstring(L, path);
lua_pushlightuserdata(L, buf);
lua_pushlightuserdata(L, filler);
lua_pushcclosure(L, lua__fuse_fill_dir, 2);
(void)off; /* unused parameter */
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(releasedir)
static int fuse__releasedir(const char* path, struct fuse_file_info* fi)
{
PROLOGUE(releasedir)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(fsyncdir)
static int fuse__fsyncdir(const char* path, int datasync, struct fuse_file_info* fi)
{
PROLOGUE(fsyncdir)
lua_pushstring(L, path);
lua_pushnumber(L, datasync);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
/* intialize the state before calling fuse_main */
static lua_State* init__23__state = 0;
static void* fuse__init__23()
{
lua_State* state = init__23__state;
/* we reset global state pointer because we may have several ones */
init__23__state = 0;
return state;
}
static void* fuse__init(struct fuse_conn_info* conn)
{
(void)conn;
return 0;
}
static void fuse__destroy(void* data)
{
(void)data;
}
DEFINE_STUB(access)
static int fuse__access(const char* path, int mask)
{
PROLOGUE(access)
lua_pushstring(L, path);
lua_pushnumber(L, mask);
CALL(0)
result = 0;
EPILOGUE()
}
DEFINE_STUB(create)
static int fuse__create(const char* path, mode_t mode, struct fuse_file_info* fi)
{
PROLOGUE(create)
lua_pushstring(L, path);
lua__push__stat_mode(L, mode);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
/*
DEFINE_STUB(ftruncate)
static int fuse__ftruncate(const char* path, off_t size, struct fuse_file_info* fi)
{
PROLOGUE(ftruncate)
lua_pushstring(L, path);
lua_pushnumber(L, size);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
*/
DEFINE_STUB(fgetattr)
static int fuse__fgetattr(const char* path, struct stat* st, struct fuse_file_info* fi)
{
PROLOGUE(fgetattr)
lua_pushstring(L, path);
lua__push__stat(L, st);
lua__push__fuse_file_info(L, fi);
CALL(0)
result = 0;
EPILOGUE()
}
/*
DEFINE_STUB(lock)
static int fuse__lock(const char* path, struct fuse_file_info* fi, int cmd, struct flock* lock)
{
PROLOGUE(lock)
lua_pushstring(L, path);
lua__push__fuse_file_info(L, fi);
lua_pushnumber(L, cmd);
lua_pushlightuserdata(L, lock);
CALL(0)
result = 0;
EPILOGUE()
}
*/
DEFINE_STUB(utimens)
static int fuse__utimens(const char* path, const struct timespec tv[2])
{
PROLOGUE(utimens)
lua_pushstring(L, path);
lua_createtable(L, 2, 0);
lua__push__timespec(L, &tv[0]); lua_rawseti(L, -2, 1);
lua__push__timespec(L, &tv[1]); lua_rawseti(L, -2, 2);
CALL(0)
result = 0;
EPILOGUE()
}
/****************************************************************************/
// Function to register all the functions from Lua into fuse for filesystem operations
static int lua__main(lua_State *L)
{
// pthread_rwlock_t* lock;
int largc, cargc;
char** argv;
int i;
struct fuse_operations oper; // Store all the filesystem operation functions
// fuse_operations is defined in fuse.h and contains prototypes of all the functions
// Arguments passed to the function are:
// 1: argv
// 2: method table
// Note luaL_check* functions always raise an error if the check is not satisfied
luaL_checktype(L, 1, LUA_TTABLE); // argv
luaL_checktype(L, 2, LUA_TTABLE); // methods
lua_settop(L, 2); // Keep only 2 elements
largc = rawlen(L, 1); // rawlen defined in compat.h it is alias for lua_rawlen in Lua 5.3
// This stores length of argv in largc
cargc = 1; // To store -s option
// Create a new table and store all the argv strings in it. TABLEA = {}
lua_newtable(L); // New table - Stack top(ST)=3
// argv is an array of string pointers
argv = (char**)lua_newuserdata(L, sizeof(char*) * (largc + cargc + 1)); // To store user data for all strings in argv (largc) + -s option and finally NULL (ST=4)
// TABLEA[0]=argv
lua_rawseti(L, -2, 0); // Put the userdata (argv) defined above in 0 index of the new Lua Table at 3, ST=3
// Now process all the arguments in argv 1 by 1
for (i=0; i < largc; ++i)
{
size_t len;
const char* str;
char* udata;
lua_rawgeti(L, 1, i+1); // Get the ith string from the argv Lua table to top of stack ST=4
if (!lua_isstring(L, -1))
return typeerror(L, 1, "array of strings");
str = lua_tolstring(L, -1, &len); // convert the Lua string to a C string on top of stack
udata = (char*)lua_newuserdata(L, len+1); // New user data on top of stack to store the string ST=5
memcpy(udata, str, len+1); // Copy the string data to the userdata data
argv[i] = udata; // Copy the userdata to the argv user data
lua_rawseti(L, -3, i+1); // Put the userdata in the TABLEA TABLEA[i+1]=udata ST=4
lua_pop(L, 1); // Pop the string from the stack ST=3
}
// Single threaded operation for the moment
{
size_t len;
const char* str;
char* udata;
len = 2;
str = "-s"; // option for single threaded operation
udata = (char*)lua_newuserdata(L, len+1); //ST=4
memcpy(udata, str, len+1);
argv[i] = udata;
lua_rawseti(L, -2, i+1); // TABLEA[i+1]=udata ST=3
}
++i;
argv[i] = NULL;
lua_replace(L, 1); // Replace argv (the table passed to this function) with the new table TABLEA
// containing all the strings in userdata and 0 index containing the argv userdata
// ST=2
// New Lua Functions table TABLEB={}
lua_newtable(L); // ST=3
memset(&oper, 0, sizeof(struct fuse_operations)); // initialize oper space with all 0s
// ## means string concatenation
// # prefix stringizes the method parameter
// CHECK_METHOD does the following:
// 1. Gets the value pointed by #method in the method table passed to this function by Lua
// 2. Checks whether the value is a function (can be nil if that function is not defined)
// ST=4
// 3. If a function then sets oper.method to the right function defined above to handle the appropriate fuse operation
// 4. Sets the Lua function into the New Lua functions table defined above TABLEB[#method] = #method function from method table - basically just copying over the method table
// In the end the New Lua functions table is left at the top and ST=3
#define CHECK_METHOD(method) \
lua_getfield(L, 2, #method); \
if (lua_isfunction(L, -1)) \
{ \
oper.method = fuse__##method; \
lua_setfield(L, -2, #method); \
} \
else \
lua_pop(L, 1);
CHECK_METHOD(getattr) // adds the fuse__getattr method to the new table created
CHECK_METHOD(readlink)
// CHECK_METHOD(getdir) /* deprecated */
CHECK_METHOD(mknod)
CHECK_METHOD(mkdir)
CHECK_METHOD(unlink)
CHECK_METHOD(rmdir)
CHECK_METHOD(symlink)
CHECK_METHOD(rename)
CHECK_METHOD(link)
CHECK_METHOD(chmod)
CHECK_METHOD(chown)
CHECK_METHOD(truncate)
// CHECK_METHOD(utime) /* deprecated */
#if FUSE_USE_VERSION >= 22
CHECK_METHOD(open)
CHECK_METHOD(read)
CHECK_METHOD(write)
#endif
#if FUSE_USE_VERSION >= 25
CHECK_METHOD(statfs)
#endif
#if FUSE_USE_VERSION >= 22
CHECK_METHOD(flush)
CHECK_METHOD(release)
CHECK_METHOD(fsync)
#endif
CHECK_METHOD(setxattr)
CHECK_METHOD(getxattr)
CHECK_METHOD(listxattr)
CHECK_METHOD(removexattr)
#if FUSE_USE_VERSION >= 23
CHECK_METHOD(opendir)
CHECK_METHOD(readdir)
CHECK_METHOD(releasedir)
CHECK_METHOD(fsyncdir)
#endif
#if FUSE_USE_VERSION >= 26
// CHECK_METHOD(init) /* :TODO: */
#elif FUSE_USE_VERSION >= 23
oper.init = fuse__init__23;
#endif
#if FUSE_USE_VERSION >= 23
// CHECK_METHOD(destroy) /* :TODO: */
#endif
#if FUSE_USE_VERSION >= 25
CHECK_METHOD(access)
CHECK_METHOD(create)
//CHECK_METHOD(ftruncate)
//CHECK_METHOD(fgetattr)
#endif
#if FUSE_USE_VERSION >= 26
// CHECK_METHOD(lock) /* :TODO: */
CHECK_METHOD(utimens)
// CHECK_METHOD(bmap) /* :TODO: */
#endif
#undef CHECK_METHOD
// ST=3
lua_replace(L, 2); // The New Lua functions table replaces the method table passed to this function
// ST=2
#if FUSE_USE_VERSION >= 26
i = fuse_main(largc + cargc, argv, &oper, L); // Also stores L in fuse_context->private_data
#elif FUSE_USE_VERSION >= 23
if (init__23__state!=0) return luaL_error(L, "another FUSE filesystem is being run, try again later");
init__23__state = L;
i = fuse_main(largc + cargc, argv, &oper);
#endif
if (i==0) {
lua_pushboolean(L,1); // main returns 0 for success
return 1;
}
else {
lua_pushnil(L);
// Now push the error message. The code is as follows (from fuse.h):
/* The following error codes may be returned from fuse_main():
* 1: Invalid option arguments
* 2: No mount point specified
* 3: FUSE setup failed
* 4: Mounting failed
* 5: Failed to daemonize (detach from session)
* 6: Failed to set up signal handlers
* 7: An error occured during the life of the file system
*/
switch(i) {
case 1:
lua_pushstring(L,"Invalid option arguments.);
break;
case 2:
lua_pushstring(L,"No mount point specified.");
break;
case 3:
lua_pushstring(L,"FUSE setup failed.");
break;
case 4:
lua_pushstring(L,"Mounting failed.");
break;
case 5:
lua_pushstring(L,"Failed to daemonize (detach from session).");
break;
case 6:
lua_pushstring(L,"Failed to set up signal handlers.");
break;
case 7:
lua_pushstring(L,"An error occured during the life of the file system");
break;
default:
lua_pushstring(L,"Unknown Error.");
}
return 2;
}
}
// Function to get the context of a fuse call by calling fuse_get_context
static int lua__get_context(lua_State *L)
{
struct fuse_context* context; // defined in fuse.h
context = fuse_get_context();
if (lua_gettop(L) == 0)
lua_createtable(L, 0, 3);
else if (lua_type(L, 1)!=LUA_TTABLE)
return typeerror(L, 1, "table or nil");
lua_pushnumber(L, context->uid); lua_setfield(L, -2, "uid");
lua_pushnumber(L, context->gid); lua_setfield(L, -2, "gid");
lua_pushnumber(L, context->pid); lua_setfield(L, -2, "pid");
return 1;
}
/****************************************************************************/
static const luaL_Reg functions[] = {
{"main", lua__main},
{"get_context", lua__get_context},
{0, 0},
};
__attribute__((visibility ("default"))) int luaopen_flu(lua_State *L)
{
#if LUA_VERSION_NUM==502 || LUA_VERSION_NUM==503
lua_newtable(L); // table flu
#elif LUA_VERSION_NUM==501
{
static luaL_Reg empty[] = { {0, 0} };
luaL_register(L, lua_tostring(L, 1), empty);
}
#else
#error unsupported Lua version
#endif
setfuncs(L, functions, 0); // Registers all the functions in the array functions into the table on the top
// flu.main = lua_main
// flu.get_context = lua__get_context
// Note the last element pushed on the stack has the index -1
// 1st element pushed on the stack has the index 1
lua_pushnumber(L, FUSE_USE_VERSION);
lua_setfield(L, -2, "FUSE_USE_VERSION"); // flu.FUSE_USE_VERSION = FUSE_USE_VERSION
push_errno_table(L);
lua_setfield(L, -2, "errno"); // flu.errno = errno table created in push_errno_table
#if LUA_VERSION_NUM==502 || LUA_VERSION_NUM==503
return 1;
#else
return 0;
#endif
}
/*
Copyright (c) Jérôme Vuarand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
// vi: ts=4 sts=4 sw=4