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 /* Determine if threads support should be included */
18 # define FUSE_USE_ITHREADS
19 # if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
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 #elif 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 /* This seems to be screwing things up... */
133 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
134 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
136 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
143 int _PLfuse_getattr(const char *file, struct stat *result) {
146 DEBUGf("getattr begin: %s\n",file);
150 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
152 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
156 fprintf(stderr,"inappropriate number of returned values from getattr\n");
163 result->st_blocks = POPi;
164 result->st_blksize = POPi;
165 result->st_ctime = POPi;
166 result->st_mtime = POPi;
167 result->st_atime = POPi;
168 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
169 result->st_rdev = POPi;
170 result->st_gid = POPi;
171 result->st_uid = POPi;
172 result->st_nlink = POPi;
173 result->st_mode = POPi;
174 result->st_ino = POPi;
175 result->st_dev = POPi;
181 DEBUGf("getattr end: %i\n",rv);
186 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
191 DEBUGf("readlink begin\n");
195 XPUSHs(sv_2mortal(newSVpv(file,0)));
197 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
203 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
206 strncpy(buf,SvPV_nolen(mysv),buflen);
214 DEBUGf("readlink end: %i\n",rv);
219 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
223 DEBUGf("getdir begin\n");
227 XPUSHs(sv_2mortal(newSVpv(file,0)));
229 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
232 /* Should yield the bottom of the current stack... */
233 swp = &TOPs - prv + 1;
235 /* Sort of a hack to walk the stack in order, instead of reverse
236 * order - trying to explain to potential users why they need to
237 * reverse the order of this array would be confusing, at best. */
239 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
241 fprintf(stderr,"getdir() handler returned nothing!\n");
247 DEBUGf("getdir end: %i\n",rv);
252 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
255 DEBUGf("mknod begin\n");
259 XPUSHs(sv_2mortal(newSVpv(file,0)));
260 XPUSHs(sv_2mortal(newSViv(mode)));
261 XPUSHs(sv_2mortal(newSViv(dev)));
263 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
265 rv = (rv ? POPi : 0);
269 DEBUGf("mknod end: %i\n",rv);
274 int _PLfuse_mkdir (const char *file, mode_t mode) {
277 DEBUGf("mkdir begin\n");
281 XPUSHs(sv_2mortal(newSVpv(file,0)));
282 XPUSHs(sv_2mortal(newSViv(mode)));
284 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
286 rv = (rv ? POPi : 0);
290 DEBUGf("mkdir end: %i\n",rv);
296 int _PLfuse_unlink (const char *file) {
299 DEBUGf("unlink begin\n");
303 XPUSHs(sv_2mortal(newSVpv(file,0)));
305 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
307 rv = (rv ? POPi : 0);
311 DEBUGf("unlink end: %i\n",rv);
316 int _PLfuse_rmdir (const char *file) {
319 DEBUGf("rmdir begin\n");
323 XPUSHs(sv_2mortal(newSVpv(file,0)));
325 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
327 rv = (rv ? POPi : 0);
331 DEBUGf("rmdir end: %i\n",rv);
336 int _PLfuse_symlink (const char *file, const char *new) {
339 DEBUGf("symlink begin\n");
343 XPUSHs(sv_2mortal(newSVpv(file,0)));
344 XPUSHs(sv_2mortal(newSVpv(new,0)));
346 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
348 rv = (rv ? POPi : 0);
352 DEBUGf("symlink end: %i\n",rv);
357 int _PLfuse_rename (const char *file, const char *new) {
360 DEBUGf("rename begin\n");
364 XPUSHs(sv_2mortal(newSVpv(file,0)));
365 XPUSHs(sv_2mortal(newSVpv(new,0)));
367 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
369 rv = (rv ? POPi : 0);
373 DEBUGf("rename end: %i\n",rv);
378 int _PLfuse_link (const char *file, const char *new) {
381 DEBUGf("link begin\n");
385 XPUSHs(sv_2mortal(newSVpv(file,0)));
386 XPUSHs(sv_2mortal(newSVpv(new,0)));
388 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
390 rv = (rv ? POPi : 0);
394 DEBUGf("link end: %i\n",rv);
399 int _PLfuse_chmod (const char *file, mode_t mode) {
402 DEBUGf("chmod begin\n");
406 XPUSHs(sv_2mortal(newSVpv(file,0)));
407 XPUSHs(sv_2mortal(newSViv(mode)));
409 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
411 rv = (rv ? POPi : 0);
415 DEBUGf("chmod end: %i\n",rv);
420 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
423 DEBUGf("chown begin\n");
427 XPUSHs(sv_2mortal(newSVpv(file,0)));
428 XPUSHs(sv_2mortal(newSViv(uid)));
429 XPUSHs(sv_2mortal(newSViv(gid)));
431 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
433 rv = (rv ? POPi : 0);
437 DEBUGf("chown end: %i\n",rv);
442 int _PLfuse_truncate (const char *file, off_t off) {
444 #ifndef PERL_HAS_64BITINT
448 DEBUGf("truncate begin\n");
452 XPUSHs(sv_2mortal(newSVpv(file,0)));
453 #ifdef PERL_HAS_64BITINT
454 XPUSHs(sv_2mortal(newSViv(off)));
456 if (asprintf(&temp, "%llu", off) == -1)
457 croak("Memory allocation failure!");
458 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
462 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
464 rv = (rv ? POPi : 0);
468 DEBUGf("truncate end: %i\n",rv);
473 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
476 DEBUGf("utime begin\n");
480 XPUSHs(sv_2mortal(newSVpv(file,0)));
481 XPUSHs(sv_2mortal(newSViv(uti->actime)));
482 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
484 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
486 rv = (rv ? POPi : 0);
490 DEBUGf("utime end: %i\n",rv);
495 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
497 int flags = fi->flags;
500 DEBUGf("open begin\n");
504 XPUSHs(sv_2mortal(newSVpv(file,0)));
505 XPUSHs(sv_2mortal(newSViv(flags)));
506 /* Create a hashref containing the details from fi
507 * which we can look at or modify.
509 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
511 #if FUSE_VERSION >= 24
512 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
513 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
515 #if FUSE_VERSION >= 28
516 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
518 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
519 /* All hashref things done */
522 /* Open called with filename, flags */
523 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
527 FH_STOREHANDLE(fi,POPs);
535 /* Success, so copy the file handle which they returned */
536 #if FUSE_VERSION >= 24
538 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
539 fi->direct_io = SvIV(*svp);
540 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
541 fi->keep_cache = SvIV(*svp);
543 #if FUSE_VERSION >= 28
544 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
545 fi->nonseekable = SvIV(*svp);
551 DEBUGf("open end: %i\n",rv);
556 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
557 struct fuse_file_info *fi) {
559 #ifndef PERL_HAS_64BITINT
563 DEBUGf("read begin\n");
567 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
568 XPUSHs(sv_2mortal(newSViv(buflen)));
569 #ifdef PERL_HAS_64BITINT
570 XPUSHs(sv_2mortal(newSViv(off)));
572 if (asprintf(&temp, "%llu", off) == -1)
573 croak("Memory allocation failure!");
574 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
577 XPUSHs(FH_GETHANDLE(fi));
579 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
585 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
594 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
596 memcpy(buf,SvPV_nolen(mysv),rv);
602 DEBUGf("read end: %i\n",rv);
607 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
609 #ifndef PERL_HAS_64BITINT
613 DEBUGf("write begin\n");
617 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
618 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
619 #ifdef PERL_HAS_64BITINT
620 XPUSHs(sv_2mortal(newSViv(off)));
622 if (asprintf(&temp, "%llu", off) == -1)
623 croak("Memory allocation failure!");
624 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
627 XPUSHs(FH_GETHANDLE(fi));
629 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
631 rv = (rv ? POPi : 0);
635 DEBUGf("write end: %i\n",rv);
640 int _PLfuse_statfs (const char *file, struct statvfs *st) {
643 DEBUGf("statfs begin\n");
648 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
650 DEBUGf("statfs got %i params\n",rv);
651 if(rv == 6 || rv == 7) {
657 st->f_namemax = POPi;
658 /* zero and fill-in other */
662 st->f_bavail = st->f_bfree;
663 st->f_favail = st->f_ffree;
671 croak("inappropriate number of returned values from statfs");
680 DEBUGf("statfs end: %i\n",rv);
685 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
688 DEBUGf("flush begin\n");
692 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
693 XPUSHs(FH_GETHANDLE(fi));
695 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
697 rv = (rv ? POPi : 0);
701 DEBUGf("flush end: %i\n",rv);
706 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
708 int flags = fi->flags;
710 DEBUGf("release begin\n");
714 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
715 XPUSHs(sv_2mortal(newSViv(flags)));
716 XPUSHs(FH_GETHANDLE(fi));
718 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
720 rv = (rv ? POPi : 0);
721 FH_RELEASEHANDLE(fi);
725 DEBUGf("release end: %i\n",rv);
730 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
732 int flags = fi->flags;
734 DEBUGf("fsync begin\n");
738 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
739 XPUSHs(sv_2mortal(newSViv(flags)));
740 XPUSHs(FH_GETHANDLE(fi));
742 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
744 rv = (rv ? POPi : 0);
748 DEBUGf("fsync end: %i\n",rv);
753 #if __FreeBSD__ >= 10
754 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
756 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
760 DEBUGf("setxattr begin\n");
764 XPUSHs(sv_2mortal(newSVpv(file,0)));
765 XPUSHs(sv_2mortal(newSVpv(name,0)));
766 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
767 XPUSHs(sv_2mortal(newSViv(flags)));
769 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
771 rv = (rv ? POPi : 0);
775 DEBUGf("setxattr end: %i\n",rv);
780 #if __FreeBSD__ >= 10
781 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
783 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
787 DEBUGf("getxattr begin\n");
791 XPUSHs(sv_2mortal(newSVpv(file,0)));
792 XPUSHs(sv_2mortal(newSVpv(name,0)));
794 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
802 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
810 if ((rv > 0) && (buflen > 0))
815 memcpy(buf,SvPV_nolen(mysv),rv);
822 DEBUGf("getxattr end: %i\n",rv);
827 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
830 DEBUGf("listxattr begin\n");
834 XPUSHs(sv_2mortal(newSVpv(file,0)));
836 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
849 /* Always nul terminate */
850 if (list && (size > 0))
860 int s = SvCUR(mysv) + 1;
863 if (p && (size > 0) && (spc >= s))
865 memcpy(p,SvPV_nolen(mysv),s);
873 * If the Perl returned an error, return that.
874 * Otherwise check that the buffer was big enough.
879 if ((size > 0) && (size < total_len))
886 DEBUGf("listxattr end: %i\n",rv);
891 int _PLfuse_removexattr (const char *file, const char *name) {
894 DEBUGf("removexattr begin\n");
898 XPUSHs(sv_2mortal(newSVpv(file,0)));
899 XPUSHs(sv_2mortal(newSVpv(name,0)));
901 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
903 rv = (rv ? POPi : 0);
907 DEBUGf("removexattr end: %i\n",rv);
912 #if FUSE_VERSION >= 23
913 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
916 DEBUGf("opendir begin\n");
920 XPUSHs(sv_2mortal(newSVpv(file,0)));
921 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
923 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
927 FH_STOREHANDLE(fi, POPs);
935 DEBUGf("opendir end: %i\n",rv);
941 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
942 off_t off, struct fuse_file_info *fi) {
944 SV *sv, **svp, **swp;
948 #ifndef PERL_HAS_64BITINT
952 DEBUGf("readdir begin\n");
956 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
957 #ifdef PERL_HAS_64BITINT
958 XPUSHs(sv_2mortal(newSViv(off)));
960 if (asprintf(&temp, "%llu", off) == -1)
961 croak("Memory allocation failure!");
962 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
965 XPUSHs(FH_GETHANDLE(fi));
967 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
970 /* Should yield the bottom of the current stack... */
971 swp = &TOPs - prv + 1;
973 memset(&st, 0, sizeof(struct stat));
974 /* Sort of a hack to walk the stack in order, instead of reverse
975 * order - trying to explain to potential users why they need to
976 * reverse the order of this array would be confusing, at best. */
977 while (swp <= &TOPs) {
979 if (!SvROK(sv) && SvPOK(sv))
980 /* Just a bare SV (probably a string; hopefully a string) */
981 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
982 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
983 if (av_len(av) >= 2) {
984 /* The third element of the array should be the args that
985 * would otherwise go to getattr(); a lot of filesystems
986 * will, or at least can, return that info as part of the
987 * enumeration process... */
988 svp = av_fetch(av, 2, FALSE);
990 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
992 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
993 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
994 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
995 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
996 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
997 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
998 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
999 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1000 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
1001 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
1002 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
1003 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1004 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1008 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1009 /* For now if the element isn't what we want, just
1010 * quietly ignore it... */
1012 if (av_len(av) >= 1) {
1013 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1014 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1015 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1018 memset(&st, 0, sizeof(struct stat));
1023 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1026 fprintf(stderr,"readdir() handler returned nothing!\n");
1032 DEBUGf("readdir end: %i\n",rv);
1037 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1040 DEBUGf("releasedir begin\n");
1044 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1045 XPUSHs(FH_GETHANDLE(fi));
1047 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1049 rv = (rv ? POPi : 0);
1050 FH_RELEASEHANDLE(fi);
1054 DEBUGf("releasedir end: %i\n",rv);
1059 int _PLfuse_fsyncdir(const char *file, int datasync,
1060 struct fuse_file_info *fi) {
1063 DEBUGf("fsyncdir begin\n");
1067 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1068 XPUSHs(sv_2mortal(newSViv(datasync)));
1069 XPUSHs(FH_GETHANDLE(fi));
1071 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1073 rv = (rv ? POPi : 0);
1077 DEBUGf("fsyncdir end: %i\n",rv);
1082 #if FUSE_VERSION >= 26
1083 void *_PLfuse_init(struct fuse_conn_info *fc)
1084 #else /* FUSE_VERSION < 26 */
1085 void *_PLfuse_init(void)
1086 #endif /* FUSE_VERSION >= 26 */
1091 DEBUGf("init begin\n");
1096 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1100 if (rv == &PL_sv_undef)
1103 rv = SvREFCNT_inc((SV *)rv);
1108 DEBUGf("init end: %p\n", rv);
1113 void _PLfuse_destroy(void *private_data) {
1115 DEBUGf("destroy begin\n");
1119 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1121 call_sv(MY_CXT.callback[30], G_VOID);
1124 SvREFCNT_dec((SV *)private_data);
1128 DEBUGf("init end\n");
1131 #endif /* FUSE_VERSION >= 23 */
1133 #if FUSE_VERSION >= 25
1134 int _PLfuse_access(const char *file, int mask) {
1137 DEBUGf("access begin\n");
1141 XPUSHs(sv_2mortal(newSVpv(file,0)));
1142 XPUSHs(sv_2mortal(newSViv(mask)));
1144 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1146 rv = (rv ? POPi : 0);
1150 DEBUGf("access end: %d\n", rv);
1155 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1159 DEBUGf("create begin\n");
1163 XPUSHs(sv_2mortal(newSVpv(file,0)));
1164 XPUSHs(sv_2mortal(newSViv(mode)));
1165 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1166 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1167 /* Create a hashref containing the details from fi
1168 * which we can look at or modify.
1171 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1172 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1173 #if FUSE_VERSION >= 28
1174 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1176 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1177 /* All hashref things done */
1180 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1184 FH_STOREHANDLE(fi,POPs);
1189 fprintf(stderr, "create() handler returned nothing!\n");
1193 /* Success, so copy the file handle which they returned */
1195 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1196 fi->direct_io = SvIV(*svp);
1197 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1198 fi->keep_cache = SvIV(*svp);
1199 #if FUSE_VERSION >= 28
1200 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1201 fi->nonseekable = SvIV(*svp);
1207 DEBUGf("create end: %d\n",rv);
1212 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1214 #ifndef PERL_HAS_64BITINT
1218 DEBUGf("ftruncate begin\n");
1222 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1223 #ifdef PERL_HAS_64BITINT
1224 XPUSHs(sv_2mortal(newSViv(off)));
1226 if (asprintf(&temp, "%llu", off) == -1)
1227 croak("Memory allocation failure!");
1228 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1231 XPUSHs(FH_GETHANDLE(fi));
1233 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1235 rv = (rv ? POPi : 0);
1239 DEBUGf("ftruncate end: %i\n",rv);
1244 int _PLfuse_fgetattr(const char *file, struct stat *result,
1245 struct fuse_file_info *fi) {
1248 DEBUGf("fgetattr begin: %s\n",file);
1252 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1253 XPUSHs(FH_GETHANDLE(fi));
1255 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1259 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1266 result->st_blocks = POPi;
1267 result->st_blksize = POPi;
1268 result->st_ctime = POPi;
1269 result->st_mtime = POPi;
1270 result->st_atime = POPi;
1271 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1272 result->st_rdev = POPi;
1273 result->st_gid = POPi;
1274 result->st_uid = POPi;
1275 result->st_nlink = POPi;
1276 result->st_mode = POPi;
1277 result->st_ino = POPi;
1278 result->st_dev = POPi;
1284 DEBUGf("fgetattr end: %i\n",rv);
1288 #endif /* FUSE_VERSION >= 25 */
1290 #if FUSE_VERSION >= 26
1291 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1292 struct flock *lockinfo) {
1296 #ifndef PERL_HAS_64BITINT
1300 DEBUGf("lock begin\n");
1304 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1305 XPUSHs(sv_2mortal(newSViv(cmd)));
1308 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1309 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1310 #ifdef PERL_HAS_64BITINT
1311 sv = newSViv(lockinfo->l_start);
1313 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1314 croak("Memory allocation failure!");
1315 sv = newSVpv(temp, 0);
1318 (void) hv_store(lihash, "l_start", 7, sv, 0);
1319 #ifdef PERL_HAS_64BITINT
1320 sv = newSViv(lockinfo->l_len);
1322 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1323 croak("Memory allocation failure!");
1324 sv = newSVpv(temp, 0);
1327 (void) hv_store(lihash, "l_len", 5, sv, 0);
1328 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1330 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1331 XPUSHs(FH_GETHANDLE(fi));
1334 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1336 rv = (rv ? POPi : 0);
1337 if (lockinfo && !rv) {
1338 /* Need to copy back any altered values from the hash into
1341 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1342 lockinfo->l_type = SvIV(*svp);
1343 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1344 lockinfo->l_whence = SvIV(*svp);
1345 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1346 lockinfo->l_start = SvNV(*svp);
1347 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1348 lockinfo->l_len = SvNV(*svp);
1349 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1350 lockinfo->l_pid = SvIV(*svp);
1355 DEBUGf("lock end: %i\n",rv);
1360 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1363 DEBUGf("utimens begin\n");
1367 XPUSHs(sv_2mortal(newSVpv(file,0)));
1368 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1369 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1371 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1373 rv = (rv ? POPi : 0);
1377 DEBUGf("utimens end: %i\n",rv);
1382 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1384 #ifndef PERL_HAS_64BITINT
1388 DEBUGf("bmap begin\n");
1392 XPUSHs(sv_2mortal(newSVpv(file,0)));
1393 XPUSHs(sv_2mortal(newSViv(blocksize)));
1394 #ifdef PERL_HAS_64BITINT
1395 XPUSHs(sv_2mortal(newSViv(*idx)));
1397 if (asprintf(&temp, "%llu", *idx) == -1)
1398 croak("Memory allocation failure!");
1399 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1403 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1405 if (rv > 0 && rv < 3) {
1411 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1417 DEBUGf("bmap end: %i\n",rv);
1421 #endif /* FUSE_VERSION >= 26 */
1423 #if FUSE_VERSION >= 28
1424 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1425 struct fuse_file_info *fi, unsigned int flags, void *data) {
1429 DEBUGf("ioctl begin\n");
1433 XPUSHs(sv_2mortal(newSVpv(file,0)));
1434 /* I don't know why cmd is a signed int in the first place;
1435 * casting as unsigned so stupid tricks don't have to be done on
1437 XPUSHs(sv_2mortal(newSViv((unsigned int)cmd)));
1438 XPUSHs(sv_2mortal(newSViv(flags)));
1439 if (_IOC_DIR(cmd) & _IOC_WRITE)
1440 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1442 XPUSHs(&PL_sv_undef);
1443 XPUSHs(FH_GETHANDLE(fi));
1445 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1447 if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
1455 if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
1458 char *rdata = SvPV(sv, len);
1460 if (len > _IOC_SIZE(cmd)) {
1461 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1465 memset(data, 0, _IOC_SIZE(cmd));
1466 memcpy(data, rdata, len);
1470 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1477 DEBUGf("ioctl end: %i\n",rv);
1482 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1483 struct fuse_pollhandle *ph, unsigned *reventsp) {
1487 DEBUGf("poll begin\n");
1491 XPUSHs(sv_2mortal(newSVpv(file,0)));
1493 /* Still gotta figure out how to do this right... */
1494 sv = newSViv(PTR2IV(ph));
1500 XPUSHs(&PL_sv_undef);
1501 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1502 XPUSHs(FH_GETHANDLE(fi));
1504 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1510 rv = (rv ? POPi : 0);
1514 DEBUGf("poll end: %i\n", rv);
1518 #endif /* FUSE_VERSION >= 28 */
1520 struct fuse_operations _available_ops = {
1521 getattr: _PLfuse_getattr,
1522 readlink: _PLfuse_readlink,
1523 getdir: _PLfuse_getdir,
1524 mknod: _PLfuse_mknod,
1525 mkdir: _PLfuse_mkdir,
1526 unlink: _PLfuse_unlink,
1527 rmdir: _PLfuse_rmdir,
1528 symlink: _PLfuse_symlink,
1529 rename: _PLfuse_rename,
1531 chmod: _PLfuse_chmod,
1532 chown: _PLfuse_chown,
1533 truncate: _PLfuse_truncate,
1534 utime: _PLfuse_utime,
1537 write: _PLfuse_write,
1538 statfs: _PLfuse_statfs,
1539 flush: _PLfuse_flush,
1540 release: _PLfuse_release,
1541 fsync: _PLfuse_fsync,
1542 setxattr: _PLfuse_setxattr,
1543 getxattr: _PLfuse_getxattr,
1544 listxattr: _PLfuse_listxattr,
1545 removexattr: _PLfuse_removexattr,
1546 #if FUSE_VERSION >= 23
1547 opendir: _PLfuse_opendir,
1548 readdir: _PLfuse_readdir,
1549 releasedir: _PLfuse_releasedir,
1550 fsyncdir: _PLfuse_fsyncdir,
1552 destroy: _PLfuse_destroy,
1553 #endif /* FUSE_VERSION >= 23 */
1554 #if FUSE_VERSION >= 25
1555 access: _PLfuse_access,
1556 create: _PLfuse_create,
1557 ftruncate: _PLfuse_ftruncate,
1558 fgetattr: _PLfuse_fgetattr,
1559 #endif /* FUSE_VERSION >= 25 */
1560 #if FUSE_VERSION >= 26
1562 utimens: _PLfuse_utimens,
1564 #endif /* FUSE_VERSION >= 26 */
1565 #if FUSE_VERSION >= 28
1566 ioctl: _PLfuse_ioctl,
1568 #endif /* FUSE_VERSION >= 28 */
1571 MODULE = Fuse PACKAGE = Fuse
1590 tTHX parent = MY_CXT.self;
1591 MY_CXT.self = my_perl;
1592 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1593 /* CLONE entered without a pointer table, so we can't safely clone static data */
1595 for(i=0;i<N_CALLBACKS;i++) {
1596 MY_CXT.callback[i] = NULL;
1598 MY_CXT.handles = newHV();
1602 CLONE_PARAMS *clone_param;
1603 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1604 clone_param = Perl_clone_params_new(parent, aTHX);
1606 CLONE_PARAMS raw_param;
1607 raw_param.flags = 0;
1608 raw_param.proto_perl = parent;
1609 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1610 clone_param = &raw_param;
1612 for(i=0;i<N_CALLBACKS;i++) {
1613 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1615 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1616 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1617 Perl_clone_params_del(clone_param);
1625 struct fuse_context *fc;
1627 fc = fuse_get_context();
1630 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1631 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1632 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1633 if (fc->private_data)
1634 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1635 #if FUSE_VERSION >= 28
1636 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1637 #endif /* FUSE_VERSION >= 28 */
1638 RETVAL = newRV_noinc((SV*)hash);
1648 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1655 RETVAL = newSViv(XATTR_CREATE);
1662 RETVAL = newSViv(XATTR_REPLACE);
1669 struct fuse_operations fops;
1673 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1674 struct fuse_chan *fc;
1677 if(items != N_CALLBACKS + 5) {
1678 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1681 memset(&fops, 0, sizeof(struct fuse_operations));
1683 debug = SvIV(ST(0));
1684 MY_CXT.threaded = SvIV(ST(1));
1685 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1686 if(MY_CXT.threaded) {
1687 #ifdef FUSE_USE_ITHREADS
1688 master_interp = aTHX;
1689 MUTEX_INIT(&MY_CXT.mutex);
1690 SvSHARE((SV*)(MY_CXT.handles));
1692 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1693 "mode, but your perl was not built with a supported "
1694 "thread model. Threads are disabled.\n");
1695 MY_CXT.threaded = 0;
1698 mountpoint = SvPV_nolen(ST(2));
1699 mountopts = SvPV_nolen(ST(3));
1700 #if FUSE_VERSION >= 28
1701 fops.flag_nullpath_ok = SvIV(ST(4));
1702 #endif /* FUSE_VERSION >= 28 */
1703 for(i=0;i<N_CALLBACKS;i++) {
1705 /* allow symbolic references, or real code references. */
1706 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1707 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1708 /* Dirty hack, to keep anything from overwriting the
1709 * flag area with a pointer. There should never be
1710 * anything passed as 'junk', but this prevents
1711 * someone from doing it and screwing things up... */
1715 MY_CXT.callback[i] = var;
1716 } else if(SvOK(var)) {
1717 croak("invalid callback (%i) passed to perl_fuse_main "
1718 "(%s is not a string, code ref, or undef).\n",
1719 i+5,SvPVbyte_nolen(var));
1721 MY_CXT.callback[i] = NULL;
1725 * XXX: What comes here is just a ridiculous use of the option parsing API
1726 * to hack on compatibility with other parts of the new API. First and
1727 * foremost, real C argc/argv would be good to get at...
1729 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1730 fuse_opt_free_args(&args);
1731 croak("out of memory\n");
1733 if (mountopts && strcmp("", mountopts) &&
1734 (fuse_opt_add_arg(&args, "-o") == -1 ||
1735 fuse_opt_add_arg(&args, mountopts) == -1)) {
1736 fuse_opt_free_args(&args);
1737 croak("out of memory\n");
1739 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1740 fuse_opt_free_args(&args);
1741 croak("out of memory\n");
1743 fc = fuse_mount(mountpoint,&args);
1745 croak("could not mount fuse filesystem!\n");
1747 if(MY_CXT.threaded) {
1748 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1751 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1752 fuse_unmount(mountpoint,fc);
1753 fuse_opt_free_args(&args);
1755 #if FUSE_VERSION >= 28
1758 pollhandle_destroy(...)
1760 struct fuse_pollhandle *ph;
1763 fprintf(stderr, "No pollhandle passed?\n");
1767 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1768 fuse_pollhandle_destroy(ph);
1773 struct fuse_pollhandle *ph;
1776 fprintf(stderr, "No pollhandle passed?\n");
1780 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1781 RETVAL = fuse_notify_poll(ph);