1 #define PERL_NO_GET_CONTEXT
9 /* Determine if threads support should be included */
12 # define FUSE_USE_ITHREADS
14 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
20 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
21 /* #if FUSE_VERSION >= 28
22 # define N_CALLBACKS 41 */
23 #if FUSE_VERSION >= 26
24 # define N_CALLBACKS 38
25 #elif FUSE_VERSION >= 25
26 # define N_CALLBACKS 35
27 #elif FUSE_VERSION >= 23
28 # define N_CALLBACKS 31
30 # define N_CALLBACKS 25
34 SV *callback[N_CALLBACKS];
42 #ifdef FUSE_USE_ITHREADS
43 tTHX master_interp = NULL;
45 #define CLONE_INTERP(parent) S_clone_interp(parent)
46 tTHX S_clone_interp(tTHX parent) {
47 dMY_CXT_INTERP(parent);
49 MUTEX_LOCK(&MY_CXT.mutex);
50 PERL_SET_CONTEXT(parent);
52 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
53 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
55 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
56 ptr_table_free(PL_ptr_table);
59 MUTEX_UNLOCK(&MY_CXT.mutex);
65 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
66 # define FUSE_CONTEXT_POST }
68 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
69 # define FUSE_CONTEXT_POST
74 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
79 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
80 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
81 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
82 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
84 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
89 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
97 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
99 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
104 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
106 #ifdef FUSE_USE_ITHREADS
107 if(MY_CXT.threaded) {
111 MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
112 fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
113 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
120 int _PLfuse_getattr(const char *file, struct stat *result) {
123 DEBUGf("getattr begin: %s\n",file);
127 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
129 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
133 fprintf(stderr,"inappropriate number of returned values from getattr\n");
140 result->st_blocks = POPi;
141 result->st_blksize = POPi;
142 result->st_ctime = POPi;
143 result->st_mtime = POPi;
144 result->st_atime = POPi;
145 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
146 result->st_rdev = POPi;
147 result->st_gid = POPi;
148 result->st_uid = POPi;
149 result->st_nlink = POPi;
150 result->st_mode = POPi;
151 result->st_ino = POPi;
152 result->st_dev = POPi;
158 DEBUGf("getattr end: %i\n",rv);
163 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
168 DEBUGf("readlink begin\n");
172 XPUSHs(sv_2mortal(newSVpv(file,0)));
174 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
180 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
183 strncpy(buf,SvPV_nolen(mysv),buflen);
191 DEBUGf("readlink end: %i\n",rv);
196 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
200 DEBUGf("getdir begin\n");
204 XPUSHs(sv_2mortal(newSVpv(file,0)));
206 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
209 /* Should yield the bottom of the current stack... */
210 swp = &TOPs - prv + 1;
212 /* Sort of a hack to walk the stack in order, instead of reverse
213 * order - trying to explain to potential users why they need to
214 * reverse the order of this array would be confusing, at best. */
216 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
218 fprintf(stderr,"getdir() handler returned nothing!\n");
224 DEBUGf("getdir end: %i\n",rv);
229 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
232 DEBUGf("mknod begin\n");
236 XPUSHs(sv_2mortal(newSVpv(file,0)));
237 XPUSHs(sv_2mortal(newSViv(mode)));
238 XPUSHs(sv_2mortal(newSViv(dev)));
240 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
242 rv = (rv ? POPi : 0);
246 DEBUGf("mknod end: %i\n",rv);
251 int _PLfuse_mkdir (const char *file, mode_t mode) {
254 DEBUGf("mkdir begin\n");
258 XPUSHs(sv_2mortal(newSVpv(file,0)));
259 XPUSHs(sv_2mortal(newSViv(mode)));
261 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
263 rv = (rv ? POPi : 0);
267 DEBUGf("mkdir end: %i\n",rv);
273 int _PLfuse_unlink (const char *file) {
276 DEBUGf("unlink begin\n");
280 XPUSHs(sv_2mortal(newSVpv(file,0)));
282 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
284 rv = (rv ? POPi : 0);
288 DEBUGf("unlink end: %i\n",rv);
293 int _PLfuse_rmdir (const char *file) {
296 DEBUGf("rmdir begin\n");
300 XPUSHs(sv_2mortal(newSVpv(file,0)));
302 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
304 rv = (rv ? POPi : 0);
308 DEBUGf("rmdir end: %i\n",rv);
313 int _PLfuse_symlink (const char *file, const char *new) {
316 DEBUGf("symlink begin\n");
320 XPUSHs(sv_2mortal(newSVpv(file,0)));
321 XPUSHs(sv_2mortal(newSVpv(new,0)));
323 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
325 rv = (rv ? POPi : 0);
329 DEBUGf("symlink end: %i\n",rv);
334 int _PLfuse_rename (const char *file, const char *new) {
337 DEBUGf("rename begin\n");
341 XPUSHs(sv_2mortal(newSVpv(file,0)));
342 XPUSHs(sv_2mortal(newSVpv(new,0)));
344 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
346 rv = (rv ? POPi : 0);
350 DEBUGf("rename end: %i\n",rv);
355 int _PLfuse_link (const char *file, const char *new) {
358 DEBUGf("link begin\n");
362 XPUSHs(sv_2mortal(newSVpv(file,0)));
363 XPUSHs(sv_2mortal(newSVpv(new,0)));
365 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
367 rv = (rv ? POPi : 0);
371 DEBUGf("link end: %i\n",rv);
376 int _PLfuse_chmod (const char *file, mode_t mode) {
379 DEBUGf("chmod begin\n");
383 XPUSHs(sv_2mortal(newSVpv(file,0)));
384 XPUSHs(sv_2mortal(newSViv(mode)));
386 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
388 rv = (rv ? POPi : 0);
392 DEBUGf("chmod end: %i\n",rv);
397 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
400 DEBUGf("chown begin\n");
404 XPUSHs(sv_2mortal(newSVpv(file,0)));
405 XPUSHs(sv_2mortal(newSViv(uid)));
406 XPUSHs(sv_2mortal(newSViv(gid)));
408 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
410 rv = (rv ? POPi : 0);
414 DEBUGf("chown end: %i\n",rv);
419 int _PLfuse_truncate (const char *file, off_t off) {
421 #ifndef PERL_HAS_64BITINT
425 DEBUGf("truncate begin\n");
429 XPUSHs(sv_2mortal(newSVpv(file,0)));
430 #ifdef PERL_HAS_64BITINT
431 XPUSHs(sv_2mortal(newSViv(off)));
433 if (asprintf(&temp, "%llu", off) == -1)
434 croak("Memory allocation failure!");
435 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
439 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
441 rv = (rv ? POPi : 0);
445 DEBUGf("truncate end: %i\n",rv);
450 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
453 DEBUGf("utime begin\n");
457 XPUSHs(sv_2mortal(newSVpv(file,0)));
458 XPUSHs(sv_2mortal(newSViv(uti->actime)));
459 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
461 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
463 rv = (rv ? POPi : 0);
467 DEBUGf("utime end: %i\n",rv);
472 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
474 int flags = fi->flags;
477 DEBUGf("open begin\n");
481 XPUSHs(sv_2mortal(newSVpv(file,0)));
482 XPUSHs(sv_2mortal(newSViv(flags)));
483 /* Create a hashref containing the details from fi
484 * which we can look at or modify.
486 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
488 #if FUSE_VERSION >= 24
489 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
490 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
492 #if FUSE_VERSION >= 29
493 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
495 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
496 /* All hashref things done */
499 /* Open called with filename, flags */
500 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
504 FH_STOREHANDLE(fi,POPs);
512 /* Success, so copy the file handle which they returned */
513 #if FUSE_VERSION >= 24
515 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
516 fi->direct_io = SvIV(*svp);
517 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
518 fi->keep_cache = SvIV(*svp);
520 #if FUSE_VERSION >= 29
521 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
522 fi->nonseekable = SvIV(*svp);
528 DEBUGf("open end: %i\n",rv);
533 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
534 struct fuse_file_info *fi) {
536 #ifndef PERL_HAS_64BITINT
540 DEBUGf("read begin\n");
544 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
545 XPUSHs(sv_2mortal(newSViv(buflen)));
546 #ifdef PERL_HAS_64BITINT
547 XPUSHs(sv_2mortal(newSViv(off)));
549 if (asprintf(&temp, "%llu", off) == -1)
550 croak("Memory allocation failure!");
551 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
554 XPUSHs(FH_GETHANDLE(fi));
556 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
562 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
571 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
573 memcpy(buf,SvPV_nolen(mysv),rv);
579 DEBUGf("read end: %i\n",rv);
584 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
586 #ifndef PERL_HAS_64BITINT
590 DEBUGf("write begin\n");
594 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
595 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
596 #ifdef PERL_HAS_64BITINT
597 XPUSHs(sv_2mortal(newSViv(off)));
599 if (asprintf(&temp, "%llu", off) == -1)
600 croak("Memory allocation failure!");
601 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
604 XPUSHs(FH_GETHANDLE(fi));
606 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
608 rv = (rv ? POPi : 0);
612 DEBUGf("write end: %i\n",rv);
617 int _PLfuse_statfs (const char *file, struct statvfs *st) {
620 DEBUGf("statfs begin\n");
625 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
627 DEBUGf("statfs got %i params\n",rv);
628 if(rv == 6 || rv == 7) {
634 st->f_namemax = POPi;
635 /* zero and fill-in other */
639 st->f_bavail = st->f_bfree;
640 st->f_favail = st->f_ffree;
648 croak("inappropriate number of returned values from statfs");
657 DEBUGf("statfs end: %i\n",rv);
662 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
665 DEBUGf("flush begin\n");
669 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
670 XPUSHs(FH_GETHANDLE(fi));
672 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
674 rv = (rv ? POPi : 0);
678 DEBUGf("flush end: %i\n",rv);
683 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
685 int flags = fi->flags;
687 DEBUGf("release begin\n");
691 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
692 XPUSHs(sv_2mortal(newSViv(flags)));
693 XPUSHs(FH_GETHANDLE(fi));
695 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
697 rv = (rv ? POPi : 0);
698 FH_RELEASEHANDLE(fi);
702 DEBUGf("release end: %i\n",rv);
707 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
709 int flags = fi->flags;
711 DEBUGf("fsync begin\n");
715 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
716 XPUSHs(sv_2mortal(newSViv(flags)));
717 XPUSHs(FH_GETHANDLE(fi));
719 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
721 rv = (rv ? POPi : 0);
725 DEBUGf("fsync end: %i\n",rv);
730 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
733 DEBUGf("setxattr begin\n");
737 XPUSHs(sv_2mortal(newSVpv(file,0)));
738 XPUSHs(sv_2mortal(newSVpv(name,0)));
739 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
740 XPUSHs(sv_2mortal(newSViv(flags)));
742 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
744 rv = (rv ? POPi : 0);
748 DEBUGf("setxattr end: %i\n",rv);
753 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
756 DEBUGf("getxattr begin\n");
760 XPUSHs(sv_2mortal(newSVpv(file,0)));
761 XPUSHs(sv_2mortal(newSVpv(name,0)));
763 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
771 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
779 if ((rv > 0) && (buflen > 0))
784 memcpy(buf,SvPV_nolen(mysv),rv);
791 DEBUGf("getxattr end: %i\n",rv);
796 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
799 DEBUGf("listxattr begin\n");
803 XPUSHs(sv_2mortal(newSVpv(file,0)));
805 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
818 /* Always nul terminate */
819 if (list && (size > 0))
829 int s = SvCUR(mysv) + 1;
832 if (p && (size > 0) && (spc >= s))
834 memcpy(p,SvPV_nolen(mysv),s);
842 * If the Perl returned an error, return that.
843 * Otherwise check that the buffer was big enough.
848 if ((size > 0) && (size < total_len))
855 DEBUGf("listxattr end: %i\n",rv);
860 int _PLfuse_removexattr (const char *file, const char *name) {
863 DEBUGf("removexattr begin\n");
867 XPUSHs(sv_2mortal(newSVpv(file,0)));
868 XPUSHs(sv_2mortal(newSVpv(name,0)));
870 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
872 rv = (rv ? POPi : 0);
876 DEBUGf("removexattr end: %i\n",rv);
881 #if FUSE_VERSION >= 23
882 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
885 DEBUGf("opendir begin\n");
889 XPUSHs(sv_2mortal(newSVpv(file,0)));
890 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
892 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
896 FH_STOREHANDLE(fi, POPs);
904 DEBUGf("opendir end: %i\n",rv);
910 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
911 off_t off, struct fuse_file_info *fi) {
913 SV *sv, **svp, **swp;
917 #ifndef PERL_HAS_64BITINT
921 DEBUGf("readdir begin\n");
925 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
926 #ifdef PERL_HAS_64BITINT
927 XPUSHs(sv_2mortal(newSViv(off)));
929 if (asprintf(&temp, "%llu", off) == -1)
930 croak("Memory allocation failure!");
931 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
934 XPUSHs(FH_GETHANDLE(fi));
936 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
939 /* Should yield the bottom of the current stack... */
940 swp = &TOPs - prv + 1;
942 memset(&st, 0, sizeof(struct stat));
943 /* Sort of a hack to walk the stack in order, instead of reverse
944 * order - trying to explain to potential users why they need to
945 * reverse the order of this array would be confusing, at best. */
946 while (swp <= &TOPs) {
948 if (!SvROK(sv) && SvPOK(sv))
949 /* Just a bare SV (probably a string; hopefully a string) */
950 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
951 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
952 if (av_len(av) >= 2) {
953 /* The third element of the array should be the args that
954 * would otherwise go to getattr(); a lot of filesystems
955 * will, or at least can, return that info as part of the
956 * enumeration process... */
957 svp = av_fetch(av, 2, FALSE);
959 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
961 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
962 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
963 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
964 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
965 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
966 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
967 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
968 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
969 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
970 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
971 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
972 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
973 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
977 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
978 /* For now if the element isn't what we want, just
979 * quietly ignore it... */
981 if (av_len(av) >= 1) {
982 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
983 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
984 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
987 memset(&st, 0, sizeof(struct stat));
992 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
995 fprintf(stderr,"readdir() handler returned nothing!\n");
1001 DEBUGf("readdir end: %i\n",rv);
1006 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1009 DEBUGf("releasedir begin\n");
1013 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1014 XPUSHs(FH_GETHANDLE(fi));
1016 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1018 rv = (rv ? POPi : 0);
1019 FH_RELEASEHANDLE(fi);
1023 DEBUGf("releasedir end: %i\n",rv);
1028 int _PLfuse_fsyncdir(const char *file, int datasync,
1029 struct fuse_file_info *fi) {
1032 DEBUGf("fsyncdir begin\n");
1036 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1037 XPUSHs(sv_2mortal(newSViv(datasync)));
1038 XPUSHs(FH_GETHANDLE(fi));
1040 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1042 rv = (rv ? POPi : 0);
1046 DEBUGf("fsyncdir end: %i\n",rv);
1051 #if FUSE_VERSION >= 26
1052 void *_PLfuse_init(struct fuse_conn_info *fc)
1053 #else /* FUSE_VERSION < 26 */
1054 void *_PLfuse_init(void)
1055 #endif /* FUSE_VERSION >= 26 */
1060 DEBUGf("init begin\n");
1065 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1069 if (rv == &PL_sv_undef)
1072 rv = SvREFCNT_inc((SV *)rv);
1077 DEBUGf("init end: %p\n", rv);
1082 void _PLfuse_destroy(void *private_data) {
1084 DEBUGf("destroy begin\n");
1088 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1090 call_sv(MY_CXT.callback[30], G_VOID);
1093 SvREFCNT_dec((SV *)private_data);
1097 DEBUGf("init end\n");
1100 #endif /* FUSE_VERSION >= 23 */
1102 #if FUSE_VERSION >= 25
1103 int _PLfuse_access(const char *file, int mask) {
1106 DEBUGf("access begin\n");
1110 XPUSHs(sv_2mortal(newSVpv(file,0)));
1111 XPUSHs(sv_2mortal(newSViv(mask)));
1113 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1115 rv = (rv ? POPi : 0);
1119 DEBUGf("access end: %d\n", rv);
1124 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1128 DEBUGf("create begin\n");
1132 XPUSHs(sv_2mortal(newSVpv(file,0)));
1133 XPUSHs(sv_2mortal(newSViv(mode)));
1134 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1135 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1136 /* Create a hashref containing the details from fi
1137 * which we can look at or modify.
1140 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1141 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1142 #if FUSE_VERSION >= 29
1143 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1145 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1146 /* All hashref things done */
1149 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1153 FH_STOREHANDLE(fi,POPs);
1158 fprintf(stderr, "create() handler returned nothing!\n");
1162 /* Success, so copy the file handle which they returned */
1164 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1165 fi->direct_io = SvIV(*svp);
1166 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1167 fi->keep_cache = SvIV(*svp);
1168 #if FUSE_VERSION >= 29
1169 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1170 fi->nonseekable = SvIV(*svp);
1176 DEBUGf("create end: %d\n",rv);
1181 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1183 #ifndef PERL_HAS_64BITINT
1187 DEBUGf("ftruncate begin\n");
1191 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1192 #ifdef PERL_HAS_64BITINT
1193 XPUSHs(sv_2mortal(newSViv(off)));
1195 if (asprintf(&temp, "%llu", off) == -1)
1196 croak("Memory allocation failure!");
1197 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1200 XPUSHs(FH_GETHANDLE(fi));
1202 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1204 rv = (rv ? POPi : 0);
1208 DEBUGf("ftruncate end: %i\n",rv);
1213 int _PLfuse_fgetattr(const char *file, struct stat *result,
1214 struct fuse_file_info *fi) {
1217 DEBUGf("fgetattr begin: %s\n",file);
1221 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1222 XPUSHs(FH_GETHANDLE(fi));
1224 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1228 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1235 result->st_blocks = POPi;
1236 result->st_blksize = POPi;
1237 result->st_ctime = POPi;
1238 result->st_mtime = POPi;
1239 result->st_atime = POPi;
1240 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1241 result->st_rdev = POPi;
1242 result->st_gid = POPi;
1243 result->st_uid = POPi;
1244 result->st_nlink = POPi;
1245 result->st_mode = POPi;
1246 result->st_ino = POPi;
1247 result->st_dev = POPi;
1253 DEBUGf("fgetattr end: %i\n",rv);
1257 #endif /* FUSE_VERSION >= 25 */
1259 #if FUSE_VERSION >= 26
1260 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1261 struct flock *lockinfo) {
1265 #ifndef PERL_HAS_64BITINT
1269 DEBUGf("lock begin\n");
1273 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1274 XPUSHs(sv_2mortal(newSViv(cmd)));
1277 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1278 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1279 #ifdef PERL_HAS_64BITINT
1280 sv = newSViv(lockinfo->l_start);
1282 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1283 croak("Memory allocation failure!");
1284 sv = newSVpv(temp, 0);
1287 (void) hv_store(lihash, "l_start", 7, sv, 0);
1288 #ifdef PERL_HAS_64BITINT
1289 sv = newSViv(lockinfo->l_len);
1291 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1292 croak("Memory allocation failure!");
1293 sv = newSVpv(temp, 0);
1296 (void) hv_store(lihash, "l_len", 5, sv, 0);
1297 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1299 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1300 XPUSHs(FH_GETHANDLE(fi));
1303 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1305 rv = (rv ? POPi : 0);
1306 if (lockinfo && !rv) {
1307 /* Need to copy back any altered values from the hash into
1310 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1311 lockinfo->l_type = SvIV(*svp);
1312 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1313 lockinfo->l_whence = SvIV(*svp);
1314 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1315 lockinfo->l_start = SvNV(*svp);
1316 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1317 lockinfo->l_len = SvNV(*svp);
1318 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1319 lockinfo->l_pid = SvIV(*svp);
1324 DEBUGf("lock end: %i\n",rv);
1329 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1332 DEBUGf("utimens begin\n");
1336 XPUSHs(sv_2mortal(newSVpv(file,0)));
1337 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1338 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1340 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1342 rv = (rv ? POPi : 0);
1346 DEBUGf("utimens end: %i\n",rv);
1351 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1353 #ifndef PERL_HAS_64BITINT
1357 DEBUGf("bmap begin\n");
1361 XPUSHs(sv_2mortal(newSVpv(file,0)));
1362 XPUSHs(sv_2mortal(newSViv(blocksize)));
1363 #ifdef PERL_HAS_64BITINT
1364 XPUSHs(sv_2mortal(newSViv(*idx)));
1366 if (asprintf(&temp, "%llu", *idx) == -1)
1367 croak("Memory allocation failure!");
1368 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1372 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1374 if (rv > 0 && rv < 3) {
1380 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1386 DEBUGf("bmap end: %i\n",rv);
1390 #endif /* FUSE_VERSION >= 26 */
1393 #if FUSE_VERSION >= 28
1394 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1395 struct fuse_file_info *fi, unsigned int flags, void *data) {
1398 DEBUGf("ioctl begin\n");
1402 XPUSHs(sv_2mortal(newSVpv(file,0)));
1403 XPUSHs(sv_2mortal(newSViv(cmd)));
1404 XPUSHs(sv_2mortal(newSViv(flags)));
1405 if (_IOC_DIR(cmd) & _IOC_READ)
1406 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1408 XPUSHs(&PL_sv_undef);
1409 XPUSHs(FH_GETHANDLE(fi));
1411 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1413 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1417 char *rdata = SvPV(sv, len);
1418 if (len > _IOC_SIZE(cmd)) {
1419 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1423 memset(data, 0, _IOC_SIZE(cmd));
1424 memcpy(data, rdata, len);
1430 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1439 DEBUGf("ioctl end: %i\n",rv);
1444 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1445 struct fuse_pollhandle *ph, unsigned *reventsp) {
1448 #endif /* FUSE_VERSION >= 28 */
1451 struct fuse_operations _available_ops = {
1452 getattr: _PLfuse_getattr,
1453 readlink: _PLfuse_readlink,
1454 getdir: _PLfuse_getdir,
1455 mknod: _PLfuse_mknod,
1456 mkdir: _PLfuse_mkdir,
1457 unlink: _PLfuse_unlink,
1458 rmdir: _PLfuse_rmdir,
1459 symlink: _PLfuse_symlink,
1460 rename: _PLfuse_rename,
1462 chmod: _PLfuse_chmod,
1463 chown: _PLfuse_chown,
1464 truncate: _PLfuse_truncate,
1465 utime: _PLfuse_utime,
1468 write: _PLfuse_write,
1469 statfs: _PLfuse_statfs,
1470 flush: _PLfuse_flush,
1471 release: _PLfuse_release,
1472 fsync: _PLfuse_fsync,
1473 setxattr: _PLfuse_setxattr,
1474 getxattr: _PLfuse_getxattr,
1475 listxattr: _PLfuse_listxattr,
1476 removexattr: _PLfuse_removexattr,
1477 #if FUSE_VERSION >= 23
1478 opendir: _PLfuse_opendir,
1479 readdir: _PLfuse_readdir,
1480 releasedir: _PLfuse_releasedir,
1481 fsyncdir: _PLfuse_fsyncdir,
1483 destroy: _PLfuse_destroy,
1484 #endif /* FUSE_VERSION >= 23 */
1485 #if FUSE_VERSION >= 25
1486 access: _PLfuse_access,
1487 create: _PLfuse_create,
1488 ftruncate: _PLfuse_ftruncate,
1489 fgetattr: _PLfuse_fgetattr,
1490 #endif /* FUSE_VERSION >= 25 */
1491 #if FUSE_VERSION >= 26
1493 utimens: _PLfuse_utimens,
1495 #endif /* FUSE_VERSION >= 26 */
1497 #if FUSE_VERSION >= 28
1498 ioctl: _PLfuse_ioctl,
1500 #endif /* FUSE_VERSION >= 28 */
1504 MODULE = Fuse PACKAGE = Fuse
1518 tTHX parent = MY_CXT.self;
1519 MY_CXT.self = my_perl;
1520 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1521 /* CLONE entered without a pointer table, so we can't safely clone static data */
1523 for(i=0;i<N_CALLBACKS;i++) {
1524 MY_CXT.callback[i] = NULL;
1526 MY_CXT.handles = newHV();
1530 CLONE_PARAMS *clone_param;
1531 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1532 clone_param = clone_params_new(parent, aTHX);
1534 CLONE_PARAMS raw_param;
1535 raw_param.flags = 0;
1536 raw_param.proto_perl = parent;
1537 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1538 clone_param = &raw_param;
1540 for(i=0;i<N_CALLBACKS;i++) {
1541 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1543 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1544 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1545 clone_params_del(clone_param);
1552 struct fuse_context *fc;
1554 fc = fuse_get_context();
1557 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1558 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1559 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1560 if (fc->private_data)
1561 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1562 #if FUSE_VERSION >= 28
1563 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1564 #endif /* FUSE_VERSION >= 28 */
1565 RETVAL = newRV_noinc((SV*)hash);
1575 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1582 RETVAL = newSViv(XATTR_CREATE);
1589 RETVAL = newSViv(XATTR_REPLACE);
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...
1656 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1657 fuse_opt_free_args(&args);
1658 croak("out of memory\n");
1661 fuse_opt_add_arg(&args, "-o") == -1 ||
1662 fuse_opt_add_arg(&args, mountopts) == -1)) {
1663 fuse_opt_free_args(&args);
1664 croak("out of memory\n");
1666 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1667 fuse_opt_free_args(&args);
1668 croak("out of memory\n");
1670 fc = fuse_mount(mountpoint,&args);
1672 croak("could not mount fuse filesystem!\n");
1674 if(MY_CXT.threaded) {
1675 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1678 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1679 fuse_unmount(mountpoint,fc);
1680 fuse_opt_free_args(&args);