1 #define PERL_NO_GET_CONTEXT
8 #if defined(__FreeBSD__) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 /* Determine if threads support should be included */
18 # define FUSE_USE_ITHREADS
19 # if (PERL_VERSION < 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 40
37 /* # define N_CALLBACKS 41 */
38 #elif FUSE_VERSION >= 26
39 # define N_CALLBACKS 38
40 #elif FUSE_VERSION >= 25
41 # define N_CALLBACKS 35
42 #elif FUSE_VERSION >= 23
43 # define N_CALLBACKS 31
45 # define N_CALLBACKS 25
49 SV *callback[N_CALLBACKS];
61 #ifdef FUSE_USE_ITHREADS
62 tTHX master_interp = NULL;
64 #define CLONE_INTERP(parent) S_clone_interp(parent)
65 tTHX S_clone_interp(tTHX parent) {
66 # if (PERL_VERSION < 10)
67 tTHX my_perl = parent;
69 dMY_CXT_INTERP(parent);
71 MUTEX_LOCK(&MY_CXT.mutex);
72 PERL_SET_CONTEXT(parent);
74 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
75 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
77 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
78 ptr_table_free(PL_ptr_table);
81 MUTEX_UNLOCK(&MY_CXT.mutex);
87 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
88 # define FUSE_CONTEXT_POST }
90 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
91 # define FUSE_CONTEXT_POST
96 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
101 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
102 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
103 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
104 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
106 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
111 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
119 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
121 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
126 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
128 #ifdef FUSE_USE_ITHREADS
129 if(MY_CXT.threaded) {
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);
135 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
142 int _PLfuse_getattr(const char *file, struct stat *result) {
145 DEBUGf("getattr begin: %s\n",file);
149 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
151 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
155 fprintf(stderr,"inappropriate number of returned values from getattr\n");
162 result->st_blocks = POPi;
163 result->st_blksize = POPi;
164 result->st_ctime = POPi;
165 result->st_mtime = POPi;
166 result->st_atime = POPi;
167 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
168 result->st_rdev = POPi;
169 result->st_gid = POPi;
170 result->st_uid = POPi;
171 result->st_nlink = POPi;
172 result->st_mode = POPi;
173 result->st_ino = POPi;
174 result->st_dev = POPi;
180 DEBUGf("getattr end: %i\n",rv);
185 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
190 DEBUGf("readlink begin\n");
194 XPUSHs(sv_2mortal(newSVpv(file,0)));
196 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
202 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
205 strncpy(buf,SvPV_nolen(mysv),buflen);
213 DEBUGf("readlink end: %i\n",rv);
218 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
222 DEBUGf("getdir begin\n");
226 XPUSHs(sv_2mortal(newSVpv(file,0)));
228 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
231 /* Should yield the bottom of the current stack... */
232 swp = &TOPs - prv + 1;
234 /* Sort of a hack to walk the stack in order, instead of reverse
235 * order - trying to explain to potential users why they need to
236 * reverse the order of this array would be confusing, at best. */
238 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
240 fprintf(stderr,"getdir() handler returned nothing!\n");
246 DEBUGf("getdir end: %i\n",rv);
251 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
254 DEBUGf("mknod begin\n");
258 XPUSHs(sv_2mortal(newSVpv(file,0)));
259 XPUSHs(sv_2mortal(newSViv(mode)));
260 XPUSHs(sv_2mortal(newSViv(dev)));
262 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
264 rv = (rv ? POPi : 0);
268 DEBUGf("mknod end: %i\n",rv);
273 int _PLfuse_mkdir (const char *file, mode_t mode) {
276 DEBUGf("mkdir begin\n");
280 XPUSHs(sv_2mortal(newSVpv(file,0)));
281 XPUSHs(sv_2mortal(newSViv(mode)));
283 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
285 rv = (rv ? POPi : 0);
289 DEBUGf("mkdir end: %i\n",rv);
295 int _PLfuse_unlink (const char *file) {
298 DEBUGf("unlink begin\n");
302 XPUSHs(sv_2mortal(newSVpv(file,0)));
304 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
306 rv = (rv ? POPi : 0);
310 DEBUGf("unlink end: %i\n",rv);
315 int _PLfuse_rmdir (const char *file) {
318 DEBUGf("rmdir begin\n");
322 XPUSHs(sv_2mortal(newSVpv(file,0)));
324 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
326 rv = (rv ? POPi : 0);
330 DEBUGf("rmdir end: %i\n",rv);
335 int _PLfuse_symlink (const char *file, const char *new) {
338 DEBUGf("symlink begin\n");
342 XPUSHs(sv_2mortal(newSVpv(file,0)));
343 XPUSHs(sv_2mortal(newSVpv(new,0)));
345 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
347 rv = (rv ? POPi : 0);
351 DEBUGf("symlink end: %i\n",rv);
356 int _PLfuse_rename (const char *file, const char *new) {
359 DEBUGf("rename begin\n");
363 XPUSHs(sv_2mortal(newSVpv(file,0)));
364 XPUSHs(sv_2mortal(newSVpv(new,0)));
366 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
368 rv = (rv ? POPi : 0);
372 DEBUGf("rename end: %i\n",rv);
377 int _PLfuse_link (const char *file, const char *new) {
380 DEBUGf("link begin\n");
384 XPUSHs(sv_2mortal(newSVpv(file,0)));
385 XPUSHs(sv_2mortal(newSVpv(new,0)));
387 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
389 rv = (rv ? POPi : 0);
393 DEBUGf("link end: %i\n",rv);
398 int _PLfuse_chmod (const char *file, mode_t mode) {
401 DEBUGf("chmod begin\n");
405 XPUSHs(sv_2mortal(newSVpv(file,0)));
406 XPUSHs(sv_2mortal(newSViv(mode)));
408 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
410 rv = (rv ? POPi : 0);
414 DEBUGf("chmod end: %i\n",rv);
419 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
422 DEBUGf("chown begin\n");
426 XPUSHs(sv_2mortal(newSVpv(file,0)));
427 XPUSHs(sv_2mortal(newSViv(uid)));
428 XPUSHs(sv_2mortal(newSViv(gid)));
430 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
432 rv = (rv ? POPi : 0);
436 DEBUGf("chown end: %i\n",rv);
441 int _PLfuse_truncate (const char *file, off_t off) {
443 #ifndef PERL_HAS_64BITINT
447 DEBUGf("truncate begin\n");
451 XPUSHs(sv_2mortal(newSVpv(file,0)));
452 #ifdef PERL_HAS_64BITINT
453 XPUSHs(sv_2mortal(newSViv(off)));
455 if (asprintf(&temp, "%llu", off) == -1)
456 croak("Memory allocation failure!");
457 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
461 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
463 rv = (rv ? POPi : 0);
467 DEBUGf("truncate end: %i\n",rv);
472 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
475 DEBUGf("utime begin\n");
479 XPUSHs(sv_2mortal(newSVpv(file,0)));
480 XPUSHs(sv_2mortal(newSViv(uti->actime)));
481 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
483 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
485 rv = (rv ? POPi : 0);
489 DEBUGf("utime end: %i\n",rv);
494 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
496 int flags = fi->flags;
499 DEBUGf("open begin\n");
503 XPUSHs(sv_2mortal(newSVpv(file,0)));
504 XPUSHs(sv_2mortal(newSViv(flags)));
505 /* Create a hashref containing the details from fi
506 * which we can look at or modify.
508 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
510 #if FUSE_VERSION >= 24
511 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
512 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
514 #if FUSE_VERSION >= 29
515 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
517 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
518 /* All hashref things done */
521 /* Open called with filename, flags */
522 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
526 FH_STOREHANDLE(fi,POPs);
534 /* Success, so copy the file handle which they returned */
535 #if FUSE_VERSION >= 24
537 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
538 fi->direct_io = SvIV(*svp);
539 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
540 fi->keep_cache = SvIV(*svp);
542 #if FUSE_VERSION >= 29
543 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
544 fi->nonseekable = SvIV(*svp);
550 DEBUGf("open end: %i\n",rv);
555 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
556 struct fuse_file_info *fi) {
558 #ifndef PERL_HAS_64BITINT
562 DEBUGf("read begin\n");
566 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
567 XPUSHs(sv_2mortal(newSViv(buflen)));
568 #ifdef PERL_HAS_64BITINT
569 XPUSHs(sv_2mortal(newSViv(off)));
571 if (asprintf(&temp, "%llu", off) == -1)
572 croak("Memory allocation failure!");
573 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
576 XPUSHs(FH_GETHANDLE(fi));
578 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
584 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
593 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
595 memcpy(buf,SvPV_nolen(mysv),rv);
601 DEBUGf("read end: %i\n",rv);
606 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
608 #ifndef PERL_HAS_64BITINT
612 DEBUGf("write begin\n");
616 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
617 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
618 #ifdef PERL_HAS_64BITINT
619 XPUSHs(sv_2mortal(newSViv(off)));
621 if (asprintf(&temp, "%llu", off) == -1)
622 croak("Memory allocation failure!");
623 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
626 XPUSHs(FH_GETHANDLE(fi));
628 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
630 rv = (rv ? POPi : 0);
634 DEBUGf("write end: %i\n",rv);
639 int _PLfuse_statfs (const char *file, struct statvfs *st) {
642 DEBUGf("statfs begin\n");
647 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
649 DEBUGf("statfs got %i params\n",rv);
650 if(rv == 6 || rv == 7) {
656 st->f_namemax = POPi;
657 /* zero and fill-in other */
661 st->f_bavail = st->f_bfree;
662 st->f_favail = st->f_ffree;
670 croak("inappropriate number of returned values from statfs");
679 DEBUGf("statfs end: %i\n",rv);
684 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
687 DEBUGf("flush begin\n");
691 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
692 XPUSHs(FH_GETHANDLE(fi));
694 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
696 rv = (rv ? POPi : 0);
700 DEBUGf("flush end: %i\n",rv);
705 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
707 int flags = fi->flags;
709 DEBUGf("release begin\n");
713 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
714 XPUSHs(sv_2mortal(newSViv(flags)));
715 XPUSHs(FH_GETHANDLE(fi));
717 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
719 rv = (rv ? POPi : 0);
720 FH_RELEASEHANDLE(fi);
724 DEBUGf("release end: %i\n",rv);
729 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
731 int flags = fi->flags;
733 DEBUGf("fsync begin\n");
737 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
738 XPUSHs(sv_2mortal(newSViv(flags)));
739 XPUSHs(FH_GETHANDLE(fi));
741 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
743 rv = (rv ? POPi : 0);
747 DEBUGf("fsync end: %i\n",rv);
752 #if __FreeBSD__ >= 10
753 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
755 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
759 DEBUGf("setxattr begin\n");
763 XPUSHs(sv_2mortal(newSVpv(file,0)));
764 XPUSHs(sv_2mortal(newSVpv(name,0)));
765 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
766 XPUSHs(sv_2mortal(newSViv(flags)));
768 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
770 rv = (rv ? POPi : 0);
774 DEBUGf("setxattr end: %i\n",rv);
779 #if __FreeBSD__ >= 10
780 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
782 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
786 DEBUGf("getxattr begin\n");
790 XPUSHs(sv_2mortal(newSVpv(file,0)));
791 XPUSHs(sv_2mortal(newSVpv(name,0)));
793 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
801 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
809 if ((rv > 0) && (buflen > 0))
814 memcpy(buf,SvPV_nolen(mysv),rv);
821 DEBUGf("getxattr end: %i\n",rv);
826 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
829 DEBUGf("listxattr begin\n");
833 XPUSHs(sv_2mortal(newSVpv(file,0)));
835 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
848 /* Always nul terminate */
849 if (list && (size > 0))
859 int s = SvCUR(mysv) + 1;
862 if (p && (size > 0) && (spc >= s))
864 memcpy(p,SvPV_nolen(mysv),s);
872 * If the Perl returned an error, return that.
873 * Otherwise check that the buffer was big enough.
878 if ((size > 0) && (size < total_len))
885 DEBUGf("listxattr end: %i\n",rv);
890 int _PLfuse_removexattr (const char *file, const char *name) {
893 DEBUGf("removexattr begin\n");
897 XPUSHs(sv_2mortal(newSVpv(file,0)));
898 XPUSHs(sv_2mortal(newSVpv(name,0)));
900 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
902 rv = (rv ? POPi : 0);
906 DEBUGf("removexattr end: %i\n",rv);
911 #if FUSE_VERSION >= 23
912 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
915 DEBUGf("opendir begin\n");
919 XPUSHs(sv_2mortal(newSVpv(file,0)));
920 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
922 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
926 FH_STOREHANDLE(fi, POPs);
934 DEBUGf("opendir end: %i\n",rv);
940 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
941 off_t off, struct fuse_file_info *fi) {
943 SV *sv, **svp, **swp;
947 #ifndef PERL_HAS_64BITINT
951 DEBUGf("readdir begin\n");
955 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
956 #ifdef PERL_HAS_64BITINT
957 XPUSHs(sv_2mortal(newSViv(off)));
959 if (asprintf(&temp, "%llu", off) == -1)
960 croak("Memory allocation failure!");
961 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
964 XPUSHs(FH_GETHANDLE(fi));
966 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
969 /* Should yield the bottom of the current stack... */
970 swp = &TOPs - prv + 1;
972 memset(&st, 0, sizeof(struct stat));
973 /* Sort of a hack to walk the stack in order, instead of reverse
974 * order - trying to explain to potential users why they need to
975 * reverse the order of this array would be confusing, at best. */
976 while (swp <= &TOPs) {
978 if (!SvROK(sv) && SvPOK(sv))
979 /* Just a bare SV (probably a string; hopefully a string) */
980 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
981 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
982 if (av_len(av) >= 2) {
983 /* The third element of the array should be the args that
984 * would otherwise go to getattr(); a lot of filesystems
985 * will, or at least can, return that info as part of the
986 * enumeration process... */
987 svp = av_fetch(av, 2, FALSE);
989 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
991 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
992 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
993 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
994 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
995 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
996 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
997 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
998 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
999 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
1000 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
1001 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
1002 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1003 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1007 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1008 /* For now if the element isn't what we want, just
1009 * quietly ignore it... */
1011 if (av_len(av) >= 1) {
1012 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1013 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1014 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1017 memset(&st, 0, sizeof(struct stat));
1022 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1025 fprintf(stderr,"readdir() handler returned nothing!\n");
1031 DEBUGf("readdir end: %i\n",rv);
1036 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1039 DEBUGf("releasedir begin\n");
1043 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1044 XPUSHs(FH_GETHANDLE(fi));
1046 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1048 rv = (rv ? POPi : 0);
1049 FH_RELEASEHANDLE(fi);
1053 DEBUGf("releasedir end: %i\n",rv);
1058 int _PLfuse_fsyncdir(const char *file, int datasync,
1059 struct fuse_file_info *fi) {
1062 DEBUGf("fsyncdir begin\n");
1066 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1067 XPUSHs(sv_2mortal(newSViv(datasync)));
1068 XPUSHs(FH_GETHANDLE(fi));
1070 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1072 rv = (rv ? POPi : 0);
1076 DEBUGf("fsyncdir end: %i\n",rv);
1081 #if FUSE_VERSION >= 26
1082 void *_PLfuse_init(struct fuse_conn_info *fc)
1083 #else /* FUSE_VERSION < 26 */
1084 void *_PLfuse_init(void)
1085 #endif /* FUSE_VERSION >= 26 */
1090 DEBUGf("init begin\n");
1095 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1099 if (rv == &PL_sv_undef)
1102 rv = SvREFCNT_inc((SV *)rv);
1107 DEBUGf("init end: %p\n", rv);
1112 void _PLfuse_destroy(void *private_data) {
1114 DEBUGf("destroy begin\n");
1118 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1120 call_sv(MY_CXT.callback[30], G_VOID);
1123 SvREFCNT_dec((SV *)private_data);
1127 DEBUGf("init end\n");
1130 #endif /* FUSE_VERSION >= 23 */
1132 #if FUSE_VERSION >= 25
1133 int _PLfuse_access(const char *file, int mask) {
1136 DEBUGf("access begin\n");
1140 XPUSHs(sv_2mortal(newSVpv(file,0)));
1141 XPUSHs(sv_2mortal(newSViv(mask)));
1143 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1145 rv = (rv ? POPi : 0);
1149 DEBUGf("access end: %d\n", rv);
1154 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1158 DEBUGf("create begin\n");
1162 XPUSHs(sv_2mortal(newSVpv(file,0)));
1163 XPUSHs(sv_2mortal(newSViv(mode)));
1164 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1165 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1166 /* Create a hashref containing the details from fi
1167 * which we can look at or modify.
1170 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1171 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1172 #if FUSE_VERSION >= 29
1173 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1175 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1176 /* All hashref things done */
1179 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1183 FH_STOREHANDLE(fi,POPs);
1188 fprintf(stderr, "create() handler returned nothing!\n");
1192 /* Success, so copy the file handle which they returned */
1194 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1195 fi->direct_io = SvIV(*svp);
1196 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1197 fi->keep_cache = SvIV(*svp);
1198 #if FUSE_VERSION >= 29
1199 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1200 fi->nonseekable = SvIV(*svp);
1206 DEBUGf("create end: %d\n",rv);
1211 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1213 #ifndef PERL_HAS_64BITINT
1217 DEBUGf("ftruncate begin\n");
1221 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1222 #ifdef PERL_HAS_64BITINT
1223 XPUSHs(sv_2mortal(newSViv(off)));
1225 if (asprintf(&temp, "%llu", off) == -1)
1226 croak("Memory allocation failure!");
1227 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1230 XPUSHs(FH_GETHANDLE(fi));
1232 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1234 rv = (rv ? POPi : 0);
1238 DEBUGf("ftruncate end: %i\n",rv);
1243 int _PLfuse_fgetattr(const char *file, struct stat *result,
1244 struct fuse_file_info *fi) {
1247 DEBUGf("fgetattr begin: %s\n",file);
1251 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1252 XPUSHs(FH_GETHANDLE(fi));
1254 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1258 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1265 result->st_blocks = POPi;
1266 result->st_blksize = POPi;
1267 result->st_ctime = POPi;
1268 result->st_mtime = POPi;
1269 result->st_atime = POPi;
1270 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1271 result->st_rdev = POPi;
1272 result->st_gid = POPi;
1273 result->st_uid = POPi;
1274 result->st_nlink = POPi;
1275 result->st_mode = POPi;
1276 result->st_ino = POPi;
1277 result->st_dev = POPi;
1283 DEBUGf("fgetattr end: %i\n",rv);
1287 #endif /* FUSE_VERSION >= 25 */
1289 #if FUSE_VERSION >= 26
1290 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1291 struct flock *lockinfo) {
1295 #ifndef PERL_HAS_64BITINT
1299 DEBUGf("lock begin\n");
1303 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1304 XPUSHs(sv_2mortal(newSViv(cmd)));
1307 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1308 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1309 #ifdef PERL_HAS_64BITINT
1310 sv = newSViv(lockinfo->l_start);
1312 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1313 croak("Memory allocation failure!");
1314 sv = newSVpv(temp, 0);
1317 (void) hv_store(lihash, "l_start", 7, sv, 0);
1318 #ifdef PERL_HAS_64BITINT
1319 sv = newSViv(lockinfo->l_len);
1321 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1322 croak("Memory allocation failure!");
1323 sv = newSVpv(temp, 0);
1326 (void) hv_store(lihash, "l_len", 5, sv, 0);
1327 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1329 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1330 XPUSHs(FH_GETHANDLE(fi));
1333 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1335 rv = (rv ? POPi : 0);
1336 if (lockinfo && !rv) {
1337 /* Need to copy back any altered values from the hash into
1340 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1341 lockinfo->l_type = SvIV(*svp);
1342 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1343 lockinfo->l_whence = SvIV(*svp);
1344 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1345 lockinfo->l_start = SvNV(*svp);
1346 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1347 lockinfo->l_len = SvNV(*svp);
1348 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1349 lockinfo->l_pid = SvIV(*svp);
1354 DEBUGf("lock end: %i\n",rv);
1359 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1362 DEBUGf("utimens begin\n");
1366 XPUSHs(sv_2mortal(newSVpv(file,0)));
1367 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1368 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1370 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1372 rv = (rv ? POPi : 0);
1376 DEBUGf("utimens end: %i\n",rv);
1381 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1383 #ifndef PERL_HAS_64BITINT
1387 DEBUGf("bmap begin\n");
1391 XPUSHs(sv_2mortal(newSVpv(file,0)));
1392 XPUSHs(sv_2mortal(newSViv(blocksize)));
1393 #ifdef PERL_HAS_64BITINT
1394 XPUSHs(sv_2mortal(newSViv(*idx)));
1396 if (asprintf(&temp, "%llu", *idx) == -1)
1397 croak("Memory allocation failure!");
1398 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1402 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1404 if (rv > 0 && rv < 3) {
1410 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1416 DEBUGf("bmap end: %i\n",rv);
1420 #endif /* FUSE_VERSION >= 26 */
1422 #if FUSE_VERSION >= 28
1423 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1424 struct fuse_file_info *fi, unsigned int flags, void *data) {
1427 DEBUGf("ioctl begin\n");
1431 XPUSHs(sv_2mortal(newSVpv(file,0)));
1432 XPUSHs(sv_2mortal(newSViv(cmd)));
1433 XPUSHs(sv_2mortal(newSViv((uintptr_t)arg)));
1434 XPUSHs(sv_2mortal(newSViv(flags)));
1435 if (_IOC_DIR(cmd) & _IOC_READ)
1436 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1438 XPUSHs(&PL_sv_undef);
1439 XPUSHs(FH_GETHANDLE(fi));
1441 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1443 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1447 char *rdata = SvPV(sv, len);
1450 if (len > _IOC_SIZE(cmd)) {
1451 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1455 memset(data, 0, _IOC_SIZE(cmd));
1456 memcpy(data, rdata, len);
1460 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1469 DEBUGf("ioctl end: %i\n",rv);
1475 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1476 struct fuse_pollhandle *ph, unsigned *reventsp) {
1480 #endif /* FUSE_VERSION >= 28 */
1482 struct fuse_operations _available_ops = {
1483 getattr: _PLfuse_getattr,
1484 readlink: _PLfuse_readlink,
1485 getdir: _PLfuse_getdir,
1486 mknod: _PLfuse_mknod,
1487 mkdir: _PLfuse_mkdir,
1488 unlink: _PLfuse_unlink,
1489 rmdir: _PLfuse_rmdir,
1490 symlink: _PLfuse_symlink,
1491 rename: _PLfuse_rename,
1493 chmod: _PLfuse_chmod,
1494 chown: _PLfuse_chown,
1495 truncate: _PLfuse_truncate,
1496 utime: _PLfuse_utime,
1499 write: _PLfuse_write,
1500 statfs: _PLfuse_statfs,
1501 flush: _PLfuse_flush,
1502 release: _PLfuse_release,
1503 fsync: _PLfuse_fsync,
1504 setxattr: _PLfuse_setxattr,
1505 getxattr: _PLfuse_getxattr,
1506 listxattr: _PLfuse_listxattr,
1507 removexattr: _PLfuse_removexattr,
1508 #if FUSE_VERSION >= 23
1509 opendir: _PLfuse_opendir,
1510 readdir: _PLfuse_readdir,
1511 releasedir: _PLfuse_releasedir,
1512 fsyncdir: _PLfuse_fsyncdir,
1514 destroy: _PLfuse_destroy,
1515 #endif /* FUSE_VERSION >= 23 */
1516 #if FUSE_VERSION >= 25
1517 access: _PLfuse_access,
1518 create: _PLfuse_create,
1519 ftruncate: _PLfuse_ftruncate,
1520 fgetattr: _PLfuse_fgetattr,
1521 #endif /* FUSE_VERSION >= 25 */
1522 #if FUSE_VERSION >= 26
1524 utimens: _PLfuse_utimens,
1526 #endif /* FUSE_VERSION >= 26 */
1527 #if FUSE_VERSION >= 28
1528 ioctl: _PLfuse_ioctl,
1532 #endif /* FUSE_VERSION >= 28 */
1535 MODULE = Fuse PACKAGE = Fuse
1554 tTHX parent = MY_CXT.self;
1555 MY_CXT.self = my_perl;
1556 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1557 /* CLONE entered without a pointer table, so we can't safely clone static data */
1559 for(i=0;i<N_CALLBACKS;i++) {
1560 MY_CXT.callback[i] = NULL;
1562 MY_CXT.handles = newHV();
1566 CLONE_PARAMS *clone_param;
1567 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1568 clone_param = Perl_clone_params_new(parent, aTHX);
1570 CLONE_PARAMS raw_param;
1571 raw_param.flags = 0;
1572 raw_param.proto_perl = parent;
1573 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1574 clone_param = &raw_param;
1576 for(i=0;i<N_CALLBACKS;i++) {
1577 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1579 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1580 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1581 Perl_clone_params_del(clone_param);
1589 struct fuse_context *fc;
1591 fc = fuse_get_context();
1594 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1595 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1596 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1597 if (fc->private_data)
1598 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1599 #if FUSE_VERSION >= 28
1600 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1601 #endif /* FUSE_VERSION >= 28 */
1602 RETVAL = newRV_noinc((SV*)hash);
1612 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1620 RETVAL = newSViv(XATTR_CREATE);
1627 RETVAL = newSViv(XATTR_REPLACE);
1636 struct fuse_operations fops;
1640 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1641 struct fuse_chan *fc;
1644 if(items != N_CALLBACKS + 5) {
1645 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1648 memset(&fops, 0, sizeof(struct fuse_operations));
1650 debug = SvIV(ST(0));
1651 MY_CXT.threaded = SvIV(ST(1));
1652 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1653 if(MY_CXT.threaded) {
1654 #ifdef FUSE_USE_ITHREADS
1655 master_interp = aTHX;
1656 MUTEX_INIT(&MY_CXT.mutex);
1657 SvSHARE((SV*)(MY_CXT.handles));
1659 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1660 "mode, but your perl was not built with a supported "
1661 "thread model. Threads are disabled.\n");
1662 MY_CXT.threaded = 0;
1665 mountpoint = SvPV_nolen(ST(2));
1666 mountopts = SvPV_nolen(ST(3));
1667 #if FUSE_VERSION >= 28
1668 fops.flag_nullpath_ok = SvIV(ST(4));
1669 #endif /* FUSE_VERSION >= 28 */
1670 for(i=0;i<N_CALLBACKS;i++) {
1672 /* allow symbolic references, or real code references. */
1673 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1674 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1675 /* Dirty hack, to keep anything from overwriting the
1676 * flag area with a pointer. There should never be
1677 * anything passed as 'junk', but this prevents
1678 * someone from doing it and screwing things up... */
1682 MY_CXT.callback[i] = var;
1683 } else if(SvOK(var)) {
1684 croak("invalid callback (%i) passed to perl_fuse_main "
1685 "(%s is not a string, code ref, or undef).\n",
1686 i+5,SvPVbyte_nolen(var));
1688 MY_CXT.callback[i] = NULL;
1692 * XXX: What comes here is just a ridiculous use of the option parsing API
1693 * to hack on compatibility with other parts of the new API. First and
1694 * foremost, real C argc/argv would be good to get at...
1696 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1697 fuse_opt_free_args(&args);
1698 croak("out of memory\n");
1700 if (mountopts && strcmp("", mountopts) &&
1701 (fuse_opt_add_arg(&args, "-o") == -1 ||
1702 fuse_opt_add_arg(&args, mountopts) == -1)) {
1703 fuse_opt_free_args(&args);
1704 croak("out of memory\n");
1706 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1707 fuse_opt_free_args(&args);
1708 croak("out of memory\n");
1710 fc = fuse_mount(mountpoint,&args);
1712 croak("could not mount fuse filesystem!\n");
1714 if(MY_CXT.threaded) {
1715 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1718 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1719 fuse_unmount(mountpoint,fc);
1720 fuse_opt_free_args(&args);