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);
248 DEBUGf("mknod end: %i\n",rv);
253 int _PLfuse_mkdir (const char *file, mode_t mode) {
256 DEBUGf("mkdir begin\n");
260 XPUSHs(sv_2mortal(newSVpv(file,0)));
261 XPUSHs(sv_2mortal(newSViv(mode)));
263 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
272 DEBUGf("mkdir end: %i\n",rv);
278 int _PLfuse_unlink (const char *file) {
281 DEBUGf("unlink begin\n");
285 XPUSHs(sv_2mortal(newSVpv(file,0)));
287 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
296 DEBUGf("unlink end: %i\n",rv);
301 int _PLfuse_rmdir (const char *file) {
304 DEBUGf("rmdir begin\n");
308 XPUSHs(sv_2mortal(newSVpv(file,0)));
310 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
319 DEBUGf("rmdir end: %i\n",rv);
324 int _PLfuse_symlink (const char *file, const char *new) {
327 DEBUGf("symlink begin\n");
331 XPUSHs(sv_2mortal(newSVpv(file,0)));
332 XPUSHs(sv_2mortal(newSVpv(new,0)));
334 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
343 DEBUGf("symlink end: %i\n",rv);
348 int _PLfuse_rename (const char *file, const char *new) {
351 DEBUGf("rename begin\n");
355 XPUSHs(sv_2mortal(newSVpv(file,0)));
356 XPUSHs(sv_2mortal(newSVpv(new,0)));
358 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
367 DEBUGf("rename end: %i\n",rv);
372 int _PLfuse_link (const char *file, const char *new) {
375 DEBUGf("link begin\n");
379 XPUSHs(sv_2mortal(newSVpv(file,0)));
380 XPUSHs(sv_2mortal(newSVpv(new,0)));
382 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
391 DEBUGf("link end: %i\n",rv);
396 int _PLfuse_chmod (const char *file, mode_t mode) {
399 DEBUGf("chmod begin\n");
403 XPUSHs(sv_2mortal(newSVpv(file,0)));
404 XPUSHs(sv_2mortal(newSViv(mode)));
406 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
415 DEBUGf("chmod end: %i\n",rv);
420 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
423 DEBUGf("chown begin\n");
427 XPUSHs(sv_2mortal(newSVpv(file,0)));
428 XPUSHs(sv_2mortal(newSViv(uid)));
429 XPUSHs(sv_2mortal(newSViv(gid)));
431 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
440 DEBUGf("chown end: %i\n",rv);
445 int _PLfuse_truncate (const char *file, off_t off) {
447 #ifndef PERL_HAS_64BITINT
451 DEBUGf("truncate begin\n");
455 XPUSHs(sv_2mortal(newSVpv(file,0)));
456 #ifdef PERL_HAS_64BITINT
457 XPUSHs(sv_2mortal(newSViv(off)));
459 asprintf(&temp, "%llu", off);
460 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
464 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
473 DEBUGf("truncate end: %i\n",rv);
478 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
481 DEBUGf("utime begin\n");
485 XPUSHs(sv_2mortal(newSVpv(file,0)));
486 XPUSHs(sv_2mortal(newSViv(uti->actime)));
487 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
489 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
498 DEBUGf("utime end: %i\n",rv);
503 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
505 int flags = fi->flags;
508 DEBUGf("open begin\n");
512 XPUSHs(sv_2mortal(newSVpv(file,0)));
513 XPUSHs(sv_2mortal(newSViv(flags)));
514 /* Create a hashref containing the details from fi
515 * which we can look at or modify.
517 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
519 #if FUSE_VERSION >= 24
520 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
521 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
523 #if FUSE_VERSION >= 29
524 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
526 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
527 /* All hashref things done */
530 /* Open called with filename, flags */
531 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
535 FH_STOREHANDLE(fi,POPs);
543 /* Success, so copy the file handle which they returned */
544 #if FUSE_VERSION >= 24
546 svp = hv_fetch(fihash, "direct_io", 9, 0);
549 fi->direct_io = SvIV(*svp);
551 svp = hv_fetch(fihash, "keep_cache", 10, 0);
554 fi->keep_cache = SvIV(*svp);
557 #if FUSE_VERSION >= 29
558 svp = hv_fetch(fihash, "nonseekable", 11, 0);
561 fi->nonseekable = SvIV(*svp);
568 DEBUGf("open end: %i\n",rv);
573 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
575 #ifndef PERL_HAS_64BITINT
579 DEBUGf("read begin\n");
583 XPUSHs(sv_2mortal(newSVpv(file,0)));
584 XPUSHs(sv_2mortal(newSViv(buflen)));
585 #ifdef PERL_HAS_64BITINT
586 XPUSHs(sv_2mortal(newSViv(off)));
588 asprintf(&temp, "%llu", off);
589 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
592 XPUSHs(FH_GETHANDLE(fi));
594 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
600 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
609 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
611 memcpy(buf,SvPV_nolen(mysv),rv);
617 DEBUGf("read end: %i\n",rv);
622 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
624 #ifndef PERL_HAS_64BITINT
628 DEBUGf("write begin\n");
632 XPUSHs(sv_2mortal(newSVpv(file,0)));
633 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
634 #ifdef PERL_HAS_64BITINT
635 XPUSHs(sv_2mortal(newSViv(off)));
637 asprintf(&temp, "%llu", off);
638 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
641 XPUSHs(FH_GETHANDLE(fi));
643 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
652 DEBUGf("write end: %i\n",rv);
657 int _PLfuse_statfs (const char *file, struct statvfs *st) {
660 DEBUGf("statfs begin\n");
665 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
667 DEBUGf("statfs got %i params\n",rv);
668 if(rv == 6 || rv == 7) {
674 st->f_namemax = POPi;
675 /* zero and fill-in other */
679 st->f_bavail = st->f_bfree;
680 st->f_favail = st->f_ffree;
688 croak("inappropriate number of returned values from statfs");
697 DEBUGf("statfs end: %i\n",rv);
702 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
705 DEBUGf("flush begin\n");
709 XPUSHs(sv_2mortal(newSVpv(file,0)));
710 XPUSHs(FH_GETHANDLE(fi));
712 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
721 DEBUGf("flush end: %i\n",rv);
726 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
728 int flags = fi->flags;
730 DEBUGf("release begin\n");
734 XPUSHs(sv_2mortal(newSVpv(file,0)));
735 XPUSHs(sv_2mortal(newSViv(flags)));
736 XPUSHs(FH_GETHANDLE(fi));
738 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
744 FH_RELEASEHANDLE(fi);
748 DEBUGf("release end: %i\n",rv);
753 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
755 int flags = fi->flags;
757 DEBUGf("fsync begin\n");
761 XPUSHs(sv_2mortal(newSVpv(file,0)));
762 XPUSHs(sv_2mortal(newSViv(flags)));
763 XPUSHs(FH_GETHANDLE(fi));
765 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
774 DEBUGf("fsync end: %i\n",rv);
779 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
782 DEBUGf("setxattr begin\n");
786 XPUSHs(sv_2mortal(newSVpv(file,0)));
787 XPUSHs(sv_2mortal(newSVpv(name,0)));
788 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
789 XPUSHs(sv_2mortal(newSViv(flags)));
791 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
800 DEBUGf("setxattr end: %i\n",rv);
805 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
808 DEBUGf("getxattr begin\n");
812 XPUSHs(sv_2mortal(newSVpv(file,0)));
813 XPUSHs(sv_2mortal(newSVpv(name,0)));
815 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
823 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
831 if ((rv > 0) && (buflen > 0))
836 memcpy(buf,SvPV_nolen(mysv),rv);
843 DEBUGf("getxattr end: %i\n",rv);
848 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
851 DEBUGf("listxattr begin\n");
855 XPUSHs(sv_2mortal(newSVpv(file,0)));
857 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
870 /* Always nul terminate */
871 if (list && (size > 0))
881 int s = SvCUR(mysv) + 1;
884 if (p && (size > 0) && (spc >= s))
886 memcpy(p,SvPV_nolen(mysv),s);
894 * If the Perl returned an error, return that.
895 * Otherwise check that the buffer was big enough.
900 if ((size > 0) && (size < total_len))
907 DEBUGf("listxattr end: %i\n",rv);
912 int _PLfuse_removexattr (const char *file, const char *name) {
915 DEBUGf("removexattr begin\n");
919 XPUSHs(sv_2mortal(newSVpv(file,0)));
920 XPUSHs(sv_2mortal(newSVpv(name,0)));
922 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
931 DEBUGf("removexattr end: %i\n",rv);
936 #if FUSE_VERSION >= 23
937 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
940 DEBUGf("opendir begin\n");
944 XPUSHs(sv_2mortal(newSVpv(file,0)));
945 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
947 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
951 FH_STOREHANDLE(fi, POPs);
959 DEBUGf("opendir end: %i\n",rv);
965 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
966 off_t off, struct fuse_file_info *fi) {
968 SV *sv, **svp, **swp;
972 #ifndef PERL_HAS_64BITINT
976 DEBUGf("readdir begin\n");
980 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
981 #ifdef PERL_HAS_64BITINT
982 XPUSHs(sv_2mortal(newSViv(off)));
984 if (asprintf(&temp, "%llu", off) == -1)
985 croak("Memory allocation failure!");
986 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
989 XPUSHs(FH_GETHANDLE(fi));
991 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
994 /* Should yield the bottom of the current stack... */
995 swp = &TOPs - prv + 1;
997 memset(&st, 0, sizeof(struct stat));
998 /* Sort of a hack to walk the stack in order, instead of reverse
999 * order - trying to explain to potential users why they need to
1000 * reverse the order of this array would be confusing, at best. */
1001 while (swp <= &TOPs) {
1003 if (!SvROK(sv) && SvPOK(sv))
1004 /* Just a bare SV (probably a string; hopefully a string) */
1005 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
1006 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
1007 if (av_len(av) >= 2) {
1008 /* The third element of the array should be the args that
1009 * would otherwise go to getattr(); a lot of filesystems
1010 * will, or at least can, return that info as part of the
1011 * enumeration process... */
1012 svp = av_fetch(av, 2, FALSE);
1014 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1015 av_len(av2) == 12) {
1016 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1017 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1018 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1019 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1020 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1021 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1022 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1023 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1024 st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
1025 st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
1026 st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
1027 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1028 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1032 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1033 /* For now if the element isn't what we want, just
1034 * quietly ignore it... */
1036 if (av_len(av) >= 1) {
1037 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1038 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1039 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1042 memset(&st, 0, sizeof(struct stat));
1047 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1050 fprintf(stderr,"readdir() handler returned nothing!\n");
1056 DEBUGf("readdir end: %i\n",rv);
1061 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1064 DEBUGf("releasedir begin\n");
1068 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1069 XPUSHs(FH_GETHANDLE(fi));
1071 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1073 rv = (rv ? POPi : 0);
1074 FH_RELEASEHANDLE(fi);
1078 DEBUGf("releasedir end: %i\n",rv);
1083 int _PLfuse_fsyncdir(const char *file, int datasync,
1084 struct fuse_file_info *fi) {
1087 DEBUGf("fsyncdir begin\n");
1091 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1092 XPUSHs(sv_2mortal(newSViv(datasync)));
1093 XPUSHs(FH_GETHANDLE(fi));
1095 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1097 rv = (rv ? POPi : 0);
1101 DEBUGf("fsyncdir end: %i\n",rv);
1106 #if FUSE_VERSION >= 26
1107 void *_PLfuse_init(struct fuse_conn_info *fc)
1108 #else /* FUSE_VERSION < 26 */
1109 void *_PLfuse_init(void)
1110 #endif /* FUSE_VERSION >= 26 */
1115 DEBUGf("init begin\n");
1120 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1124 if (rv == &PL_sv_undef)
1127 rv = SvREFCNT_inc((SV *)rv);
1132 DEBUGf("init end: %p\n", rv);
1137 void _PLfuse_destroy(void *private_data) {
1139 DEBUGf("destroy begin\n");
1143 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1145 call_sv(MY_CXT.callback[30], G_VOID);
1148 SvREFCNT_dec((SV *)private_data);
1152 DEBUGf("init end\n");
1155 #endif /* FUSE_VERSION >= 23 */
1157 #if FUSE_VERSION >= 25
1158 int _PLfuse_access(const char *file, int mask) {
1161 DEBUGf("access begin\n");
1165 XPUSHs(sv_2mortal(newSVpv(file,0)));
1166 XPUSHs(sv_2mortal(newSViv(mask)));
1168 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1170 rv = (rv ? POPi : 0);
1174 DEBUGf("access end: %d\n", rv);
1179 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1183 DEBUGf("create begin\n");
1187 XPUSHs(sv_2mortal(newSVpv(file,0)));
1188 XPUSHs(sv_2mortal(newSViv(mode)));
1189 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1190 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1191 /* Create a hashref containing the details from fi
1192 * which we can look at or modify.
1195 #if FUSE_VERSION >= 24
1196 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1197 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1199 #if FUSE_VERSION >= 29
1200 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1202 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1203 /* All hashref things done */
1206 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1210 FH_STOREHANDLE(fi,POPs);
1215 fprintf(stderr, "create() handler returned nothing!\n");
1219 /* Success, so copy the file handle which they returned */
1220 #if FUSE_VERSION >= 24
1222 svp = hv_fetch(fihash, "direct_io", 9, 0);
1224 fi->direct_io = SvIV(*svp);
1225 svp = hv_fetch(fihash, "keep_cache", 10, 0);
1227 fi->keep_cache = SvIV(*svp);
1229 #if FUSE_VERSION >= 29
1230 svp = hv_fetch(fihash, "nonseekable", 11, 0);
1232 fi->nonseekable = SvIV(*svp);
1238 DEBUGf("create end: %d\n",rv);
1243 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1245 #ifndef PERL_HAS_64BITINT
1249 DEBUGf("ftruncate begin\n");
1253 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1254 #ifdef PERL_HAS_64BITINT
1255 XPUSHs(sv_2mortal(newSViv(off)));
1257 if (asprintf(&temp, "%llu", off) == -1)
1258 croak("Memory allocation failure!");
1259 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1262 XPUSHs(FH_GETHANDLE(fi));
1264 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1266 rv = (rv ? POPi : 0);
1270 DEBUGf("ftruncate end: %i\n",rv);
1275 int _PLfuse_fgetattr(const char *file, struct stat *result,
1276 struct fuse_file_info *fi) {
1279 DEBUGf("fgetattr begin: %s\n",file);
1283 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1284 XPUSHs(FH_GETHANDLE(fi));
1286 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1290 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1297 result->st_blocks = POPi;
1298 result->st_blksize = POPi;
1299 result->st_ctime = POPi;
1300 result->st_mtime = POPi;
1301 result->st_atime = POPi;
1302 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1303 result->st_rdev = POPi;
1304 result->st_gid = POPi;
1305 result->st_uid = POPi;
1306 result->st_nlink = POPi;
1307 result->st_mode = POPi;
1308 result->st_ino = POPi;
1309 result->st_dev = POPi;
1315 DEBUGf("fgetattr end: %i\n",rv);
1319 #endif /* FUSE_VERSION >= 25 */
1321 #if FUSE_VERSION >= 26
1322 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1323 struct flock *lockinfo) {
1327 #ifndef PERL_HAS_64BITINT
1331 DEBUGf("lock begin\n");
1335 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1336 XPUSHs(sv_2mortal(newSViv(cmd)));
1339 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1340 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1341 #ifdef PERL_HAS_64BITINT
1342 sv = newSViv(lockinfo->l_start);
1344 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1345 croak("Memory allocation failure!");
1346 sv = newSVpv(temp, 0);
1349 (void) hv_store(lihash, "l_start", 7, sv, 0);
1350 #ifdef PERL_HAS_64BITINT
1351 sv = newSViv(lockinfo->l_len);
1353 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1354 croak("Memory allocation failure!");
1355 sv = newSVpv(temp, 0);
1358 (void) hv_store(lihash, "l_len", 5, sv, 0);
1359 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1361 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1362 XPUSHs(FH_GETHANDLE(fi));
1365 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1367 rv = (rv ? POPi : 0);
1368 if (lockinfo && !rv) {
1369 /* Need to copy back any altered values from the hash into
1372 if ((svp = hv_fetch(lihash, "l_type", 6, 0)))
1373 lockinfo->l_type = SvIV(*svp);
1374 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)))
1375 lockinfo->l_whence = SvIV(*svp);
1376 if ((svp = hv_fetch(lihash, "l_start", 7, 0)))
1377 lockinfo->l_start = SvNV(*svp);
1378 if ((svp = hv_fetch(lihash, "l_len", 5, 0)))
1379 lockinfo->l_len = SvNV(*svp);
1380 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)))
1381 lockinfo->l_pid = SvIV(*svp);
1386 DEBUGf("lock end: %i\n",rv);
1391 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1394 DEBUGf("utimens begin\n");
1398 XPUSHs(sv_2mortal(newSVpv(file,0)));
1399 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1400 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1402 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1404 rv = (rv ? POPi : 0);
1408 DEBUGf("utimens end: %i\n",rv);
1413 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1415 #ifndef PERL_HAS_64BITINT
1419 DEBUGf("bmap begin\n");
1423 XPUSHs(sv_2mortal(newSVpv(file,0)));
1424 XPUSHs(sv_2mortal(newSViv(blocksize)));
1425 #ifdef PERL_HAS_64BITINT
1426 XPUSHs(sv_2mortal(newSViv(*idx)));
1428 if (asprintf(&temp, "%llu", *idx) == -1)
1429 croak("Memory allocation failure!");
1430 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1434 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1436 if (rv > 0 && rv < 3) {
1442 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1448 DEBUGf("bmap end: %i\n",rv);
1452 #endif /* FUSE_VERSION >= 26 */
1455 #if FUSE_VERSION >= 28
1456 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1457 struct fuse_file_info *fi, unsigned int flags, void *data) {
1460 DEBUGf("ioctl begin\n");
1464 XPUSHs(sv_2mortal(newSVpv(file,0)));
1465 XPUSHs(sv_2mortal(newSViv(cmd)));
1466 XPUSHs(sv_2mortal(newSViv(flags)));
1467 if (_IOC_DIR(cmd) & _IOC_READ)
1468 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1470 XPUSHs(&PL_sv_undef);
1471 XPUSHs(FH_GETHANDLE(fi));
1473 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1475 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1479 char *rdata = SvPV(sv, len);
1480 if (len > _IOC_SIZE(cmd)) {
1481 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1485 memset(data, 0, _IOC_SIZE(cmd));
1486 memcpy(data, rdata, len);
1492 fprintf(stderr, "ioctl(): ioctl was a write op, but no data was returned from call?\n");
1501 DEBUGf("ioctl end: %i\n",rv);
1506 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1507 struct fuse_pollhandle *ph, unsigned *reventsp) {
1510 #endif /* FUSE_VERSION >= 28 */
1513 struct fuse_operations _available_ops = {
1514 getattr: _PLfuse_getattr,
1515 readlink: _PLfuse_readlink,
1516 getdir: _PLfuse_getdir,
1517 mknod: _PLfuse_mknod,
1518 mkdir: _PLfuse_mkdir,
1519 unlink: _PLfuse_unlink,
1520 rmdir: _PLfuse_rmdir,
1521 rename: _PLfuse_rename,
1523 chmod: _PLfuse_chmod,
1524 chown: _PLfuse_chown,
1525 truncate: _PLfuse_truncate,
1526 utime: _PLfuse_utime,
1529 write: _PLfuse_write,
1530 statfs: _PLfuse_statfs,
1531 flush: _PLfuse_flush,
1532 release: _PLfuse_release,
1533 fsync: _PLfuse_fsync,
1534 setxattr: _PLfuse_setxattr,
1535 getxattr: _PLfuse_getxattr,
1536 listxattr: _PLfuse_listxattr,
1537 removexattr: _PLfuse_removexattr,
1538 #if FUSE_VERSION >= 23
1539 opendir: _PLfuse_opendir,
1540 readdir: _PLfuse_readdir,
1541 releasedir: _PLfuse_releasedir,
1542 fsyncdir: _PLfuse_fsyncdir,
1544 destroy: _PLfuse_destroy,
1545 #endif /* FUSE_VERSION >= 23 */
1546 #if FUSE_VERSION >= 25
1547 access: _PLfuse_access,
1548 create: _PLfuse_create,
1549 ftruncate: _PLfuse_ftruncate,
1550 fgetattr: _PLfuse_fgetattr,
1551 #endif /* FUSE_VERSION >= 25 */
1552 #if FUSE_VERSION >= 26
1554 utimens: _PLfuse_utimens,
1556 #endif /* FUSE_VERSION >= 26 */
1558 #if FUSE_VERSION >= 28
1559 ioctl: _PLfuse_ioctl,
1561 #endif /* FUSE_VERSION >= 28 */
1565 MODULE = Fuse PACKAGE = Fuse
1579 tTHX parent = MY_CXT.self;
1580 MY_CXT.self = my_perl;
1581 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1582 /* CLONE entered without a pointer table, so we can't safely clone static data */
1584 for(i=0;i<N_CALLBACKS;i++) {
1585 MY_CXT.callback[i] = NULL;
1587 MY_CXT.handles = newHV();
1591 CLONE_PARAMS *clone_param;
1592 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1593 clone_param = clone_params_new(parent, aTHX);
1595 CLONE_PARAMS raw_param;
1596 raw_param.flags = 0;
1597 raw_param.proto_perl = parent;
1598 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1599 clone_param = &raw_param;
1601 for(i=0;i<N_CALLBACKS;i++) {
1602 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1604 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1605 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1606 clone_params_del(clone_param);
1613 struct fuse_context *fc;
1615 fc = fuse_get_context();
1618 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1619 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1620 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1621 if (fc->private_data)
1622 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1623 #if FUSE_VERSION >= 28
1624 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1625 #endif /* FUSE_VERSION >= 28 */
1627 RETVAL = newRV_noinc((SV*)hash);
1637 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1644 struct fuse_operations fops;
1648 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1649 struct fuse_chan *fc;
1652 if(items != N_CALLBACKS + 5) {
1653 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1656 memset(&fops, 0, sizeof(struct fuse_operations));
1658 debug = SvIV(ST(0));
1659 MY_CXT.threaded = SvIV(ST(1));
1660 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1661 if(MY_CXT.threaded) {
1662 #ifdef FUSE_USE_ITHREADS
1663 master_interp = aTHX;
1664 MUTEX_INIT(&MY_CXT.mutex);
1665 SvSHARE((SV*)(MY_CXT.handles));
1667 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1668 "mode, but your perl was not built with a supported "
1669 "thread model. Threads are disabled.\n");
1670 MY_CXT.threaded = 0;
1673 mountpoint = SvPV_nolen(ST(2));
1674 mountopts = SvPV_nolen(ST(3));
1675 #if FUSE_VERSION >= 28
1676 fops.flag_nullpath_ok = SvIV(ST(4));
1677 #endif /* FUSE_VERSION >= 28 */
1678 for(i=0;i<N_CALLBACKS;i++) {
1680 /* allow symbolic references, or real code references. */
1681 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1682 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1683 /* Dirty hack, to keep anything from overwriting the
1684 * flag area with a pointer. There should never be
1685 * anything passed as 'junk', but this prevents
1686 * someone from doing it and screwing things up... */
1690 MY_CXT.callback[i] = var;
1691 } else if(SvOK(var)) {
1692 croak("invalid callback (%i) passed to perl_fuse_main "
1693 "(%s is not a string, code ref, or undef).\n",
1694 i+4,SvPVbyte_nolen(var));
1696 MY_CXT.callback[i] = NULL;
1700 * XXX: What comes here is just a ridiculous use of the option parsing API
1701 * to hack on compatibility with other parts of the new API. First and
1702 * foremost, real C argc/argv would be good to get at...
1705 (fuse_opt_add_arg(&args, "") == -1 ||
1706 fuse_opt_add_arg(&args, "-o") == -1 ||
1707 fuse_opt_add_arg(&args, mountopts) == -1)) {
1708 fuse_opt_free_args(&args);
1709 croak("out of memory\n");
1711 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1712 fuse_opt_free_args(&args);
1713 croak("out of memory\n");
1715 fc = fuse_mount(mountpoint,&args);
1717 croak("could not mount fuse filesystem!\n");
1719 if(MY_CXT.threaded) {
1720 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1723 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1724 fuse_unmount(mountpoint,fc);
1725 fuse_opt_free_args(&args);