1 #define PERL_NO_GET_CONTEXT
8 #if (defined(__FreeBSD__) && __FreeBSD__ < 10) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 #if defined(__linux__)
16 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim.tv_nsec)
18 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
21 /* Implement a macro to handle multiple formats (integer, float, and array
22 * containing seconds and nanoseconds). */
23 #define PULL_TIME(st, st_xtim, svp) \
27 AV *av = (AV *)SvRV(sv); \
28 if (SvTYPE((SV *)av) != SVt_PVAV) { \
29 Perl_croak_nocontext("Reference was not array ref"); \
31 if (av_len(av) != 1) { \
32 Perl_croak_nocontext("Array of incorrect dimension"); \
34 (st)->st_xtim##e = SvIV(*(av_fetch(av, 0, FALSE))); \
35 STAT_NSEC(st, st_xtim) = SvIV(*(av_fetch(av, 1, FALSE))); \
37 else if (SvNOK(sv) || SvIOK(sv)) { \
38 double tm = SvNV(sv); \
39 (st)->st_xtim##e = (int)tm; \
40 STAT_NSEC(st, st_xtim) = (tm - (int)tm) * 1000000000; \
43 Perl_croak_nocontext("Invalid data type passed"); \
47 /* Determine if threads support should be included */
50 # define FUSE_USE_ITHREADS
51 # if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
52 # define tTHX PerlInterpreter*
53 # define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
54 # define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
55 # define dMY_CXT_INTERP(interp) \
56 SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
57 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
60 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
66 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
67 #if FUSE_VERSION >= 28
68 # define N_CALLBACKS 41
70 # define N_CALLBACKS 38
74 SV *callback[N_CALLBACKS];
87 #ifdef FUSE_USE_ITHREADS
88 tTHX master_interp = NULL;
90 #define CLONE_INTERP(parent) S_clone_interp(parent)
91 tTHX S_clone_interp(tTHX parent) {
92 # if (PERL_VERSION < 10)
93 tTHX my_perl = parent;
95 dMY_CXT_INTERP(parent);
97 MUTEX_LOCK(&MY_CXT.mutex);
98 PERL_SET_CONTEXT(parent);
100 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
101 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
103 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
104 ptr_table_free(PL_ptr_table);
107 MUTEX_UNLOCK(&MY_CXT.mutex);
113 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
114 # define FUSE_CONTEXT_POST }
116 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
117 # define FUSE_CONTEXT_POST
122 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
127 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
128 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
129 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
130 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
132 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
137 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
145 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
147 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
152 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
154 #ifdef FUSE_USE_ITHREADS
155 if(MY_CXT.threaded) {
159 /* This seems to be screwing things up... */
160 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
161 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
163 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
170 int _PLfuse_getattr(const char *file, struct stat *result) {
173 DEBUGf("getattr begin: %s\n",file);
177 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
179 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
183 fprintf(stderr,"inappropriate number of returned values from getattr\n");
190 result->st_blocks = POPi;
191 result->st_blksize = POPi;
192 PULL_TIME(result, st_ctim, POPs);
193 PULL_TIME(result, st_mtim, POPs);
194 PULL_TIME(result, st_atim, POPs);
195 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
196 result->st_rdev = POPi;
197 result->st_gid = POPi;
198 result->st_uid = POPi;
199 result->st_nlink = POPi;
200 result->st_mode = POPi;
201 result->st_ino = POPi;
202 result->st_dev = POPi;
208 DEBUGf("getattr end: %i\n",rv);
213 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
218 DEBUGf("readlink begin\n");
222 XPUSHs(sv_2mortal(newSVpv(file,0)));
224 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
230 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
233 strncpy(buf,SvPV_nolen(mysv),buflen);
241 DEBUGf("readlink end: %i\n",rv);
246 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
250 DEBUGf("getdir begin\n");
254 XPUSHs(sv_2mortal(newSVpv(file,0)));
256 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
259 /* Should yield the bottom of the current stack... */
260 swp = &TOPs - prv + 1;
262 /* Sort of a hack to walk the stack in order, instead of reverse
263 * order - trying to explain to potential users why they need to
264 * reverse the order of this array would be confusing, at best. */
266 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
268 fprintf(stderr,"getdir() handler returned nothing!\n");
274 DEBUGf("getdir end: %i\n",rv);
279 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
282 DEBUGf("mknod begin\n");
286 XPUSHs(sv_2mortal(newSVpv(file,0)));
287 XPUSHs(sv_2mortal(newSViv(mode)));
288 XPUSHs(sv_2mortal(newSViv(dev)));
290 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
292 rv = (rv ? POPi : 0);
296 DEBUGf("mknod end: %i\n",rv);
301 int _PLfuse_mkdir (const char *file, mode_t mode) {
304 DEBUGf("mkdir begin\n");
308 XPUSHs(sv_2mortal(newSVpv(file,0)));
309 XPUSHs(sv_2mortal(newSViv(mode)));
311 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
313 rv = (rv ? POPi : 0);
317 DEBUGf("mkdir end: %i\n",rv);
323 int _PLfuse_unlink (const char *file) {
326 DEBUGf("unlink begin\n");
330 XPUSHs(sv_2mortal(newSVpv(file,0)));
332 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
334 rv = (rv ? POPi : 0);
338 DEBUGf("unlink end: %i\n",rv);
343 int _PLfuse_rmdir (const char *file) {
346 DEBUGf("rmdir begin\n");
350 XPUSHs(sv_2mortal(newSVpv(file,0)));
352 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
354 rv = (rv ? POPi : 0);
358 DEBUGf("rmdir end: %i\n",rv);
363 int _PLfuse_symlink (const char *file, const char *new) {
366 DEBUGf("symlink begin\n");
370 XPUSHs(sv_2mortal(newSVpv(file,0)));
371 XPUSHs(sv_2mortal(newSVpv(new,0)));
373 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
375 rv = (rv ? POPi : 0);
379 DEBUGf("symlink end: %i\n",rv);
384 int _PLfuse_rename (const char *file, const char *new) {
387 DEBUGf("rename begin\n");
391 XPUSHs(sv_2mortal(newSVpv(file,0)));
392 XPUSHs(sv_2mortal(newSVpv(new,0)));
394 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
396 rv = (rv ? POPi : 0);
400 DEBUGf("rename end: %i\n",rv);
405 int _PLfuse_link (const char *file, const char *new) {
408 DEBUGf("link begin\n");
412 XPUSHs(sv_2mortal(newSVpv(file,0)));
413 XPUSHs(sv_2mortal(newSVpv(new,0)));
415 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
417 rv = (rv ? POPi : 0);
421 DEBUGf("link end: %i\n",rv);
426 int _PLfuse_chmod (const char *file, mode_t mode) {
429 DEBUGf("chmod begin\n");
433 XPUSHs(sv_2mortal(newSVpv(file,0)));
434 XPUSHs(sv_2mortal(newSViv(mode)));
436 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
438 rv = (rv ? POPi : 0);
442 DEBUGf("chmod end: %i\n",rv);
447 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
450 DEBUGf("chown begin\n");
454 XPUSHs(sv_2mortal(newSVpv(file,0)));
455 XPUSHs(sv_2mortal(newSViv(uid)));
456 XPUSHs(sv_2mortal(newSViv(gid)));
458 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
460 rv = (rv ? POPi : 0);
464 DEBUGf("chown end: %i\n",rv);
469 int _PLfuse_truncate (const char *file, off_t off) {
471 #ifndef PERL_HAS_64BITINT
475 DEBUGf("truncate begin\n");
479 XPUSHs(sv_2mortal(newSVpv(file,0)));
480 #ifdef PERL_HAS_64BITINT
481 XPUSHs(sv_2mortal(newSViv(off)));
483 if (asprintf(&temp, "%llu", off) == -1)
484 croak("Memory allocation failure!");
485 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
489 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
491 rv = (rv ? POPi : 0);
495 DEBUGf("truncate end: %i\n",rv);
500 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
503 DEBUGf("utime begin\n");
507 XPUSHs(sv_2mortal(newSVpv(file,0)));
508 XPUSHs(sv_2mortal(newSViv(uti->actime)));
509 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
511 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
513 rv = (rv ? POPi : 0);
517 DEBUGf("utime end: %i\n",rv);
522 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
524 int flags = fi->flags;
527 DEBUGf("open begin\n");
531 XPUSHs(sv_2mortal(newSVpv(file,0)));
532 XPUSHs(sv_2mortal(newSViv(flags)));
533 /* Create a hashref containing the details from fi
534 * which we can look at or modify.
536 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
538 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
539 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
540 #if FUSE_VERSION >= 28
541 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
543 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
544 /* All hashref things done */
547 /* Open called with filename, flags */
548 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
552 FH_STOREHANDLE(fi,POPs);
560 /* Success, so copy the file handle which they returned */
562 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
563 fi->direct_io = SvIV(*svp);
564 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
565 fi->keep_cache = SvIV(*svp);
566 #if FUSE_VERSION >= 28
567 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
568 fi->nonseekable = SvIV(*svp);
574 DEBUGf("open end: %i\n",rv);
579 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
580 struct fuse_file_info *fi) {
582 #ifndef PERL_HAS_64BITINT
586 DEBUGf("read begin\n");
590 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
591 XPUSHs(sv_2mortal(newSViv(buflen)));
592 #ifdef PERL_HAS_64BITINT
593 XPUSHs(sv_2mortal(newSViv(off)));
595 if (asprintf(&temp, "%llu", off) == -1)
596 croak("Memory allocation failure!");
597 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
600 XPUSHs(FH_GETHANDLE(fi));
602 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
608 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
617 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
619 memcpy(buf,SvPV_nolen(mysv),rv);
625 DEBUGf("read end: %i\n",rv);
630 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
632 #ifndef PERL_HAS_64BITINT
636 DEBUGf("write begin\n");
640 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
641 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
642 #ifdef PERL_HAS_64BITINT
643 XPUSHs(sv_2mortal(newSViv(off)));
645 if (asprintf(&temp, "%llu", off) == -1)
646 croak("Memory allocation failure!");
647 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
650 XPUSHs(FH_GETHANDLE(fi));
652 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
654 rv = (rv ? POPi : 0);
658 DEBUGf("write end: %i\n",rv);
663 int _PLfuse_statfs (const char *file, struct statvfs *st) {
666 DEBUGf("statfs begin\n");
671 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
673 DEBUGf("statfs got %i params\n",rv);
674 if(rv == 6 || rv == 7) {
680 st->f_namemax = POPi;
681 /* zero and fill-in other */
685 st->f_bavail = st->f_bfree;
686 st->f_favail = st->f_ffree;
694 croak("inappropriate number of returned values from statfs");
703 DEBUGf("statfs end: %i\n",rv);
708 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
711 DEBUGf("flush begin\n");
715 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
716 XPUSHs(FH_GETHANDLE(fi));
718 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
720 rv = (rv ? POPi : 0);
724 DEBUGf("flush end: %i\n",rv);
729 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
731 int flags = fi->flags;
733 DEBUGf("release begin\n");
737 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
738 XPUSHs(sv_2mortal(newSViv(flags)));
739 XPUSHs(FH_GETHANDLE(fi));
741 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
743 rv = (rv ? POPi : 0);
744 FH_RELEASEHANDLE(fi);
748 DEBUGf("release end: %i\n",rv);
753 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
755 int flags = fi->flags;
757 DEBUGf("fsync begin\n");
761 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
762 XPUSHs(sv_2mortal(newSViv(flags)));
763 XPUSHs(FH_GETHANDLE(fi));
765 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
767 rv = (rv ? POPi : 0);
771 DEBUGf("fsync end: %i\n",rv);
776 #if __FreeBSD__ >= 10
777 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
779 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
783 DEBUGf("setxattr begin\n");
787 XPUSHs(sv_2mortal(newSVpv(file,0)));
788 XPUSHs(sv_2mortal(newSVpv(name,0)));
789 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
790 XPUSHs(sv_2mortal(newSViv(flags)));
792 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
794 rv = (rv ? POPi : 0);
798 DEBUGf("setxattr end: %i\n",rv);
803 #if __FreeBSD__ >= 10
804 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
806 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
810 DEBUGf("getxattr begin\n");
814 XPUSHs(sv_2mortal(newSVpv(file,0)));
815 XPUSHs(sv_2mortal(newSVpv(name,0)));
817 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
825 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
833 if ((rv > 0) && (buflen > 0))
838 memcpy(buf,SvPV_nolen(mysv),rv);
845 DEBUGf("getxattr end: %i\n",rv);
850 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
853 DEBUGf("listxattr begin\n");
857 XPUSHs(sv_2mortal(newSVpv(file,0)));
859 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
872 /* Always nul terminate */
873 if (list && (size > 0))
883 int s = SvCUR(mysv) + 1;
886 if (p && (size > 0) && (spc >= s))
888 memcpy(p,SvPV_nolen(mysv),s);
896 * If the Perl returned an error, return that.
897 * Otherwise check that the buffer was big enough.
902 if ((size > 0) && (size < total_len))
909 DEBUGf("listxattr end: %i\n",rv);
914 int _PLfuse_removexattr (const char *file, const char *name) {
917 DEBUGf("removexattr begin\n");
921 XPUSHs(sv_2mortal(newSVpv(file,0)));
922 XPUSHs(sv_2mortal(newSVpv(name,0)));
924 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
926 rv = (rv ? POPi : 0);
930 DEBUGf("removexattr end: %i\n",rv);
935 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
938 DEBUGf("opendir begin\n");
942 XPUSHs(sv_2mortal(newSVpv(file,0)));
943 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
945 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
949 FH_STOREHANDLE(fi, POPs);
957 DEBUGf("opendir end: %i\n",rv);
963 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
964 off_t off, struct fuse_file_info *fi) {
966 SV *sv, **svp, **swp;
970 #ifndef PERL_HAS_64BITINT
974 DEBUGf("readdir begin\n");
978 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
979 #ifdef PERL_HAS_64BITINT
980 XPUSHs(sv_2mortal(newSViv(off)));
982 if (asprintf(&temp, "%llu", off) == -1)
983 croak("Memory allocation failure!");
984 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
987 XPUSHs(FH_GETHANDLE(fi));
989 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
992 /* Should yield the bottom of the current stack... */
993 swp = &TOPs - prv + 1;
995 memset(&st, 0, sizeof(struct stat));
996 /* Sort of a hack to walk the stack in order, instead of reverse
997 * order - trying to explain to potential users why they need to
998 * reverse the order of this array would be confusing, at best. */
999 while (swp <= &TOPs) {
1001 if (!SvROK(sv) && SvPOK(sv))
1002 /* Just a bare SV (probably a string; hopefully a string) */
1003 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
1004 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
1005 if (av_len(av) >= 2) {
1006 /* The third element of the array should be the args that
1007 * would otherwise go to getattr(); a lot of filesystems
1008 * will, or at least can, return that info as part of the
1009 * enumeration process... */
1010 svp = av_fetch(av, 2, FALSE);
1012 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1013 av_len(av2) == 12) {
1014 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1015 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1016 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1017 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1018 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1019 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1020 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1021 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1022 PULL_TIME(&st, st_atim, *(av_fetch(av2, 8, FALSE)));
1023 PULL_TIME(&st, st_mtim, *(av_fetch(av2, 9, FALSE)));
1024 PULL_TIME(&st, st_ctim, *(av_fetch(av2, 10, FALSE)));
1025 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1026 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1030 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1031 /* For now if the element isn't what we want, just
1032 * quietly ignore it... */
1034 if (av_len(av) >= 1) {
1035 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1036 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1037 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1040 memset(&st, 0, sizeof(struct stat));
1045 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1048 fprintf(stderr,"readdir() handler returned nothing!\n");
1054 DEBUGf("readdir end: %i\n",rv);
1059 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1062 DEBUGf("releasedir begin\n");
1066 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1067 XPUSHs(FH_GETHANDLE(fi));
1069 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1071 rv = (rv ? POPi : 0);
1072 FH_RELEASEHANDLE(fi);
1076 DEBUGf("releasedir end: %i\n",rv);
1081 int _PLfuse_fsyncdir(const char *file, int datasync,
1082 struct fuse_file_info *fi) {
1085 DEBUGf("fsyncdir begin\n");
1089 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1090 XPUSHs(sv_2mortal(newSViv(datasync)));
1091 XPUSHs(FH_GETHANDLE(fi));
1093 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1095 rv = (rv ? POPi : 0);
1099 DEBUGf("fsyncdir end: %i\n",rv);
1104 void *_PLfuse_init(struct fuse_conn_info *fc)
1109 DEBUGf("init begin\n");
1114 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1118 if (rv == &PL_sv_undef)
1121 rv = SvREFCNT_inc((SV *)rv);
1126 DEBUGf("init end: %p\n", rv);
1131 void _PLfuse_destroy(void *private_data) {
1133 DEBUGf("destroy begin\n");
1137 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1139 call_sv(MY_CXT.callback[30], G_VOID);
1142 SvREFCNT_dec((SV *)private_data);
1146 DEBUGf("init end\n");
1150 int _PLfuse_access(const char *file, int mask) {
1153 DEBUGf("access begin\n");
1157 XPUSHs(sv_2mortal(newSVpv(file,0)));
1158 XPUSHs(sv_2mortal(newSViv(mask)));
1160 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1162 rv = (rv ? POPi : 0);
1166 DEBUGf("access end: %d\n", rv);
1171 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1175 DEBUGf("create begin\n");
1179 XPUSHs(sv_2mortal(newSVpv(file,0)));
1180 XPUSHs(sv_2mortal(newSViv(mode)));
1181 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1182 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1183 /* Create a hashref containing the details from fi
1184 * which we can look at or modify.
1187 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1188 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1189 #if FUSE_VERSION >= 28
1190 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1192 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1193 /* All hashref things done */
1196 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1200 FH_STOREHANDLE(fi,POPs);
1205 fprintf(stderr, "create() handler returned nothing!\n");
1209 /* Success, so copy the file handle which they returned */
1211 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1212 fi->direct_io = SvIV(*svp);
1213 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1214 fi->keep_cache = SvIV(*svp);
1215 #if FUSE_VERSION >= 28
1216 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1217 fi->nonseekable = SvIV(*svp);
1223 DEBUGf("create end: %d\n",rv);
1228 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1230 #ifndef PERL_HAS_64BITINT
1234 DEBUGf("ftruncate begin\n");
1238 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1239 #ifdef PERL_HAS_64BITINT
1240 XPUSHs(sv_2mortal(newSViv(off)));
1242 if (asprintf(&temp, "%llu", off) == -1)
1243 croak("Memory allocation failure!");
1244 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1247 XPUSHs(FH_GETHANDLE(fi));
1249 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1251 rv = (rv ? POPi : 0);
1255 DEBUGf("ftruncate end: %i\n",rv);
1260 int _PLfuse_fgetattr(const char *file, struct stat *result,
1261 struct fuse_file_info *fi) {
1264 DEBUGf("fgetattr begin: %s\n",file);
1268 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1269 XPUSHs(FH_GETHANDLE(fi));
1271 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1275 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1282 result->st_blocks = POPi;
1283 result->st_blksize = POPi;
1284 PULL_TIME(result, st_ctim, POPs);
1285 PULL_TIME(result, st_mtim, POPs);
1286 PULL_TIME(result, st_atim, POPs);
1287 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1288 result->st_rdev = POPi;
1289 result->st_gid = POPi;
1290 result->st_uid = POPi;
1291 result->st_nlink = POPi;
1292 result->st_mode = POPi;
1293 result->st_ino = POPi;
1294 result->st_dev = POPi;
1300 DEBUGf("fgetattr end: %i\n",rv);
1305 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1306 struct flock *lockinfo) {
1310 #ifndef PERL_HAS_64BITINT
1314 DEBUGf("lock begin\n");
1318 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1319 XPUSHs(sv_2mortal(newSViv(cmd)));
1322 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1323 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1324 #ifdef PERL_HAS_64BITINT
1325 sv = newSViv(lockinfo->l_start);
1327 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1328 croak("Memory allocation failure!");
1329 sv = newSVpv(temp, 0);
1332 (void) hv_store(lihash, "l_start", 7, sv, 0);
1333 #ifdef PERL_HAS_64BITINT
1334 sv = newSViv(lockinfo->l_len);
1336 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1337 croak("Memory allocation failure!");
1338 sv = newSVpv(temp, 0);
1341 (void) hv_store(lihash, "l_len", 5, sv, 0);
1342 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1344 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1345 XPUSHs(FH_GETHANDLE(fi));
1348 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1350 rv = (rv ? POPi : 0);
1351 if (lockinfo && !rv) {
1352 /* Need to copy back any altered values from the hash into
1355 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1356 lockinfo->l_type = SvIV(*svp);
1357 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1358 lockinfo->l_whence = SvIV(*svp);
1359 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1360 lockinfo->l_start = SvNV(*svp);
1361 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1362 lockinfo->l_len = SvNV(*svp);
1363 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1364 lockinfo->l_pid = SvIV(*svp);
1369 DEBUGf("lock end: %i\n",rv);
1374 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1377 DEBUGf("utimens begin\n");
1381 XPUSHs(sv_2mortal(newSVpv(file,0)));
1382 if (MY_CXT.utimens_as_array) {
1383 /* Pushing timespecs as 2-element arrays (if tv is present). */
1387 av_push(av, newSViv(tv[0].tv_sec));
1388 av_push(av, newSViv(tv[0].tv_nsec));
1389 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1391 av_push(av, newSViv(tv[1].tv_sec));
1392 av_push(av, newSViv(tv[1].tv_nsec));
1393 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1396 XPUSHs(&PL_sv_undef);
1397 XPUSHs(&PL_sv_undef);
1402 /* Pushing timespecs as floating point (double) values. */
1403 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1404 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1407 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1409 rv = (rv ? POPi : 0);
1413 DEBUGf("utimens end: %i\n",rv);
1418 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1420 #ifndef PERL_HAS_64BITINT
1424 DEBUGf("bmap begin\n");
1428 XPUSHs(sv_2mortal(newSVpv(file,0)));
1429 XPUSHs(sv_2mortal(newSViv(blocksize)));
1430 #ifdef PERL_HAS_64BITINT
1431 XPUSHs(sv_2mortal(newSViv(*idx)));
1433 if (asprintf(&temp, "%llu", *idx) == -1)
1434 croak("Memory allocation failure!");
1435 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1439 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1441 if (rv > 0 && rv < 3) {
1447 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1453 DEBUGf("bmap end: %i\n",rv);
1458 #if FUSE_VERSION >= 28
1459 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1460 struct fuse_file_info *fi, unsigned int flags, void *data) {
1464 DEBUGf("ioctl begin\n");
1468 XPUSHs(sv_2mortal(newSVpv(file,0)));
1469 /* I don't know why cmd is a signed int in the first place;
1470 * casting as unsigned so stupid tricks don't have to be done on
1472 XPUSHs(sv_2mortal(newSVuv((unsigned int)cmd)));
1473 XPUSHs(sv_2mortal(newSViv(flags)));
1474 if (_IOC_DIR(cmd) & _IOC_WRITE)
1475 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1477 XPUSHs(&PL_sv_undef);
1478 XPUSHs(FH_GETHANDLE(fi));
1480 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1482 if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
1490 if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
1493 char *rdata = SvPV(sv, len);
1495 if (len > _IOC_SIZE(cmd)) {
1496 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1500 memset(data, 0, _IOC_SIZE(cmd));
1501 memcpy(data, rdata, len);
1505 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1512 DEBUGf("ioctl end: %i\n",rv);
1517 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1518 struct fuse_pollhandle *ph, unsigned *reventsp) {
1522 DEBUGf("poll begin\n");
1526 XPUSHs(sv_2mortal(newSVpv(file,0)));
1528 /* Still gotta figure out how to do this right... */
1529 sv = newSViv(PTR2IV(ph));
1535 XPUSHs(&PL_sv_undef);
1536 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1537 XPUSHs(FH_GETHANDLE(fi));
1539 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1545 rv = (rv ? POPi : 0);
1549 DEBUGf("poll end: %i\n", rv);
1553 #endif /* FUSE_VERSION >= 28 */
1555 struct fuse_operations _available_ops = {
1556 getattr: _PLfuse_getattr,
1557 readlink: _PLfuse_readlink,
1558 getdir: _PLfuse_getdir,
1559 mknod: _PLfuse_mknod,
1560 mkdir: _PLfuse_mkdir,
1561 unlink: _PLfuse_unlink,
1562 rmdir: _PLfuse_rmdir,
1563 symlink: _PLfuse_symlink,
1564 rename: _PLfuse_rename,
1566 chmod: _PLfuse_chmod,
1567 chown: _PLfuse_chown,
1568 truncate: _PLfuse_truncate,
1569 utime: _PLfuse_utime,
1572 write: _PLfuse_write,
1573 statfs: _PLfuse_statfs,
1574 flush: _PLfuse_flush,
1575 release: _PLfuse_release,
1576 fsync: _PLfuse_fsync,
1577 setxattr: _PLfuse_setxattr,
1578 getxattr: _PLfuse_getxattr,
1579 listxattr: _PLfuse_listxattr,
1580 removexattr: _PLfuse_removexattr,
1581 opendir: _PLfuse_opendir,
1582 readdir: _PLfuse_readdir,
1583 releasedir: _PLfuse_releasedir,
1584 fsyncdir: _PLfuse_fsyncdir,
1586 destroy: _PLfuse_destroy,
1587 access: _PLfuse_access,
1588 create: _PLfuse_create,
1589 ftruncate: _PLfuse_ftruncate,
1590 fgetattr: _PLfuse_fgetattr,
1592 utimens: _PLfuse_utimens,
1594 #if FUSE_VERSION >= 28
1595 ioctl: _PLfuse_ioctl,
1597 #endif /* FUSE_VERSION >= 28 */
1600 MODULE = Fuse PACKAGE = Fuse
1619 tTHX parent = MY_CXT.self;
1620 MY_CXT.self = my_perl;
1621 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1622 /* CLONE entered without a pointer table, so we can't safely clone static data */
1624 for(i=0;i<N_CALLBACKS;i++) {
1625 MY_CXT.callback[i] = NULL;
1627 MY_CXT.handles = newHV();
1631 CLONE_PARAMS *clone_param;
1632 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1633 clone_param = Perl_clone_params_new(parent, aTHX);
1635 CLONE_PARAMS raw_param;
1636 raw_param.flags = 0;
1637 raw_param.proto_perl = parent;
1638 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1639 clone_param = &raw_param;
1641 for(i=0;i<N_CALLBACKS;i++) {
1642 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1644 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1645 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1646 Perl_clone_params_del(clone_param);
1654 struct fuse_context *fc;
1656 fc = fuse_get_context();
1659 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1660 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1661 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1662 if (fc->private_data)
1663 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1664 #if FUSE_VERSION >= 28
1665 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1666 #endif /* FUSE_VERSION >= 28 */
1667 RETVAL = newRV_noinc((SV*)hash);
1677 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1684 RETVAL = newSViv(XATTR_CREATE);
1691 RETVAL = newSViv(XATTR_REPLACE);
1698 struct fuse_operations fops;
1702 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1703 struct fuse_chan *fc;
1706 if(items != N_CALLBACKS + 6) {
1707 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1710 memset(&fops, 0, sizeof(struct fuse_operations));
1712 debug = SvIV(ST(0));
1713 MY_CXT.threaded = SvIV(ST(1));
1714 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1715 if(MY_CXT.threaded) {
1716 #ifdef FUSE_USE_ITHREADS
1717 master_interp = aTHX;
1718 MUTEX_INIT(&MY_CXT.mutex);
1719 SvSHARE((SV*)(MY_CXT.handles));
1721 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1722 "mode, but your perl was not built with a supported "
1723 "thread model. Threads are disabled.\n");
1724 MY_CXT.threaded = 0;
1727 mountpoint = SvPV_nolen(ST(2));
1728 mountopts = SvPV_nolen(ST(3));
1729 #if FUSE_VERSION >= 28
1730 fops.flag_nullpath_ok = SvIV(ST(4));
1731 #endif /* FUSE_VERSION >= 28 */
1732 MY_CXT.utimens_as_array = SvIV(ST(5));
1733 for(i=0;i<N_CALLBACKS;i++) {
1735 /* allow symbolic references, or real code references. */
1736 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1737 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1738 /* Dirty hack, to keep anything from overwriting the
1739 * flag area with a pointer. There should never be
1740 * anything passed as 'junk', but this prevents
1741 * someone from doing it and screwing things up... */
1745 MY_CXT.callback[i] = var;
1746 } else if(SvOK(var)) {
1747 croak("invalid callback (%i) passed to perl_fuse_main "
1748 "(%s is not a string, code ref, or undef).\n",
1749 i+6,SvPVbyte_nolen(var));
1751 MY_CXT.callback[i] = NULL;
1755 * XXX: What comes here is just a ridiculous use of the option parsing API
1756 * to hack on compatibility with other parts of the new API. First and
1757 * foremost, real C argc/argv would be good to get at...
1759 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1760 fuse_opt_free_args(&args);
1761 croak("out of memory\n");
1763 if (mountopts && strcmp("", mountopts) &&
1764 (fuse_opt_add_arg(&args, "-o") == -1 ||
1765 fuse_opt_add_arg(&args, mountopts) == -1)) {
1766 fuse_opt_free_args(&args);
1767 croak("out of memory\n");
1769 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1770 fuse_opt_free_args(&args);
1771 croak("out of memory\n");
1773 fc = fuse_mount(mountpoint,&args);
1775 croak("could not mount fuse filesystem!\n");
1777 if(MY_CXT.threaded) {
1778 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1781 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1782 fuse_unmount(mountpoint,fc);
1783 fuse_opt_free_args(&args);
1785 #if FUSE_VERSION >= 28
1788 pollhandle_destroy(...)
1790 struct fuse_pollhandle *ph;
1793 fprintf(stderr, "No pollhandle passed?\n");
1797 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1798 fuse_pollhandle_destroy(ph);
1803 struct fuse_pollhandle *ph;
1806 fprintf(stderr, "No pollhandle passed?\n");
1810 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1811 RETVAL = fuse_notify_poll(ph);