1 #define PERL_NO_GET_CONTEXT
8 #if defined(__FreeBSD__) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 /* Determine if threads support should be included */
18 # define FUSE_USE_ITHREADS
19 # if (PERL_VERSION < 10)
20 # define tTHX PerlInterpreter*
21 # define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
22 # define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
23 # define dMY_CXT_INTERP(interp) \
24 SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
25 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
28 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
34 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
35 /* #if FUSE_VERSION >= 28
36 # define N_CALLBACKS 41 */
37 #if FUSE_VERSION >= 26
38 # define N_CALLBACKS 38
39 #elif FUSE_VERSION >= 25
40 # define N_CALLBACKS 35
41 #elif FUSE_VERSION >= 23
42 # define N_CALLBACKS 31
44 # define N_CALLBACKS 25
48 SV *callback[N_CALLBACKS];
60 #ifdef FUSE_USE_ITHREADS
61 tTHX master_interp = NULL;
63 #define CLONE_INTERP(parent) S_clone_interp(parent)
64 tTHX S_clone_interp(tTHX parent) {
65 #if (PERL_VERSION < 10)
66 tTHX my_perl = parent;
68 dMY_CXT_INTERP(parent);
70 MUTEX_LOCK(&MY_CXT.mutex);
71 PERL_SET_CONTEXT(parent);
73 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
74 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
76 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
77 ptr_table_free(PL_ptr_table);
80 MUTEX_UNLOCK(&MY_CXT.mutex);
86 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
87 # define FUSE_CONTEXT_POST }
89 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
90 # define FUSE_CONTEXT_POST
95 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
100 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
101 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
102 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
103 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
105 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
110 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
118 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
120 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
125 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
127 #ifdef FUSE_USE_ITHREADS
128 if(MY_CXT.threaded) {
132 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
133 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
134 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
141 int _PLfuse_getattr(const char *file, struct stat *result) {
144 DEBUGf("getattr begin: %s\n",file);
148 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
150 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
154 fprintf(stderr,"inappropriate number of returned values from getattr\n");
161 result->st_blocks = POPi;
162 result->st_blksize = POPi;
163 result->st_ctime = POPi;
164 result->st_mtime = POPi;
165 result->st_atime = POPi;
166 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
167 result->st_rdev = POPi;
168 result->st_gid = POPi;
169 result->st_uid = POPi;
170 result->st_nlink = POPi;
171 result->st_mode = POPi;
172 result->st_ino = POPi;
173 result->st_dev = POPi;
179 DEBUGf("getattr end: %i\n",rv);
184 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
189 DEBUGf("readlink begin\n");
193 XPUSHs(sv_2mortal(newSVpv(file,0)));
195 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
201 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
204 strncpy(buf,SvPV_nolen(mysv),buflen);
212 DEBUGf("readlink end: %i\n",rv);
217 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
221 DEBUGf("getdir begin\n");
225 XPUSHs(sv_2mortal(newSVpv(file,0)));
227 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
230 /* Should yield the bottom of the current stack... */
231 swp = &TOPs - prv + 1;
233 /* Sort of a hack to walk the stack in order, instead of reverse
234 * order - trying to explain to potential users why they need to
235 * reverse the order of this array would be confusing, at best. */
237 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
239 fprintf(stderr,"getdir() handler returned nothing!\n");
245 DEBUGf("getdir end: %i\n",rv);
250 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
253 DEBUGf("mknod begin\n");
257 XPUSHs(sv_2mortal(newSVpv(file,0)));
258 XPUSHs(sv_2mortal(newSViv(mode)));
259 XPUSHs(sv_2mortal(newSViv(dev)));
261 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
263 rv = (rv ? POPi : 0);
267 DEBUGf("mknod end: %i\n",rv);
272 int _PLfuse_mkdir (const char *file, mode_t mode) {
275 DEBUGf("mkdir begin\n");
279 XPUSHs(sv_2mortal(newSVpv(file,0)));
280 XPUSHs(sv_2mortal(newSViv(mode)));
282 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
284 rv = (rv ? POPi : 0);
288 DEBUGf("mkdir end: %i\n",rv);
294 int _PLfuse_unlink (const char *file) {
297 DEBUGf("unlink begin\n");
301 XPUSHs(sv_2mortal(newSVpv(file,0)));
303 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
305 rv = (rv ? POPi : 0);
309 DEBUGf("unlink end: %i\n",rv);
314 int _PLfuse_rmdir (const char *file) {
317 DEBUGf("rmdir begin\n");
321 XPUSHs(sv_2mortal(newSVpv(file,0)));
323 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
325 rv = (rv ? POPi : 0);
329 DEBUGf("rmdir end: %i\n",rv);
334 int _PLfuse_symlink (const char *file, const char *new) {
337 DEBUGf("symlink begin\n");
341 XPUSHs(sv_2mortal(newSVpv(file,0)));
342 XPUSHs(sv_2mortal(newSVpv(new,0)));
344 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
346 rv = (rv ? POPi : 0);
350 DEBUGf("symlink end: %i\n",rv);
355 int _PLfuse_rename (const char *file, const char *new) {
358 DEBUGf("rename begin\n");
362 XPUSHs(sv_2mortal(newSVpv(file,0)));
363 XPUSHs(sv_2mortal(newSVpv(new,0)));
365 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
367 rv = (rv ? POPi : 0);
371 DEBUGf("rename end: %i\n",rv);
376 int _PLfuse_link (const char *file, const char *new) {
379 DEBUGf("link begin\n");
383 XPUSHs(sv_2mortal(newSVpv(file,0)));
384 XPUSHs(sv_2mortal(newSVpv(new,0)));
386 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
388 rv = (rv ? POPi : 0);
392 DEBUGf("link end: %i\n",rv);
397 int _PLfuse_chmod (const char *file, mode_t mode) {
400 DEBUGf("chmod begin\n");
404 XPUSHs(sv_2mortal(newSVpv(file,0)));
405 XPUSHs(sv_2mortal(newSViv(mode)));
407 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
409 rv = (rv ? POPi : 0);
413 DEBUGf("chmod end: %i\n",rv);
418 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
421 DEBUGf("chown begin\n");
425 XPUSHs(sv_2mortal(newSVpv(file,0)));
426 XPUSHs(sv_2mortal(newSViv(uid)));
427 XPUSHs(sv_2mortal(newSViv(gid)));
429 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
431 rv = (rv ? POPi : 0);
435 DEBUGf("chown end: %i\n",rv);
440 int _PLfuse_truncate (const char *file, off_t off) {
442 #ifndef PERL_HAS_64BITINT
446 DEBUGf("truncate begin\n");
450 XPUSHs(sv_2mortal(newSVpv(file,0)));
451 #ifdef PERL_HAS_64BITINT
452 XPUSHs(sv_2mortal(newSViv(off)));
454 if (asprintf(&temp, "%llu", off) == -1)
455 croak("Memory allocation failure!");
456 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
460 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
462 rv = (rv ? POPi : 0);
466 DEBUGf("truncate end: %i\n",rv);
471 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
474 DEBUGf("utime begin\n");
478 XPUSHs(sv_2mortal(newSVpv(file,0)));
479 XPUSHs(sv_2mortal(newSViv(uti->actime)));
480 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
482 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
484 rv = (rv ? POPi : 0);
488 DEBUGf("utime end: %i\n",rv);
493 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
495 int flags = fi->flags;
498 DEBUGf("open begin\n");
502 XPUSHs(sv_2mortal(newSVpv(file,0)));
503 XPUSHs(sv_2mortal(newSViv(flags)));
504 /* Create a hashref containing the details from fi
505 * which we can look at or modify.
507 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
509 #if FUSE_VERSION >= 24
510 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
511 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
513 #if FUSE_VERSION >= 29
514 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
516 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
517 /* All hashref things done */
520 /* Open called with filename, flags */
521 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
525 FH_STOREHANDLE(fi,POPs);
533 /* Success, so copy the file handle which they returned */
534 #if FUSE_VERSION >= 24
536 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
537 fi->direct_io = SvIV(*svp);
538 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
539 fi->keep_cache = SvIV(*svp);
541 #if FUSE_VERSION >= 29
542 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
543 fi->nonseekable = SvIV(*svp);
549 DEBUGf("open end: %i\n",rv);
554 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
555 struct fuse_file_info *fi) {
557 #ifndef PERL_HAS_64BITINT
561 DEBUGf("read begin\n");
565 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
566 XPUSHs(sv_2mortal(newSViv(buflen)));
567 #ifdef PERL_HAS_64BITINT
568 XPUSHs(sv_2mortal(newSViv(off)));
570 if (asprintf(&temp, "%llu", off) == -1)
571 croak("Memory allocation failure!");
572 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
575 XPUSHs(FH_GETHANDLE(fi));
577 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
583 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
592 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
594 memcpy(buf,SvPV_nolen(mysv),rv);
600 DEBUGf("read end: %i\n",rv);
605 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
607 #ifndef PERL_HAS_64BITINT
611 DEBUGf("write begin\n");
615 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
616 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
617 #ifdef PERL_HAS_64BITINT
618 XPUSHs(sv_2mortal(newSViv(off)));
620 if (asprintf(&temp, "%llu", off) == -1)
621 croak("Memory allocation failure!");
622 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
625 XPUSHs(FH_GETHANDLE(fi));
627 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
629 rv = (rv ? POPi : 0);
633 DEBUGf("write end: %i\n",rv);
638 int _PLfuse_statfs (const char *file, struct statvfs *st) {
641 DEBUGf("statfs begin\n");
646 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
648 DEBUGf("statfs got %i params\n",rv);
649 if(rv == 6 || rv == 7) {
655 st->f_namemax = POPi;
656 /* zero and fill-in other */
660 st->f_bavail = st->f_bfree;
661 st->f_favail = st->f_ffree;
669 croak("inappropriate number of returned values from statfs");
678 DEBUGf("statfs end: %i\n",rv);
683 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
686 DEBUGf("flush begin\n");
690 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
691 XPUSHs(FH_GETHANDLE(fi));
693 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
695 rv = (rv ? POPi : 0);
699 DEBUGf("flush end: %i\n",rv);
704 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
706 int flags = fi->flags;
708 DEBUGf("release begin\n");
712 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
713 XPUSHs(sv_2mortal(newSViv(flags)));
714 XPUSHs(FH_GETHANDLE(fi));
716 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
718 rv = (rv ? POPi : 0);
719 FH_RELEASEHANDLE(fi);
723 DEBUGf("release end: %i\n",rv);
728 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
730 int flags = fi->flags;
732 DEBUGf("fsync begin\n");
736 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
737 XPUSHs(sv_2mortal(newSViv(flags)));
738 XPUSHs(FH_GETHANDLE(fi));
740 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
742 rv = (rv ? POPi : 0);
746 DEBUGf("fsync end: %i\n",rv);
751 #if __FreeBSD__ >= 10
752 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
754 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
758 DEBUGf("setxattr begin\n");
762 XPUSHs(sv_2mortal(newSVpv(file,0)));
763 XPUSHs(sv_2mortal(newSVpv(name,0)));
764 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
765 XPUSHs(sv_2mortal(newSViv(flags)));
767 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
769 rv = (rv ? POPi : 0);
773 DEBUGf("setxattr end: %i\n",rv);
778 #if __FreeBSD__ >= 10
779 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
781 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
785 DEBUGf("getxattr begin\n");
789 XPUSHs(sv_2mortal(newSVpv(file,0)));
790 XPUSHs(sv_2mortal(newSVpv(name,0)));
792 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
800 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
808 if ((rv > 0) && (buflen > 0))
813 memcpy(buf,SvPV_nolen(mysv),rv);
820 DEBUGf("getxattr end: %i\n",rv);
825 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
828 DEBUGf("listxattr begin\n");
832 XPUSHs(sv_2mortal(newSVpv(file,0)));
834 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
847 /* Always nul terminate */
848 if (list && (size > 0))
858 int s = SvCUR(mysv) + 1;
861 if (p && (size > 0) && (spc >= s))
863 memcpy(p,SvPV_nolen(mysv),s);
871 * If the Perl returned an error, return that.
872 * Otherwise check that the buffer was big enough.
877 if ((size > 0) && (size < total_len))
884 DEBUGf("listxattr end: %i\n",rv);
889 int _PLfuse_removexattr (const char *file, const char *name) {
892 DEBUGf("removexattr begin\n");
896 XPUSHs(sv_2mortal(newSVpv(file,0)));
897 XPUSHs(sv_2mortal(newSVpv(name,0)));
899 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
901 rv = (rv ? POPi : 0);
905 DEBUGf("removexattr end: %i\n",rv);
910 #if FUSE_VERSION >= 23
911 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
914 DEBUGf("opendir begin\n");
918 XPUSHs(sv_2mortal(newSVpv(file,0)));
919 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
921 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
925 FH_STOREHANDLE(fi, POPs);
933 DEBUGf("opendir end: %i\n",rv);
939 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
940 off_t off, struct fuse_file_info *fi) {
942 SV *sv, **svp, **swp;
946 #ifndef PERL_HAS_64BITINT
950 DEBUGf("readdir begin\n");
954 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
955 #ifdef PERL_HAS_64BITINT
956 XPUSHs(sv_2mortal(newSViv(off)));
958 if (asprintf(&temp, "%llu", off) == -1)
959 croak("Memory allocation failure!");
960 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
963 XPUSHs(FH_GETHANDLE(fi));
965 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
968 /* Should yield the bottom of the current stack... */
969 swp = &TOPs - prv + 1;
971 memset(&st, 0, sizeof(struct stat));
972 /* Sort of a hack to walk the stack in order, instead of reverse
973 * order - trying to explain to potential users why they need to
974 * reverse the order of this array would be confusing, at best. */
975 while (swp <= &TOPs) {
977 if (!SvROK(sv) && SvPOK(sv))
978 /* Just a bare SV (probably a string; hopefully a string) */
979 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
980 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
981 if (av_len(av) >= 2) {
982 /* The third element of the array should be the args that
983 * would otherwise go to getattr(); a lot of filesystems
984 * will, or at least can, return that info as part of the
985 * enumeration process... */
986 svp = av_fetch(av, 2, FALSE);
988 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
990 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
991 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
992 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
993 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
994 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
995 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
996 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
997 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
998 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
999 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
1000 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
1001 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1002 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1006 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1007 /* For now if the element isn't what we want, just
1008 * quietly ignore it... */
1010 if (av_len(av) >= 1) {
1011 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1012 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1013 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1016 memset(&st, 0, sizeof(struct stat));
1021 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1024 fprintf(stderr,"readdir() handler returned nothing!\n");
1030 DEBUGf("readdir end: %i\n",rv);
1035 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1038 DEBUGf("releasedir begin\n");
1042 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1043 XPUSHs(FH_GETHANDLE(fi));
1045 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1047 rv = (rv ? POPi : 0);
1048 FH_RELEASEHANDLE(fi);
1052 DEBUGf("releasedir end: %i\n",rv);
1057 int _PLfuse_fsyncdir(const char *file, int datasync,
1058 struct fuse_file_info *fi) {
1061 DEBUGf("fsyncdir begin\n");
1065 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1066 XPUSHs(sv_2mortal(newSViv(datasync)));
1067 XPUSHs(FH_GETHANDLE(fi));
1069 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1071 rv = (rv ? POPi : 0);
1075 DEBUGf("fsyncdir end: %i\n",rv);
1080 #if FUSE_VERSION >= 26
1081 void *_PLfuse_init(struct fuse_conn_info *fc)
1082 #else /* FUSE_VERSION < 26 */
1083 void *_PLfuse_init(void)
1084 #endif /* FUSE_VERSION >= 26 */
1089 DEBUGf("init begin\n");
1094 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1098 if (rv == &PL_sv_undef)
1101 rv = SvREFCNT_inc((SV *)rv);
1106 DEBUGf("init end: %p\n", rv);
1111 void _PLfuse_destroy(void *private_data) {
1113 DEBUGf("destroy begin\n");
1117 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1119 call_sv(MY_CXT.callback[30], G_VOID);
1122 SvREFCNT_dec((SV *)private_data);
1126 DEBUGf("init end\n");
1129 #endif /* FUSE_VERSION >= 23 */
1131 #if FUSE_VERSION >= 25
1132 int _PLfuse_access(const char *file, int mask) {
1135 DEBUGf("access begin\n");
1139 XPUSHs(sv_2mortal(newSVpv(file,0)));
1140 XPUSHs(sv_2mortal(newSViv(mask)));
1142 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1144 rv = (rv ? POPi : 0);
1148 DEBUGf("access end: %d\n", rv);
1153 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1157 DEBUGf("create begin\n");
1161 XPUSHs(sv_2mortal(newSVpv(file,0)));
1162 XPUSHs(sv_2mortal(newSViv(mode)));
1163 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1164 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1165 /* Create a hashref containing the details from fi
1166 * which we can look at or modify.
1169 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1170 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1171 #if FUSE_VERSION >= 29
1172 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1174 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1175 /* All hashref things done */
1178 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1182 FH_STOREHANDLE(fi,POPs);
1187 fprintf(stderr, "create() handler returned nothing!\n");
1191 /* Success, so copy the file handle which they returned */
1193 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1194 fi->direct_io = SvIV(*svp);
1195 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1196 fi->keep_cache = SvIV(*svp);
1197 #if FUSE_VERSION >= 29
1198 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1199 fi->nonseekable = SvIV(*svp);
1205 DEBUGf("create end: %d\n",rv);
1210 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1212 #ifndef PERL_HAS_64BITINT
1216 DEBUGf("ftruncate begin\n");
1220 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1221 #ifdef PERL_HAS_64BITINT
1222 XPUSHs(sv_2mortal(newSViv(off)));
1224 if (asprintf(&temp, "%llu", off) == -1)
1225 croak("Memory allocation failure!");
1226 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1229 XPUSHs(FH_GETHANDLE(fi));
1231 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1233 rv = (rv ? POPi : 0);
1237 DEBUGf("ftruncate end: %i\n",rv);
1242 int _PLfuse_fgetattr(const char *file, struct stat *result,
1243 struct fuse_file_info *fi) {
1246 DEBUGf("fgetattr begin: %s\n",file);
1250 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1251 XPUSHs(FH_GETHANDLE(fi));
1253 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1257 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1264 result->st_blocks = POPi;
1265 result->st_blksize = POPi;
1266 result->st_ctime = POPi;
1267 result->st_mtime = POPi;
1268 result->st_atime = POPi;
1269 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1270 result->st_rdev = POPi;
1271 result->st_gid = POPi;
1272 result->st_uid = POPi;
1273 result->st_nlink = POPi;
1274 result->st_mode = POPi;
1275 result->st_ino = POPi;
1276 result->st_dev = POPi;
1282 DEBUGf("fgetattr end: %i\n",rv);
1286 #endif /* FUSE_VERSION >= 25 */
1288 #if FUSE_VERSION >= 26
1289 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1290 struct flock *lockinfo) {
1294 #ifndef PERL_HAS_64BITINT
1298 DEBUGf("lock begin\n");
1302 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1303 XPUSHs(sv_2mortal(newSViv(cmd)));
1306 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1307 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1308 #ifdef PERL_HAS_64BITINT
1309 sv = newSViv(lockinfo->l_start);
1311 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1312 croak("Memory allocation failure!");
1313 sv = newSVpv(temp, 0);
1316 (void) hv_store(lihash, "l_start", 7, sv, 0);
1317 #ifdef PERL_HAS_64BITINT
1318 sv = newSViv(lockinfo->l_len);
1320 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1321 croak("Memory allocation failure!");
1322 sv = newSVpv(temp, 0);
1325 (void) hv_store(lihash, "l_len", 5, sv, 0);
1326 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1328 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1329 XPUSHs(FH_GETHANDLE(fi));
1332 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1334 rv = (rv ? POPi : 0);
1335 if (lockinfo && !rv) {
1336 /* Need to copy back any altered values from the hash into
1339 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1340 lockinfo->l_type = SvIV(*svp);
1341 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1342 lockinfo->l_whence = SvIV(*svp);
1343 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1344 lockinfo->l_start = SvNV(*svp);
1345 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1346 lockinfo->l_len = SvNV(*svp);
1347 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1348 lockinfo->l_pid = SvIV(*svp);
1353 DEBUGf("lock end: %i\n",rv);
1358 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1361 DEBUGf("utimens begin\n");
1365 XPUSHs(sv_2mortal(newSVpv(file,0)));
1366 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1367 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1369 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1371 rv = (rv ? POPi : 0);
1375 DEBUGf("utimens end: %i\n",rv);
1380 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1382 #ifndef PERL_HAS_64BITINT
1386 DEBUGf("bmap begin\n");
1390 XPUSHs(sv_2mortal(newSVpv(file,0)));
1391 XPUSHs(sv_2mortal(newSViv(blocksize)));
1392 #ifdef PERL_HAS_64BITINT
1393 XPUSHs(sv_2mortal(newSViv(*idx)));
1395 if (asprintf(&temp, "%llu", *idx) == -1)
1396 croak("Memory allocation failure!");
1397 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1401 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1403 if (rv > 0 && rv < 3) {
1409 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1415 DEBUGf("bmap end: %i\n",rv);
1419 #endif /* FUSE_VERSION >= 26 */
1422 #if FUSE_VERSION >= 28
1423 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1424 struct fuse_file_info *fi, unsigned int flags, void *data) {
1427 DEBUGf("ioctl begin\n");
1431 XPUSHs(sv_2mortal(newSVpv(file,0)));
1432 XPUSHs(sv_2mortal(newSViv(cmd)));
1433 XPUSHs(sv_2mortal(newSViv(flags)));
1434 if (_IOC_DIR(cmd) & _IOC_READ)
1435 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1437 XPUSHs(&PL_sv_undef);
1438 XPUSHs(FH_GETHANDLE(fi));
1440 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1442 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1446 char *rdata = SvPV(sv, len);
1447 if (len > _IOC_SIZE(cmd)) {
1448 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1452 memset(data, 0, _IOC_SIZE(cmd));
1453 memcpy(data, rdata, len);
1459 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1468 DEBUGf("ioctl end: %i\n",rv);
1473 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1474 struct fuse_pollhandle *ph, unsigned *reventsp) {
1477 #endif /* FUSE_VERSION >= 28 */
1480 struct fuse_operations _available_ops = {
1481 getattr: _PLfuse_getattr,
1482 readlink: _PLfuse_readlink,
1483 getdir: _PLfuse_getdir,
1484 mknod: _PLfuse_mknod,
1485 mkdir: _PLfuse_mkdir,
1486 unlink: _PLfuse_unlink,
1487 rmdir: _PLfuse_rmdir,
1488 symlink: _PLfuse_symlink,
1489 rename: _PLfuse_rename,
1491 chmod: _PLfuse_chmod,
1492 chown: _PLfuse_chown,
1493 truncate: _PLfuse_truncate,
1494 utime: _PLfuse_utime,
1497 write: _PLfuse_write,
1498 statfs: _PLfuse_statfs,
1499 flush: _PLfuse_flush,
1500 release: _PLfuse_release,
1501 fsync: _PLfuse_fsync,
1502 setxattr: _PLfuse_setxattr,
1503 getxattr: _PLfuse_getxattr,
1504 listxattr: _PLfuse_listxattr,
1505 removexattr: _PLfuse_removexattr,
1506 #if FUSE_VERSION >= 23
1507 opendir: _PLfuse_opendir,
1508 readdir: _PLfuse_readdir,
1509 releasedir: _PLfuse_releasedir,
1510 fsyncdir: _PLfuse_fsyncdir,
1512 destroy: _PLfuse_destroy,
1513 #endif /* FUSE_VERSION >= 23 */
1514 #if FUSE_VERSION >= 25
1515 access: _PLfuse_access,
1516 create: _PLfuse_create,
1517 ftruncate: _PLfuse_ftruncate,
1518 fgetattr: _PLfuse_fgetattr,
1519 #endif /* FUSE_VERSION >= 25 */
1520 #if FUSE_VERSION >= 26
1522 utimens: _PLfuse_utimens,
1524 #endif /* FUSE_VERSION >= 26 */
1526 #if FUSE_VERSION >= 28
1527 ioctl: _PLfuse_ioctl,
1529 #endif /* FUSE_VERSION >= 28 */
1533 MODULE = Fuse PACKAGE = Fuse
1552 tTHX parent = MY_CXT.self;
1553 MY_CXT.self = my_perl;
1554 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1555 /* CLONE entered without a pointer table, so we can't safely clone static data */
1557 for(i=0;i<N_CALLBACKS;i++) {
1558 MY_CXT.callback[i] = NULL;
1560 MY_CXT.handles = newHV();
1564 CLONE_PARAMS *clone_param;
1565 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1566 clone_param = Perl_clone_params_new(parent, aTHX);
1568 CLONE_PARAMS raw_param;
1569 raw_param.flags = 0;
1570 raw_param.proto_perl = parent;
1571 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1572 clone_param = &raw_param;
1574 for(i=0;i<N_CALLBACKS;i++) {
1575 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1577 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1578 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1579 Perl_clone_params_del(clone_param);
1587 struct fuse_context *fc;
1589 fc = fuse_get_context();
1592 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1593 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1594 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1595 if (fc->private_data)
1596 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1597 #if FUSE_VERSION >= 28
1598 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1599 #endif /* FUSE_VERSION >= 28 */
1600 RETVAL = newRV_noinc((SV*)hash);
1610 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1618 RETVAL = newSViv(XATTR_CREATE);
1625 RETVAL = newSViv(XATTR_REPLACE);
1634 struct fuse_operations fops;
1638 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1639 struct fuse_chan *fc;
1642 if(items != N_CALLBACKS + 5) {
1643 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1646 memset(&fops, 0, sizeof(struct fuse_operations));
1648 debug = SvIV(ST(0));
1649 MY_CXT.threaded = SvIV(ST(1));
1650 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1651 if(MY_CXT.threaded) {
1652 #ifdef FUSE_USE_ITHREADS
1653 master_interp = aTHX;
1654 MUTEX_INIT(&MY_CXT.mutex);
1655 SvSHARE((SV*)(MY_CXT.handles));
1657 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1658 "mode, but your perl was not built with a supported "
1659 "thread model. Threads are disabled.\n");
1660 MY_CXT.threaded = 0;
1663 mountpoint = SvPV_nolen(ST(2));
1664 mountopts = SvPV_nolen(ST(3));
1665 #if FUSE_VERSION >= 28
1666 fops.flag_nullpath_ok = SvIV(ST(4));
1667 #endif /* FUSE_VERSION >= 28 */
1668 for(i=0;i<N_CALLBACKS;i++) {
1670 /* allow symbolic references, or real code references. */
1671 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1672 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1673 /* Dirty hack, to keep anything from overwriting the
1674 * flag area with a pointer. There should never be
1675 * anything passed as 'junk', but this prevents
1676 * someone from doing it and screwing things up... */
1680 MY_CXT.callback[i] = var;
1681 } else if(SvOK(var)) {
1682 croak("invalid callback (%i) passed to perl_fuse_main "
1683 "(%s is not a string, code ref, or undef).\n",
1684 i+5,SvPVbyte_nolen(var));
1686 MY_CXT.callback[i] = NULL;
1690 * XXX: What comes here is just a ridiculous use of the option parsing API
1691 * to hack on compatibility with other parts of the new API. First and
1692 * foremost, real C argc/argv would be good to get at...
1694 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1695 fuse_opt_free_args(&args);
1696 croak("out of memory\n");
1698 if (mountopts && strcmp("", mountopts) &&
1699 (fuse_opt_add_arg(&args, "-o") == -1 ||
1700 fuse_opt_add_arg(&args, mountopts) == -1)) {
1701 fuse_opt_free_args(&args);
1702 croak("out of memory\n");
1704 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1705 fuse_opt_free_args(&args);
1706 croak("out of memory\n");
1708 fc = fuse_mount(mountpoint,&args);
1710 croak("could not mount fuse filesystem!\n");
1712 if(MY_CXT.threaded) {
1713 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1716 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1717 fuse_unmount(mountpoint,fc);
1718 fuse_opt_free_args(&args);