1 #define PERL_NO_GET_CONTEXT
7 * XXX: Fuse on FreeBSD does not support extended attributes (see
8 * /usr/local/share/doc/fusefs/kmod/doc.text). Also, 'extattr_set_file' syscall
9 * declared in 'sys/extattr.h' does not accept any flags. We could either skip
10 * defining XATTR_CREATE and XATTR_REPLACE constants (as further below, and
11 * which breaks their usage if Perl module exports them), set them to some
12 * dummy values, or not allow them to be exported at all on FreeBSD.
15 # include <sys/xattr.h>
19 /* Determine if threads support should be included */
22 # define FUSE_USE_ITHREADS
24 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
30 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
31 /* #if FUSE_VERSION >= 28
32 # define N_CALLBACKS 41 */
33 #if FUSE_VERSION >= 26
34 # define N_CALLBACKS 38
35 #elif FUSE_VERSION >= 25
36 # define N_CALLBACKS 35
37 #elif FUSE_VERSION >= 23
38 # define N_CALLBACKS 31
40 # define N_CALLBACKS 25
44 SV *callback[N_CALLBACKS];
52 #ifdef FUSE_USE_ITHREADS
53 tTHX master_interp = NULL;
55 #define CLONE_INTERP(parent) S_clone_interp(parent)
56 tTHX S_clone_interp(tTHX parent) {
57 dMY_CXT_INTERP(parent);
59 MUTEX_LOCK(&MY_CXT.mutex);
60 PERL_SET_CONTEXT(parent);
62 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
63 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
65 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
66 ptr_table_free(PL_ptr_table);
69 MUTEX_UNLOCK(&MY_CXT.mutex);
75 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
76 # define FUSE_CONTEXT_POST }
78 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
79 # define FUSE_CONTEXT_POST
84 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
89 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
90 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
91 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
92 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
94 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
99 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
107 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
109 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
114 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
116 #ifdef FUSE_USE_ITHREADS
117 if(MY_CXT.threaded) {
121 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
122 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
123 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
130 int _PLfuse_getattr(const char *file, struct stat *result) {
133 DEBUGf("getattr begin: %s\n",file);
137 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
139 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
143 fprintf(stderr,"inappropriate number of returned values from getattr\n");
150 result->st_blocks = POPi;
151 result->st_blksize = POPi;
152 result->st_ctime = POPi;
153 result->st_mtime = POPi;
154 result->st_atime = POPi;
155 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
156 result->st_rdev = POPi;
157 result->st_gid = POPi;
158 result->st_uid = POPi;
159 result->st_nlink = POPi;
160 result->st_mode = POPi;
161 result->st_ino = POPi;
162 result->st_dev = POPi;
168 DEBUGf("getattr end: %i\n",rv);
173 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
178 DEBUGf("readlink begin\n");
182 XPUSHs(sv_2mortal(newSVpv(file,0)));
184 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
190 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
193 strncpy(buf,SvPV_nolen(mysv),buflen);
201 DEBUGf("readlink end: %i\n",rv);
206 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
210 DEBUGf("getdir begin\n");
214 XPUSHs(sv_2mortal(newSVpv(file,0)));
216 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
219 /* Should yield the bottom of the current stack... */
220 swp = &TOPs - prv + 1;
222 /* Sort of a hack to walk the stack in order, instead of reverse
223 * order - trying to explain to potential users why they need to
224 * reverse the order of this array would be confusing, at best. */
226 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
228 fprintf(stderr,"getdir() handler returned nothing!\n");
234 DEBUGf("getdir end: %i\n",rv);
239 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
242 DEBUGf("mknod begin\n");
246 XPUSHs(sv_2mortal(newSVpv(file,0)));
247 XPUSHs(sv_2mortal(newSViv(mode)));
248 XPUSHs(sv_2mortal(newSViv(dev)));
250 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
252 rv = (rv ? POPi : 0);
256 DEBUGf("mknod end: %i\n",rv);
261 int _PLfuse_mkdir (const char *file, mode_t mode) {
264 DEBUGf("mkdir begin\n");
268 XPUSHs(sv_2mortal(newSVpv(file,0)));
269 XPUSHs(sv_2mortal(newSViv(mode)));
271 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
273 rv = (rv ? POPi : 0);
277 DEBUGf("mkdir end: %i\n",rv);
283 int _PLfuse_unlink (const char *file) {
286 DEBUGf("unlink begin\n");
290 XPUSHs(sv_2mortal(newSVpv(file,0)));
292 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
294 rv = (rv ? POPi : 0);
298 DEBUGf("unlink end: %i\n",rv);
303 int _PLfuse_rmdir (const char *file) {
306 DEBUGf("rmdir begin\n");
310 XPUSHs(sv_2mortal(newSVpv(file,0)));
312 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
314 rv = (rv ? POPi : 0);
318 DEBUGf("rmdir end: %i\n",rv);
323 int _PLfuse_symlink (const char *file, const char *new) {
326 DEBUGf("symlink begin\n");
330 XPUSHs(sv_2mortal(newSVpv(file,0)));
331 XPUSHs(sv_2mortal(newSVpv(new,0)));
333 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
335 rv = (rv ? POPi : 0);
339 DEBUGf("symlink end: %i\n",rv);
344 int _PLfuse_rename (const char *file, const char *new) {
347 DEBUGf("rename begin\n");
351 XPUSHs(sv_2mortal(newSVpv(file,0)));
352 XPUSHs(sv_2mortal(newSVpv(new,0)));
354 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
356 rv = (rv ? POPi : 0);
360 DEBUGf("rename end: %i\n",rv);
365 int _PLfuse_link (const char *file, const char *new) {
368 DEBUGf("link begin\n");
372 XPUSHs(sv_2mortal(newSVpv(file,0)));
373 XPUSHs(sv_2mortal(newSVpv(new,0)));
375 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
377 rv = (rv ? POPi : 0);
381 DEBUGf("link end: %i\n",rv);
386 int _PLfuse_chmod (const char *file, mode_t mode) {
389 DEBUGf("chmod begin\n");
393 XPUSHs(sv_2mortal(newSVpv(file,0)));
394 XPUSHs(sv_2mortal(newSViv(mode)));
396 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
398 rv = (rv ? POPi : 0);
402 DEBUGf("chmod end: %i\n",rv);
407 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
410 DEBUGf("chown begin\n");
414 XPUSHs(sv_2mortal(newSVpv(file,0)));
415 XPUSHs(sv_2mortal(newSViv(uid)));
416 XPUSHs(sv_2mortal(newSViv(gid)));
418 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
420 rv = (rv ? POPi : 0);
424 DEBUGf("chown end: %i\n",rv);
429 int _PLfuse_truncate (const char *file, off_t off) {
431 #ifndef PERL_HAS_64BITINT
435 DEBUGf("truncate begin\n");
439 XPUSHs(sv_2mortal(newSVpv(file,0)));
440 #ifdef PERL_HAS_64BITINT
441 XPUSHs(sv_2mortal(newSViv(off)));
443 if (asprintf(&temp, "%llu", off) == -1)
444 croak("Memory allocation failure!");
445 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
449 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
451 rv = (rv ? POPi : 0);
455 DEBUGf("truncate end: %i\n",rv);
460 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
463 DEBUGf("utime begin\n");
467 XPUSHs(sv_2mortal(newSVpv(file,0)));
468 XPUSHs(sv_2mortal(newSViv(uti->actime)));
469 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
471 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
473 rv = (rv ? POPi : 0);
477 DEBUGf("utime end: %i\n",rv);
482 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
484 int flags = fi->flags;
487 DEBUGf("open begin\n");
491 XPUSHs(sv_2mortal(newSVpv(file,0)));
492 XPUSHs(sv_2mortal(newSViv(flags)));
493 /* Create a hashref containing the details from fi
494 * which we can look at or modify.
496 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
498 #if FUSE_VERSION >= 24
499 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
500 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
502 #if FUSE_VERSION >= 29
503 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
505 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
506 /* All hashref things done */
509 /* Open called with filename, flags */
510 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
514 FH_STOREHANDLE(fi,POPs);
522 /* Success, so copy the file handle which they returned */
523 #if FUSE_VERSION >= 24
525 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
526 fi->direct_io = SvIV(*svp);
527 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
528 fi->keep_cache = SvIV(*svp);
530 #if FUSE_VERSION >= 29
531 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
532 fi->nonseekable = SvIV(*svp);
538 DEBUGf("open end: %i\n",rv);
543 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
544 struct fuse_file_info *fi) {
546 #ifndef PERL_HAS_64BITINT
550 DEBUGf("read begin\n");
554 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
555 XPUSHs(sv_2mortal(newSViv(buflen)));
556 #ifdef PERL_HAS_64BITINT
557 XPUSHs(sv_2mortal(newSViv(off)));
559 if (asprintf(&temp, "%llu", off) == -1)
560 croak("Memory allocation failure!");
561 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
564 XPUSHs(FH_GETHANDLE(fi));
566 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
572 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
581 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
583 memcpy(buf,SvPV_nolen(mysv),rv);
589 DEBUGf("read end: %i\n",rv);
594 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
596 #ifndef PERL_HAS_64BITINT
600 DEBUGf("write begin\n");
604 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
605 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
606 #ifdef PERL_HAS_64BITINT
607 XPUSHs(sv_2mortal(newSViv(off)));
609 if (asprintf(&temp, "%llu", off) == -1)
610 croak("Memory allocation failure!");
611 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
614 XPUSHs(FH_GETHANDLE(fi));
616 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
618 rv = (rv ? POPi : 0);
622 DEBUGf("write end: %i\n",rv);
627 int _PLfuse_statfs (const char *file, struct statvfs *st) {
630 DEBUGf("statfs begin\n");
635 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
637 DEBUGf("statfs got %i params\n",rv);
638 if(rv == 6 || rv == 7) {
644 st->f_namemax = POPi;
645 /* zero and fill-in other */
649 st->f_bavail = st->f_bfree;
650 st->f_favail = st->f_ffree;
658 croak("inappropriate number of returned values from statfs");
667 DEBUGf("statfs end: %i\n",rv);
672 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
675 DEBUGf("flush begin\n");
679 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
680 XPUSHs(FH_GETHANDLE(fi));
682 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
684 rv = (rv ? POPi : 0);
688 DEBUGf("flush end: %i\n",rv);
693 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
695 int flags = fi->flags;
697 DEBUGf("release begin\n");
701 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
702 XPUSHs(sv_2mortal(newSViv(flags)));
703 XPUSHs(FH_GETHANDLE(fi));
705 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
707 rv = (rv ? POPi : 0);
708 FH_RELEASEHANDLE(fi);
712 DEBUGf("release end: %i\n",rv);
717 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
719 int flags = fi->flags;
721 DEBUGf("fsync begin\n");
725 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
726 XPUSHs(sv_2mortal(newSViv(flags)));
727 XPUSHs(FH_GETHANDLE(fi));
729 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
731 rv = (rv ? POPi : 0);
735 DEBUGf("fsync end: %i\n",rv);
740 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
743 DEBUGf("setxattr begin\n");
747 XPUSHs(sv_2mortal(newSVpv(file,0)));
748 XPUSHs(sv_2mortal(newSVpv(name,0)));
749 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
750 XPUSHs(sv_2mortal(newSViv(flags)));
752 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
754 rv = (rv ? POPi : 0);
758 DEBUGf("setxattr end: %i\n",rv);
763 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
766 DEBUGf("getxattr begin\n");
770 XPUSHs(sv_2mortal(newSVpv(file,0)));
771 XPUSHs(sv_2mortal(newSVpv(name,0)));
773 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
781 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
789 if ((rv > 0) && (buflen > 0))
794 memcpy(buf,SvPV_nolen(mysv),rv);
801 DEBUGf("getxattr end: %i\n",rv);
806 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
809 DEBUGf("listxattr begin\n");
813 XPUSHs(sv_2mortal(newSVpv(file,0)));
815 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
828 /* Always nul terminate */
829 if (list && (size > 0))
839 int s = SvCUR(mysv) + 1;
842 if (p && (size > 0) && (spc >= s))
844 memcpy(p,SvPV_nolen(mysv),s);
852 * If the Perl returned an error, return that.
853 * Otherwise check that the buffer was big enough.
858 if ((size > 0) && (size < total_len))
865 DEBUGf("listxattr end: %i\n",rv);
870 int _PLfuse_removexattr (const char *file, const char *name) {
873 DEBUGf("removexattr begin\n");
877 XPUSHs(sv_2mortal(newSVpv(file,0)));
878 XPUSHs(sv_2mortal(newSVpv(name,0)));
880 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
882 rv = (rv ? POPi : 0);
886 DEBUGf("removexattr end: %i\n",rv);
891 #if FUSE_VERSION >= 23
892 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
895 DEBUGf("opendir begin\n");
899 XPUSHs(sv_2mortal(newSVpv(file,0)));
900 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
902 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
906 FH_STOREHANDLE(fi, POPs);
914 DEBUGf("opendir end: %i\n",rv);
920 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
921 off_t off, struct fuse_file_info *fi) {
923 SV *sv, **svp, **swp;
927 #ifndef PERL_HAS_64BITINT
931 DEBUGf("readdir begin\n");
935 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
936 #ifdef PERL_HAS_64BITINT
937 XPUSHs(sv_2mortal(newSViv(off)));
939 if (asprintf(&temp, "%llu", off) == -1)
940 croak("Memory allocation failure!");
941 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
944 XPUSHs(FH_GETHANDLE(fi));
946 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
949 /* Should yield the bottom of the current stack... */
950 swp = &TOPs - prv + 1;
952 memset(&st, 0, sizeof(struct stat));
953 /* Sort of a hack to walk the stack in order, instead of reverse
954 * order - trying to explain to potential users why they need to
955 * reverse the order of this array would be confusing, at best. */
956 while (swp <= &TOPs) {
958 if (!SvROK(sv) && SvPOK(sv))
959 /* Just a bare SV (probably a string; hopefully a string) */
960 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
961 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
962 if (av_len(av) >= 2) {
963 /* The third element of the array should be the args that
964 * would otherwise go to getattr(); a lot of filesystems
965 * will, or at least can, return that info as part of the
966 * enumeration process... */
967 svp = av_fetch(av, 2, FALSE);
969 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
971 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
972 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
973 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
974 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
975 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
976 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
977 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
978 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
979 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
980 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
981 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
982 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
983 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
987 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
988 /* For now if the element isn't what we want, just
989 * quietly ignore it... */
991 if (av_len(av) >= 1) {
992 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
993 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
994 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
997 memset(&st, 0, sizeof(struct stat));
1002 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1005 fprintf(stderr,"readdir() handler returned nothing!\n");
1011 DEBUGf("readdir end: %i\n",rv);
1016 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1019 DEBUGf("releasedir begin\n");
1023 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1024 XPUSHs(FH_GETHANDLE(fi));
1026 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1028 rv = (rv ? POPi : 0);
1029 FH_RELEASEHANDLE(fi);
1033 DEBUGf("releasedir end: %i\n",rv);
1038 int _PLfuse_fsyncdir(const char *file, int datasync,
1039 struct fuse_file_info *fi) {
1042 DEBUGf("fsyncdir begin\n");
1046 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1047 XPUSHs(sv_2mortal(newSViv(datasync)));
1048 XPUSHs(FH_GETHANDLE(fi));
1050 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1052 rv = (rv ? POPi : 0);
1056 DEBUGf("fsyncdir end: %i\n",rv);
1061 #if FUSE_VERSION >= 26
1062 void *_PLfuse_init(struct fuse_conn_info *fc)
1063 #else /* FUSE_VERSION < 26 */
1064 void *_PLfuse_init(void)
1065 #endif /* FUSE_VERSION >= 26 */
1070 DEBUGf("init begin\n");
1075 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1079 if (rv == &PL_sv_undef)
1082 rv = SvREFCNT_inc((SV *)rv);
1087 DEBUGf("init end: %p\n", rv);
1092 void _PLfuse_destroy(void *private_data) {
1094 DEBUGf("destroy begin\n");
1098 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1100 call_sv(MY_CXT.callback[30], G_VOID);
1103 SvREFCNT_dec((SV *)private_data);
1107 DEBUGf("init end\n");
1110 #endif /* FUSE_VERSION >= 23 */
1112 #if FUSE_VERSION >= 25
1113 int _PLfuse_access(const char *file, int mask) {
1116 DEBUGf("access begin\n");
1120 XPUSHs(sv_2mortal(newSVpv(file,0)));
1121 XPUSHs(sv_2mortal(newSViv(mask)));
1123 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1125 rv = (rv ? POPi : 0);
1129 DEBUGf("access end: %d\n", rv);
1134 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1138 DEBUGf("create begin\n");
1142 XPUSHs(sv_2mortal(newSVpv(file,0)));
1143 XPUSHs(sv_2mortal(newSViv(mode)));
1144 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1145 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1146 /* Create a hashref containing the details from fi
1147 * which we can look at or modify.
1150 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1151 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1152 #if FUSE_VERSION >= 29
1153 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1155 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1156 /* All hashref things done */
1159 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1163 FH_STOREHANDLE(fi,POPs);
1168 fprintf(stderr, "create() handler returned nothing!\n");
1172 /* Success, so copy the file handle which they returned */
1174 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1175 fi->direct_io = SvIV(*svp);
1176 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1177 fi->keep_cache = SvIV(*svp);
1178 #if FUSE_VERSION >= 29
1179 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1180 fi->nonseekable = SvIV(*svp);
1186 DEBUGf("create end: %d\n",rv);
1191 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1193 #ifndef PERL_HAS_64BITINT
1197 DEBUGf("ftruncate begin\n");
1201 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1202 #ifdef PERL_HAS_64BITINT
1203 XPUSHs(sv_2mortal(newSViv(off)));
1205 if (asprintf(&temp, "%llu", off) == -1)
1206 croak("Memory allocation failure!");
1207 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1210 XPUSHs(FH_GETHANDLE(fi));
1212 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1214 rv = (rv ? POPi : 0);
1218 DEBUGf("ftruncate end: %i\n",rv);
1223 int _PLfuse_fgetattr(const char *file, struct stat *result,
1224 struct fuse_file_info *fi) {
1227 DEBUGf("fgetattr begin: %s\n",file);
1231 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1232 XPUSHs(FH_GETHANDLE(fi));
1234 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1238 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1245 result->st_blocks = POPi;
1246 result->st_blksize = POPi;
1247 result->st_ctime = POPi;
1248 result->st_mtime = POPi;
1249 result->st_atime = POPi;
1250 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1251 result->st_rdev = POPi;
1252 result->st_gid = POPi;
1253 result->st_uid = POPi;
1254 result->st_nlink = POPi;
1255 result->st_mode = POPi;
1256 result->st_ino = POPi;
1257 result->st_dev = POPi;
1263 DEBUGf("fgetattr end: %i\n",rv);
1267 #endif /* FUSE_VERSION >= 25 */
1269 #if FUSE_VERSION >= 26
1270 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1271 struct flock *lockinfo) {
1275 #ifndef PERL_HAS_64BITINT
1279 DEBUGf("lock begin\n");
1283 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1284 XPUSHs(sv_2mortal(newSViv(cmd)));
1287 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1288 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1289 #ifdef PERL_HAS_64BITINT
1290 sv = newSViv(lockinfo->l_start);
1292 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1293 croak("Memory allocation failure!");
1294 sv = newSVpv(temp, 0);
1297 (void) hv_store(lihash, "l_start", 7, sv, 0);
1298 #ifdef PERL_HAS_64BITINT
1299 sv = newSViv(lockinfo->l_len);
1301 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1302 croak("Memory allocation failure!");
1303 sv = newSVpv(temp, 0);
1306 (void) hv_store(lihash, "l_len", 5, sv, 0);
1307 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1309 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1310 XPUSHs(FH_GETHANDLE(fi));
1313 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1315 rv = (rv ? POPi : 0);
1316 if (lockinfo && !rv) {
1317 /* Need to copy back any altered values from the hash into
1320 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1321 lockinfo->l_type = SvIV(*svp);
1322 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1323 lockinfo->l_whence = SvIV(*svp);
1324 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1325 lockinfo->l_start = SvNV(*svp);
1326 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1327 lockinfo->l_len = SvNV(*svp);
1328 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1329 lockinfo->l_pid = SvIV(*svp);
1334 DEBUGf("lock end: %i\n",rv);
1339 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1342 DEBUGf("utimens begin\n");
1346 XPUSHs(sv_2mortal(newSVpv(file,0)));
1347 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1348 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1350 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1352 rv = (rv ? POPi : 0);
1356 DEBUGf("utimens end: %i\n",rv);
1361 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1363 #ifndef PERL_HAS_64BITINT
1367 DEBUGf("bmap begin\n");
1371 XPUSHs(sv_2mortal(newSVpv(file,0)));
1372 XPUSHs(sv_2mortal(newSViv(blocksize)));
1373 #ifdef PERL_HAS_64BITINT
1374 XPUSHs(sv_2mortal(newSViv(*idx)));
1376 if (asprintf(&temp, "%llu", *idx) == -1)
1377 croak("Memory allocation failure!");
1378 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1382 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1384 if (rv > 0 && rv < 3) {
1390 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1396 DEBUGf("bmap end: %i\n",rv);
1400 #endif /* FUSE_VERSION >= 26 */
1403 #if FUSE_VERSION >= 28
1404 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1405 struct fuse_file_info *fi, unsigned int flags, void *data) {
1408 DEBUGf("ioctl begin\n");
1412 XPUSHs(sv_2mortal(newSVpv(file,0)));
1413 XPUSHs(sv_2mortal(newSViv(cmd)));
1414 XPUSHs(sv_2mortal(newSViv(flags)));
1415 if (_IOC_DIR(cmd) & _IOC_READ)
1416 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1418 XPUSHs(&PL_sv_undef);
1419 XPUSHs(FH_GETHANDLE(fi));
1421 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1423 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1427 char *rdata = SvPV(sv, len);
1428 if (len > _IOC_SIZE(cmd)) {
1429 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1433 memset(data, 0, _IOC_SIZE(cmd));
1434 memcpy(data, rdata, len);
1440 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1449 DEBUGf("ioctl end: %i\n",rv);
1454 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1455 struct fuse_pollhandle *ph, unsigned *reventsp) {
1458 #endif /* FUSE_VERSION >= 28 */
1461 struct fuse_operations _available_ops = {
1462 getattr: _PLfuse_getattr,
1463 readlink: _PLfuse_readlink,
1464 getdir: _PLfuse_getdir,
1465 mknod: _PLfuse_mknod,
1466 mkdir: _PLfuse_mkdir,
1467 unlink: _PLfuse_unlink,
1468 rmdir: _PLfuse_rmdir,
1469 symlink: _PLfuse_symlink,
1470 rename: _PLfuse_rename,
1472 chmod: _PLfuse_chmod,
1473 chown: _PLfuse_chown,
1474 truncate: _PLfuse_truncate,
1475 utime: _PLfuse_utime,
1478 write: _PLfuse_write,
1479 statfs: _PLfuse_statfs,
1480 flush: _PLfuse_flush,
1481 release: _PLfuse_release,
1482 fsync: _PLfuse_fsync,
1483 setxattr: _PLfuse_setxattr,
1484 getxattr: _PLfuse_getxattr,
1485 listxattr: _PLfuse_listxattr,
1486 removexattr: _PLfuse_removexattr,
1487 #if FUSE_VERSION >= 23
1488 opendir: _PLfuse_opendir,
1489 readdir: _PLfuse_readdir,
1490 releasedir: _PLfuse_releasedir,
1491 fsyncdir: _PLfuse_fsyncdir,
1493 destroy: _PLfuse_destroy,
1494 #endif /* FUSE_VERSION >= 23 */
1495 #if FUSE_VERSION >= 25
1496 access: _PLfuse_access,
1497 create: _PLfuse_create,
1498 ftruncate: _PLfuse_ftruncate,
1499 fgetattr: _PLfuse_fgetattr,
1500 #endif /* FUSE_VERSION >= 25 */
1501 #if FUSE_VERSION >= 26
1503 utimens: _PLfuse_utimens,
1505 #endif /* FUSE_VERSION >= 26 */
1507 #if FUSE_VERSION >= 28
1508 ioctl: _PLfuse_ioctl,
1510 #endif /* FUSE_VERSION >= 28 */
1514 MODULE = Fuse PACKAGE = Fuse
1528 tTHX parent = MY_CXT.self;
1529 MY_CXT.self = my_perl;
1530 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1531 /* CLONE entered without a pointer table, so we can't safely clone static data */
1533 for(i=0;i<N_CALLBACKS;i++) {
1534 MY_CXT.callback[i] = NULL;
1536 MY_CXT.handles = newHV();
1540 CLONE_PARAMS *clone_param;
1541 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1542 clone_param = clone_params_new(parent, aTHX);
1544 CLONE_PARAMS raw_param;
1545 raw_param.flags = 0;
1546 raw_param.proto_perl = parent;
1547 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1548 clone_param = &raw_param;
1550 for(i=0;i<N_CALLBACKS;i++) {
1551 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1553 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1554 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1555 clone_params_del(clone_param);
1562 struct fuse_context *fc;
1564 fc = fuse_get_context();
1567 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1568 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1569 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1570 if (fc->private_data)
1571 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1572 #if FUSE_VERSION >= 28
1573 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1574 #endif /* FUSE_VERSION >= 28 */
1575 RETVAL = newRV_noinc((SV*)hash);
1585 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1593 RETVAL = newSViv(XATTR_CREATE);
1600 RETVAL = newSViv(XATTR_REPLACE);
1609 struct fuse_operations fops;
1613 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1614 struct fuse_chan *fc;
1617 if(items != N_CALLBACKS + 5) {
1618 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1621 memset(&fops, 0, sizeof(struct fuse_operations));
1623 debug = SvIV(ST(0));
1624 MY_CXT.threaded = SvIV(ST(1));
1625 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1626 if(MY_CXT.threaded) {
1627 #ifdef FUSE_USE_ITHREADS
1628 master_interp = aTHX;
1629 MUTEX_INIT(&MY_CXT.mutex);
1630 SvSHARE((SV*)(MY_CXT.handles));
1632 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1633 "mode, but your perl was not built with a supported "
1634 "thread model. Threads are disabled.\n");
1635 MY_CXT.threaded = 0;
1638 mountpoint = SvPV_nolen(ST(2));
1639 mountopts = SvPV_nolen(ST(3));
1640 #if FUSE_VERSION >= 28
1641 fops.flag_nullpath_ok = SvIV(ST(4));
1642 #endif /* FUSE_VERSION >= 28 */
1643 for(i=0;i<N_CALLBACKS;i++) {
1645 /* allow symbolic references, or real code references. */
1646 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1647 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1648 /* Dirty hack, to keep anything from overwriting the
1649 * flag area with a pointer. There should never be
1650 * anything passed as 'junk', but this prevents
1651 * someone from doing it and screwing things up... */
1655 MY_CXT.callback[i] = var;
1656 } else if(SvOK(var)) {
1657 croak("invalid callback (%i) passed to perl_fuse_main "
1658 "(%s is not a string, code ref, or undef).\n",
1659 i+5,SvPVbyte_nolen(var));
1661 MY_CXT.callback[i] = NULL;
1665 * XXX: What comes here is just a ridiculous use of the option parsing API
1666 * to hack on compatibility with other parts of the new API. First and
1667 * foremost, real C argc/argv would be good to get at...
1669 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1670 fuse_opt_free_args(&args);
1671 croak("out of memory\n");
1673 if (mountopts && strcmp("", mountopts) &&
1674 (fuse_opt_add_arg(&args, "-o") == -1 ||
1675 fuse_opt_add_arg(&args, mountopts) == -1)) {
1676 fuse_opt_free_args(&args);
1677 croak("out of memory\n");
1679 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1680 fuse_opt_free_args(&args);
1681 croak("out of memory\n");
1683 fc = fuse_mount(mountpoint,&args);
1685 croak("could not mount fuse filesystem!\n");
1687 if(MY_CXT.threaded) {
1688 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1691 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1692 fuse_unmount(mountpoint,fc);
1693 fuse_opt_free_args(&args);