1 #define PERL_NO_GET_CONTEXT
8 #if (defined(__FreeBSD__) && !defined(__APPLE__)) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 #if defined(__linux__)
16 # define STAT_SEC(st, st_xtim) ((st)->st_xtim.tv_sec)
17 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim.tv_nsec)
19 # define STAT_SEC(st, st_xtim) ((st)->st_xtim##espec.tv_sec)
20 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
23 /* Implement a macro to handle multiple formats (integer, float, and array
24 * containing seconds and nanoseconds). */
25 #define PULL_TIME(st, st_xtim, svp) \
29 AV *av = (AV *)SvRV(sv); \
30 if (SvTYPE((SV *)av) != SVt_PVAV) { \
31 Perl_croak_nocontext("Reference was not array ref"); \
33 if (av_len(av) != 1) { \
34 Perl_croak_nocontext("Array of incorrect dimension"); \
36 STAT_SEC(st, st_xtim) = SvIV(*(av_fetch(av, 0, FALSE))); \
37 STAT_NSEC(st, st_xtim) = SvIV(*(av_fetch(av, 1, FALSE))); \
39 else if (SvNOK(sv) || SvIOK(sv)) { \
40 double tm = SvNV(sv); \
41 STAT_SEC(st, st_xtim) = (int)tm; \
42 STAT_NSEC(st, st_xtim) = (tm - (int)tm) * 1000000000; \
45 Perl_croak_nocontext("Invalid data type passed"); \
49 /* Determine if threads support should be included */
52 # define FUSE_USE_ITHREADS
53 # if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
54 # define tTHX PerlInterpreter*
55 # define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
56 # define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
57 # define dMY_CXT_INTERP(interp) \
58 SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
59 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
62 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
68 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
69 #if FUSE_VERSION >= 28
70 # define N_CALLBACKS 41
72 # define N_CALLBACKS 38
76 SV *callback[N_CALLBACKS];
89 #ifdef FUSE_USE_ITHREADS
90 tTHX master_interp = NULL;
92 #define CLONE_INTERP(parent) S_clone_interp(parent)
93 tTHX S_clone_interp(tTHX parent) {
94 # if (PERL_VERSION < 10)
95 tTHX my_perl = parent;
97 dMY_CXT_INTERP(parent);
99 MUTEX_LOCK(&MY_CXT.mutex);
100 PERL_SET_CONTEXT(parent);
102 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
103 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
105 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
106 ptr_table_free(PL_ptr_table);
109 MUTEX_UNLOCK(&MY_CXT.mutex);
115 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
116 # define FUSE_CONTEXT_POST }
118 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
119 # define FUSE_CONTEXT_POST
124 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
129 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
130 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
131 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
132 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
134 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
139 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
147 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
149 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
154 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
156 #ifdef FUSE_USE_ITHREADS
157 if(MY_CXT.threaded) {
161 /* This seems to be screwing things up... */
162 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
163 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
165 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
172 int _PLfuse_getattr(const char *file, struct stat *result) {
175 DEBUGf("getattr begin: %s\n",file);
179 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
181 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
185 fprintf(stderr,"inappropriate number of returned values from getattr\n");
192 result->st_blocks = POPi;
193 result->st_blksize = POPi;
194 PULL_TIME(result, st_ctim, POPs);
195 PULL_TIME(result, st_mtim, POPs);
196 PULL_TIME(result, st_atim, POPs);
197 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
198 result->st_rdev = POPi;
199 result->st_gid = POPi;
200 result->st_uid = POPi;
201 result->st_nlink = POPi;
202 result->st_mode = POPi;
203 result->st_ino = POPi;
204 result->st_dev = POPi;
210 DEBUGf("getattr end: %i\n",rv);
215 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
220 DEBUGf("readlink begin\n");
224 XPUSHs(sv_2mortal(newSVpv(file,0)));
226 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
232 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
235 strncpy(buf,SvPV_nolen(mysv),buflen);
243 DEBUGf("readlink end: %i\n",rv);
248 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
252 DEBUGf("getdir begin\n");
256 XPUSHs(sv_2mortal(newSVpv(file,0)));
258 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
261 /* Should yield the bottom of the current stack... */
262 swp = &TOPs - prv + 1;
264 /* Sort of a hack to walk the stack in order, instead of reverse
265 * order - trying to explain to potential users why they need to
266 * reverse the order of this array would be confusing, at best. */
268 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
270 fprintf(stderr,"getdir() handler returned nothing!\n");
276 DEBUGf("getdir end: %i\n",rv);
281 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
284 DEBUGf("mknod begin\n");
288 XPUSHs(sv_2mortal(newSVpv(file,0)));
289 XPUSHs(sv_2mortal(newSViv(mode)));
290 XPUSHs(sv_2mortal(newSViv(dev)));
292 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
294 rv = (rv ? POPi : 0);
298 DEBUGf("mknod end: %i\n",rv);
303 int _PLfuse_mkdir (const char *file, mode_t mode) {
306 DEBUGf("mkdir begin\n");
310 XPUSHs(sv_2mortal(newSVpv(file,0)));
311 XPUSHs(sv_2mortal(newSViv(mode)));
313 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
315 rv = (rv ? POPi : 0);
319 DEBUGf("mkdir end: %i\n",rv);
324 int _PLfuse_unlink (const char *file) {
327 DEBUGf("unlink begin\n");
331 XPUSHs(sv_2mortal(newSVpv(file,0)));
333 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
335 rv = (rv ? POPi : 0);
339 DEBUGf("unlink end: %i\n",rv);
344 int _PLfuse_rmdir (const char *file) {
347 DEBUGf("rmdir begin\n");
351 XPUSHs(sv_2mortal(newSVpv(file,0)));
353 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
355 rv = (rv ? POPi : 0);
359 DEBUGf("rmdir end: %i\n",rv);
364 int _PLfuse_symlink (const char *file, const char *new) {
367 DEBUGf("symlink begin\n");
371 XPUSHs(sv_2mortal(newSVpv(file,0)));
372 XPUSHs(sv_2mortal(newSVpv(new,0)));
374 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
376 rv = (rv ? POPi : 0);
380 DEBUGf("symlink end: %i\n",rv);
385 int _PLfuse_rename (const char *file, const char *new) {
388 DEBUGf("rename begin\n");
392 XPUSHs(sv_2mortal(newSVpv(file,0)));
393 XPUSHs(sv_2mortal(newSVpv(new,0)));
395 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
397 rv = (rv ? POPi : 0);
401 DEBUGf("rename end: %i\n",rv);
406 int _PLfuse_link (const char *file, const char *new) {
409 DEBUGf("link begin\n");
413 XPUSHs(sv_2mortal(newSVpv(file,0)));
414 XPUSHs(sv_2mortal(newSVpv(new,0)));
416 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
418 rv = (rv ? POPi : 0);
422 DEBUGf("link end: %i\n",rv);
427 int _PLfuse_chmod (const char *file, mode_t mode) {
430 DEBUGf("chmod begin\n");
434 XPUSHs(sv_2mortal(newSVpv(file,0)));
435 XPUSHs(sv_2mortal(newSViv(mode)));
437 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
439 rv = (rv ? POPi : 0);
443 DEBUGf("chmod end: %i\n",rv);
448 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
451 DEBUGf("chown begin\n");
455 XPUSHs(sv_2mortal(newSVpv(file,0)));
456 XPUSHs(sv_2mortal(newSViv(uid)));
457 XPUSHs(sv_2mortal(newSViv(gid)));
459 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
461 rv = (rv ? POPi : 0);
465 DEBUGf("chown end: %i\n",rv);
470 int _PLfuse_truncate (const char *file, off_t off) {
472 #ifndef PERL_HAS_64BITINT
476 DEBUGf("truncate begin\n");
480 XPUSHs(sv_2mortal(newSVpv(file,0)));
481 #ifdef PERL_HAS_64BITINT
482 XPUSHs(sv_2mortal(newSViv(off)));
484 if (asprintf(&temp, "%llu", off) == -1)
485 croak("Memory allocation failure!");
486 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
490 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
492 rv = (rv ? POPi : 0);
496 DEBUGf("truncate end: %i\n",rv);
501 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
504 DEBUGf("utime begin\n");
508 XPUSHs(sv_2mortal(newSVpv(file,0)));
509 XPUSHs(sv_2mortal(newSViv(uti->actime)));
510 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
512 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
514 rv = (rv ? POPi : 0);
518 DEBUGf("utime end: %i\n",rv);
523 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
525 int flags = fi->flags;
528 DEBUGf("open begin\n");
532 XPUSHs(sv_2mortal(newSVpv(file,0)));
533 XPUSHs(sv_2mortal(newSViv(flags)));
534 /* Create a hashref containing the details from fi
535 * which we can look at or modify.
537 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
539 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
540 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
541 #if FUSE_VERSION >= 28
542 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
544 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
545 /* All hashref things done */
548 /* Open called with filename, flags */
549 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
553 FH_STOREHANDLE(fi,POPs);
561 /* Success, so copy the file handle which they returned */
563 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
564 fi->direct_io = SvIV(*svp);
565 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
566 fi->keep_cache = SvIV(*svp);
567 #if FUSE_VERSION >= 28
568 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
569 fi->nonseekable = SvIV(*svp);
575 DEBUGf("open end: %i\n",rv);
580 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
581 struct fuse_file_info *fi) {
583 #ifndef PERL_HAS_64BITINT
587 DEBUGf("read begin\n");
591 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
592 XPUSHs(sv_2mortal(newSViv(buflen)));
593 #ifdef PERL_HAS_64BITINT
594 XPUSHs(sv_2mortal(newSViv(off)));
596 if (asprintf(&temp, "%llu", off) == -1)
597 croak("Memory allocation failure!");
598 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
601 XPUSHs(FH_GETHANDLE(fi));
603 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
609 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
618 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
620 memcpy(buf,SvPV_nolen(mysv),rv);
626 DEBUGf("read end: %i\n",rv);
631 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
633 #ifndef PERL_HAS_64BITINT
637 DEBUGf("write begin\n");
641 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
642 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
643 #ifdef PERL_HAS_64BITINT
644 XPUSHs(sv_2mortal(newSViv(off)));
646 if (asprintf(&temp, "%llu", off) == -1)
647 croak("Memory allocation failure!");
648 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
651 XPUSHs(FH_GETHANDLE(fi));
653 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
655 rv = (rv ? POPi : 0);
659 DEBUGf("write end: %i\n",rv);
664 int _PLfuse_statfs (const char *file, struct statvfs *st) {
667 DEBUGf("statfs begin\n");
672 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
674 DEBUGf("statfs got %i params\n",rv);
675 if(rv == 6 || rv == 7) {
681 st->f_namemax = POPi;
682 /* zero and fill-in other */
686 st->f_bavail = st->f_bfree;
687 st->f_favail = st->f_ffree;
695 croak("inappropriate number of returned values from statfs");
704 DEBUGf("statfs end: %i\n",rv);
709 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
712 DEBUGf("flush begin\n");
716 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
717 XPUSHs(FH_GETHANDLE(fi));
719 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
721 rv = (rv ? POPi : 0);
725 DEBUGf("flush end: %i\n",rv);
730 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
732 int flags = fi->flags;
734 DEBUGf("release begin\n");
738 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
739 XPUSHs(sv_2mortal(newSViv(flags)));
740 XPUSHs(FH_GETHANDLE(fi));
742 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
744 rv = (rv ? POPi : 0);
745 FH_RELEASEHANDLE(fi);
749 DEBUGf("release end: %i\n",rv);
754 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
756 int flags = fi->flags;
758 DEBUGf("fsync begin\n");
762 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
763 XPUSHs(sv_2mortal(newSViv(flags)));
764 XPUSHs(FH_GETHANDLE(fi));
766 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
768 rv = (rv ? POPi : 0);
772 DEBUGf("fsync end: %i\n",rv);
778 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
780 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
784 DEBUGf("setxattr begin\n");
788 XPUSHs(sv_2mortal(newSVpv(file,0)));
789 XPUSHs(sv_2mortal(newSVpv(name,0)));
790 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
791 XPUSHs(sv_2mortal(newSViv(flags)));
793 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
795 rv = (rv ? POPi : 0);
799 DEBUGf("setxattr end: %i\n",rv);
805 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
807 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
811 DEBUGf("getxattr begin\n");
815 XPUSHs(sv_2mortal(newSVpv(file,0)));
816 XPUSHs(sv_2mortal(newSVpv(name,0)));
818 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
826 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
834 if ((rv > 0) && (buflen > 0))
839 memcpy(buf,SvPV_nolen(mysv),rv);
846 DEBUGf("getxattr end: %i\n",rv);
851 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
854 DEBUGf("listxattr begin\n");
858 XPUSHs(sv_2mortal(newSVpv(file,0)));
860 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
873 /* Always nul terminate */
874 if (list && (size > 0))
884 int s = SvCUR(mysv) + 1;
887 if (p && (size > 0) && (spc >= s))
889 memcpy(p,SvPV_nolen(mysv),s);
897 * If the Perl returned an error, return that.
898 * Otherwise check that the buffer was big enough.
903 if ((size > 0) && (size < total_len))
910 DEBUGf("listxattr end: %i\n",rv);
915 int _PLfuse_removexattr (const char *file, const char *name) {
918 DEBUGf("removexattr begin\n");
922 XPUSHs(sv_2mortal(newSVpv(file,0)));
923 XPUSHs(sv_2mortal(newSVpv(name,0)));
925 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
927 rv = (rv ? POPi : 0);
931 DEBUGf("removexattr end: %i\n",rv);
936 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
939 DEBUGf("opendir begin\n");
943 XPUSHs(sv_2mortal(newSVpv(file,0)));
944 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
946 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
950 FH_STOREHANDLE(fi, POPs);
958 DEBUGf("opendir end: %i\n",rv);
963 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
964 off_t off, struct fuse_file_info *fi) {
966 SV *sv, **svp, **swp;
970 #ifndef PERL_HAS_64BITINT
974 DEBUGf("readdir begin\n");
978 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
979 #ifdef PERL_HAS_64BITINT
980 XPUSHs(sv_2mortal(newSViv(off)));
982 if (asprintf(&temp, "%llu", off) == -1)
983 croak("Memory allocation failure!");
984 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
987 XPUSHs(FH_GETHANDLE(fi));
989 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
992 /* Should yield the bottom of the current stack... */
993 swp = &TOPs - prv + 1;
995 memset(&st, 0, sizeof(struct stat));
996 /* Sort of a hack to walk the stack in order, instead of reverse
997 * order - trying to explain to potential users why they need to
998 * reverse the order of this array would be confusing, at best. */
999 while (swp <= &TOPs) {
1001 if (!SvROK(sv) && SvPOK(sv))
1002 /* Just a bare SV (probably a string; hopefully a string) */
1003 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
1004 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
1005 if (av_len(av) >= 2) {
1006 /* The third element of the array should be the args that
1007 * would otherwise go to getattr(); a lot of filesystems
1008 * will, or at least can, return that info as part of the
1009 * enumeration process... */
1010 svp = av_fetch(av, 2, FALSE);
1012 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1013 av_len(av2) == 12) {
1014 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1015 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1016 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1017 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1018 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1019 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1020 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1021 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1022 PULL_TIME(&st, st_atim, *(av_fetch(av2, 8, FALSE)));
1023 PULL_TIME(&st, st_mtim, *(av_fetch(av2, 9, FALSE)));
1024 PULL_TIME(&st, st_ctim, *(av_fetch(av2, 10, FALSE)));
1025 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1026 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1030 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1031 /* For now if the element isn't what we want, just
1032 * quietly ignore it... */
1034 if (av_len(av) >= 1) {
1035 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1036 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1037 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1040 memset(&st, 0, sizeof(struct stat));
1045 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1048 fprintf(stderr,"readdir() handler returned nothing!\n");
1054 DEBUGf("readdir end: %i\n",rv);
1059 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1062 DEBUGf("releasedir begin\n");
1066 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1067 XPUSHs(FH_GETHANDLE(fi));
1069 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1071 rv = (rv ? POPi : 0);
1072 FH_RELEASEHANDLE(fi);
1076 DEBUGf("releasedir end: %i\n",rv);
1081 int _PLfuse_fsyncdir(const char *file, int datasync,
1082 struct fuse_file_info *fi) {
1085 DEBUGf("fsyncdir begin\n");
1089 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1090 XPUSHs(sv_2mortal(newSViv(datasync)));
1091 XPUSHs(FH_GETHANDLE(fi));
1093 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1095 rv = (rv ? POPi : 0);
1099 DEBUGf("fsyncdir end: %i\n",rv);
1104 void *_PLfuse_init(struct fuse_conn_info *fc)
1109 DEBUGf("init begin\n");
1114 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1118 if (rv == &PL_sv_undef)
1121 rv = SvREFCNT_inc((SV *)rv);
1126 DEBUGf("init end: %p\n", rv);
1131 void _PLfuse_destroy(void *private_data) {
1133 DEBUGf("destroy begin\n");
1137 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1139 call_sv(MY_CXT.callback[30], G_VOID);
1142 SvREFCNT_dec((SV *)private_data);
1146 DEBUGf("init end\n");
1150 int _PLfuse_access(const char *file, int mask) {
1153 DEBUGf("access begin\n");
1157 XPUSHs(sv_2mortal(newSVpv(file,0)));
1158 XPUSHs(sv_2mortal(newSViv(mask)));
1160 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1162 rv = (rv ? POPi : 0);
1166 DEBUGf("access end: %d\n", rv);
1171 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1175 DEBUGf("create begin\n");
1179 XPUSHs(sv_2mortal(newSVpv(file,0)));
1180 XPUSHs(sv_2mortal(newSViv(mode)));
1181 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1182 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1183 /* Create a hashref containing the details from fi
1184 * which we can look at or modify.
1187 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1188 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1189 #if FUSE_VERSION >= 28
1190 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1192 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1193 /* All hashref things done */
1196 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1200 FH_STOREHANDLE(fi,POPs);
1205 fprintf(stderr, "create() handler returned nothing!\n");
1209 /* Success, so copy the file handle which they returned */
1211 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1212 fi->direct_io = SvIV(*svp);
1213 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1214 fi->keep_cache = SvIV(*svp);
1215 #if FUSE_VERSION >= 28
1216 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1217 fi->nonseekable = SvIV(*svp);
1223 DEBUGf("create end: %d\n",rv);
1228 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1230 #ifndef PERL_HAS_64BITINT
1234 DEBUGf("ftruncate begin\n");
1238 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1239 #ifdef PERL_HAS_64BITINT
1240 XPUSHs(sv_2mortal(newSViv(off)));
1242 if (asprintf(&temp, "%llu", off) == -1)
1243 croak("Memory allocation failure!");
1244 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1247 XPUSHs(FH_GETHANDLE(fi));
1249 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1251 rv = (rv ? POPi : 0);
1255 DEBUGf("ftruncate end: %i\n",rv);
1260 int _PLfuse_fgetattr(const char *file, struct stat *result,
1261 struct fuse_file_info *fi) {
1264 DEBUGf("fgetattr begin: %s\n",file);
1268 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1269 XPUSHs(FH_GETHANDLE(fi));
1271 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1275 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1282 result->st_blocks = POPi;
1283 result->st_blksize = POPi;
1284 PULL_TIME(result, st_ctim, POPs);
1285 PULL_TIME(result, st_mtim, POPs);
1286 PULL_TIME(result, st_atim, POPs);
1287 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1288 result->st_rdev = POPi;
1289 result->st_gid = POPi;
1290 result->st_uid = POPi;
1291 result->st_nlink = POPi;
1292 result->st_mode = POPi;
1293 result->st_ino = POPi;
1294 result->st_dev = POPi;
1300 DEBUGf("fgetattr end: %i\n",rv);
1305 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1306 struct flock *lockinfo) {
1310 #ifndef PERL_HAS_64BITINT
1314 DEBUGf("lock begin\n");
1318 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1319 XPUSHs(sv_2mortal(newSViv(cmd)));
1322 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1323 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1324 #ifdef PERL_HAS_64BITINT
1325 sv = newSViv(lockinfo->l_start);
1327 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1328 croak("Memory allocation failure!");
1329 sv = newSVpv(temp, 0);
1332 (void) hv_store(lihash, "l_start", 7, sv, 0);
1333 #ifdef PERL_HAS_64BITINT
1334 sv = newSViv(lockinfo->l_len);
1336 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1337 croak("Memory allocation failure!");
1338 sv = newSVpv(temp, 0);
1341 (void) hv_store(lihash, "l_len", 5, sv, 0);
1342 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1344 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1345 XPUSHs(FH_GETHANDLE(fi));
1348 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1350 rv = (rv ? POPi : 0);
1351 if (lockinfo && !rv) {
1352 /* Need to copy back any altered values from the hash into
1355 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1356 lockinfo->l_type = SvIV(*svp);
1357 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1358 lockinfo->l_whence = SvIV(*svp);
1359 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1360 lockinfo->l_start = SvNV(*svp);
1361 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1362 lockinfo->l_len = SvNV(*svp);
1363 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1364 lockinfo->l_pid = SvIV(*svp);
1369 DEBUGf("lock end: %i\n",rv);
1374 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1377 DEBUGf("utimens begin\n");
1381 XPUSHs(sv_2mortal(newSVpv(file,0)));
1382 if (MY_CXT.utimens_as_array) {
1383 /* Pushing timespecs as 2-element arrays (if tv is present). */
1387 av_push(av, newSViv(tv[0].tv_sec));
1388 av_push(av, newSViv(tv[0].tv_nsec));
1389 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1391 av_push(av, newSViv(tv[1].tv_sec));
1392 av_push(av, newSViv(tv[1].tv_nsec));
1393 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1396 XPUSHs(&PL_sv_undef);
1397 XPUSHs(&PL_sv_undef);
1402 /* Pushing timespecs as floating point (double) values. */
1403 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1404 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1407 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1409 rv = (rv ? POPi : 0);
1413 DEBUGf("utimens end: %i\n",rv);
1418 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1420 #ifndef PERL_HAS_64BITINT
1424 DEBUGf("bmap begin\n");
1428 XPUSHs(sv_2mortal(newSVpv(file,0)));
1429 XPUSHs(sv_2mortal(newSViv(blocksize)));
1430 #ifdef PERL_HAS_64BITINT
1431 XPUSHs(sv_2mortal(newSViv(*idx)));
1433 if (asprintf(&temp, "%llu", *idx) == -1)
1434 croak("Memory allocation failure!");
1435 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1439 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1441 if (rv > 0 && rv < 3) {
1447 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1453 DEBUGf("bmap end: %i\n",rv);
1458 #if FUSE_VERSION >= 28
1461 # define _IOC_SIZE(n) IOCPARM_LEN(n)
1464 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1465 struct fuse_file_info *fi, unsigned int flags, void *data) {
1469 DEBUGf("ioctl begin\n");
1473 XPUSHs(sv_2mortal(newSVpv(file,0)));
1474 /* I don't know why cmd is a signed int in the first place;
1475 * casting as unsigned so stupid tricks don't have to be done on
1477 XPUSHs(sv_2mortal(newSVuv((unsigned int)cmd)));
1478 XPUSHs(sv_2mortal(newSViv(flags)));
1480 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1482 XPUSHs(&PL_sv_undef);
1483 XPUSHs(FH_GETHANDLE(fi));
1485 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1487 if ((cmd & IOC_OUT) && (rv == 2)) {
1495 if ((cmd & IOC_OUT) && !rv) {
1498 char *rdata = SvPV(sv, len);
1500 if (len > _IOC_SIZE(cmd)) {
1501 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1505 memset(data, 0, _IOC_SIZE(cmd));
1506 memcpy(data, rdata, len);
1510 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1517 DEBUGf("ioctl end: %i\n",rv);
1522 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1523 struct fuse_pollhandle *ph, unsigned *reventsp) {
1527 DEBUGf("poll begin\n");
1531 XPUSHs(sv_2mortal(newSVpv(file,0)));
1533 /* Still gotta figure out how to do this right... */
1534 sv = newSViv(PTR2IV(ph));
1540 XPUSHs(&PL_sv_undef);
1541 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1542 XPUSHs(FH_GETHANDLE(fi));
1544 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1550 rv = (rv ? POPi : 0);
1554 DEBUGf("poll end: %i\n", rv);
1558 #endif /* FUSE_VERSION >= 28 */
1560 struct fuse_operations _available_ops = {
1561 .getattr = _PLfuse_getattr,
1562 .readlink = _PLfuse_readlink,
1563 .getdir = _PLfuse_getdir,
1564 .mknod = _PLfuse_mknod,
1565 .mkdir = _PLfuse_mkdir,
1566 .unlink = _PLfuse_unlink,
1567 .rmdir = _PLfuse_rmdir,
1568 .symlink = _PLfuse_symlink,
1569 .rename = _PLfuse_rename,
1570 .link = _PLfuse_link,
1571 .chmod = _PLfuse_chmod,
1572 .chown = _PLfuse_chown,
1573 .truncate = _PLfuse_truncate,
1574 .utime = _PLfuse_utime,
1575 .open = _PLfuse_open,
1576 .read = _PLfuse_read,
1577 .write = _PLfuse_write,
1578 .statfs = _PLfuse_statfs,
1579 .flush = _PLfuse_flush,
1580 .release = _PLfuse_release,
1581 .fsync = _PLfuse_fsync,
1582 .setxattr = _PLfuse_setxattr,
1583 .getxattr = _PLfuse_getxattr,
1584 .listxattr = _PLfuse_listxattr,
1585 .removexattr = _PLfuse_removexattr,
1586 .opendir = _PLfuse_opendir,
1587 .readdir = _PLfuse_readdir,
1588 .releasedir = _PLfuse_releasedir,
1589 .fsyncdir = _PLfuse_fsyncdir,
1590 .init = _PLfuse_init,
1591 .destroy = _PLfuse_destroy,
1592 .access = _PLfuse_access,
1593 .create = _PLfuse_create,
1594 .ftruncate = _PLfuse_ftruncate,
1595 .fgetattr = _PLfuse_fgetattr,
1596 .lock = _PLfuse_lock,
1597 .utimens = _PLfuse_utimens,
1598 .bmap = _PLfuse_bmap,
1599 #if FUSE_VERSION >= 28
1600 .ioctl = _PLfuse_ioctl,
1601 .poll = _PLfuse_poll,
1602 #endif /* FUSE_VERSION >= 28 */
1605 MODULE = Fuse PACKAGE = Fuse
1624 tTHX parent = MY_CXT.self;
1625 MY_CXT.self = my_perl;
1626 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1627 /* CLONE entered without a pointer table, so we can't safely clone static data */
1629 for(i=0;i<N_CALLBACKS;i++) {
1630 MY_CXT.callback[i] = NULL;
1632 MY_CXT.handles = newHV();
1636 CLONE_PARAMS *clone_param;
1637 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1638 clone_param = Perl_clone_params_new(parent, aTHX);
1640 CLONE_PARAMS raw_param;
1641 raw_param.flags = 0;
1642 raw_param.proto_perl = parent;
1643 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1644 clone_param = &raw_param;
1646 for(i=0;i<N_CALLBACKS;i++) {
1647 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1649 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1650 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1651 Perl_clone_params_del(clone_param);
1659 struct fuse_context *fc;
1661 fc = fuse_get_context();
1664 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1665 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1666 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1667 if (fc->private_data)
1668 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1669 #if FUSE_VERSION >= 28
1670 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1671 #endif /* FUSE_VERSION >= 28 */
1672 RETVAL = newRV_noinc((SV*)hash);
1682 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1689 RETVAL = newSViv(XATTR_CREATE);
1696 RETVAL = newSViv(XATTR_REPLACE);
1703 struct fuse_operations fops;
1707 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1708 struct fuse_chan *fc;
1711 if(items != N_CALLBACKS + 6) {
1712 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1715 memset(&fops, 0, sizeof(struct fuse_operations));
1717 debug = SvIV(ST(0));
1718 MY_CXT.threaded = SvIV(ST(1));
1719 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1720 if(MY_CXT.threaded) {
1721 #ifdef FUSE_USE_ITHREADS
1722 master_interp = aTHX;
1723 MUTEX_INIT(&MY_CXT.mutex);
1724 SvSHARE((SV*)(MY_CXT.handles));
1726 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1727 "mode, but your perl was not built with a supported "
1728 "thread model. Threads are disabled.\n");
1729 MY_CXT.threaded = 0;
1732 mountpoint = SvPV_nolen(ST(2));
1733 mountopts = SvPV_nolen(ST(3));
1734 #if FUSE_VERSION >= 28
1735 fops.flag_nullpath_ok = SvIV(ST(4));
1736 #endif /* FUSE_VERSION >= 28 */
1737 MY_CXT.utimens_as_array = SvIV(ST(5));
1738 for(i=0;i<N_CALLBACKS;i++) {
1740 /* allow symbolic references, or real code references. */
1741 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1742 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1743 /* Dirty hack, to keep anything from overwriting the
1744 * flag area with a pointer. There should never be
1745 * anything passed as 'junk', but this prevents
1746 * someone from doing it and screwing things up... */
1750 MY_CXT.callback[i] = var;
1751 } else if(SvOK(var)) {
1752 croak("invalid callback (%i) passed to perl_fuse_main "
1753 "(%s is not a string, code ref, or undef).\n",
1754 i+6,SvPVbyte_nolen(var));
1756 MY_CXT.callback[i] = NULL;
1760 * XXX: What comes here is just a ridiculous use of the option parsing API
1761 * to hack on compatibility with other parts of the new API. First and
1762 * foremost, real C argc/argv would be good to get at...
1764 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1765 fuse_opt_free_args(&args);
1766 croak("out of memory\n");
1768 if (mountopts && strcmp("", mountopts) &&
1769 (fuse_opt_add_arg(&args, "-o") == -1 ||
1770 fuse_opt_add_arg(&args, mountopts) == -1)) {
1771 fuse_opt_free_args(&args);
1772 croak("out of memory\n");
1774 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1775 fuse_opt_free_args(&args);
1776 croak("out of memory\n");
1778 fc = fuse_mount(mountpoint,&args);
1780 croak("could not mount fuse filesystem!\n");
1782 if(MY_CXT.threaded) {
1783 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1786 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1787 fuse_unmount(mountpoint,fc);
1788 fuse_opt_free_args(&args);
1790 #if FUSE_VERSION >= 28
1793 pollhandle_destroy(...)
1795 struct fuse_pollhandle *ph;
1798 fprintf(stderr, "No pollhandle passed?\n");
1802 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1803 fuse_pollhandle_destroy(ph);
1808 struct fuse_pollhandle *ph;
1811 fprintf(stderr, "No pollhandle passed?\n");
1815 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1816 RETVAL = fuse_notify_poll(ph);