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 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
739 DEBUGf("setxattr begin\n");
743 XPUSHs(sv_2mortal(newSVpv(file,0)));
744 XPUSHs(sv_2mortal(newSVpv(name,0)));
745 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
746 XPUSHs(sv_2mortal(newSViv(flags)));
748 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
750 rv = (rv ? POPi : 0);
754 DEBUGf("setxattr end: %i\n",rv);
759 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
762 DEBUGf("getxattr begin\n");
766 XPUSHs(sv_2mortal(newSVpv(file,0)));
767 XPUSHs(sv_2mortal(newSVpv(name,0)));
769 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
777 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
785 if ((rv > 0) && (buflen > 0))
790 memcpy(buf,SvPV_nolen(mysv),rv);
797 DEBUGf("getxattr end: %i\n",rv);
802 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
805 DEBUGf("listxattr begin\n");
809 XPUSHs(sv_2mortal(newSVpv(file,0)));
811 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
824 /* Always nul terminate */
825 if (list && (size > 0))
835 int s = SvCUR(mysv) + 1;
838 if (p && (size > 0) && (spc >= s))
840 memcpy(p,SvPV_nolen(mysv),s);
848 * If the Perl returned an error, return that.
849 * Otherwise check that the buffer was big enough.
854 if ((size > 0) && (size < total_len))
861 DEBUGf("listxattr end: %i\n",rv);
866 int _PLfuse_removexattr (const char *file, const char *name) {
869 DEBUGf("removexattr begin\n");
873 XPUSHs(sv_2mortal(newSVpv(file,0)));
874 XPUSHs(sv_2mortal(newSVpv(name,0)));
876 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
878 rv = (rv ? POPi : 0);
882 DEBUGf("removexattr end: %i\n",rv);
887 #if FUSE_VERSION >= 23
888 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
891 DEBUGf("opendir begin\n");
895 XPUSHs(sv_2mortal(newSVpv(file,0)));
896 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
898 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
902 FH_STOREHANDLE(fi, POPs);
910 DEBUGf("opendir end: %i\n",rv);
916 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
917 off_t off, struct fuse_file_info *fi) {
919 SV *sv, **svp, **swp;
923 #ifndef PERL_HAS_64BITINT
927 DEBUGf("readdir begin\n");
931 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
932 #ifdef PERL_HAS_64BITINT
933 XPUSHs(sv_2mortal(newSViv(off)));
935 if (asprintf(&temp, "%llu", off) == -1)
936 croak("Memory allocation failure!");
937 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
940 XPUSHs(FH_GETHANDLE(fi));
942 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
945 /* Should yield the bottom of the current stack... */
946 swp = &TOPs - prv + 1;
948 memset(&st, 0, sizeof(struct stat));
949 /* Sort of a hack to walk the stack in order, instead of reverse
950 * order - trying to explain to potential users why they need to
951 * reverse the order of this array would be confusing, at best. */
952 while (swp <= &TOPs) {
954 if (!SvROK(sv) && SvPOK(sv))
955 /* Just a bare SV (probably a string; hopefully a string) */
956 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
957 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
958 if (av_len(av) >= 2) {
959 /* The third element of the array should be the args that
960 * would otherwise go to getattr(); a lot of filesystems
961 * will, or at least can, return that info as part of the
962 * enumeration process... */
963 svp = av_fetch(av, 2, FALSE);
965 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
967 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
968 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
969 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
970 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
971 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
972 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
973 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
974 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
975 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
976 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
977 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
978 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
979 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
983 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
984 /* For now if the element isn't what we want, just
985 * quietly ignore it... */
987 if (av_len(av) >= 1) {
988 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
989 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
990 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
993 memset(&st, 0, sizeof(struct stat));
998 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1001 fprintf(stderr,"readdir() handler returned nothing!\n");
1007 DEBUGf("readdir end: %i\n",rv);
1012 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1015 DEBUGf("releasedir begin\n");
1019 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1020 XPUSHs(FH_GETHANDLE(fi));
1022 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1024 rv = (rv ? POPi : 0);
1025 FH_RELEASEHANDLE(fi);
1029 DEBUGf("releasedir end: %i\n",rv);
1034 int _PLfuse_fsyncdir(const char *file, int datasync,
1035 struct fuse_file_info *fi) {
1038 DEBUGf("fsyncdir begin\n");
1042 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1043 XPUSHs(sv_2mortal(newSViv(datasync)));
1044 XPUSHs(FH_GETHANDLE(fi));
1046 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1048 rv = (rv ? POPi : 0);
1052 DEBUGf("fsyncdir end: %i\n",rv);
1057 #if FUSE_VERSION >= 26
1058 void *_PLfuse_init(struct fuse_conn_info *fc)
1059 #else /* FUSE_VERSION < 26 */
1060 void *_PLfuse_init(void)
1061 #endif /* FUSE_VERSION >= 26 */
1066 DEBUGf("init begin\n");
1071 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1075 if (rv == &PL_sv_undef)
1078 rv = SvREFCNT_inc((SV *)rv);
1083 DEBUGf("init end: %p\n", rv);
1088 void _PLfuse_destroy(void *private_data) {
1090 DEBUGf("destroy begin\n");
1094 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1096 call_sv(MY_CXT.callback[30], G_VOID);
1099 SvREFCNT_dec((SV *)private_data);
1103 DEBUGf("init end\n");
1106 #endif /* FUSE_VERSION >= 23 */
1108 #if FUSE_VERSION >= 25
1109 int _PLfuse_access(const char *file, int mask) {
1112 DEBUGf("access begin\n");
1116 XPUSHs(sv_2mortal(newSVpv(file,0)));
1117 XPUSHs(sv_2mortal(newSViv(mask)));
1119 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1121 rv = (rv ? POPi : 0);
1125 DEBUGf("access end: %d\n", rv);
1130 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1134 DEBUGf("create begin\n");
1138 XPUSHs(sv_2mortal(newSVpv(file,0)));
1139 XPUSHs(sv_2mortal(newSViv(mode)));
1140 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1141 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1142 /* Create a hashref containing the details from fi
1143 * which we can look at or modify.
1146 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1147 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1148 #if FUSE_VERSION >= 29
1149 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1151 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1152 /* All hashref things done */
1155 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1159 FH_STOREHANDLE(fi,POPs);
1164 fprintf(stderr, "create() handler returned nothing!\n");
1168 /* Success, so copy the file handle which they returned */
1170 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1171 fi->direct_io = SvIV(*svp);
1172 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1173 fi->keep_cache = SvIV(*svp);
1174 #if FUSE_VERSION >= 29
1175 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1176 fi->nonseekable = SvIV(*svp);
1182 DEBUGf("create end: %d\n",rv);
1187 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1189 #ifndef PERL_HAS_64BITINT
1193 DEBUGf("ftruncate begin\n");
1197 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1198 #ifdef PERL_HAS_64BITINT
1199 XPUSHs(sv_2mortal(newSViv(off)));
1201 if (asprintf(&temp, "%llu", off) == -1)
1202 croak("Memory allocation failure!");
1203 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1206 XPUSHs(FH_GETHANDLE(fi));
1208 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1210 rv = (rv ? POPi : 0);
1214 DEBUGf("ftruncate end: %i\n",rv);
1219 int _PLfuse_fgetattr(const char *file, struct stat *result,
1220 struct fuse_file_info *fi) {
1223 DEBUGf("fgetattr begin: %s\n",file);
1227 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1228 XPUSHs(FH_GETHANDLE(fi));
1230 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1234 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1241 result->st_blocks = POPi;
1242 result->st_blksize = POPi;
1243 result->st_ctime = POPi;
1244 result->st_mtime = POPi;
1245 result->st_atime = POPi;
1246 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1247 result->st_rdev = POPi;
1248 result->st_gid = POPi;
1249 result->st_uid = POPi;
1250 result->st_nlink = POPi;
1251 result->st_mode = POPi;
1252 result->st_ino = POPi;
1253 result->st_dev = POPi;
1259 DEBUGf("fgetattr end: %i\n",rv);
1263 #endif /* FUSE_VERSION >= 25 */
1265 #if FUSE_VERSION >= 26
1266 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1267 struct flock *lockinfo) {
1271 #ifndef PERL_HAS_64BITINT
1275 DEBUGf("lock begin\n");
1279 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1280 XPUSHs(sv_2mortal(newSViv(cmd)));
1283 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1284 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1285 #ifdef PERL_HAS_64BITINT
1286 sv = newSViv(lockinfo->l_start);
1288 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1289 croak("Memory allocation failure!");
1290 sv = newSVpv(temp, 0);
1293 (void) hv_store(lihash, "l_start", 7, sv, 0);
1294 #ifdef PERL_HAS_64BITINT
1295 sv = newSViv(lockinfo->l_len);
1297 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1298 croak("Memory allocation failure!");
1299 sv = newSVpv(temp, 0);
1302 (void) hv_store(lihash, "l_len", 5, sv, 0);
1303 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1305 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1306 XPUSHs(FH_GETHANDLE(fi));
1309 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1311 rv = (rv ? POPi : 0);
1312 if (lockinfo && !rv) {
1313 /* Need to copy back any altered values from the hash into
1316 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1317 lockinfo->l_type = SvIV(*svp);
1318 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1319 lockinfo->l_whence = SvIV(*svp);
1320 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1321 lockinfo->l_start = SvNV(*svp);
1322 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1323 lockinfo->l_len = SvNV(*svp);
1324 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1325 lockinfo->l_pid = SvIV(*svp);
1330 DEBUGf("lock end: %i\n",rv);
1335 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1338 DEBUGf("utimens begin\n");
1342 XPUSHs(sv_2mortal(newSVpv(file,0)));
1343 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1344 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1346 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1348 rv = (rv ? POPi : 0);
1352 DEBUGf("utimens end: %i\n",rv);
1357 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1359 #ifndef PERL_HAS_64BITINT
1363 DEBUGf("bmap begin\n");
1367 XPUSHs(sv_2mortal(newSVpv(file,0)));
1368 XPUSHs(sv_2mortal(newSViv(blocksize)));
1369 #ifdef PERL_HAS_64BITINT
1370 XPUSHs(sv_2mortal(newSViv(*idx)));
1372 if (asprintf(&temp, "%llu", *idx) == -1)
1373 croak("Memory allocation failure!");
1374 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1378 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1380 if (rv > 0 && rv < 3) {
1386 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1392 DEBUGf("bmap end: %i\n",rv);
1396 #endif /* FUSE_VERSION >= 26 */
1399 #if FUSE_VERSION >= 28
1400 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1401 struct fuse_file_info *fi, unsigned int flags, void *data) {
1404 DEBUGf("ioctl begin\n");
1408 XPUSHs(sv_2mortal(newSVpv(file,0)));
1409 XPUSHs(sv_2mortal(newSViv(cmd)));
1410 XPUSHs(sv_2mortal(newSViv(flags)));
1411 if (_IOC_DIR(cmd) & _IOC_READ)
1412 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1414 XPUSHs(&PL_sv_undef);
1415 XPUSHs(FH_GETHANDLE(fi));
1417 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1419 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1423 char *rdata = SvPV(sv, len);
1424 if (len > _IOC_SIZE(cmd)) {
1425 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1429 memset(data, 0, _IOC_SIZE(cmd));
1430 memcpy(data, rdata, len);
1436 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1445 DEBUGf("ioctl end: %i\n",rv);
1450 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1451 struct fuse_pollhandle *ph, unsigned *reventsp) {
1454 #endif /* FUSE_VERSION >= 28 */
1457 struct fuse_operations _available_ops = {
1458 getattr: _PLfuse_getattr,
1459 readlink: _PLfuse_readlink,
1460 getdir: _PLfuse_getdir,
1461 mknod: _PLfuse_mknod,
1462 mkdir: _PLfuse_mkdir,
1463 unlink: _PLfuse_unlink,
1464 rmdir: _PLfuse_rmdir,
1465 symlink: _PLfuse_symlink,
1466 rename: _PLfuse_rename,
1468 chmod: _PLfuse_chmod,
1469 chown: _PLfuse_chown,
1470 truncate: _PLfuse_truncate,
1471 utime: _PLfuse_utime,
1474 write: _PLfuse_write,
1475 statfs: _PLfuse_statfs,
1476 flush: _PLfuse_flush,
1477 release: _PLfuse_release,
1478 fsync: _PLfuse_fsync,
1479 setxattr: _PLfuse_setxattr,
1480 getxattr: _PLfuse_getxattr,
1481 listxattr: _PLfuse_listxattr,
1482 removexattr: _PLfuse_removexattr,
1483 #if FUSE_VERSION >= 23
1484 opendir: _PLfuse_opendir,
1485 readdir: _PLfuse_readdir,
1486 releasedir: _PLfuse_releasedir,
1487 fsyncdir: _PLfuse_fsyncdir,
1489 destroy: _PLfuse_destroy,
1490 #endif /* FUSE_VERSION >= 23 */
1491 #if FUSE_VERSION >= 25
1492 access: _PLfuse_access,
1493 create: _PLfuse_create,
1494 ftruncate: _PLfuse_ftruncate,
1495 fgetattr: _PLfuse_fgetattr,
1496 #endif /* FUSE_VERSION >= 25 */
1497 #if FUSE_VERSION >= 26
1499 utimens: _PLfuse_utimens,
1501 #endif /* FUSE_VERSION >= 26 */
1503 #if FUSE_VERSION >= 28
1504 ioctl: _PLfuse_ioctl,
1506 #endif /* FUSE_VERSION >= 28 */
1510 MODULE = Fuse PACKAGE = Fuse
1524 tTHX parent = MY_CXT.self;
1525 MY_CXT.self = my_perl;
1526 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1527 /* CLONE entered without a pointer table, so we can't safely clone static data */
1529 for(i=0;i<N_CALLBACKS;i++) {
1530 MY_CXT.callback[i] = NULL;
1532 MY_CXT.handles = newHV();
1536 CLONE_PARAMS *clone_param;
1537 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1538 clone_param = clone_params_new(parent, aTHX);
1540 CLONE_PARAMS raw_param;
1541 raw_param.flags = 0;
1542 raw_param.proto_perl = parent;
1543 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1544 clone_param = &raw_param;
1546 for(i=0;i<N_CALLBACKS;i++) {
1547 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1549 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1550 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1551 clone_params_del(clone_param);
1558 struct fuse_context *fc;
1560 fc = fuse_get_context();
1563 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1564 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1565 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1566 if (fc->private_data)
1567 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1568 #if FUSE_VERSION >= 28
1569 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1570 #endif /* FUSE_VERSION >= 28 */
1571 RETVAL = newRV_noinc((SV*)hash);
1581 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1589 RETVAL = newSViv(XATTR_CREATE);
1596 RETVAL = newSViv(XATTR_REPLACE);
1605 struct fuse_operations fops;
1609 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1610 struct fuse_chan *fc;
1613 if(items != N_CALLBACKS + 5) {
1614 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1617 memset(&fops, 0, sizeof(struct fuse_operations));
1619 debug = SvIV(ST(0));
1620 MY_CXT.threaded = SvIV(ST(1));
1621 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1622 if(MY_CXT.threaded) {
1623 #ifdef FUSE_USE_ITHREADS
1624 master_interp = aTHX;
1625 MUTEX_INIT(&MY_CXT.mutex);
1626 SvSHARE((SV*)(MY_CXT.handles));
1628 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1629 "mode, but your perl was not built with a supported "
1630 "thread model. Threads are disabled.\n");
1631 MY_CXT.threaded = 0;
1634 mountpoint = SvPV_nolen(ST(2));
1635 mountopts = SvPV_nolen(ST(3));
1636 #if FUSE_VERSION >= 28
1637 fops.flag_nullpath_ok = SvIV(ST(4));
1638 #endif /* FUSE_VERSION >= 28 */
1639 for(i=0;i<N_CALLBACKS;i++) {
1641 /* allow symbolic references, or real code references. */
1642 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1643 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1644 /* Dirty hack, to keep anything from overwriting the
1645 * flag area with a pointer. There should never be
1646 * anything passed as 'junk', but this prevents
1647 * someone from doing it and screwing things up... */
1651 MY_CXT.callback[i] = var;
1652 } else if(SvOK(var)) {
1653 croak("invalid callback (%i) passed to perl_fuse_main "
1654 "(%s is not a string, code ref, or undef).\n",
1655 i+5,SvPVbyte_nolen(var));
1657 MY_CXT.callback[i] = NULL;
1661 * XXX: What comes here is just a ridiculous use of the option parsing API
1662 * to hack on compatibility with other parts of the new API. First and
1663 * foremost, real C argc/argv would be good to get at...
1665 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1666 fuse_opt_free_args(&args);
1667 croak("out of memory\n");
1669 if (mountopts && strcmp("", mountopts) &&
1670 (fuse_opt_add_arg(&args, "-o") == -1 ||
1671 fuse_opt_add_arg(&args, mountopts) == -1)) {
1672 fuse_opt_free_args(&args);
1673 croak("out of memory\n");
1675 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1676 fuse_opt_free_args(&args);
1677 croak("out of memory\n");
1679 fc = fuse_mount(mountpoint,&args);
1681 croak("could not mount fuse filesystem!\n");
1683 if(MY_CXT.threaded) {
1684 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1687 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1688 fuse_unmount(mountpoint,fc);
1689 fuse_opt_free_args(&args);