-
Notifications
You must be signed in to change notification settings - Fork 18
/
Fuse.xs
executable file
·2291 lines (2175 loc) · 53.5 KB
/
Fuse.xs
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
992
993
994
995
996
997
998
999
1000
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <fuse.h>
#if defined(__linux__) || defined(__APPLE__)
# include <sys/xattr.h>
#else
# define XATTR_CREATE 1
# define XATTR_REPLACE 2
#endif
#if defined(__linux__) || defined(__sun__)
# define STAT_SEC(st, st_xtim) ((st)->st_xtim.tv_sec)
# define STAT_NSEC(st, st_xtim) ((st)->st_xtim.tv_nsec)
#else
# define STAT_SEC(st, st_xtim) ((st)->st_xtim##espec.tv_sec)
# define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
#endif
/* Implement a macro to handle multiple formats (integer, float, and array
* containing seconds and nanoseconds). */
#define PULL_TIME(st, st_xtim, svp) \
{ \
SV *sv = svp; \
if (SvROK(sv)) { \
AV *av = (AV *)SvRV(sv); \
if (SvTYPE((SV *)av) != SVt_PVAV) { \
Perl_croak_nocontext("Reference was not array ref"); \
} \
if (av_len(av) != 1) { \
Perl_croak_nocontext("Array of incorrect dimension"); \
} \
STAT_SEC(st, st_xtim) = SvIV(*(av_fetch(av, 0, FALSE))); \
STAT_NSEC(st, st_xtim) = SvIV(*(av_fetch(av, 1, FALSE))); \
} \
else if (SvNOK(sv) || SvIOK(sv) || SvPOK(sv)) { \
double tm = SvNV(sv); \
STAT_SEC(st, st_xtim) = (int)tm; \
STAT_NSEC(st, st_xtim) = (tm - (int)tm) * 1000000000; \
} \
else { \
Perl_croak_nocontext("Invalid data type passed"); \
} \
}
/* Determine if threads support should be included */
#ifdef USE_ITHREADS
# ifdef I_PTHREAD
# define FUSE_USE_ITHREADS
# if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
# define tTHX PerlInterpreter*
# define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
# define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
# define dMY_CXT_INTERP(interp) \
SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
# endif
# else
# warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
# endif
#endif
/* Global Data */
#define MY_CXT_KEY "Fuse::_guts" XS_VERSION
#if FUSE_VERSION >= 29
# if FUSE_FOUND_MICRO_VER >= 1
# define N_CALLBACKS 45
# else /* FUSE_FOUND_MICRO_VER < 1 */
# define N_CALLBACKS 44
# endif
#elif FUSE_VERSION >= 28
# define N_CALLBACKS 41
#else /* FUSE_VERSION < 28 */
# define N_CALLBACKS 38
#endif
#define N_FLAGS 8
typedef struct {
SV *callback[N_CALLBACKS];
HV *handles;
#ifdef USE_ITHREADS
tTHX self;
#endif
int threaded;
#ifdef USE_ITHREADS
perl_mutex mutex;
#endif
int utimens_as_array;
} my_cxt_t;
START_MY_CXT;
#ifdef FUSE_USE_ITHREADS
tTHX master_interp = NULL;
#define CLONE_INTERP(parent) S_clone_interp(parent)
tTHX S_clone_interp(tTHX parent) {
# if (PERL_VERSION < 10)
tTHX my_perl = parent;
#endif
dMY_CXT_INTERP(parent);
if(MY_CXT.threaded) {
MUTEX_LOCK(&MY_CXT.mutex);
PERL_SET_CONTEXT(parent);
dTHX;
#if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_COPY_STACKS);
#else
tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_COPY_STACKS | CLONEf_KEEP_PTR_TABLE);
ptr_table_free(PL_ptr_table);
PL_ptr_table = NULL;
#endif
MUTEX_UNLOCK(&MY_CXT.mutex);
return child;
}
return NULL;
}
# define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
# define FUSE_CONTEXT_POST }
#else
# define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
# define FUSE_CONTEXT_POST
#endif
#undef DEBUGf
#if 0
#define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
#else
#define DEBUGf(a...)
#endif
#define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
#define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
#define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
#define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
SV *val;
val = &PL_sv_undef;
if(fi->fh != 0) {
HE *he;
if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
val = HeVAL(he);
SvGETMAGIC(val);
}
}
return val;
}
void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
if(fi->fh != 0) {
(void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
fi->fh = 0;
}
}
void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
if(SvOK(sv)) {
#ifdef FUSE_USE_ITHREADS
if(MY_CXT.threaded) {
SvSHARE(sv);
}
#endif
/* This seems to be screwing things up... */
// MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
// fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
fi->fh = PTR2IV(sv);
if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
SvREFCNT_dec(sv);
}
SvSETMAGIC(sv);
}
}
int _PLfuse_getattr(const char *file, struct stat *result) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("getattr begin: %s\n",file);
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
PUTBACK;
rv = call_sv(MY_CXT.callback[0],G_ARRAY);
SPAGAIN;
if(rv != 13) {
if(rv > 1) {
fprintf(stderr,"inappropriate number of returned values from getattr\n");
rv = -ENOSYS;
} else if(rv)
rv = POPi;
else
rv = -ENOENT;
} else {
result->st_blocks = POPi;
result->st_blksize = POPi;
PULL_TIME(result, st_ctim, POPs);
PULL_TIME(result, st_mtim, POPs);
PULL_TIME(result, st_atim, POPs);
result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
result->st_rdev = POPi;
result->st_gid = POPi;
result->st_uid = POPi;
result->st_nlink = POPi;
result->st_mode = POPi;
result->st_ino = POPi;
result->st_dev = POPi;
rv = 0;
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("getattr end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
int rv;
if(buflen < 1)
return EINVAL;
FUSE_CONTEXT_PRE;
DEBUGf("readlink begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[1],G_SCALAR);
SPAGAIN;
if(!rv)
rv = -ENOENT;
else {
SV *mysv = POPs;
if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
rv = SvIV(mysv);
else {
/* as a safer choice instead of strncpy()... */
snprintf(buf, buflen, "%s", SvPV_nolen(mysv));
rv = 0;
}
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("readlink end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
int prv, rv;
SV **swp;
FUSE_CONTEXT_PRE;
DEBUGf("getdir begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
PUTBACK;
prv = call_sv(MY_CXT.callback[2],G_ARRAY);
SPAGAIN;
if(prv) {
/* Should yield the bottom of the current stack... */
swp = SP - prv + 1;
rv = POPi;
/* Sort of a hack to walk the stack in order, instead of reverse
* order - trying to explain to potential users why they need to
* reverse the order of this array would be confusing, at best. */
while (swp <= SP)
dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
SP -= prv - 1;
} else {
fprintf(stderr,"getdir() handler returned nothing!\n");
rv = -ENOSYS;
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("getdir end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("mknod begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(mode)));
XPUSHs(sv_2mortal(newSViv(dev)));
PUTBACK;
rv = call_sv(MY_CXT.callback[3],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("mknod end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_mkdir (const char *file, mode_t mode) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("mkdir begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(mode)));
PUTBACK;
rv = call_sv(MY_CXT.callback[4],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("mkdir end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_unlink (const char *file) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("unlink begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[5],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("unlink end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_rmdir (const char *file) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("rmdir begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[6],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("rmdir end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_symlink (const char *file, const char *new) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("symlink begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(new,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[7],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("symlink end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_rename (const char *file, const char *new) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("rename begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(new,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[8],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("rename end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_link (const char *file, const char *new) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("link begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(new,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[9],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("link end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_chmod (const char *file, mode_t mode) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("chmod begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(mode)));
PUTBACK;
rv = call_sv(MY_CXT.callback[10],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("chmod end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("chown begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(uid)));
XPUSHs(sv_2mortal(newSViv(gid)));
PUTBACK;
rv = call_sv(MY_CXT.callback[11],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("chown end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_truncate (const char *file, off_t off) {
int rv;
#ifndef PERL_HAS_64BITINT
char *temp;
#endif
FUSE_CONTEXT_PRE;
DEBUGf("truncate begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
#ifdef PERL_HAS_64BITINT
XPUSHs(sv_2mortal(newSViv(off)));
#else
if (asprintf(&temp, "%llu", off) == -1)
croak("Memory allocation failure!");
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
free(temp);
#endif
PUTBACK;
rv = call_sv(MY_CXT.callback[12],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("truncate end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_utime (const char *file, struct utimbuf *uti) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("utime begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(uti->actime)));
XPUSHs(sv_2mortal(newSViv(uti->modtime)));
PUTBACK;
rv = call_sv(MY_CXT.callback[13],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("utime end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
int rv;
int flags = fi->flags;
HV *fihash;
FUSE_CONTEXT_PRE;
DEBUGf("open begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSViv(flags)));
/* Create a hashref containing the details from fi
* which we can look at or modify.
*/
fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
fihash = newHV();
(void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
(void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
#if FUSE_VERSION >= 28
(void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
#endif
XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
/* All hashref things done */
PUTBACK;
/* Open called with filename, flags */
rv = call_sv(MY_CXT.callback[14],G_ARRAY);
SPAGAIN;
if(rv) {
if(rv > 1) {
FH_STOREHANDLE(fi,POPs);
}
rv = POPi;
}
else
rv = 0;
if (rv == 0)
{
/* Success, so copy the file handle which they returned */
SV **svp;
if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
fi->direct_io = SvIV(*svp);
if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
fi->keep_cache = SvIV(*svp);
#if FUSE_VERSION >= 28
if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
fi->nonseekable = SvIV(*svp);
#endif
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("open end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
struct fuse_file_info *fi) {
int rv;
#ifndef PERL_HAS_64BITINT
char *temp;
#endif
FUSE_CONTEXT_PRE;
DEBUGf("read begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
XPUSHs(sv_2mortal(newSViv(buflen)));
#ifdef PERL_HAS_64BITINT
XPUSHs(sv_2mortal(newSViv(off)));
#else
if (asprintf(&temp, "%llu", off) == -1)
croak("Memory allocation failure!");
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
free(temp);
#endif
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;
rv = call_sv(MY_CXT.callback[15],G_SCALAR);
SPAGAIN;
if(!rv)
rv = -ENOENT;
else {
SV *mysv = POPs;
if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
rv = SvIV(mysv);
else {
if(SvPOK(mysv)) {
rv = SvCUR(mysv);
} else {
rv = 0;
}
if(rv > buflen)
croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
if(rv)
memcpy(buf,SvPV_nolen(mysv),rv);
}
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("read end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
int rv;
SV *sv;
#ifndef PERL_HAS_64BITINT
char *temp;
#endif
FUSE_CONTEXT_PRE;
DEBUGf("write begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
#if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
sv = newSV(0);
sv_upgrade(sv, SVt_PV);
#else
sv = newSV_type(SVt_PV);
#endif
SvPV_set(sv, (char *)buf);
SvLEN_set(sv, 0);
SvCUR_set(sv, buflen);
SvPOK_on(sv);
SvREADONLY_on(sv);
XPUSHs(sv_2mortal(sv));
#ifdef PERL_HAS_64BITINT
XPUSHs(sv_2mortal(newSViv(off)));
#else
if (asprintf(&temp, "%llu", off) == -1)
croak("Memory allocation failure!");
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
free(temp);
#endif
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;
rv = call_sv(MY_CXT.callback[16],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("write end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_statfs (const char *file, struct statvfs *st) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("statfs begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
PUTBACK;
rv = call_sv(MY_CXT.callback[17],G_ARRAY);
SPAGAIN;
DEBUGf("statfs got %i params\n",rv);
if(rv == 6 || rv == 7) {
st->f_bsize = POPi;
st->f_bfree = POPi;
st->f_blocks = POPi;
st->f_ffree = POPi;
st->f_files = POPi;
st->f_namemax = POPi;
/* zero and fill-in other */
st->f_fsid = 0;
st->f_flag = 0;
st->f_frsize = st->f_bsize;
st->f_bavail = st->f_bfree;
st->f_favail = st->f_ffree;
if(rv == 7)
rv = POPi;
else
rv = 0;
} else
if(rv > 1)
croak("inappropriate number of returned values from statfs");
else
if(rv)
rv = POPi;
else
rv = -ENOSYS;
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("statfs end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("flush begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;
rv = call_sv(MY_CXT.callback[18],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("flush end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
int rv;
int flags = fi->flags;
#if FUSE_VERSION >= 29 && !defined(PERL_HAS_64BITINT)
char *temp;
#endif
FUSE_CONTEXT_PRE;
DEBUGf("release begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
XPUSHs(sv_2mortal(newSViv(flags)));
XPUSHs(FH_GETHANDLE(fi));
#if FUSE_VERSION >= 29
XPUSHs(fi->flock_release ? sv_2mortal(newSViv(1)) : &PL_sv_undef);
# ifdef PERL_HAS_64BITINT
XPUSHs(sv_2mortal(newSViv(fi->lock_owner)));
# else
if (asprintf(&temp, "%llu", fi->lock_owner) == -1)
croak("Memory allocation failure!");
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
# endif
#endif
PUTBACK;
rv = call_sv(MY_CXT.callback[19],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FH_RELEASEHANDLE(fi);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("release end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
int rv;
int flags = fi->flags;
FUSE_CONTEXT_PRE;
DEBUGf("fsync begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
XPUSHs(sv_2mortal(newSViv(flags)));
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;
rv = call_sv(MY_CXT.callback[20],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("fsync end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
#ifdef __APPLE__
int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
#else
int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
#endif
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("setxattr begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(name,0)));
XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
XPUSHs(sv_2mortal(newSViv(flags)));
PUTBACK;
rv = call_sv(MY_CXT.callback[21],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("setxattr end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
#ifdef __APPLE__
int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
#else
int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
#endif
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("getxattr begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(name,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[22],G_SCALAR);
SPAGAIN;
if(!rv)
rv = -ENOENT;
else {
SV *mysv = POPs;
rv = 0;
if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
rv = SvIV(mysv);
else {
if(SvPOK(mysv)) {
rv = SvCUR(mysv);
} else {
rv = 0;
}
if ((rv > 0) && (buflen > 0))
{
if(rv > buflen)
rv = -ERANGE;
else
memcpy(buf,SvPV_nolen(mysv),rv);
}
}
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("getxattr end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_listxattr (const char *file, char *list, size_t size) {
int prv, rv;
FUSE_CONTEXT_PRE;
DEBUGf("listxattr begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
PUTBACK;
prv = call_sv(MY_CXT.callback[23],G_ARRAY);
SPAGAIN;
if(!prv)
rv = -ENOENT;
else {
char *p = list;
int spc = size;
int total_len = 0;
rv = POPi;
prv--;
/* Always nul terminate */
if (list && (size > 0))
list[0] = '\0';
while (prv > 0)
{
SV *mysv = POPs;
prv--;
if (SvPOK(mysv)) {
/* Copy nul too */
int s = SvCUR(mysv) + 1;
total_len += s;
if (p && (size > 0) && (spc >= s))
{
memcpy(p,SvPV_nolen(mysv),s);
p += s;
spc -= s;
}
}
}
/*
* If the Perl returned an error, return that.
* Otherwise check that the buffer was big enough.
*/
if (rv == 0)
{
rv = total_len;
if ((size > 0) && (size < total_len))
rv = -ERANGE;
}
}
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("listxattr end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_removexattr (const char *file, const char *name) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("removexattr begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
XPUSHs(sv_2mortal(newSVpv(name,0)));
PUTBACK;
rv = call_sv(MY_CXT.callback[24],G_SCALAR);
SPAGAIN;
rv = (rv ? POPi : 0);
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("removexattr end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
int rv;
FUSE_CONTEXT_PRE;
DEBUGf("opendir begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv(file,0)));
fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
PUTBACK;
rv = call_sv(MY_CXT.callback[25], G_ARRAY);
SPAGAIN;
if (rv) {
if (rv > 1) {
FH_STOREHANDLE(fi, POPs);
}
rv = POPi;
} else
rv = 0;
FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("opendir end: %i\n",rv);
FUSE_CONTEXT_POST;
return rv;
}
int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
off_t off, struct fuse_file_info *fi) {
int prv = 0, rv;
SV *sv, **svp, **swp;
AV *av, *av2;