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];
48 #ifdef FUSE_USE_ITHREADS
49 tTHX master_interp = NULL;
51 #define CLONE_INTERP(parent) S_clone_interp(parent)
52 tTHX S_clone_interp(tTHX parent) {
53 dMY_CXT_INTERP(parent);
55 MUTEX_LOCK(&MY_CXT.mutex);
56 PERL_SET_CONTEXT(parent);
58 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
59 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
61 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
62 ptr_table_free(PL_ptr_table);
65 MUTEX_UNLOCK(&MY_CXT.mutex);
71 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
72 # define FUSE_CONTEXT_POST }
74 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
75 # define FUSE_CONTEXT_POST
80 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
85 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
86 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
87 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
88 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
90 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
95 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
103 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
105 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
110 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
112 #ifdef FUSE_USE_ITHREADS
113 if(MY_CXT.threaded) {
117 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
118 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
119 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
126 int _PLfuse_getattr(const char *file, struct stat *result) {
129 DEBUGf("getattr begin: %s\n",file);
133 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
135 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
139 fprintf(stderr,"inappropriate number of returned values from getattr\n");
146 result->st_blocks = POPi;
147 result->st_blksize = POPi;
148 result->st_ctime = POPi;
149 result->st_mtime = POPi;
150 result->st_atime = POPi;
151 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
152 result->st_rdev = POPi;
153 result->st_gid = POPi;
154 result->st_uid = POPi;
155 result->st_nlink = POPi;
156 result->st_mode = POPi;
157 result->st_ino = POPi;
158 result->st_dev = POPi;
164 DEBUGf("getattr end: %i\n",rv);
169 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
174 DEBUGf("readlink begin\n");
178 XPUSHs(sv_2mortal(newSVpv(file,0)));
180 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
186 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
189 strncpy(buf,SvPV_nolen(mysv),buflen);
197 DEBUGf("readlink end: %i\n",rv);
202 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
206 DEBUGf("getdir begin\n");
210 XPUSHs(sv_2mortal(newSVpv(file,0)));
212 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
215 /* Should yield the bottom of the current stack... */
216 swp = &TOPs - prv + 1;
218 /* Sort of a hack to walk the stack in order, instead of reverse
219 * order - trying to explain to potential users why they need to
220 * reverse the order of this array would be confusing, at best. */
222 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
224 fprintf(stderr,"getdir() handler returned nothing!\n");
230 DEBUGf("getdir end: %i\n",rv);
235 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
238 DEBUGf("mknod begin\n");
242 XPUSHs(sv_2mortal(newSVpv(file,0)));
243 XPUSHs(sv_2mortal(newSViv(mode)));
244 XPUSHs(sv_2mortal(newSViv(dev)));
246 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
248 rv = (rv ? POPi : 0);
252 DEBUGf("mknod end: %i\n",rv);
257 int _PLfuse_mkdir (const char *file, mode_t mode) {
260 DEBUGf("mkdir begin\n");
264 XPUSHs(sv_2mortal(newSVpv(file,0)));
265 XPUSHs(sv_2mortal(newSViv(mode)));
267 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
269 rv = (rv ? POPi : 0);
273 DEBUGf("mkdir end: %i\n",rv);
279 int _PLfuse_unlink (const char *file) {
282 DEBUGf("unlink begin\n");
286 XPUSHs(sv_2mortal(newSVpv(file,0)));
288 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
290 rv = (rv ? POPi : 0);
294 DEBUGf("unlink end: %i\n",rv);
299 int _PLfuse_rmdir (const char *file) {
302 DEBUGf("rmdir begin\n");
306 XPUSHs(sv_2mortal(newSVpv(file,0)));
308 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
310 rv = (rv ? POPi : 0);
314 DEBUGf("rmdir end: %i\n",rv);
319 int _PLfuse_symlink (const char *file, const char *new) {
322 DEBUGf("symlink begin\n");
326 XPUSHs(sv_2mortal(newSVpv(file,0)));
327 XPUSHs(sv_2mortal(newSVpv(new,0)));
329 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
331 rv = (rv ? POPi : 0);
335 DEBUGf("symlink end: %i\n",rv);
340 int _PLfuse_rename (const char *file, const char *new) {
343 DEBUGf("rename begin\n");
347 XPUSHs(sv_2mortal(newSVpv(file,0)));
348 XPUSHs(sv_2mortal(newSVpv(new,0)));
350 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
352 rv = (rv ? POPi : 0);
356 DEBUGf("rename end: %i\n",rv);
361 int _PLfuse_link (const char *file, const char *new) {
364 DEBUGf("link begin\n");
368 XPUSHs(sv_2mortal(newSVpv(file,0)));
369 XPUSHs(sv_2mortal(newSVpv(new,0)));
371 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
373 rv = (rv ? POPi : 0);
377 DEBUGf("link end: %i\n",rv);
382 int _PLfuse_chmod (const char *file, mode_t mode) {
385 DEBUGf("chmod begin\n");
389 XPUSHs(sv_2mortal(newSVpv(file,0)));
390 XPUSHs(sv_2mortal(newSViv(mode)));
392 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
394 rv = (rv ? POPi : 0);
398 DEBUGf("chmod end: %i\n",rv);
403 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
406 DEBUGf("chown begin\n");
410 XPUSHs(sv_2mortal(newSVpv(file,0)));
411 XPUSHs(sv_2mortal(newSViv(uid)));
412 XPUSHs(sv_2mortal(newSViv(gid)));
414 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
416 rv = (rv ? POPi : 0);
420 DEBUGf("chown end: %i\n",rv);
425 int _PLfuse_truncate (const char *file, off_t off) {
427 #ifndef PERL_HAS_64BITINT
431 DEBUGf("truncate begin\n");
435 XPUSHs(sv_2mortal(newSVpv(file,0)));
436 #ifdef PERL_HAS_64BITINT
437 XPUSHs(sv_2mortal(newSViv(off)));
439 if (asprintf(&temp, "%llu", off) == -1)
440 croak("Memory allocation failure!");
441 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
445 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
447 rv = (rv ? POPi : 0);
451 DEBUGf("truncate end: %i\n",rv);
456 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
459 DEBUGf("utime begin\n");
463 XPUSHs(sv_2mortal(newSVpv(file,0)));
464 XPUSHs(sv_2mortal(newSViv(uti->actime)));
465 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
467 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
469 rv = (rv ? POPi : 0);
473 DEBUGf("utime end: %i\n",rv);
478 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
480 int flags = fi->flags;
483 DEBUGf("open begin\n");
487 XPUSHs(sv_2mortal(newSVpv(file,0)));
488 XPUSHs(sv_2mortal(newSViv(flags)));
489 /* Create a hashref containing the details from fi
490 * which we can look at or modify.
492 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
494 #if FUSE_VERSION >= 24
495 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
496 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
498 #if FUSE_VERSION >= 29
499 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
501 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
502 /* All hashref things done */
505 /* Open called with filename, flags */
506 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
510 FH_STOREHANDLE(fi,POPs);
518 /* Success, so copy the file handle which they returned */
519 #if FUSE_VERSION >= 24
521 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
522 fi->direct_io = SvIV(*svp);
523 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
524 fi->keep_cache = SvIV(*svp);
526 #if FUSE_VERSION >= 29
527 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
528 fi->nonseekable = SvIV(*svp);
534 DEBUGf("open end: %i\n",rv);
539 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
540 struct fuse_file_info *fi) {
542 #ifndef PERL_HAS_64BITINT
546 DEBUGf("read begin\n");
550 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
551 XPUSHs(sv_2mortal(newSViv(buflen)));
552 #ifdef PERL_HAS_64BITINT
553 XPUSHs(sv_2mortal(newSViv(off)));
555 if (asprintf(&temp, "%llu", off) == -1)
556 croak("Memory allocation failure!");
557 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
560 XPUSHs(FH_GETHANDLE(fi));
562 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
568 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
577 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
579 memcpy(buf,SvPV_nolen(mysv),rv);
585 DEBUGf("read end: %i\n",rv);
590 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
592 #ifndef PERL_HAS_64BITINT
596 DEBUGf("write begin\n");
600 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
601 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
602 #ifdef PERL_HAS_64BITINT
603 XPUSHs(sv_2mortal(newSViv(off)));
605 if (asprintf(&temp, "%llu", off) == -1)
606 croak("Memory allocation failure!");
607 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
610 XPUSHs(FH_GETHANDLE(fi));
612 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
614 rv = (rv ? POPi : 0);
618 DEBUGf("write end: %i\n",rv);
623 int _PLfuse_statfs (const char *file, struct statvfs *st) {
626 DEBUGf("statfs begin\n");
631 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
633 DEBUGf("statfs got %i params\n",rv);
634 if(rv == 6 || rv == 7) {
640 st->f_namemax = POPi;
641 /* zero and fill-in other */
645 st->f_bavail = st->f_bfree;
646 st->f_favail = st->f_ffree;
654 croak("inappropriate number of returned values from statfs");
663 DEBUGf("statfs end: %i\n",rv);
668 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
671 DEBUGf("flush begin\n");
675 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
676 XPUSHs(FH_GETHANDLE(fi));
678 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
680 rv = (rv ? POPi : 0);
684 DEBUGf("flush end: %i\n",rv);
689 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
691 int flags = fi->flags;
693 DEBUGf("release begin\n");
697 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
698 XPUSHs(sv_2mortal(newSViv(flags)));
699 XPUSHs(FH_GETHANDLE(fi));
701 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
703 rv = (rv ? POPi : 0);
704 FH_RELEASEHANDLE(fi);
708 DEBUGf("release end: %i\n",rv);
713 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
715 int flags = fi->flags;
717 DEBUGf("fsync begin\n");
721 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
722 XPUSHs(sv_2mortal(newSViv(flags)));
723 XPUSHs(FH_GETHANDLE(fi));
725 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
727 rv = (rv ? POPi : 0);
731 DEBUGf("fsync end: %i\n",rv);
736 #if __FreeBSD__ >= 10
737 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
739 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 #if __FreeBSD__ >= 10
764 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
766 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
770 DEBUGf("getxattr begin\n");
774 XPUSHs(sv_2mortal(newSVpv(file,0)));
775 XPUSHs(sv_2mortal(newSVpv(name,0)));
777 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
785 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
793 if ((rv > 0) && (buflen > 0))
798 memcpy(buf,SvPV_nolen(mysv),rv);
805 DEBUGf("getxattr end: %i\n",rv);
810 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
813 DEBUGf("listxattr begin\n");
817 XPUSHs(sv_2mortal(newSVpv(file,0)));
819 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
832 /* Always nul terminate */
833 if (list && (size > 0))
843 int s = SvCUR(mysv) + 1;
846 if (p && (size > 0) && (spc >= s))
848 memcpy(p,SvPV_nolen(mysv),s);
856 * If the Perl returned an error, return that.
857 * Otherwise check that the buffer was big enough.
862 if ((size > 0) && (size < total_len))
869 DEBUGf("listxattr end: %i\n",rv);
874 int _PLfuse_removexattr (const char *file, const char *name) {
877 DEBUGf("removexattr begin\n");
881 XPUSHs(sv_2mortal(newSVpv(file,0)));
882 XPUSHs(sv_2mortal(newSVpv(name,0)));
884 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
886 rv = (rv ? POPi : 0);
890 DEBUGf("removexattr end: %i\n",rv);
895 #if FUSE_VERSION >= 23
896 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
899 DEBUGf("opendir begin\n");
903 XPUSHs(sv_2mortal(newSVpv(file,0)));
904 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
906 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
910 FH_STOREHANDLE(fi, POPs);
918 DEBUGf("opendir end: %i\n",rv);
924 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
925 off_t off, struct fuse_file_info *fi) {
927 SV *sv, **svp, **swp;
931 #ifndef PERL_HAS_64BITINT
935 DEBUGf("readdir begin\n");
939 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
940 #ifdef PERL_HAS_64BITINT
941 XPUSHs(sv_2mortal(newSViv(off)));
943 if (asprintf(&temp, "%llu", off) == -1)
944 croak("Memory allocation failure!");
945 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
948 XPUSHs(FH_GETHANDLE(fi));
950 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
953 /* Should yield the bottom of the current stack... */
954 swp = &TOPs - prv + 1;
956 memset(&st, 0, sizeof(struct stat));
957 /* Sort of a hack to walk the stack in order, instead of reverse
958 * order - trying to explain to potential users why they need to
959 * reverse the order of this array would be confusing, at best. */
960 while (swp <= &TOPs) {
962 if (!SvROK(sv) && SvPOK(sv))
963 /* Just a bare SV (probably a string; hopefully a string) */
964 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
965 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
966 if (av_len(av) >= 2) {
967 /* The third element of the array should be the args that
968 * would otherwise go to getattr(); a lot of filesystems
969 * will, or at least can, return that info as part of the
970 * enumeration process... */
971 svp = av_fetch(av, 2, FALSE);
973 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
975 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
976 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
977 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
978 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
979 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
980 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
981 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
982 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
983 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
984 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
985 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
986 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
987 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
991 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
992 /* For now if the element isn't what we want, just
993 * quietly ignore it... */
995 if (av_len(av) >= 1) {
996 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
997 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
998 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1001 memset(&st, 0, sizeof(struct stat));
1006 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1009 fprintf(stderr,"readdir() handler returned nothing!\n");
1015 DEBUGf("readdir end: %i\n",rv);
1020 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1023 DEBUGf("releasedir begin\n");
1027 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1028 XPUSHs(FH_GETHANDLE(fi));
1030 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1032 rv = (rv ? POPi : 0);
1033 FH_RELEASEHANDLE(fi);
1037 DEBUGf("releasedir end: %i\n",rv);
1042 int _PLfuse_fsyncdir(const char *file, int datasync,
1043 struct fuse_file_info *fi) {
1046 DEBUGf("fsyncdir begin\n");
1050 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1051 XPUSHs(sv_2mortal(newSViv(datasync)));
1052 XPUSHs(FH_GETHANDLE(fi));
1054 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1056 rv = (rv ? POPi : 0);
1060 DEBUGf("fsyncdir end: %i\n",rv);
1065 #if FUSE_VERSION >= 26
1066 void *_PLfuse_init(struct fuse_conn_info *fc)
1067 #else /* FUSE_VERSION < 26 */
1068 void *_PLfuse_init(void)
1069 #endif /* FUSE_VERSION >= 26 */
1074 DEBUGf("init begin\n");
1079 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1083 if (rv == &PL_sv_undef)
1086 rv = SvREFCNT_inc((SV *)rv);
1091 DEBUGf("init end: %p\n", rv);
1096 void _PLfuse_destroy(void *private_data) {
1098 DEBUGf("destroy begin\n");
1102 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1104 call_sv(MY_CXT.callback[30], G_VOID);
1107 SvREFCNT_dec((SV *)private_data);
1111 DEBUGf("init end\n");
1114 #endif /* FUSE_VERSION >= 23 */
1116 #if FUSE_VERSION >= 25
1117 int _PLfuse_access(const char *file, int mask) {
1120 DEBUGf("access begin\n");
1124 XPUSHs(sv_2mortal(newSVpv(file,0)));
1125 XPUSHs(sv_2mortal(newSViv(mask)));
1127 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1129 rv = (rv ? POPi : 0);
1133 DEBUGf("access end: %d\n", rv);
1138 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1142 DEBUGf("create begin\n");
1146 XPUSHs(sv_2mortal(newSVpv(file,0)));
1147 XPUSHs(sv_2mortal(newSViv(mode)));
1148 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1149 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1150 /* Create a hashref containing the details from fi
1151 * which we can look at or modify.
1154 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1155 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1156 #if FUSE_VERSION >= 29
1157 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1159 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1160 /* All hashref things done */
1163 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1167 FH_STOREHANDLE(fi,POPs);
1172 fprintf(stderr, "create() handler returned nothing!\n");
1176 /* Success, so copy the file handle which they returned */
1178 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1179 fi->direct_io = SvIV(*svp);
1180 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1181 fi->keep_cache = SvIV(*svp);
1182 #if FUSE_VERSION >= 29
1183 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1184 fi->nonseekable = SvIV(*svp);
1190 DEBUGf("create end: %d\n",rv);
1195 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1197 #ifndef PERL_HAS_64BITINT
1201 DEBUGf("ftruncate begin\n");
1205 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1206 #ifdef PERL_HAS_64BITINT
1207 XPUSHs(sv_2mortal(newSViv(off)));
1209 if (asprintf(&temp, "%llu", off) == -1)
1210 croak("Memory allocation failure!");
1211 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1214 XPUSHs(FH_GETHANDLE(fi));
1216 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1218 rv = (rv ? POPi : 0);
1222 DEBUGf("ftruncate end: %i\n",rv);
1227 int _PLfuse_fgetattr(const char *file, struct stat *result,
1228 struct fuse_file_info *fi) {
1231 DEBUGf("fgetattr begin: %s\n",file);
1235 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1236 XPUSHs(FH_GETHANDLE(fi));
1238 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1242 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1249 result->st_blocks = POPi;
1250 result->st_blksize = POPi;
1251 result->st_ctime = POPi;
1252 result->st_mtime = POPi;
1253 result->st_atime = POPi;
1254 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1255 result->st_rdev = POPi;
1256 result->st_gid = POPi;
1257 result->st_uid = POPi;
1258 result->st_nlink = POPi;
1259 result->st_mode = POPi;
1260 result->st_ino = POPi;
1261 result->st_dev = POPi;
1267 DEBUGf("fgetattr end: %i\n",rv);
1271 #endif /* FUSE_VERSION >= 25 */
1273 #if FUSE_VERSION >= 26
1274 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1275 struct flock *lockinfo) {
1279 #ifndef PERL_HAS_64BITINT
1283 DEBUGf("lock begin\n");
1287 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1288 XPUSHs(sv_2mortal(newSViv(cmd)));
1291 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1292 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1293 #ifdef PERL_HAS_64BITINT
1294 sv = newSViv(lockinfo->l_start);
1296 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1297 croak("Memory allocation failure!");
1298 sv = newSVpv(temp, 0);
1301 (void) hv_store(lihash, "l_start", 7, sv, 0);
1302 #ifdef PERL_HAS_64BITINT
1303 sv = newSViv(lockinfo->l_len);
1305 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1306 croak("Memory allocation failure!");
1307 sv = newSVpv(temp, 0);
1310 (void) hv_store(lihash, "l_len", 5, sv, 0);
1311 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1313 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1314 XPUSHs(FH_GETHANDLE(fi));
1317 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1319 rv = (rv ? POPi : 0);
1320 if (lockinfo && !rv) {
1321 /* Need to copy back any altered values from the hash into
1324 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1325 lockinfo->l_type = SvIV(*svp);
1326 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1327 lockinfo->l_whence = SvIV(*svp);
1328 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1329 lockinfo->l_start = SvNV(*svp);
1330 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1331 lockinfo->l_len = SvNV(*svp);
1332 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1333 lockinfo->l_pid = SvIV(*svp);
1338 DEBUGf("lock end: %i\n",rv);
1343 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1346 DEBUGf("utimens begin\n");
1350 XPUSHs(sv_2mortal(newSVpv(file,0)));
1351 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1352 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1354 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1356 rv = (rv ? POPi : 0);
1360 DEBUGf("utimens end: %i\n",rv);
1365 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1367 #ifndef PERL_HAS_64BITINT
1371 DEBUGf("bmap begin\n");
1375 XPUSHs(sv_2mortal(newSVpv(file,0)));
1376 XPUSHs(sv_2mortal(newSViv(blocksize)));
1377 #ifdef PERL_HAS_64BITINT
1378 XPUSHs(sv_2mortal(newSViv(*idx)));
1380 if (asprintf(&temp, "%llu", *idx) == -1)
1381 croak("Memory allocation failure!");
1382 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1386 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1388 if (rv > 0 && rv < 3) {
1394 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1400 DEBUGf("bmap end: %i\n",rv);
1404 #endif /* FUSE_VERSION >= 26 */
1407 #if FUSE_VERSION >= 28
1408 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1409 struct fuse_file_info *fi, unsigned int flags, void *data) {
1412 DEBUGf("ioctl begin\n");
1416 XPUSHs(sv_2mortal(newSVpv(file,0)));
1417 XPUSHs(sv_2mortal(newSViv(cmd)));
1418 XPUSHs(sv_2mortal(newSViv(flags)));
1419 if (_IOC_DIR(cmd) & _IOC_READ)
1420 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1422 XPUSHs(&PL_sv_undef);
1423 XPUSHs(FH_GETHANDLE(fi));
1425 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1427 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1431 char *rdata = SvPV(sv, len);
1432 if (len > _IOC_SIZE(cmd)) {
1433 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1437 memset(data, 0, _IOC_SIZE(cmd));
1438 memcpy(data, rdata, len);
1444 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1453 DEBUGf("ioctl end: %i\n",rv);
1458 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1459 struct fuse_pollhandle *ph, unsigned *reventsp) {
1462 #endif /* FUSE_VERSION >= 28 */
1465 struct fuse_operations _available_ops = {
1466 getattr: _PLfuse_getattr,
1467 readlink: _PLfuse_readlink,
1468 getdir: _PLfuse_getdir,
1469 mknod: _PLfuse_mknod,
1470 mkdir: _PLfuse_mkdir,
1471 unlink: _PLfuse_unlink,
1472 rmdir: _PLfuse_rmdir,
1473 symlink: _PLfuse_symlink,
1474 rename: _PLfuse_rename,
1476 chmod: _PLfuse_chmod,
1477 chown: _PLfuse_chown,
1478 truncate: _PLfuse_truncate,
1479 utime: _PLfuse_utime,
1482 write: _PLfuse_write,
1483 statfs: _PLfuse_statfs,
1484 flush: _PLfuse_flush,
1485 release: _PLfuse_release,
1486 fsync: _PLfuse_fsync,
1487 setxattr: _PLfuse_setxattr,
1488 getxattr: _PLfuse_getxattr,
1489 listxattr: _PLfuse_listxattr,
1490 removexattr: _PLfuse_removexattr,
1491 #if FUSE_VERSION >= 23
1492 opendir: _PLfuse_opendir,
1493 readdir: _PLfuse_readdir,
1494 releasedir: _PLfuse_releasedir,
1495 fsyncdir: _PLfuse_fsyncdir,
1497 destroy: _PLfuse_destroy,
1498 #endif /* FUSE_VERSION >= 23 */
1499 #if FUSE_VERSION >= 25
1500 access: _PLfuse_access,
1501 create: _PLfuse_create,
1502 ftruncate: _PLfuse_ftruncate,
1503 fgetattr: _PLfuse_fgetattr,
1504 #endif /* FUSE_VERSION >= 25 */
1505 #if FUSE_VERSION >= 26
1507 utimens: _PLfuse_utimens,
1509 #endif /* FUSE_VERSION >= 26 */
1511 #if FUSE_VERSION >= 28
1512 ioctl: _PLfuse_ioctl,
1514 #endif /* FUSE_VERSION >= 28 */
1518 MODULE = Fuse PACKAGE = Fuse
1532 tTHX parent = MY_CXT.self;
1533 MY_CXT.self = my_perl;
1534 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1535 /* CLONE entered without a pointer table, so we can't safely clone static data */
1537 for(i=0;i<N_CALLBACKS;i++) {
1538 MY_CXT.callback[i] = NULL;
1540 MY_CXT.handles = newHV();
1544 CLONE_PARAMS *clone_param;
1545 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1546 clone_param = clone_params_new(parent, aTHX);
1548 CLONE_PARAMS raw_param;
1549 raw_param.flags = 0;
1550 raw_param.proto_perl = parent;
1551 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1552 clone_param = &raw_param;
1554 for(i=0;i<N_CALLBACKS;i++) {
1555 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1557 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1558 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1559 clone_params_del(clone_param);
1566 struct fuse_context *fc;
1568 fc = fuse_get_context();
1571 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1572 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1573 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1574 if (fc->private_data)
1575 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1576 #if FUSE_VERSION >= 28
1577 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1578 #endif /* FUSE_VERSION >= 28 */
1579 RETVAL = newRV_noinc((SV*)hash);
1589 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1597 RETVAL = newSViv(XATTR_CREATE);
1604 RETVAL = newSViv(XATTR_REPLACE);
1613 struct fuse_operations fops;
1617 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1618 struct fuse_chan *fc;
1621 if(items != N_CALLBACKS + 5) {
1622 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1625 memset(&fops, 0, sizeof(struct fuse_operations));
1627 debug = SvIV(ST(0));
1628 MY_CXT.threaded = SvIV(ST(1));
1629 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1630 if(MY_CXT.threaded) {
1631 #ifdef FUSE_USE_ITHREADS
1632 master_interp = aTHX;
1633 MUTEX_INIT(&MY_CXT.mutex);
1634 SvSHARE((SV*)(MY_CXT.handles));
1636 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1637 "mode, but your perl was not built with a supported "
1638 "thread model. Threads are disabled.\n");
1639 MY_CXT.threaded = 0;
1642 mountpoint = SvPV_nolen(ST(2));
1643 mountopts = SvPV_nolen(ST(3));
1644 #if FUSE_VERSION >= 28
1645 fops.flag_nullpath_ok = SvIV(ST(4));
1646 #endif /* FUSE_VERSION >= 28 */
1647 for(i=0;i<N_CALLBACKS;i++) {
1649 /* allow symbolic references, or real code references. */
1650 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1651 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1652 /* Dirty hack, to keep anything from overwriting the
1653 * flag area with a pointer. There should never be
1654 * anything passed as 'junk', but this prevents
1655 * someone from doing it and screwing things up... */
1659 MY_CXT.callback[i] = var;
1660 } else if(SvOK(var)) {
1661 croak("invalid callback (%i) passed to perl_fuse_main "
1662 "(%s is not a string, code ref, or undef).\n",
1663 i+5,SvPVbyte_nolen(var));
1665 MY_CXT.callback[i] = NULL;
1669 * XXX: What comes here is just a ridiculous use of the option parsing API
1670 * to hack on compatibility with other parts of the new API. First and
1671 * foremost, real C argc/argv would be good to get at...
1673 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1674 fuse_opt_free_args(&args);
1675 croak("out of memory\n");
1677 if (mountopts && strcmp("", mountopts) &&
1678 (fuse_opt_add_arg(&args, "-o") == -1 ||
1679 fuse_opt_add_arg(&args, mountopts) == -1)) {
1680 fuse_opt_free_args(&args);
1681 croak("out of memory\n");
1683 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1684 fuse_opt_free_args(&args);
1685 croak("out of memory\n");
1687 fc = fuse_mount(mountpoint,&args);
1689 croak("could not mount fuse filesystem!\n");
1691 if(MY_CXT.threaded) {
1692 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1695 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1696 fuse_unmount(mountpoint,fc);
1697 fuse_opt_free_args(&args);