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
20 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
26 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
27 /* #if FUSE_VERSION >= 28
28 # define N_CALLBACKS 41 */
29 #if FUSE_VERSION >= 26
30 # define N_CALLBACKS 38
31 #elif FUSE_VERSION >= 25
32 # define N_CALLBACKS 35
33 #elif FUSE_VERSION >= 23
34 # define N_CALLBACKS 31
36 # define N_CALLBACKS 25
40 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 #if __FreeBSD__ >= 10
741 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
743 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
747 DEBUGf("setxattr begin\n");
751 XPUSHs(sv_2mortal(newSVpv(file,0)));
752 XPUSHs(sv_2mortal(newSVpv(name,0)));
753 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
754 XPUSHs(sv_2mortal(newSViv(flags)));
756 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
758 rv = (rv ? POPi : 0);
762 DEBUGf("setxattr end: %i\n",rv);
767 #if __FreeBSD__ >= 10
768 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
770 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
774 DEBUGf("getxattr begin\n");
778 XPUSHs(sv_2mortal(newSVpv(file,0)));
779 XPUSHs(sv_2mortal(newSVpv(name,0)));
781 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
789 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
797 if ((rv > 0) && (buflen > 0))
802 memcpy(buf,SvPV_nolen(mysv),rv);
809 DEBUGf("getxattr end: %i\n",rv);
814 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
817 DEBUGf("listxattr begin\n");
821 XPUSHs(sv_2mortal(newSVpv(file,0)));
823 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
836 /* Always nul terminate */
837 if (list && (size > 0))
847 int s = SvCUR(mysv) + 1;
850 if (p && (size > 0) && (spc >= s))
852 memcpy(p,SvPV_nolen(mysv),s);
860 * If the Perl returned an error, return that.
861 * Otherwise check that the buffer was big enough.
866 if ((size > 0) && (size < total_len))
873 DEBUGf("listxattr end: %i\n",rv);
878 int _PLfuse_removexattr (const char *file, const char *name) {
881 DEBUGf("removexattr begin\n");
885 XPUSHs(sv_2mortal(newSVpv(file,0)));
886 XPUSHs(sv_2mortal(newSVpv(name,0)));
888 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
890 rv = (rv ? POPi : 0);
894 DEBUGf("removexattr end: %i\n",rv);
899 #if FUSE_VERSION >= 23
900 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
903 DEBUGf("opendir begin\n");
907 XPUSHs(sv_2mortal(newSVpv(file,0)));
908 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
910 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
914 FH_STOREHANDLE(fi, POPs);
922 DEBUGf("opendir end: %i\n",rv);
928 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
929 off_t off, struct fuse_file_info *fi) {
931 SV *sv, **svp, **swp;
935 #ifndef PERL_HAS_64BITINT
939 DEBUGf("readdir begin\n");
943 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
944 #ifdef PERL_HAS_64BITINT
945 XPUSHs(sv_2mortal(newSViv(off)));
947 if (asprintf(&temp, "%llu", off) == -1)
948 croak("Memory allocation failure!");
949 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
952 XPUSHs(FH_GETHANDLE(fi));
954 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
957 /* Should yield the bottom of the current stack... */
958 swp = &TOPs - prv + 1;
960 memset(&st, 0, sizeof(struct stat));
961 /* Sort of a hack to walk the stack in order, instead of reverse
962 * order - trying to explain to potential users why they need to
963 * reverse the order of this array would be confusing, at best. */
964 while (swp <= &TOPs) {
966 if (!SvROK(sv) && SvPOK(sv))
967 /* Just a bare SV (probably a string; hopefully a string) */
968 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
969 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
970 if (av_len(av) >= 2) {
971 /* The third element of the array should be the args that
972 * would otherwise go to getattr(); a lot of filesystems
973 * will, or at least can, return that info as part of the
974 * enumeration process... */
975 svp = av_fetch(av, 2, FALSE);
977 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
979 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
980 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
981 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
982 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
983 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
984 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
985 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
986 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
987 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
988 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
989 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
990 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
991 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
995 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
996 /* For now if the element isn't what we want, just
997 * quietly ignore it... */
999 if (av_len(av) >= 1) {
1000 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1001 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1002 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1005 memset(&st, 0, sizeof(struct stat));
1010 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1013 fprintf(stderr,"readdir() handler returned nothing!\n");
1019 DEBUGf("readdir end: %i\n",rv);
1024 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1027 DEBUGf("releasedir begin\n");
1031 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1032 XPUSHs(FH_GETHANDLE(fi));
1034 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1036 rv = (rv ? POPi : 0);
1037 FH_RELEASEHANDLE(fi);
1041 DEBUGf("releasedir end: %i\n",rv);
1046 int _PLfuse_fsyncdir(const char *file, int datasync,
1047 struct fuse_file_info *fi) {
1050 DEBUGf("fsyncdir begin\n");
1054 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1055 XPUSHs(sv_2mortal(newSViv(datasync)));
1056 XPUSHs(FH_GETHANDLE(fi));
1058 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1060 rv = (rv ? POPi : 0);
1064 DEBUGf("fsyncdir end: %i\n",rv);
1069 #if FUSE_VERSION >= 26
1070 void *_PLfuse_init(struct fuse_conn_info *fc)
1071 #else /* FUSE_VERSION < 26 */
1072 void *_PLfuse_init(void)
1073 #endif /* FUSE_VERSION >= 26 */
1078 DEBUGf("init begin\n");
1083 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1087 if (rv == &PL_sv_undef)
1090 rv = SvREFCNT_inc((SV *)rv);
1095 DEBUGf("init end: %p\n", rv);
1100 void _PLfuse_destroy(void *private_data) {
1102 DEBUGf("destroy begin\n");
1106 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1108 call_sv(MY_CXT.callback[30], G_VOID);
1111 SvREFCNT_dec((SV *)private_data);
1115 DEBUGf("init end\n");
1118 #endif /* FUSE_VERSION >= 23 */
1120 #if FUSE_VERSION >= 25
1121 int _PLfuse_access(const char *file, int mask) {
1124 DEBUGf("access begin\n");
1128 XPUSHs(sv_2mortal(newSVpv(file,0)));
1129 XPUSHs(sv_2mortal(newSViv(mask)));
1131 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1133 rv = (rv ? POPi : 0);
1137 DEBUGf("access end: %d\n", rv);
1142 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1146 DEBUGf("create begin\n");
1150 XPUSHs(sv_2mortal(newSVpv(file,0)));
1151 XPUSHs(sv_2mortal(newSViv(mode)));
1152 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1153 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1154 /* Create a hashref containing the details from fi
1155 * which we can look at or modify.
1158 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1159 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1160 #if FUSE_VERSION >= 29
1161 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1163 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1164 /* All hashref things done */
1167 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1171 FH_STOREHANDLE(fi,POPs);
1176 fprintf(stderr, "create() handler returned nothing!\n");
1180 /* Success, so copy the file handle which they returned */
1182 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1183 fi->direct_io = SvIV(*svp);
1184 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1185 fi->keep_cache = SvIV(*svp);
1186 #if FUSE_VERSION >= 29
1187 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1188 fi->nonseekable = SvIV(*svp);
1194 DEBUGf("create end: %d\n",rv);
1199 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1201 #ifndef PERL_HAS_64BITINT
1205 DEBUGf("ftruncate begin\n");
1209 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1210 #ifdef PERL_HAS_64BITINT
1211 XPUSHs(sv_2mortal(newSViv(off)));
1213 if (asprintf(&temp, "%llu", off) == -1)
1214 croak("Memory allocation failure!");
1215 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1218 XPUSHs(FH_GETHANDLE(fi));
1220 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1222 rv = (rv ? POPi : 0);
1226 DEBUGf("ftruncate end: %i\n",rv);
1231 int _PLfuse_fgetattr(const char *file, struct stat *result,
1232 struct fuse_file_info *fi) {
1235 DEBUGf("fgetattr begin: %s\n",file);
1239 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1240 XPUSHs(FH_GETHANDLE(fi));
1242 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1246 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1253 result->st_blocks = POPi;
1254 result->st_blksize = POPi;
1255 result->st_ctime = POPi;
1256 result->st_mtime = POPi;
1257 result->st_atime = POPi;
1258 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1259 result->st_rdev = POPi;
1260 result->st_gid = POPi;
1261 result->st_uid = POPi;
1262 result->st_nlink = POPi;
1263 result->st_mode = POPi;
1264 result->st_ino = POPi;
1265 result->st_dev = POPi;
1271 DEBUGf("fgetattr end: %i\n",rv);
1275 #endif /* FUSE_VERSION >= 25 */
1277 #if FUSE_VERSION >= 26
1278 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1279 struct flock *lockinfo) {
1283 #ifndef PERL_HAS_64BITINT
1287 DEBUGf("lock begin\n");
1291 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1292 XPUSHs(sv_2mortal(newSViv(cmd)));
1295 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1296 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1297 #ifdef PERL_HAS_64BITINT
1298 sv = newSViv(lockinfo->l_start);
1300 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1301 croak("Memory allocation failure!");
1302 sv = newSVpv(temp, 0);
1305 (void) hv_store(lihash, "l_start", 7, sv, 0);
1306 #ifdef PERL_HAS_64BITINT
1307 sv = newSViv(lockinfo->l_len);
1309 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1310 croak("Memory allocation failure!");
1311 sv = newSVpv(temp, 0);
1314 (void) hv_store(lihash, "l_len", 5, sv, 0);
1315 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1317 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1318 XPUSHs(FH_GETHANDLE(fi));
1321 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1323 rv = (rv ? POPi : 0);
1324 if (lockinfo && !rv) {
1325 /* Need to copy back any altered values from the hash into
1328 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1329 lockinfo->l_type = SvIV(*svp);
1330 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1331 lockinfo->l_whence = SvIV(*svp);
1332 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1333 lockinfo->l_start = SvNV(*svp);
1334 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1335 lockinfo->l_len = SvNV(*svp);
1336 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1337 lockinfo->l_pid = SvIV(*svp);
1342 DEBUGf("lock end: %i\n",rv);
1347 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1350 DEBUGf("utimens begin\n");
1354 XPUSHs(sv_2mortal(newSVpv(file,0)));
1355 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1356 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1358 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1360 rv = (rv ? POPi : 0);
1364 DEBUGf("utimens end: %i\n",rv);
1369 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1371 #ifndef PERL_HAS_64BITINT
1375 DEBUGf("bmap begin\n");
1379 XPUSHs(sv_2mortal(newSVpv(file,0)));
1380 XPUSHs(sv_2mortal(newSViv(blocksize)));
1381 #ifdef PERL_HAS_64BITINT
1382 XPUSHs(sv_2mortal(newSViv(*idx)));
1384 if (asprintf(&temp, "%llu", *idx) == -1)
1385 croak("Memory allocation failure!");
1386 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1390 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1392 if (rv > 0 && rv < 3) {
1398 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1404 DEBUGf("bmap end: %i\n",rv);
1408 #endif /* FUSE_VERSION >= 26 */
1411 #if FUSE_VERSION >= 28
1412 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1413 struct fuse_file_info *fi, unsigned int flags, void *data) {
1416 DEBUGf("ioctl begin\n");
1420 XPUSHs(sv_2mortal(newSVpv(file,0)));
1421 XPUSHs(sv_2mortal(newSViv(cmd)));
1422 XPUSHs(sv_2mortal(newSViv(flags)));
1423 if (_IOC_DIR(cmd) & _IOC_READ)
1424 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1426 XPUSHs(&PL_sv_undef);
1427 XPUSHs(FH_GETHANDLE(fi));
1429 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1431 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1435 char *rdata = SvPV(sv, len);
1436 if (len > _IOC_SIZE(cmd)) {
1437 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1441 memset(data, 0, _IOC_SIZE(cmd));
1442 memcpy(data, rdata, len);
1448 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1457 DEBUGf("ioctl end: %i\n",rv);
1462 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1463 struct fuse_pollhandle *ph, unsigned *reventsp) {
1466 #endif /* FUSE_VERSION >= 28 */
1469 struct fuse_operations _available_ops = {
1470 getattr: _PLfuse_getattr,
1471 readlink: _PLfuse_readlink,
1472 getdir: _PLfuse_getdir,
1473 mknod: _PLfuse_mknod,
1474 mkdir: _PLfuse_mkdir,
1475 unlink: _PLfuse_unlink,
1476 rmdir: _PLfuse_rmdir,
1477 symlink: _PLfuse_symlink,
1478 rename: _PLfuse_rename,
1480 chmod: _PLfuse_chmod,
1481 chown: _PLfuse_chown,
1482 truncate: _PLfuse_truncate,
1483 utime: _PLfuse_utime,
1486 write: _PLfuse_write,
1487 statfs: _PLfuse_statfs,
1488 flush: _PLfuse_flush,
1489 release: _PLfuse_release,
1490 fsync: _PLfuse_fsync,
1491 setxattr: _PLfuse_setxattr,
1492 getxattr: _PLfuse_getxattr,
1493 listxattr: _PLfuse_listxattr,
1494 removexattr: _PLfuse_removexattr,
1495 #if FUSE_VERSION >= 23
1496 opendir: _PLfuse_opendir,
1497 readdir: _PLfuse_readdir,
1498 releasedir: _PLfuse_releasedir,
1499 fsyncdir: _PLfuse_fsyncdir,
1501 destroy: _PLfuse_destroy,
1502 #endif /* FUSE_VERSION >= 23 */
1503 #if FUSE_VERSION >= 25
1504 access: _PLfuse_access,
1505 create: _PLfuse_create,
1506 ftruncate: _PLfuse_ftruncate,
1507 fgetattr: _PLfuse_fgetattr,
1508 #endif /* FUSE_VERSION >= 25 */
1509 #if FUSE_VERSION >= 26
1511 utimens: _PLfuse_utimens,
1513 #endif /* FUSE_VERSION >= 26 */
1515 #if FUSE_VERSION >= 28
1516 ioctl: _PLfuse_ioctl,
1518 #endif /* FUSE_VERSION >= 28 */
1522 MODULE = Fuse PACKAGE = Fuse
1541 tTHX parent = MY_CXT.self;
1542 MY_CXT.self = my_perl;
1543 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1544 /* CLONE entered without a pointer table, so we can't safely clone static data */
1546 for(i=0;i<N_CALLBACKS;i++) {
1547 MY_CXT.callback[i] = NULL;
1549 MY_CXT.handles = newHV();
1553 CLONE_PARAMS *clone_param;
1554 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1555 clone_param = clone_params_new(parent, aTHX);
1557 CLONE_PARAMS raw_param;
1558 raw_param.flags = 0;
1559 raw_param.proto_perl = parent;
1560 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1561 clone_param = &raw_param;
1563 for(i=0;i<N_CALLBACKS;i++) {
1564 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1566 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1567 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1568 clone_params_del(clone_param);
1576 struct fuse_context *fc;
1578 fc = fuse_get_context();
1581 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1582 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1583 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1584 if (fc->private_data)
1585 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1586 #if FUSE_VERSION >= 28
1587 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1588 #endif /* FUSE_VERSION >= 28 */
1589 RETVAL = newRV_noinc((SV*)hash);
1599 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1607 RETVAL = newSViv(XATTR_CREATE);
1614 RETVAL = newSViv(XATTR_REPLACE);
1623 struct fuse_operations fops;
1627 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1628 struct fuse_chan *fc;
1631 if(items != N_CALLBACKS + 5) {
1632 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1635 memset(&fops, 0, sizeof(struct fuse_operations));
1637 debug = SvIV(ST(0));
1638 MY_CXT.threaded = SvIV(ST(1));
1639 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1640 if(MY_CXT.threaded) {
1641 #ifdef FUSE_USE_ITHREADS
1642 master_interp = aTHX;
1643 MUTEX_INIT(&MY_CXT.mutex);
1644 SvSHARE((SV*)(MY_CXT.handles));
1646 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1647 "mode, but your perl was not built with a supported "
1648 "thread model. Threads are disabled.\n");
1649 MY_CXT.threaded = 0;
1652 mountpoint = SvPV_nolen(ST(2));
1653 mountopts = SvPV_nolen(ST(3));
1654 #if FUSE_VERSION >= 28
1655 fops.flag_nullpath_ok = SvIV(ST(4));
1656 #endif /* FUSE_VERSION >= 28 */
1657 for(i=0;i<N_CALLBACKS;i++) {
1659 /* allow symbolic references, or real code references. */
1660 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1661 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1662 /* Dirty hack, to keep anything from overwriting the
1663 * flag area with a pointer. There should never be
1664 * anything passed as 'junk', but this prevents
1665 * someone from doing it and screwing things up... */
1669 MY_CXT.callback[i] = var;
1670 } else if(SvOK(var)) {
1671 croak("invalid callback (%i) passed to perl_fuse_main "
1672 "(%s is not a string, code ref, or undef).\n",
1673 i+5,SvPVbyte_nolen(var));
1675 MY_CXT.callback[i] = NULL;
1679 * XXX: What comes here is just a ridiculous use of the option parsing API
1680 * to hack on compatibility with other parts of the new API. First and
1681 * foremost, real C argc/argv would be good to get at...
1683 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1684 fuse_opt_free_args(&args);
1685 croak("out of memory\n");
1687 if (mountopts && strcmp("", mountopts) &&
1688 (fuse_opt_add_arg(&args, "-o") == -1 ||
1689 fuse_opt_add_arg(&args, mountopts) == -1)) {
1690 fuse_opt_free_args(&args);
1691 croak("out of memory\n");
1693 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1694 fuse_opt_free_args(&args);
1695 croak("out of memory\n");
1697 fc = fuse_mount(mountpoint,&args);
1699 croak("could not mount fuse filesystem!\n");
1701 if(MY_CXT.threaded) {
1702 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1705 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1706 fuse_unmount(mountpoint,fc);
1707 fuse_opt_free_args(&args);