1 #define PERL_NO_GET_CONTEXT
8 #if (defined(__FreeBSD__) && __FreeBSD__ < 10) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 #if defined(__linux__)
16 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim.tv_nsec)
18 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
21 /* Determine if threads support should be included */
24 # define FUSE_USE_ITHREADS
25 # if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
26 # define tTHX PerlInterpreter*
27 # define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
28 # define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
29 # define dMY_CXT_INTERP(interp) \
30 SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
31 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
34 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
40 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
41 #if FUSE_VERSION >= 28
42 # define N_CALLBACKS 41
43 #elif FUSE_VERSION >= 26
44 # define N_CALLBACKS 38
45 #elif FUSE_VERSION >= 25
46 # define N_CALLBACKS 35
47 #elif FUSE_VERSION >= 23
48 # define N_CALLBACKS 31
50 # define N_CALLBACKS 25
54 SV *callback[N_CALLBACKS];
66 #ifdef FUSE_USE_ITHREADS
67 tTHX master_interp = NULL;
69 #define CLONE_INTERP(parent) S_clone_interp(parent)
70 tTHX S_clone_interp(tTHX parent) {
71 # if (PERL_VERSION < 10)
72 tTHX my_perl = parent;
74 dMY_CXT_INTERP(parent);
76 MUTEX_LOCK(&MY_CXT.mutex);
77 PERL_SET_CONTEXT(parent);
79 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
80 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
82 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
83 ptr_table_free(PL_ptr_table);
86 MUTEX_UNLOCK(&MY_CXT.mutex);
92 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
93 # define FUSE_CONTEXT_POST }
95 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
96 # define FUSE_CONTEXT_POST
101 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
106 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
107 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
108 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
109 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
111 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
116 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
124 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
126 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
131 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
133 #ifdef FUSE_USE_ITHREADS
134 if(MY_CXT.threaded) {
138 /* This seems to be screwing things up... */
139 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
140 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
142 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
149 int _PLfuse_getattr(const char *file, struct stat *result) {
152 DEBUGf("getattr begin: %s\n",file);
156 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
158 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
162 fprintf(stderr,"inappropriate number of returned values from getattr\n");
170 result->st_blocks = POPi;
171 result->st_blksize = POPi;
172 /* Do a little gymnastics to transform the fractional part into nsec */
174 result->st_ctime = (int)tm;
175 STAT_NSEC(result, st_ctim) = (tm - (int)tm) * 1000000000;
177 result->st_mtime = (int)tm;
178 STAT_NSEC(result, st_mtim) = (tm - (int)tm) * 1000000000;
180 result->st_atime = (int)tm;
181 STAT_NSEC(result, st_atim) = (tm - (int)tm) * 1000000000;
182 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
183 result->st_rdev = POPi;
184 result->st_gid = POPi;
185 result->st_uid = POPi;
186 result->st_nlink = POPi;
187 result->st_mode = POPi;
188 result->st_ino = POPi;
189 result->st_dev = POPi;
195 DEBUGf("getattr end: %i\n",rv);
200 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
205 DEBUGf("readlink begin\n");
209 XPUSHs(sv_2mortal(newSVpv(file,0)));
211 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
217 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
220 strncpy(buf,SvPV_nolen(mysv),buflen);
228 DEBUGf("readlink end: %i\n",rv);
233 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
237 DEBUGf("getdir begin\n");
241 XPUSHs(sv_2mortal(newSVpv(file,0)));
243 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
246 /* Should yield the bottom of the current stack... */
247 swp = &TOPs - prv + 1;
249 /* Sort of a hack to walk the stack in order, instead of reverse
250 * order - trying to explain to potential users why they need to
251 * reverse the order of this array would be confusing, at best. */
253 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
255 fprintf(stderr,"getdir() handler returned nothing!\n");
261 DEBUGf("getdir end: %i\n",rv);
266 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
269 DEBUGf("mknod begin\n");
273 XPUSHs(sv_2mortal(newSVpv(file,0)));
274 XPUSHs(sv_2mortal(newSViv(mode)));
275 XPUSHs(sv_2mortal(newSViv(dev)));
277 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
279 rv = (rv ? POPi : 0);
283 DEBUGf("mknod end: %i\n",rv);
288 int _PLfuse_mkdir (const char *file, mode_t mode) {
291 DEBUGf("mkdir begin\n");
295 XPUSHs(sv_2mortal(newSVpv(file,0)));
296 XPUSHs(sv_2mortal(newSViv(mode)));
298 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
300 rv = (rv ? POPi : 0);
304 DEBUGf("mkdir end: %i\n",rv);
310 int _PLfuse_unlink (const char *file) {
313 DEBUGf("unlink begin\n");
317 XPUSHs(sv_2mortal(newSVpv(file,0)));
319 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
321 rv = (rv ? POPi : 0);
325 DEBUGf("unlink end: %i\n",rv);
330 int _PLfuse_rmdir (const char *file) {
333 DEBUGf("rmdir begin\n");
337 XPUSHs(sv_2mortal(newSVpv(file,0)));
339 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
341 rv = (rv ? POPi : 0);
345 DEBUGf("rmdir end: %i\n",rv);
350 int _PLfuse_symlink (const char *file, const char *new) {
353 DEBUGf("symlink begin\n");
357 XPUSHs(sv_2mortal(newSVpv(file,0)));
358 XPUSHs(sv_2mortal(newSVpv(new,0)));
360 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
362 rv = (rv ? POPi : 0);
366 DEBUGf("symlink end: %i\n",rv);
371 int _PLfuse_rename (const char *file, const char *new) {
374 DEBUGf("rename begin\n");
378 XPUSHs(sv_2mortal(newSVpv(file,0)));
379 XPUSHs(sv_2mortal(newSVpv(new,0)));
381 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
383 rv = (rv ? POPi : 0);
387 DEBUGf("rename end: %i\n",rv);
392 int _PLfuse_link (const char *file, const char *new) {
395 DEBUGf("link begin\n");
399 XPUSHs(sv_2mortal(newSVpv(file,0)));
400 XPUSHs(sv_2mortal(newSVpv(new,0)));
402 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
404 rv = (rv ? POPi : 0);
408 DEBUGf("link end: %i\n",rv);
413 int _PLfuse_chmod (const char *file, mode_t mode) {
416 DEBUGf("chmod begin\n");
420 XPUSHs(sv_2mortal(newSVpv(file,0)));
421 XPUSHs(sv_2mortal(newSViv(mode)));
423 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
425 rv = (rv ? POPi : 0);
429 DEBUGf("chmod end: %i\n",rv);
434 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
437 DEBUGf("chown begin\n");
441 XPUSHs(sv_2mortal(newSVpv(file,0)));
442 XPUSHs(sv_2mortal(newSViv(uid)));
443 XPUSHs(sv_2mortal(newSViv(gid)));
445 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
447 rv = (rv ? POPi : 0);
451 DEBUGf("chown end: %i\n",rv);
456 int _PLfuse_truncate (const char *file, off_t off) {
458 #ifndef PERL_HAS_64BITINT
462 DEBUGf("truncate begin\n");
466 XPUSHs(sv_2mortal(newSVpv(file,0)));
467 #ifdef PERL_HAS_64BITINT
468 XPUSHs(sv_2mortal(newSViv(off)));
470 if (asprintf(&temp, "%llu", off) == -1)
471 croak("Memory allocation failure!");
472 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
476 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
478 rv = (rv ? POPi : 0);
482 DEBUGf("truncate end: %i\n",rv);
487 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
490 DEBUGf("utime begin\n");
494 XPUSHs(sv_2mortal(newSVpv(file,0)));
495 XPUSHs(sv_2mortal(newSViv(uti->actime)));
496 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
498 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
500 rv = (rv ? POPi : 0);
504 DEBUGf("utime end: %i\n",rv);
509 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
511 int flags = fi->flags;
514 DEBUGf("open begin\n");
518 XPUSHs(sv_2mortal(newSVpv(file,0)));
519 XPUSHs(sv_2mortal(newSViv(flags)));
520 /* Create a hashref containing the details from fi
521 * which we can look at or modify.
523 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
525 #if FUSE_VERSION >= 24
526 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
527 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
529 #if FUSE_VERSION >= 28
530 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
532 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
533 /* All hashref things done */
536 /* Open called with filename, flags */
537 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
541 FH_STOREHANDLE(fi,POPs);
549 /* Success, so copy the file handle which they returned */
550 #if FUSE_VERSION >= 24
552 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
553 fi->direct_io = SvIV(*svp);
554 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
555 fi->keep_cache = SvIV(*svp);
557 #if FUSE_VERSION >= 28
558 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
559 fi->nonseekable = SvIV(*svp);
565 DEBUGf("open end: %i\n",rv);
570 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
571 struct fuse_file_info *fi) {
573 #ifndef PERL_HAS_64BITINT
577 DEBUGf("read begin\n");
581 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
582 XPUSHs(sv_2mortal(newSViv(buflen)));
583 #ifdef PERL_HAS_64BITINT
584 XPUSHs(sv_2mortal(newSViv(off)));
586 if (asprintf(&temp, "%llu", off) == -1)
587 croak("Memory allocation failure!");
588 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
591 XPUSHs(FH_GETHANDLE(fi));
593 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
599 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
608 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
610 memcpy(buf,SvPV_nolen(mysv),rv);
616 DEBUGf("read end: %i\n",rv);
621 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
623 #ifndef PERL_HAS_64BITINT
627 DEBUGf("write begin\n");
631 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
632 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
633 #ifdef PERL_HAS_64BITINT
634 XPUSHs(sv_2mortal(newSViv(off)));
636 if (asprintf(&temp, "%llu", off) == -1)
637 croak("Memory allocation failure!");
638 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
641 XPUSHs(FH_GETHANDLE(fi));
643 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
645 rv = (rv ? POPi : 0);
649 DEBUGf("write end: %i\n",rv);
654 int _PLfuse_statfs (const char *file, struct statvfs *st) {
657 DEBUGf("statfs begin\n");
662 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
664 DEBUGf("statfs got %i params\n",rv);
665 if(rv == 6 || rv == 7) {
671 st->f_namemax = POPi;
672 /* zero and fill-in other */
676 st->f_bavail = st->f_bfree;
677 st->f_favail = st->f_ffree;
685 croak("inappropriate number of returned values from statfs");
694 DEBUGf("statfs end: %i\n",rv);
699 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
702 DEBUGf("flush begin\n");
706 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
707 XPUSHs(FH_GETHANDLE(fi));
709 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
711 rv = (rv ? POPi : 0);
715 DEBUGf("flush end: %i\n",rv);
720 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
722 int flags = fi->flags;
724 DEBUGf("release begin\n");
728 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
729 XPUSHs(sv_2mortal(newSViv(flags)));
730 XPUSHs(FH_GETHANDLE(fi));
732 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
734 rv = (rv ? POPi : 0);
735 FH_RELEASEHANDLE(fi);
739 DEBUGf("release end: %i\n",rv);
744 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
746 int flags = fi->flags;
748 DEBUGf("fsync begin\n");
752 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
753 XPUSHs(sv_2mortal(newSViv(flags)));
754 XPUSHs(FH_GETHANDLE(fi));
756 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
758 rv = (rv ? POPi : 0);
762 DEBUGf("fsync end: %i\n",rv);
767 #if __FreeBSD__ >= 10
768 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
770 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
774 DEBUGf("setxattr begin\n");
778 XPUSHs(sv_2mortal(newSVpv(file,0)));
779 XPUSHs(sv_2mortal(newSVpv(name,0)));
780 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
781 XPUSHs(sv_2mortal(newSViv(flags)));
783 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
785 rv = (rv ? POPi : 0);
789 DEBUGf("setxattr end: %i\n",rv);
794 #if __FreeBSD__ >= 10
795 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
797 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
801 DEBUGf("getxattr begin\n");
805 XPUSHs(sv_2mortal(newSVpv(file,0)));
806 XPUSHs(sv_2mortal(newSVpv(name,0)));
808 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
816 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
824 if ((rv > 0) && (buflen > 0))
829 memcpy(buf,SvPV_nolen(mysv),rv);
836 DEBUGf("getxattr end: %i\n",rv);
841 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
844 DEBUGf("listxattr begin\n");
848 XPUSHs(sv_2mortal(newSVpv(file,0)));
850 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
863 /* Always nul terminate */
864 if (list && (size > 0))
874 int s = SvCUR(mysv) + 1;
877 if (p && (size > 0) && (spc >= s))
879 memcpy(p,SvPV_nolen(mysv),s);
887 * If the Perl returned an error, return that.
888 * Otherwise check that the buffer was big enough.
893 if ((size > 0) && (size < total_len))
900 DEBUGf("listxattr end: %i\n",rv);
905 int _PLfuse_removexattr (const char *file, const char *name) {
908 DEBUGf("removexattr begin\n");
912 XPUSHs(sv_2mortal(newSVpv(file,0)));
913 XPUSHs(sv_2mortal(newSVpv(name,0)));
915 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
917 rv = (rv ? POPi : 0);
921 DEBUGf("removexattr end: %i\n",rv);
926 #if FUSE_VERSION >= 23
927 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
930 DEBUGf("opendir begin\n");
934 XPUSHs(sv_2mortal(newSVpv(file,0)));
935 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
937 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
941 FH_STOREHANDLE(fi, POPs);
949 DEBUGf("opendir end: %i\n",rv);
955 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
956 off_t off, struct fuse_file_info *fi) {
958 SV *sv, **svp, **swp;
962 #ifndef PERL_HAS_64BITINT
966 DEBUGf("readdir begin\n");
970 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
971 #ifdef PERL_HAS_64BITINT
972 XPUSHs(sv_2mortal(newSViv(off)));
974 if (asprintf(&temp, "%llu", off) == -1)
975 croak("Memory allocation failure!");
976 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
979 XPUSHs(FH_GETHANDLE(fi));
981 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
984 /* Should yield the bottom of the current stack... */
985 swp = &TOPs - prv + 1;
987 memset(&st, 0, sizeof(struct stat));
988 /* Sort of a hack to walk the stack in order, instead of reverse
989 * order - trying to explain to potential users why they need to
990 * reverse the order of this array would be confusing, at best. */
991 while (swp <= &TOPs) {
993 if (!SvROK(sv) && SvPOK(sv))
994 /* Just a bare SV (probably a string; hopefully a string) */
995 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
996 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
997 if (av_len(av) >= 2) {
998 /* The third element of the array should be the args that
999 * would otherwise go to getattr(); a lot of filesystems
1000 * will, or at least can, return that info as part of the
1001 * enumeration process... */
1002 svp = av_fetch(av, 2, FALSE);
1004 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1005 av_len(av2) == 12) {
1007 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1008 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1009 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1010 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1011 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1012 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1013 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1014 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1015 tm = SvNV(*(av_fetch(av2, 8, FALSE)));
1016 st.st_atime = (int)tm;
1017 STAT_NSEC(&st, st_atim) = (tm - (int)tm) * 1000000000;
1018 tm = SvNV(*(av_fetch(av2, 9, FALSE)));
1019 st.st_mtime = (int)tm;
1020 STAT_NSEC(&st, st_mtim) = (tm - (int)tm) * 1000000000;
1021 tm = SvNV(*(av_fetch(av2, 10, FALSE)));
1022 st.st_ctime = (int)tm;
1023 STAT_NSEC(&st, st_ctim) = (tm - (int)tm) * 1000000000;
1024 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1025 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1029 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1030 /* For now if the element isn't what we want, just
1031 * quietly ignore it... */
1033 if (av_len(av) >= 1) {
1034 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1035 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1036 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1039 memset(&st, 0, sizeof(struct stat));
1044 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1047 fprintf(stderr,"readdir() handler returned nothing!\n");
1053 DEBUGf("readdir end: %i\n",rv);
1058 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1061 DEBUGf("releasedir begin\n");
1065 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1066 XPUSHs(FH_GETHANDLE(fi));
1068 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1070 rv = (rv ? POPi : 0);
1071 FH_RELEASEHANDLE(fi);
1075 DEBUGf("releasedir end: %i\n",rv);
1080 int _PLfuse_fsyncdir(const char *file, int datasync,
1081 struct fuse_file_info *fi) {
1084 DEBUGf("fsyncdir begin\n");
1088 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1089 XPUSHs(sv_2mortal(newSViv(datasync)));
1090 XPUSHs(FH_GETHANDLE(fi));
1092 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1094 rv = (rv ? POPi : 0);
1098 DEBUGf("fsyncdir end: %i\n",rv);
1103 #if FUSE_VERSION >= 26
1104 void *_PLfuse_init(struct fuse_conn_info *fc)
1105 #else /* FUSE_VERSION < 26 */
1106 void *_PLfuse_init(void)
1107 #endif /* FUSE_VERSION >= 26 */
1112 DEBUGf("init begin\n");
1117 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1121 if (rv == &PL_sv_undef)
1124 rv = SvREFCNT_inc((SV *)rv);
1129 DEBUGf("init end: %p\n", rv);
1134 void _PLfuse_destroy(void *private_data) {
1136 DEBUGf("destroy begin\n");
1140 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1142 call_sv(MY_CXT.callback[30], G_VOID);
1145 SvREFCNT_dec((SV *)private_data);
1149 DEBUGf("init end\n");
1152 #endif /* FUSE_VERSION >= 23 */
1154 #if FUSE_VERSION >= 25
1155 int _PLfuse_access(const char *file, int mask) {
1158 DEBUGf("access begin\n");
1162 XPUSHs(sv_2mortal(newSVpv(file,0)));
1163 XPUSHs(sv_2mortal(newSViv(mask)));
1165 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1167 rv = (rv ? POPi : 0);
1171 DEBUGf("access end: %d\n", rv);
1176 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1180 DEBUGf("create begin\n");
1184 XPUSHs(sv_2mortal(newSVpv(file,0)));
1185 XPUSHs(sv_2mortal(newSViv(mode)));
1186 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1187 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1188 /* Create a hashref containing the details from fi
1189 * which we can look at or modify.
1192 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1193 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1194 #if FUSE_VERSION >= 28
1195 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1197 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1198 /* All hashref things done */
1201 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1205 FH_STOREHANDLE(fi,POPs);
1210 fprintf(stderr, "create() handler returned nothing!\n");
1214 /* Success, so copy the file handle which they returned */
1216 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1217 fi->direct_io = SvIV(*svp);
1218 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1219 fi->keep_cache = SvIV(*svp);
1220 #if FUSE_VERSION >= 28
1221 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1222 fi->nonseekable = SvIV(*svp);
1228 DEBUGf("create end: %d\n",rv);
1233 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1235 #ifndef PERL_HAS_64BITINT
1239 DEBUGf("ftruncate begin\n");
1243 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1244 #ifdef PERL_HAS_64BITINT
1245 XPUSHs(sv_2mortal(newSViv(off)));
1247 if (asprintf(&temp, "%llu", off) == -1)
1248 croak("Memory allocation failure!");
1249 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1252 XPUSHs(FH_GETHANDLE(fi));
1254 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1256 rv = (rv ? POPi : 0);
1260 DEBUGf("ftruncate end: %i\n",rv);
1265 int _PLfuse_fgetattr(const char *file, struct stat *result,
1266 struct fuse_file_info *fi) {
1269 DEBUGf("fgetattr begin: %s\n",file);
1273 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1274 XPUSHs(FH_GETHANDLE(fi));
1276 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1280 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1288 result->st_blocks = POPi;
1289 result->st_blksize = POPi;
1290 /* Do a little gymnastics to transform the fractional part into nsec */
1292 result->st_ctime = (int)tm;
1293 STAT_NSEC(result, st_ctim) = (tm - (int)tm) * 1000000000;
1295 result->st_mtime = (int)tm;
1296 STAT_NSEC(result, st_mtim) = (tm - (int)tm) * 1000000000;
1298 result->st_atime = (int)tm;
1299 STAT_NSEC(result, st_atim) = (tm - (int)tm) * 1000000000;
1300 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1301 result->st_rdev = POPi;
1302 result->st_gid = POPi;
1303 result->st_uid = POPi;
1304 result->st_nlink = POPi;
1305 result->st_mode = POPi;
1306 result->st_ino = POPi;
1307 result->st_dev = POPi;
1313 DEBUGf("fgetattr end: %i\n",rv);
1317 #endif /* FUSE_VERSION >= 25 */
1319 #if FUSE_VERSION >= 26
1320 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1321 struct flock *lockinfo) {
1325 #ifndef PERL_HAS_64BITINT
1329 DEBUGf("lock begin\n");
1333 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1334 XPUSHs(sv_2mortal(newSViv(cmd)));
1337 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1338 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1339 #ifdef PERL_HAS_64BITINT
1340 sv = newSViv(lockinfo->l_start);
1342 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1343 croak("Memory allocation failure!");
1344 sv = newSVpv(temp, 0);
1347 (void) hv_store(lihash, "l_start", 7, sv, 0);
1348 #ifdef PERL_HAS_64BITINT
1349 sv = newSViv(lockinfo->l_len);
1351 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1352 croak("Memory allocation failure!");
1353 sv = newSVpv(temp, 0);
1356 (void) hv_store(lihash, "l_len", 5, sv, 0);
1357 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1359 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1360 XPUSHs(FH_GETHANDLE(fi));
1363 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1365 rv = (rv ? POPi : 0);
1366 if (lockinfo && !rv) {
1367 /* Need to copy back any altered values from the hash into
1370 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1371 lockinfo->l_type = SvIV(*svp);
1372 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1373 lockinfo->l_whence = SvIV(*svp);
1374 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1375 lockinfo->l_start = SvNV(*svp);
1376 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1377 lockinfo->l_len = SvNV(*svp);
1378 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1379 lockinfo->l_pid = SvIV(*svp);
1384 DEBUGf("lock end: %i\n",rv);
1389 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1392 DEBUGf("utimens begin\n");
1396 XPUSHs(sv_2mortal(newSVpv(file,0)));
1397 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1398 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1400 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1402 rv = (rv ? POPi : 0);
1406 DEBUGf("utimens end: %i\n",rv);
1411 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1413 #ifndef PERL_HAS_64BITINT
1417 DEBUGf("bmap begin\n");
1421 XPUSHs(sv_2mortal(newSVpv(file,0)));
1422 XPUSHs(sv_2mortal(newSViv(blocksize)));
1423 #ifdef PERL_HAS_64BITINT
1424 XPUSHs(sv_2mortal(newSViv(*idx)));
1426 if (asprintf(&temp, "%llu", *idx) == -1)
1427 croak("Memory allocation failure!");
1428 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1432 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1434 if (rv > 0 && rv < 3) {
1440 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1446 DEBUGf("bmap end: %i\n",rv);
1450 #endif /* FUSE_VERSION >= 26 */
1452 #if FUSE_VERSION >= 28
1453 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1454 struct fuse_file_info *fi, unsigned int flags, void *data) {
1458 DEBUGf("ioctl begin\n");
1462 XPUSHs(sv_2mortal(newSVpv(file,0)));
1463 /* I don't know why cmd is a signed int in the first place;
1464 * casting as unsigned so stupid tricks don't have to be done on
1466 XPUSHs(sv_2mortal(newSViv((unsigned int)cmd)));
1467 XPUSHs(sv_2mortal(newSViv(flags)));
1468 if (_IOC_DIR(cmd) & _IOC_WRITE)
1469 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1471 XPUSHs(&PL_sv_undef);
1472 XPUSHs(FH_GETHANDLE(fi));
1474 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1476 if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
1484 if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
1487 char *rdata = SvPV(sv, len);
1489 if (len > _IOC_SIZE(cmd)) {
1490 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1494 memset(data, 0, _IOC_SIZE(cmd));
1495 memcpy(data, rdata, len);
1499 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1506 DEBUGf("ioctl end: %i\n",rv);
1511 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1512 struct fuse_pollhandle *ph, unsigned *reventsp) {
1516 DEBUGf("poll begin\n");
1520 XPUSHs(sv_2mortal(newSVpv(file,0)));
1522 /* Still gotta figure out how to do this right... */
1523 sv = newSViv(PTR2IV(ph));
1529 XPUSHs(&PL_sv_undef);
1530 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1531 XPUSHs(FH_GETHANDLE(fi));
1533 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1539 rv = (rv ? POPi : 0);
1543 DEBUGf("poll end: %i\n", rv);
1547 #endif /* FUSE_VERSION >= 28 */
1549 struct fuse_operations _available_ops = {
1550 getattr: _PLfuse_getattr,
1551 readlink: _PLfuse_readlink,
1552 getdir: _PLfuse_getdir,
1553 mknod: _PLfuse_mknod,
1554 mkdir: _PLfuse_mkdir,
1555 unlink: _PLfuse_unlink,
1556 rmdir: _PLfuse_rmdir,
1557 symlink: _PLfuse_symlink,
1558 rename: _PLfuse_rename,
1560 chmod: _PLfuse_chmod,
1561 chown: _PLfuse_chown,
1562 truncate: _PLfuse_truncate,
1563 utime: _PLfuse_utime,
1566 write: _PLfuse_write,
1567 statfs: _PLfuse_statfs,
1568 flush: _PLfuse_flush,
1569 release: _PLfuse_release,
1570 fsync: _PLfuse_fsync,
1571 setxattr: _PLfuse_setxattr,
1572 getxattr: _PLfuse_getxattr,
1573 listxattr: _PLfuse_listxattr,
1574 removexattr: _PLfuse_removexattr,
1575 #if FUSE_VERSION >= 23
1576 opendir: _PLfuse_opendir,
1577 readdir: _PLfuse_readdir,
1578 releasedir: _PLfuse_releasedir,
1579 fsyncdir: _PLfuse_fsyncdir,
1581 destroy: _PLfuse_destroy,
1582 #endif /* FUSE_VERSION >= 23 */
1583 #if FUSE_VERSION >= 25
1584 access: _PLfuse_access,
1585 create: _PLfuse_create,
1586 ftruncate: _PLfuse_ftruncate,
1587 fgetattr: _PLfuse_fgetattr,
1588 #endif /* FUSE_VERSION >= 25 */
1589 #if FUSE_VERSION >= 26
1591 utimens: _PLfuse_utimens,
1593 #endif /* FUSE_VERSION >= 26 */
1594 #if FUSE_VERSION >= 28
1595 ioctl: _PLfuse_ioctl,
1597 #endif /* FUSE_VERSION >= 28 */
1600 MODULE = Fuse PACKAGE = Fuse
1619 tTHX parent = MY_CXT.self;
1620 MY_CXT.self = my_perl;
1621 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1622 /* CLONE entered without a pointer table, so we can't safely clone static data */
1624 for(i=0;i<N_CALLBACKS;i++) {
1625 MY_CXT.callback[i] = NULL;
1627 MY_CXT.handles = newHV();
1631 CLONE_PARAMS *clone_param;
1632 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1633 clone_param = Perl_clone_params_new(parent, aTHX);
1635 CLONE_PARAMS raw_param;
1636 raw_param.flags = 0;
1637 raw_param.proto_perl = parent;
1638 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1639 clone_param = &raw_param;
1641 for(i=0;i<N_CALLBACKS;i++) {
1642 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1644 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1645 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1646 Perl_clone_params_del(clone_param);
1654 struct fuse_context *fc;
1656 fc = fuse_get_context();
1659 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1660 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1661 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1662 if (fc->private_data)
1663 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1664 #if FUSE_VERSION >= 28
1665 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1666 #endif /* FUSE_VERSION >= 28 */
1667 RETVAL = newRV_noinc((SV*)hash);
1677 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1684 RETVAL = newSViv(XATTR_CREATE);
1691 RETVAL = newSViv(XATTR_REPLACE);
1698 struct fuse_operations fops;
1702 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1703 struct fuse_chan *fc;
1706 if(items != N_CALLBACKS + 5) {
1707 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1710 memset(&fops, 0, sizeof(struct fuse_operations));
1712 debug = SvIV(ST(0));
1713 MY_CXT.threaded = SvIV(ST(1));
1714 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1715 if(MY_CXT.threaded) {
1716 #ifdef FUSE_USE_ITHREADS
1717 master_interp = aTHX;
1718 MUTEX_INIT(&MY_CXT.mutex);
1719 SvSHARE((SV*)(MY_CXT.handles));
1721 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1722 "mode, but your perl was not built with a supported "
1723 "thread model. Threads are disabled.\n");
1724 MY_CXT.threaded = 0;
1727 mountpoint = SvPV_nolen(ST(2));
1728 mountopts = SvPV_nolen(ST(3));
1729 #if FUSE_VERSION >= 28
1730 fops.flag_nullpath_ok = SvIV(ST(4));
1731 #endif /* FUSE_VERSION >= 28 */
1732 for(i=0;i<N_CALLBACKS;i++) {
1734 /* allow symbolic references, or real code references. */
1735 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1736 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1737 /* Dirty hack, to keep anything from overwriting the
1738 * flag area with a pointer. There should never be
1739 * anything passed as 'junk', but this prevents
1740 * someone from doing it and screwing things up... */
1744 MY_CXT.callback[i] = var;
1745 } else if(SvOK(var)) {
1746 croak("invalid callback (%i) passed to perl_fuse_main "
1747 "(%s is not a string, code ref, or undef).\n",
1748 i+5,SvPVbyte_nolen(var));
1750 MY_CXT.callback[i] = NULL;
1754 * XXX: What comes here is just a ridiculous use of the option parsing API
1755 * to hack on compatibility with other parts of the new API. First and
1756 * foremost, real C argc/argv would be good to get at...
1758 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1759 fuse_opt_free_args(&args);
1760 croak("out of memory\n");
1762 if (mountopts && strcmp("", mountopts) &&
1763 (fuse_opt_add_arg(&args, "-o") == -1 ||
1764 fuse_opt_add_arg(&args, mountopts) == -1)) {
1765 fuse_opt_free_args(&args);
1766 croak("out of memory\n");
1768 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1769 fuse_opt_free_args(&args);
1770 croak("out of memory\n");
1772 fc = fuse_mount(mountpoint,&args);
1774 croak("could not mount fuse filesystem!\n");
1776 if(MY_CXT.threaded) {
1777 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1780 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1781 fuse_unmount(mountpoint,fc);
1782 fuse_opt_free_args(&args);
1784 #if FUSE_VERSION >= 28
1787 pollhandle_destroy(...)
1789 struct fuse_pollhandle *ph;
1792 fprintf(stderr, "No pollhandle passed?\n");
1796 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1797 fuse_pollhandle_destroy(ph);
1802 struct fuse_pollhandle *ph;
1805 fprintf(stderr, "No pollhandle passed?\n");
1809 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1810 RETVAL = fuse_notify_poll(ph);