1 #define PERL_NO_GET_CONTEXT
8 /* Determine if threads support should be included */
11 # define FUSE_USE_ITHREADS
13 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
19 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
20 /* #if FUSE_VERSION >= 28
21 # define N_CALLBACKS 41 */
22 #if FUSE_VERSION >= 26
23 # define N_CALLBACKS 38
24 #elif FUSE_VERSION >= 25
25 # define N_CALLBACKS 35
26 #elif FUSE_VERSION >= 23
27 # define N_CALLBACKS 31
29 # define N_CALLBACKS 25
33 SV *callback[N_CALLBACKS];
41 #ifdef FUSE_USE_ITHREADS
42 tTHX master_interp = NULL;
44 #define CLONE_INTERP(parent) S_clone_interp(parent)
45 tTHX S_clone_interp(tTHX parent) {
46 dMY_CXT_INTERP(parent);
48 MUTEX_LOCK(&MY_CXT.mutex);
49 PERL_SET_CONTEXT(parent);
51 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
52 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
54 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
55 ptr_table_free(PL_ptr_table);
58 MUTEX_UNLOCK(&MY_CXT.mutex);
64 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
65 # define FUSE_CONTEXT_POST }
67 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
68 # define FUSE_CONTEXT_POST
73 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
78 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
79 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
80 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
81 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
83 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
88 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
96 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
98 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
103 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
105 #ifdef FUSE_USE_ITHREADS
106 if(MY_CXT.threaded) {
110 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
111 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
112 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
119 int _PLfuse_getattr(const char *file, struct stat *result) {
122 DEBUGf("getattr begin: %s\n",file);
126 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
128 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
132 fprintf(stderr,"inappropriate number of returned values from getattr\n");
139 result->st_blocks = POPi;
140 result->st_blksize = POPi;
141 result->st_ctime = POPi;
142 result->st_mtime = POPi;
143 result->st_atime = POPi;
144 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
145 result->st_rdev = POPi;
146 result->st_gid = POPi;
147 result->st_uid = POPi;
148 result->st_nlink = POPi;
149 result->st_mode = POPi;
150 result->st_ino = POPi;
151 result->st_dev = POPi;
157 DEBUGf("getattr end: %i\n",rv);
162 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
167 DEBUGf("readlink begin\n");
171 XPUSHs(sv_2mortal(newSVpv(file,0)));
173 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
179 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
182 strncpy(buf,SvPV_nolen(mysv),buflen);
190 DEBUGf("readlink end: %i\n",rv);
195 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
199 DEBUGf("getdir begin\n");
203 XPUSHs(sv_2mortal(newSVpv(file,0)));
205 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
208 /* Should yield the bottom of the current stack... */
209 swp = &TOPs - prv + 1;
211 /* Sort of a hack to walk the stack in order, instead of reverse
212 * order - trying to explain to potential users why they need to
213 * reverse the order of this array would be confusing, at best. */
215 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
217 fprintf(stderr,"getdir() handler returned nothing!\n");
223 DEBUGf("getdir end: %i\n",rv);
228 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
231 DEBUGf("mknod begin\n");
235 XPUSHs(sv_2mortal(newSVpv(file,0)));
236 XPUSHs(sv_2mortal(newSViv(mode)));
237 XPUSHs(sv_2mortal(newSViv(dev)));
239 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
241 rv = (rv ? POPi : 0);
245 DEBUGf("mknod end: %i\n",rv);
250 int _PLfuse_mkdir (const char *file, mode_t mode) {
253 DEBUGf("mkdir begin\n");
257 XPUSHs(sv_2mortal(newSVpv(file,0)));
258 XPUSHs(sv_2mortal(newSViv(mode)));
260 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
262 rv = (rv ? POPi : 0);
266 DEBUGf("mkdir end: %i\n",rv);
272 int _PLfuse_unlink (const char *file) {
275 DEBUGf("unlink begin\n");
279 XPUSHs(sv_2mortal(newSVpv(file,0)));
281 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
283 rv = (rv ? POPi : 0);
287 DEBUGf("unlink end: %i\n",rv);
292 int _PLfuse_rmdir (const char *file) {
295 DEBUGf("rmdir begin\n");
299 XPUSHs(sv_2mortal(newSVpv(file,0)));
301 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
303 rv = (rv ? POPi : 0);
307 DEBUGf("rmdir end: %i\n",rv);
312 int _PLfuse_symlink (const char *file, const char *new) {
315 DEBUGf("symlink begin\n");
319 XPUSHs(sv_2mortal(newSVpv(file,0)));
320 XPUSHs(sv_2mortal(newSVpv(new,0)));
322 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
324 rv = (rv ? POPi : 0);
328 DEBUGf("symlink end: %i\n",rv);
333 int _PLfuse_rename (const char *file, const char *new) {
336 DEBUGf("rename begin\n");
340 XPUSHs(sv_2mortal(newSVpv(file,0)));
341 XPUSHs(sv_2mortal(newSVpv(new,0)));
343 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
345 rv = (rv ? POPi : 0);
349 DEBUGf("rename end: %i\n",rv);
354 int _PLfuse_link (const char *file, const char *new) {
357 DEBUGf("link begin\n");
361 XPUSHs(sv_2mortal(newSVpv(file,0)));
362 XPUSHs(sv_2mortal(newSVpv(new,0)));
364 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
366 rv = (rv ? POPi : 0);
370 DEBUGf("link end: %i\n",rv);
375 int _PLfuse_chmod (const char *file, mode_t mode) {
378 DEBUGf("chmod begin\n");
382 XPUSHs(sv_2mortal(newSVpv(file,0)));
383 XPUSHs(sv_2mortal(newSViv(mode)));
385 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
387 rv = (rv ? POPi : 0);
391 DEBUGf("chmod end: %i\n",rv);
396 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
399 DEBUGf("chown begin\n");
403 XPUSHs(sv_2mortal(newSVpv(file,0)));
404 XPUSHs(sv_2mortal(newSViv(uid)));
405 XPUSHs(sv_2mortal(newSViv(gid)));
407 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
409 rv = (rv ? POPi : 0);
413 DEBUGf("chown end: %i\n",rv);
418 int _PLfuse_truncate (const char *file, off_t off) {
420 #ifndef PERL_HAS_64BITINT
424 DEBUGf("truncate begin\n");
428 XPUSHs(sv_2mortal(newSVpv(file,0)));
429 #ifdef PERL_HAS_64BITINT
430 XPUSHs(sv_2mortal(newSViv(off)));
432 if (asprintf(&temp, "%llu", off) == -1)
433 croak("Memory allocation failure!");
434 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
438 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
440 rv = (rv ? POPi : 0);
444 DEBUGf("truncate end: %i\n",rv);
449 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
452 DEBUGf("utime begin\n");
456 XPUSHs(sv_2mortal(newSVpv(file,0)));
457 XPUSHs(sv_2mortal(newSViv(uti->actime)));
458 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
460 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
462 rv = (rv ? POPi : 0);
466 DEBUGf("utime end: %i\n",rv);
471 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
473 int flags = fi->flags;
476 DEBUGf("open begin\n");
480 XPUSHs(sv_2mortal(newSVpv(file,0)));
481 XPUSHs(sv_2mortal(newSViv(flags)));
482 /* Create a hashref containing the details from fi
483 * which we can look at or modify.
485 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
487 #if FUSE_VERSION >= 24
488 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
489 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
491 #if FUSE_VERSION >= 29
492 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
494 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
495 /* All hashref things done */
498 /* Open called with filename, flags */
499 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
503 FH_STOREHANDLE(fi,POPs);
511 /* Success, so copy the file handle which they returned */
512 #if FUSE_VERSION >= 24
514 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
515 fi->direct_io = SvIV(*svp);
516 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
517 fi->keep_cache = SvIV(*svp);
519 #if FUSE_VERSION >= 29
520 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
521 fi->nonseekable = SvIV(*svp);
527 DEBUGf("open end: %i\n",rv);
532 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
533 struct fuse_file_info *fi) {
535 #ifndef PERL_HAS_64BITINT
539 DEBUGf("read begin\n");
543 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
544 XPUSHs(sv_2mortal(newSViv(buflen)));
545 #ifdef PERL_HAS_64BITINT
546 XPUSHs(sv_2mortal(newSViv(off)));
548 if (asprintf(&temp, "%llu", off) == -1)
549 croak("Memory allocation failure!");
550 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
553 XPUSHs(FH_GETHANDLE(fi));
555 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
561 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
570 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
572 memcpy(buf,SvPV_nolen(mysv),rv);
578 DEBUGf("read end: %i\n",rv);
583 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
585 #ifndef PERL_HAS_64BITINT
589 DEBUGf("write begin\n");
593 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
594 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
595 #ifdef PERL_HAS_64BITINT
596 XPUSHs(sv_2mortal(newSViv(off)));
598 if (asprintf(&temp, "%llu", off) == -1)
599 croak("Memory allocation failure!");
600 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
603 XPUSHs(FH_GETHANDLE(fi));
605 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
607 rv = (rv ? POPi : 0);
611 DEBUGf("write end: %i\n",rv);
616 int _PLfuse_statfs (const char *file, struct statvfs *st) {
619 DEBUGf("statfs begin\n");
624 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
626 DEBUGf("statfs got %i params\n",rv);
627 if(rv == 6 || rv == 7) {
633 st->f_namemax = POPi;
634 /* zero and fill-in other */
638 st->f_bavail = st->f_bfree;
639 st->f_favail = st->f_ffree;
647 croak("inappropriate number of returned values from statfs");
656 DEBUGf("statfs end: %i\n",rv);
661 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
664 DEBUGf("flush begin\n");
668 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
669 XPUSHs(FH_GETHANDLE(fi));
671 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
673 rv = (rv ? POPi : 0);
677 DEBUGf("flush end: %i\n",rv);
682 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
684 int flags = fi->flags;
686 DEBUGf("release begin\n");
690 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
691 XPUSHs(sv_2mortal(newSViv(flags)));
692 XPUSHs(FH_GETHANDLE(fi));
694 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
696 rv = (rv ? POPi : 0);
697 FH_RELEASEHANDLE(fi);
701 DEBUGf("release end: %i\n",rv);
706 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
708 int flags = fi->flags;
710 DEBUGf("fsync 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[20],G_SCALAR);
720 rv = (rv ? POPi : 0);
724 DEBUGf("fsync end: %i\n",rv);
729 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
732 DEBUGf("setxattr begin\n");
736 XPUSHs(sv_2mortal(newSVpv(file,0)));
737 XPUSHs(sv_2mortal(newSVpv(name,0)));
738 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
739 XPUSHs(sv_2mortal(newSViv(flags)));
741 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
743 rv = (rv ? POPi : 0);
747 DEBUGf("setxattr end: %i\n",rv);
752 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
755 DEBUGf("getxattr begin\n");
759 XPUSHs(sv_2mortal(newSVpv(file,0)));
760 XPUSHs(sv_2mortal(newSVpv(name,0)));
762 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
770 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
778 if ((rv > 0) && (buflen > 0))
783 memcpy(buf,SvPV_nolen(mysv),rv);
790 DEBUGf("getxattr end: %i\n",rv);
795 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
798 DEBUGf("listxattr begin\n");
802 XPUSHs(sv_2mortal(newSVpv(file,0)));
804 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
817 /* Always nul terminate */
818 if (list && (size > 0))
828 int s = SvCUR(mysv) + 1;
831 if (p && (size > 0) && (spc >= s))
833 memcpy(p,SvPV_nolen(mysv),s);
841 * If the Perl returned an error, return that.
842 * Otherwise check that the buffer was big enough.
847 if ((size > 0) && (size < total_len))
854 DEBUGf("listxattr end: %i\n",rv);
859 int _PLfuse_removexattr (const char *file, const char *name) {
862 DEBUGf("removexattr begin\n");
866 XPUSHs(sv_2mortal(newSVpv(file,0)));
867 XPUSHs(sv_2mortal(newSVpv(name,0)));
869 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
871 rv = (rv ? POPi : 0);
875 DEBUGf("removexattr end: %i\n",rv);
880 #if FUSE_VERSION >= 23
881 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
884 DEBUGf("opendir begin\n");
888 XPUSHs(sv_2mortal(newSVpv(file,0)));
889 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
891 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
895 FH_STOREHANDLE(fi, POPs);
903 DEBUGf("opendir end: %i\n",rv);
909 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
910 off_t off, struct fuse_file_info *fi) {
912 SV *sv, **svp, **swp;
916 #ifndef PERL_HAS_64BITINT
920 DEBUGf("readdir begin\n");
924 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
925 #ifdef PERL_HAS_64BITINT
926 XPUSHs(sv_2mortal(newSViv(off)));
928 if (asprintf(&temp, "%llu", off) == -1)
929 croak("Memory allocation failure!");
930 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
933 XPUSHs(FH_GETHANDLE(fi));
935 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
938 /* Should yield the bottom of the current stack... */
939 swp = &TOPs - prv + 1;
941 memset(&st, 0, sizeof(struct stat));
942 /* Sort of a hack to walk the stack in order, instead of reverse
943 * order - trying to explain to potential users why they need to
944 * reverse the order of this array would be confusing, at best. */
945 while (swp <= &TOPs) {
947 if (!SvROK(sv) && SvPOK(sv))
948 /* Just a bare SV (probably a string; hopefully a string) */
949 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
950 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
951 if (av_len(av) >= 2) {
952 /* The third element of the array should be the args that
953 * would otherwise go to getattr(); a lot of filesystems
954 * will, or at least can, return that info as part of the
955 * enumeration process... */
956 svp = av_fetch(av, 2, FALSE);
958 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
960 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
961 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
962 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
963 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
964 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
965 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
966 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
967 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
968 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
969 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
970 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
971 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
972 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
976 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
977 /* For now if the element isn't what we want, just
978 * quietly ignore it... */
980 if (av_len(av) >= 1) {
981 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
982 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
983 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
986 memset(&st, 0, sizeof(struct stat));
991 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
994 fprintf(stderr,"readdir() handler returned nothing!\n");
1000 DEBUGf("readdir end: %i\n",rv);
1005 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1008 DEBUGf("releasedir begin\n");
1012 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1013 XPUSHs(FH_GETHANDLE(fi));
1015 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1017 rv = (rv ? POPi : 0);
1018 FH_RELEASEHANDLE(fi);
1022 DEBUGf("releasedir end: %i\n",rv);
1027 int _PLfuse_fsyncdir(const char *file, int datasync,
1028 struct fuse_file_info *fi) {
1031 DEBUGf("fsyncdir begin\n");
1035 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1036 XPUSHs(sv_2mortal(newSViv(datasync)));
1037 XPUSHs(FH_GETHANDLE(fi));
1039 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1041 rv = (rv ? POPi : 0);
1045 DEBUGf("fsyncdir end: %i\n",rv);
1050 #if FUSE_VERSION >= 26
1051 void *_PLfuse_init(struct fuse_conn_info *fc)
1052 #else /* FUSE_VERSION < 26 */
1053 void *_PLfuse_init(void)
1054 #endif /* FUSE_VERSION >= 26 */
1059 DEBUGf("init begin\n");
1064 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1068 if (rv == &PL_sv_undef)
1071 rv = SvREFCNT_inc((SV *)rv);
1076 DEBUGf("init end: %p\n", rv);
1081 void _PLfuse_destroy(void *private_data) {
1083 DEBUGf("destroy begin\n");
1087 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1089 call_sv(MY_CXT.callback[30], G_VOID);
1092 SvREFCNT_dec((SV *)private_data);
1096 DEBUGf("init end\n");
1099 #endif /* FUSE_VERSION >= 23 */
1101 #if FUSE_VERSION >= 25
1102 int _PLfuse_access(const char *file, int mask) {
1105 DEBUGf("access begin\n");
1109 XPUSHs(sv_2mortal(newSVpv(file,0)));
1110 XPUSHs(sv_2mortal(newSViv(mask)));
1112 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1114 rv = (rv ? POPi : 0);
1118 DEBUGf("access end: %d\n", rv);
1123 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1127 DEBUGf("create begin\n");
1131 XPUSHs(sv_2mortal(newSVpv(file,0)));
1132 XPUSHs(sv_2mortal(newSViv(mode)));
1133 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1134 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1135 /* Create a hashref containing the details from fi
1136 * which we can look at or modify.
1139 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1140 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1141 #if FUSE_VERSION >= 29
1142 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1144 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1145 /* All hashref things done */
1148 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1152 FH_STOREHANDLE(fi,POPs);
1157 fprintf(stderr, "create() handler returned nothing!\n");
1161 /* Success, so copy the file handle which they returned */
1163 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1164 fi->direct_io = SvIV(*svp);
1165 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1166 fi->keep_cache = SvIV(*svp);
1167 #if FUSE_VERSION >= 29
1168 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1169 fi->nonseekable = SvIV(*svp);
1175 DEBUGf("create end: %d\n",rv);
1180 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1182 #ifndef PERL_HAS_64BITINT
1186 DEBUGf("ftruncate begin\n");
1190 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1191 #ifdef PERL_HAS_64BITINT
1192 XPUSHs(sv_2mortal(newSViv(off)));
1194 if (asprintf(&temp, "%llu", off) == -1)
1195 croak("Memory allocation failure!");
1196 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1199 XPUSHs(FH_GETHANDLE(fi));
1201 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1203 rv = (rv ? POPi : 0);
1207 DEBUGf("ftruncate end: %i\n",rv);
1212 int _PLfuse_fgetattr(const char *file, struct stat *result,
1213 struct fuse_file_info *fi) {
1216 DEBUGf("fgetattr begin: %s\n",file);
1220 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1221 XPUSHs(FH_GETHANDLE(fi));
1223 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1227 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1234 result->st_blocks = POPi;
1235 result->st_blksize = POPi;
1236 result->st_ctime = POPi;
1237 result->st_mtime = POPi;
1238 result->st_atime = POPi;
1239 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1240 result->st_rdev = POPi;
1241 result->st_gid = POPi;
1242 result->st_uid = POPi;
1243 result->st_nlink = POPi;
1244 result->st_mode = POPi;
1245 result->st_ino = POPi;
1246 result->st_dev = POPi;
1252 DEBUGf("fgetattr end: %i\n",rv);
1256 #endif /* FUSE_VERSION >= 25 */
1258 #if FUSE_VERSION >= 26
1259 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1260 struct flock *lockinfo) {
1264 #ifndef PERL_HAS_64BITINT
1268 DEBUGf("lock begin\n");
1272 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1273 XPUSHs(sv_2mortal(newSViv(cmd)));
1276 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1277 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1278 #ifdef PERL_HAS_64BITINT
1279 sv = newSViv(lockinfo->l_start);
1281 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1282 croak("Memory allocation failure!");
1283 sv = newSVpv(temp, 0);
1286 (void) hv_store(lihash, "l_start", 7, sv, 0);
1287 #ifdef PERL_HAS_64BITINT
1288 sv = newSViv(lockinfo->l_len);
1290 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1291 croak("Memory allocation failure!");
1292 sv = newSVpv(temp, 0);
1295 (void) hv_store(lihash, "l_len", 5, sv, 0);
1296 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1298 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1299 XPUSHs(FH_GETHANDLE(fi));
1302 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1304 rv = (rv ? POPi : 0);
1305 if (lockinfo && !rv) {
1306 /* Need to copy back any altered values from the hash into
1309 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1310 lockinfo->l_type = SvIV(*svp);
1311 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1312 lockinfo->l_whence = SvIV(*svp);
1313 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1314 lockinfo->l_start = SvNV(*svp);
1315 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1316 lockinfo->l_len = SvNV(*svp);
1317 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1318 lockinfo->l_pid = SvIV(*svp);
1323 DEBUGf("lock end: %i\n",rv);
1328 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1331 DEBUGf("utimens begin\n");
1335 XPUSHs(sv_2mortal(newSVpv(file,0)));
1336 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1337 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1339 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1341 rv = (rv ? POPi : 0);
1345 DEBUGf("utimens end: %i\n",rv);
1350 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1352 #ifndef PERL_HAS_64BITINT
1356 DEBUGf("bmap begin\n");
1360 XPUSHs(sv_2mortal(newSVpv(file,0)));
1361 XPUSHs(sv_2mortal(newSViv(blocksize)));
1362 #ifdef PERL_HAS_64BITINT
1363 XPUSHs(sv_2mortal(newSViv(*idx)));
1365 if (asprintf(&temp, "%llu", *idx) == -1)
1366 croak("Memory allocation failure!");
1367 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1371 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1373 if (rv > 0 && rv < 3) {
1379 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1385 DEBUGf("bmap end: %i\n",rv);
1389 #endif /* FUSE_VERSION >= 26 */
1392 #if FUSE_VERSION >= 28
1393 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1394 struct fuse_file_info *fi, unsigned int flags, void *data) {
1397 DEBUGf("ioctl begin\n");
1401 XPUSHs(sv_2mortal(newSVpv(file,0)));
1402 XPUSHs(sv_2mortal(newSViv(cmd)));
1403 XPUSHs(sv_2mortal(newSViv(flags)));
1404 if (_IOC_DIR(cmd) & _IOC_READ)
1405 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1407 XPUSHs(&PL_sv_undef);
1408 XPUSHs(FH_GETHANDLE(fi));
1410 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1412 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1416 char *rdata = SvPV(sv, len);
1417 if (len > _IOC_SIZE(cmd)) {
1418 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1422 memset(data, 0, _IOC_SIZE(cmd));
1423 memcpy(data, rdata, len);
1429 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1438 DEBUGf("ioctl end: %i\n",rv);
1443 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1444 struct fuse_pollhandle *ph, unsigned *reventsp) {
1447 #endif /* FUSE_VERSION >= 28 */
1450 struct fuse_operations _available_ops = {
1451 getattr: _PLfuse_getattr,
1452 readlink: _PLfuse_readlink,
1453 getdir: _PLfuse_getdir,
1454 mknod: _PLfuse_mknod,
1455 mkdir: _PLfuse_mkdir,
1456 unlink: _PLfuse_unlink,
1457 rmdir: _PLfuse_rmdir,
1458 symlink: _PLfuse_symlink,
1459 rename: _PLfuse_rename,
1461 chmod: _PLfuse_chmod,
1462 chown: _PLfuse_chown,
1463 truncate: _PLfuse_truncate,
1464 utime: _PLfuse_utime,
1467 write: _PLfuse_write,
1468 statfs: _PLfuse_statfs,
1469 flush: _PLfuse_flush,
1470 release: _PLfuse_release,
1471 fsync: _PLfuse_fsync,
1472 setxattr: _PLfuse_setxattr,
1473 getxattr: _PLfuse_getxattr,
1474 listxattr: _PLfuse_listxattr,
1475 removexattr: _PLfuse_removexattr,
1476 #if FUSE_VERSION >= 23
1477 opendir: _PLfuse_opendir,
1478 readdir: _PLfuse_readdir,
1479 releasedir: _PLfuse_releasedir,
1480 fsyncdir: _PLfuse_fsyncdir,
1482 destroy: _PLfuse_destroy,
1483 #endif /* FUSE_VERSION >= 23 */
1484 #if FUSE_VERSION >= 25
1485 access: _PLfuse_access,
1486 create: _PLfuse_create,
1487 ftruncate: _PLfuse_ftruncate,
1488 fgetattr: _PLfuse_fgetattr,
1489 #endif /* FUSE_VERSION >= 25 */
1490 #if FUSE_VERSION >= 26
1492 utimens: _PLfuse_utimens,
1494 #endif /* FUSE_VERSION >= 26 */
1496 #if FUSE_VERSION >= 28
1497 ioctl: _PLfuse_ioctl,
1499 #endif /* FUSE_VERSION >= 28 */
1503 MODULE = Fuse PACKAGE = Fuse
1517 tTHX parent = MY_CXT.self;
1518 MY_CXT.self = my_perl;
1519 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1520 /* CLONE entered without a pointer table, so we can't safely clone static data */
1522 for(i=0;i<N_CALLBACKS;i++) {
1523 MY_CXT.callback[i] = NULL;
1525 MY_CXT.handles = newHV();
1529 CLONE_PARAMS *clone_param;
1530 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1531 clone_param = clone_params_new(parent, aTHX);
1533 CLONE_PARAMS raw_param;
1534 raw_param.flags = 0;
1535 raw_param.proto_perl = parent;
1536 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1537 clone_param = &raw_param;
1539 for(i=0;i<N_CALLBACKS;i++) {
1540 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1542 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1543 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1544 clone_params_del(clone_param);
1551 struct fuse_context *fc;
1553 fc = fuse_get_context();
1556 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1557 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1558 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1559 if (fc->private_data)
1560 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1561 #if FUSE_VERSION >= 28
1562 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1563 #endif /* FUSE_VERSION >= 28 */
1564 RETVAL = newRV_noinc((SV*)hash);
1574 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1581 struct fuse_operations fops;
1585 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1586 struct fuse_chan *fc;
1589 if(items != N_CALLBACKS + 5) {
1590 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1593 memset(&fops, 0, sizeof(struct fuse_operations));
1595 debug = SvIV(ST(0));
1596 MY_CXT.threaded = SvIV(ST(1));
1597 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1598 if(MY_CXT.threaded) {
1599 #ifdef FUSE_USE_ITHREADS
1600 master_interp = aTHX;
1601 MUTEX_INIT(&MY_CXT.mutex);
1602 SvSHARE((SV*)(MY_CXT.handles));
1604 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1605 "mode, but your perl was not built with a supported "
1606 "thread model. Threads are disabled.\n");
1607 MY_CXT.threaded = 0;
1610 mountpoint = SvPV_nolen(ST(2));
1611 mountopts = SvPV_nolen(ST(3));
1612 #if FUSE_VERSION >= 28
1613 fops.flag_nullpath_ok = SvIV(ST(4));
1614 #endif /* FUSE_VERSION >= 28 */
1615 for(i=0;i<N_CALLBACKS;i++) {
1617 /* allow symbolic references, or real code references. */
1618 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1619 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1620 /* Dirty hack, to keep anything from overwriting the
1621 * flag area with a pointer. There should never be
1622 * anything passed as 'junk', but this prevents
1623 * someone from doing it and screwing things up... */
1627 MY_CXT.callback[i] = var;
1628 } else if(SvOK(var)) {
1629 croak("invalid callback (%i) passed to perl_fuse_main "
1630 "(%s is not a string, code ref, or undef).\n",
1631 i+4,SvPVbyte_nolen(var));
1633 MY_CXT.callback[i] = NULL;
1637 * XXX: What comes here is just a ridiculous use of the option parsing API
1638 * to hack on compatibility with other parts of the new API. First and
1639 * foremost, real C argc/argv would be good to get at...
1642 (fuse_opt_add_arg(&args, "") == -1 ||
1643 fuse_opt_add_arg(&args, "-o") == -1 ||
1644 fuse_opt_add_arg(&args, mountopts) == -1)) {
1645 fuse_opt_free_args(&args);
1646 croak("out of memory\n");
1648 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1649 fuse_opt_free_args(&args);
1650 croak("out of memory\n");
1652 fc = fuse_mount(mountpoint,&args);
1654 croak("could not mount fuse filesystem!\n");
1656 if(MY_CXT.threaded) {
1657 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1660 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1661 fuse_unmount(mountpoint,fc);
1662 fuse_opt_free_args(&args);