1 #define PERL_NO_GET_CONTEXT
8 /* Determine if threads support should be included */
11 # define FUSE_USE_ITHREADS
13 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
19 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
20 /* #if FUSE_VERSION >= 28
21 # define N_CALLBACKS 41 */
22 #if FUSE_VERSION >= 26
23 # define N_CALLBACKS 38
24 #elif FUSE_VERSION >= 25
25 # define N_CALLBACKS 35
26 #elif FUSE_VERSION >= 23
27 # define N_CALLBACKS 31
29 # define N_CALLBACKS 25
33 SV *callback[N_CALLBACKS];
41 #ifdef FUSE_USE_ITHREADS
42 tTHX master_interp = NULL;
44 #define CLONE_INTERP(parent) S_clone_interp(parent)
45 tTHX S_clone_interp(tTHX parent) {
46 dMY_CXT_INTERP(parent);
48 MUTEX_LOCK(&MY_CXT.mutex);
49 PERL_SET_CONTEXT(parent);
51 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
52 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
54 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
55 ptr_table_free(PL_ptr_table);
58 MUTEX_UNLOCK(&MY_CXT.mutex);
64 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
65 # define FUSE_CONTEXT_POST }
67 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
68 # define FUSE_CONTEXT_POST
73 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
78 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
79 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
80 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
81 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
83 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
88 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
96 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
98 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
103 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
105 #ifdef FUSE_USE_ITHREADS
106 if(MY_CXT.threaded) {
110 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
111 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
112 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
119 int _PLfuse_getattr(const char *file, struct stat *result) {
122 DEBUGf("getattr begin: %s\n",file);
126 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
128 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
132 fprintf(stderr,"inappropriate number of returned values from getattr\n");
139 result->st_blocks = POPi;
140 result->st_blksize = POPi;
141 result->st_ctime = POPi;
142 result->st_mtime = POPi;
143 result->st_atime = POPi;
144 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
145 result->st_rdev = POPi;
146 result->st_gid = POPi;
147 result->st_uid = POPi;
148 result->st_nlink = POPi;
149 result->st_mode = POPi;
150 result->st_ino = POPi;
151 result->st_dev = POPi;
157 DEBUGf("getattr end: %i\n",rv);
162 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
167 DEBUGf("readlink begin\n");
171 XPUSHs(sv_2mortal(newSVpv(file,0)));
173 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
179 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
182 strncpy(buf,SvPV_nolen(mysv),buflen);
190 DEBUGf("readlink end: %i\n",rv);
195 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
199 DEBUGf("getdir begin\n");
203 XPUSHs(sv_2mortal(newSVpv(file,0)));
205 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
208 /* Should yield the bottom of the current stack... */
209 swp = &TOPs - prv + 1;
211 /* Sort of a hack to walk the stack in order, instead of reverse
212 * order - trying to explain to potential users why they need to
213 * reverse the order of this array would be confusing, at best. */
215 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
217 fprintf(stderr,"getdir() handler returned nothing!\n");
223 DEBUGf("getdir end: %i\n",rv);
228 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
231 DEBUGf("mknod begin\n");
235 XPUSHs(sv_2mortal(newSVpv(file,0)));
236 XPUSHs(sv_2mortal(newSViv(mode)));
237 XPUSHs(sv_2mortal(newSViv(dev)));
239 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
241 rv = (rv ? POPi : 0);
245 DEBUGf("mknod end: %i\n",rv);
250 int _PLfuse_mkdir (const char *file, mode_t mode) {
253 DEBUGf("mkdir begin\n");
257 XPUSHs(sv_2mortal(newSVpv(file,0)));
258 XPUSHs(sv_2mortal(newSViv(mode)));
260 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
262 rv = (rv ? POPi : 0);
266 DEBUGf("mkdir end: %i\n",rv);
272 int _PLfuse_unlink (const char *file) {
275 DEBUGf("unlink begin\n");
279 XPUSHs(sv_2mortal(newSVpv(file,0)));
281 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
283 rv = (rv ? POPi : 0);
287 DEBUGf("unlink end: %i\n",rv);
292 int _PLfuse_rmdir (const char *file) {
295 DEBUGf("rmdir begin\n");
299 XPUSHs(sv_2mortal(newSVpv(file,0)));
301 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
303 rv = (rv ? POPi : 0);
307 DEBUGf("rmdir end: %i\n",rv);
312 int _PLfuse_symlink (const char *file, const char *new) {
315 DEBUGf("symlink begin\n");
319 XPUSHs(sv_2mortal(newSVpv(file,0)));
320 XPUSHs(sv_2mortal(newSVpv(new,0)));
322 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
324 rv = (rv ? POPi : 0);
328 DEBUGf("symlink end: %i\n",rv);
333 int _PLfuse_rename (const char *file, const char *new) {
336 DEBUGf("rename begin\n");
340 XPUSHs(sv_2mortal(newSVpv(file,0)));
341 XPUSHs(sv_2mortal(newSVpv(new,0)));
343 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
345 rv = (rv ? POPi : 0);
349 DEBUGf("rename end: %i\n",rv);
354 int _PLfuse_link (const char *file, const char *new) {
357 DEBUGf("link begin\n");
361 XPUSHs(sv_2mortal(newSVpv(file,0)));
362 XPUSHs(sv_2mortal(newSVpv(new,0)));
364 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
366 rv = (rv ? POPi : 0);
370 DEBUGf("link end: %i\n",rv);
375 int _PLfuse_chmod (const char *file, mode_t mode) {
378 DEBUGf("chmod begin\n");
382 XPUSHs(sv_2mortal(newSVpv(file,0)));
383 XPUSHs(sv_2mortal(newSViv(mode)));
385 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
387 rv = (rv ? POPi : 0);
391 DEBUGf("chmod end: %i\n",rv);
396 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
399 DEBUGf("chown begin\n");
403 XPUSHs(sv_2mortal(newSVpv(file,0)));
404 XPUSHs(sv_2mortal(newSViv(uid)));
405 XPUSHs(sv_2mortal(newSViv(gid)));
407 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
409 rv = (rv ? POPi : 0);
413 DEBUGf("chown end: %i\n",rv);
418 int _PLfuse_truncate (const char *file, off_t off) {
420 #ifndef PERL_HAS_64BITINT
424 DEBUGf("truncate begin\n");
428 XPUSHs(sv_2mortal(newSVpv(file,0)));
429 #ifdef PERL_HAS_64BITINT
430 XPUSHs(sv_2mortal(newSViv(off)));
432 if (asprintf(&temp, "%llu", off) == -1)
433 croak("Memory allocation failure!");
434 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
438 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
440 rv = (rv ? POPi : 0);
444 DEBUGf("truncate end: %i\n",rv);
449 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
452 DEBUGf("utime begin\n");
456 XPUSHs(sv_2mortal(newSVpv(file,0)));
457 XPUSHs(sv_2mortal(newSViv(uti->actime)));
458 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
460 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
462 rv = (rv ? POPi : 0);
466 DEBUGf("utime end: %i\n",rv);
471 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
473 int flags = fi->flags;
476 DEBUGf("open begin\n");
480 XPUSHs(sv_2mortal(newSVpv(file,0)));
481 XPUSHs(sv_2mortal(newSViv(flags)));
482 /* Create a hashref containing the details from fi
483 * which we can look at or modify.
485 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
487 #if FUSE_VERSION >= 24
488 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
489 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
491 #if FUSE_VERSION >= 29
492 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
494 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
495 /* All hashref things done */
498 /* Open called with filename, flags */
499 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
503 FH_STOREHANDLE(fi,POPs);
511 /* Success, so copy the file handle which they returned */
512 #if FUSE_VERSION >= 24
514 svp = hv_fetch(fihash, "direct_io", 9, 0);
517 fi->direct_io = SvIV(*svp);
519 svp = hv_fetch(fihash, "keep_cache", 10, 0);
522 fi->keep_cache = SvIV(*svp);
525 #if FUSE_VERSION >= 29
526 svp = hv_fetch(fihash, "nonseekable", 11, 0);
529 fi->nonseekable = SvIV(*svp);
536 DEBUGf("open end: %i\n",rv);
541 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
543 #ifndef PERL_HAS_64BITINT
547 DEBUGf("read begin\n");
551 XPUSHs(sv_2mortal(newSVpv(file,0)));
552 XPUSHs(sv_2mortal(newSViv(buflen)));
553 #ifdef PERL_HAS_64BITINT
554 XPUSHs(sv_2mortal(newSViv(off)));
556 if (asprintf(&temp, "%llu", off) == -1)
557 croak("Memory allocation failure!");
558 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
561 XPUSHs(FH_GETHANDLE(fi));
563 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
569 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
578 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
580 memcpy(buf,SvPV_nolen(mysv),rv);
586 DEBUGf("read end: %i\n",rv);
591 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
593 #ifndef PERL_HAS_64BITINT
597 DEBUGf("write begin\n");
601 XPUSHs(sv_2mortal(newSVpv(file,0)));
602 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
603 #ifdef PERL_HAS_64BITINT
604 XPUSHs(sv_2mortal(newSViv(off)));
606 if (asprintf(&temp, "%llu", off) == -1)
607 croak("Memory allocation failure!");
608 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
611 XPUSHs(FH_GETHANDLE(fi));
613 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
615 rv = (rv ? POPi : 0);
619 DEBUGf("write end: %i\n",rv);
624 int _PLfuse_statfs (const char *file, struct statvfs *st) {
627 DEBUGf("statfs begin\n");
632 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
634 DEBUGf("statfs got %i params\n",rv);
635 if(rv == 6 || rv == 7) {
641 st->f_namemax = POPi;
642 /* zero and fill-in other */
646 st->f_bavail = st->f_bfree;
647 st->f_favail = st->f_ffree;
655 croak("inappropriate number of returned values from statfs");
664 DEBUGf("statfs end: %i\n",rv);
669 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
672 DEBUGf("flush begin\n");
676 XPUSHs(sv_2mortal(newSVpv(file,0)));
677 XPUSHs(FH_GETHANDLE(fi));
679 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
681 rv = (rv ? POPi : 0);
685 DEBUGf("flush end: %i\n",rv);
690 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
692 int flags = fi->flags;
694 DEBUGf("release begin\n");
698 XPUSHs(sv_2mortal(newSVpv(file,0)));
699 XPUSHs(sv_2mortal(newSViv(flags)));
700 XPUSHs(FH_GETHANDLE(fi));
702 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
704 rv = (rv ? POPi : 0);
705 FH_RELEASEHANDLE(fi);
709 DEBUGf("release end: %i\n",rv);
714 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
716 int flags = fi->flags;
718 DEBUGf("fsync begin\n");
722 XPUSHs(sv_2mortal(newSVpv(file,0)));
723 XPUSHs(sv_2mortal(newSViv(flags)));
724 XPUSHs(FH_GETHANDLE(fi));
726 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
728 rv = (rv ? POPi : 0);
732 DEBUGf("fsync end: %i\n",rv);
737 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
740 DEBUGf("setxattr begin\n");
744 XPUSHs(sv_2mortal(newSVpv(file,0)));
745 XPUSHs(sv_2mortal(newSVpv(name,0)));
746 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
747 XPUSHs(sv_2mortal(newSViv(flags)));
749 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
751 rv = (rv ? POPi : 0);
755 DEBUGf("setxattr end: %i\n",rv);
760 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
763 DEBUGf("getxattr begin\n");
767 XPUSHs(sv_2mortal(newSVpv(file,0)));
768 XPUSHs(sv_2mortal(newSVpv(name,0)));
770 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
778 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
786 if ((rv > 0) && (buflen > 0))
791 memcpy(buf,SvPV_nolen(mysv),rv);
798 DEBUGf("getxattr end: %i\n",rv);
803 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
806 DEBUGf("listxattr begin\n");
810 XPUSHs(sv_2mortal(newSVpv(file,0)));
812 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
825 /* Always nul terminate */
826 if (list && (size > 0))
836 int s = SvCUR(mysv) + 1;
839 if (p && (size > 0) && (spc >= s))
841 memcpy(p,SvPV_nolen(mysv),s);
849 * If the Perl returned an error, return that.
850 * Otherwise check that the buffer was big enough.
855 if ((size > 0) && (size < total_len))
862 DEBUGf("listxattr end: %i\n",rv);
867 int _PLfuse_removexattr (const char *file, const char *name) {
870 DEBUGf("removexattr begin\n");
874 XPUSHs(sv_2mortal(newSVpv(file,0)));
875 XPUSHs(sv_2mortal(newSVpv(name,0)));
877 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
879 rv = (rv ? POPi : 0);
883 DEBUGf("removexattr end: %i\n",rv);
888 #if FUSE_VERSION >= 23
889 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
892 DEBUGf("opendir begin\n");
896 XPUSHs(sv_2mortal(newSVpv(file,0)));
897 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
899 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
903 FH_STOREHANDLE(fi, POPs);
911 DEBUGf("opendir end: %i\n",rv);
917 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
918 off_t off, struct fuse_file_info *fi) {
920 SV *sv, **svp, **swp;
924 #ifndef PERL_HAS_64BITINT
928 DEBUGf("readdir begin\n");
932 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
933 #ifdef PERL_HAS_64BITINT
934 XPUSHs(sv_2mortal(newSViv(off)));
936 if (asprintf(&temp, "%llu", off) == -1)
937 croak("Memory allocation failure!");
938 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
941 XPUSHs(FH_GETHANDLE(fi));
943 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
946 /* Should yield the bottom of the current stack... */
947 swp = &TOPs - prv + 1;
949 memset(&st, 0, sizeof(struct stat));
950 /* Sort of a hack to walk the stack in order, instead of reverse
951 * order - trying to explain to potential users why they need to
952 * reverse the order of this array would be confusing, at best. */
953 while (swp <= &TOPs) {
955 if (!SvROK(sv) && SvPOK(sv))
956 /* Just a bare SV (probably a string; hopefully a string) */
957 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
958 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
959 if (av_len(av) >= 2) {
960 /* The third element of the array should be the args that
961 * would otherwise go to getattr(); a lot of filesystems
962 * will, or at least can, return that info as part of the
963 * enumeration process... */
964 svp = av_fetch(av, 2, FALSE);
966 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
968 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
969 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
970 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
971 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
972 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
973 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
974 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
975 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
976 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
977 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
978 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
979 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
980 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
984 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
985 /* For now if the element isn't what we want, just
986 * quietly ignore it... */
988 if (av_len(av) >= 1) {
989 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
990 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
991 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
994 memset(&st, 0, sizeof(struct stat));
999 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1002 fprintf(stderr,"readdir() handler returned nothing!\n");
1008 DEBUGf("readdir end: %i\n",rv);
1013 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1016 DEBUGf("releasedir begin\n");
1020 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1021 XPUSHs(FH_GETHANDLE(fi));
1023 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1025 rv = (rv ? POPi : 0);
1026 FH_RELEASEHANDLE(fi);
1030 DEBUGf("releasedir end: %i\n",rv);
1035 int _PLfuse_fsyncdir(const char *file, int datasync,
1036 struct fuse_file_info *fi) {
1039 DEBUGf("fsyncdir begin\n");
1043 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1044 XPUSHs(sv_2mortal(newSViv(datasync)));
1045 XPUSHs(FH_GETHANDLE(fi));
1047 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1049 rv = (rv ? POPi : 0);
1053 DEBUGf("fsyncdir end: %i\n",rv);
1058 #if FUSE_VERSION >= 26
1059 void *_PLfuse_init(struct fuse_conn_info *fc)
1060 #else /* FUSE_VERSION < 26 */
1061 void *_PLfuse_init(void)
1062 #endif /* FUSE_VERSION >= 26 */
1067 DEBUGf("init begin\n");
1072 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1076 if (rv == &PL_sv_undef)
1079 rv = SvREFCNT_inc((SV *)rv);
1084 DEBUGf("init end: %p\n", rv);
1089 void _PLfuse_destroy(void *private_data) {
1091 DEBUGf("destroy begin\n");
1095 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1097 call_sv(MY_CXT.callback[30], G_VOID);
1100 SvREFCNT_dec((SV *)private_data);
1104 DEBUGf("init end\n");
1107 #endif /* FUSE_VERSION >= 23 */
1109 #if FUSE_VERSION >= 25
1110 int _PLfuse_access(const char *file, int mask) {
1113 DEBUGf("access begin\n");
1117 XPUSHs(sv_2mortal(newSVpv(file,0)));
1118 XPUSHs(sv_2mortal(newSViv(mask)));
1120 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1122 rv = (rv ? POPi : 0);
1126 DEBUGf("access end: %d\n", rv);
1131 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1135 DEBUGf("create begin\n");
1139 XPUSHs(sv_2mortal(newSVpv(file,0)));
1140 XPUSHs(sv_2mortal(newSViv(mode)));
1141 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1142 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1143 /* Create a hashref containing the details from fi
1144 * which we can look at or modify.
1147 #if FUSE_VERSION >= 24
1148 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1149 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1151 #if FUSE_VERSION >= 29
1152 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1154 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1155 /* All hashref things done */
1158 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1162 FH_STOREHANDLE(fi,POPs);
1167 fprintf(stderr, "create() handler returned nothing!\n");
1171 /* Success, so copy the file handle which they returned */
1172 #if FUSE_VERSION >= 24
1174 svp = hv_fetch(fihash, "direct_io", 9, 0);
1176 fi->direct_io = SvIV(*svp);
1177 svp = hv_fetch(fihash, "keep_cache", 10, 0);
1179 fi->keep_cache = SvIV(*svp);
1181 #if FUSE_VERSION >= 29
1182 svp = hv_fetch(fihash, "nonseekable", 11, 0);
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)))
1325 lockinfo->l_type = SvIV(*svp);
1326 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)))
1327 lockinfo->l_whence = SvIV(*svp);
1328 if ((svp = hv_fetch(lihash, "l_start", 7, 0)))
1329 lockinfo->l_start = SvNV(*svp);
1330 if ((svp = hv_fetch(lihash, "l_len", 5, 0)))
1331 lockinfo->l_len = SvNV(*svp);
1332 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)))
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 rename: _PLfuse_rename,
1475 chmod: _PLfuse_chmod,
1476 chown: _PLfuse_chown,
1477 truncate: _PLfuse_truncate,
1478 utime: _PLfuse_utime,
1481 write: _PLfuse_write,
1482 statfs: _PLfuse_statfs,
1483 flush: _PLfuse_flush,
1484 release: _PLfuse_release,
1485 fsync: _PLfuse_fsync,
1486 setxattr: _PLfuse_setxattr,
1487 getxattr: _PLfuse_getxattr,
1488 listxattr: _PLfuse_listxattr,
1489 removexattr: _PLfuse_removexattr,
1490 #if FUSE_VERSION >= 23
1491 opendir: _PLfuse_opendir,
1492 readdir: _PLfuse_readdir,
1493 releasedir: _PLfuse_releasedir,
1494 fsyncdir: _PLfuse_fsyncdir,
1496 destroy: _PLfuse_destroy,
1497 #endif /* FUSE_VERSION >= 23 */
1498 #if FUSE_VERSION >= 25
1499 access: _PLfuse_access,
1500 create: _PLfuse_create,
1501 ftruncate: _PLfuse_ftruncate,
1502 fgetattr: _PLfuse_fgetattr,
1503 #endif /* FUSE_VERSION >= 25 */
1504 #if FUSE_VERSION >= 26
1506 utimens: _PLfuse_utimens,
1508 #endif /* FUSE_VERSION >= 26 */
1510 #if FUSE_VERSION >= 28
1511 ioctl: _PLfuse_ioctl,
1513 #endif /* FUSE_VERSION >= 28 */
1517 MODULE = Fuse PACKAGE = Fuse
1531 tTHX parent = MY_CXT.self;
1532 MY_CXT.self = my_perl;
1533 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1534 /* CLONE entered without a pointer table, so we can't safely clone static data */
1536 for(i=0;i<N_CALLBACKS;i++) {
1537 MY_CXT.callback[i] = NULL;
1539 MY_CXT.handles = newHV();
1543 CLONE_PARAMS *clone_param;
1544 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1545 clone_param = clone_params_new(parent, aTHX);
1547 CLONE_PARAMS raw_param;
1548 raw_param.flags = 0;
1549 raw_param.proto_perl = parent;
1550 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1551 clone_param = &raw_param;
1553 for(i=0;i<N_CALLBACKS;i++) {
1554 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1556 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1557 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1558 clone_params_del(clone_param);
1565 struct fuse_context *fc;
1567 fc = fuse_get_context();
1570 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1571 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1572 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1573 if (fc->private_data)
1574 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1575 #if FUSE_VERSION >= 28
1576 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1577 #endif /* FUSE_VERSION >= 28 */
1579 RETVAL = newRV_noinc((SV*)hash);
1589 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1596 struct fuse_operations fops;
1600 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1601 struct fuse_chan *fc;
1604 if(items != N_CALLBACKS + 5) {
1605 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1608 memset(&fops, 0, sizeof(struct fuse_operations));
1610 debug = SvIV(ST(0));
1611 MY_CXT.threaded = SvIV(ST(1));
1612 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1613 if(MY_CXT.threaded) {
1614 #ifdef FUSE_USE_ITHREADS
1615 master_interp = aTHX;
1616 MUTEX_INIT(&MY_CXT.mutex);
1617 SvSHARE((SV*)(MY_CXT.handles));
1619 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1620 "mode, but your perl was not built with a supported "
1621 "thread model. Threads are disabled.\n");
1622 MY_CXT.threaded = 0;
1625 mountpoint = SvPV_nolen(ST(2));
1626 mountopts = SvPV_nolen(ST(3));
1627 #if FUSE_VERSION >= 28
1628 fops.flag_nullpath_ok = SvIV(ST(4));
1629 #endif /* FUSE_VERSION >= 28 */
1630 for(i=0;i<N_CALLBACKS;i++) {
1632 /* allow symbolic references, or real code references. */
1633 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1634 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1635 /* Dirty hack, to keep anything from overwriting the
1636 * flag area with a pointer. There should never be
1637 * anything passed as 'junk', but this prevents
1638 * someone from doing it and screwing things up... */
1642 MY_CXT.callback[i] = var;
1643 } else if(SvOK(var)) {
1644 croak("invalid callback (%i) passed to perl_fuse_main "
1645 "(%s is not a string, code ref, or undef).\n",
1646 i+4,SvPVbyte_nolen(var));
1648 MY_CXT.callback[i] = NULL;
1652 * XXX: What comes here is just a ridiculous use of the option parsing API
1653 * to hack on compatibility with other parts of the new API. First and
1654 * foremost, real C argc/argv would be good to get at...
1657 (fuse_opt_add_arg(&args, "") == -1 ||
1658 fuse_opt_add_arg(&args, "-o") == -1 ||
1659 fuse_opt_add_arg(&args, mountopts) == -1)) {
1660 fuse_opt_free_args(&args);
1661 croak("out of memory\n");
1663 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1664 fuse_opt_free_args(&args);
1665 croak("out of memory\n");
1667 fc = fuse_mount(mountpoint,&args);
1669 croak("could not mount fuse filesystem!\n");
1671 if(MY_CXT.threaded) {
1672 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1675 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1676 fuse_unmount(mountpoint,fc);
1677 fuse_opt_free_args(&args);