1 #define PERL_NO_GET_CONTEXT
8 #if (defined(__FreeBSD__) && __FreeBSD__ < 10) || defined(__NetBSD__)
9 # define XATTR_CREATE 1
10 # define XATTR_REPLACE 2
12 # include <sys/xattr.h>
15 #if defined(__linux__)
16 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim.tv_nsec)
18 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
21 /* Implement a macro to handle multiple formats (integer, float, and array
22 * containing seconds and nanoseconds). */
23 #define PULL_TIME(st, st_xtim, svp) \
27 AV *av = (AV *)SvRV(sv); \
28 if (SvTYPE((SV *)av) != SVt_PVAV) { \
29 Perl_croak_nocontext("Reference was not array ref"); \
31 if (av_len(av) != 1) { \
32 Perl_croak_nocontext("Array of incorrect dimension"); \
34 (st)->st_xtim##e = SvIV(*(av_fetch(av, 0, FALSE))); \
35 STAT_NSEC(st, st_xtim) = SvIV(*(av_fetch(av, 1, FALSE))); \
37 else if (SvNOK(sv) || SvIOK(sv)) { \
38 double tm = SvNV(sv); \
39 (st)->st_xtim##e = (int)tm; \
40 STAT_NSEC(st, st_xtim) = (tm - (int)tm) * 1000000000; \
43 Perl_croak_nocontext("Invalid data type passed"); \
47 /* Determine if threads support should be included */
50 # define FUSE_USE_ITHREADS
51 # if (PERL_VERSION < 8) || (PERL_VERSION == 8 && PERL_SUBVERSION < 9)
52 # define tTHX PerlInterpreter*
53 # define STR_WITH_LEN(s) ("" s ""), (sizeof(s)-1)
54 # define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
55 # define dMY_CXT_INTERP(interp) \
56 SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
57 my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
60 # warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
66 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
67 #if FUSE_VERSION >= 28
68 # define N_CALLBACKS 41
69 #elif FUSE_VERSION >= 26
70 # define N_CALLBACKS 38
71 #elif FUSE_VERSION >= 25
72 # define N_CALLBACKS 35
73 #elif FUSE_VERSION >= 23
74 # define N_CALLBACKS 31
76 # define N_CALLBACKS 25
80 SV *callback[N_CALLBACKS];
93 #ifdef FUSE_USE_ITHREADS
94 tTHX master_interp = NULL;
96 #define CLONE_INTERP(parent) S_clone_interp(parent)
97 tTHX S_clone_interp(tTHX parent) {
98 # if (PERL_VERSION < 10)
99 tTHX my_perl = parent;
101 dMY_CXT_INTERP(parent);
102 if(MY_CXT.threaded) {
103 MUTEX_LOCK(&MY_CXT.mutex);
104 PERL_SET_CONTEXT(parent);
106 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
107 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
109 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
110 ptr_table_free(PL_ptr_table);
113 MUTEX_UNLOCK(&MY_CXT.mutex);
119 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
120 # define FUSE_CONTEXT_POST }
122 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
123 # define FUSE_CONTEXT_POST
128 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
133 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
134 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
135 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
136 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
138 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
143 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
151 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
153 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
158 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
160 #ifdef FUSE_USE_ITHREADS
161 if(MY_CXT.threaded) {
165 /* This seems to be screwing things up... */
166 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
167 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
169 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
176 int _PLfuse_getattr(const char *file, struct stat *result) {
179 DEBUGf("getattr begin: %s\n",file);
183 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
185 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
189 fprintf(stderr,"inappropriate number of returned values from getattr\n");
196 result->st_blocks = POPi;
197 result->st_blksize = POPi;
198 PULL_TIME(result, st_ctim, POPs);
199 PULL_TIME(result, st_mtim, POPs);
200 PULL_TIME(result, st_atim, POPs);
201 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
202 result->st_rdev = POPi;
203 result->st_gid = POPi;
204 result->st_uid = POPi;
205 result->st_nlink = POPi;
206 result->st_mode = POPi;
207 result->st_ino = POPi;
208 result->st_dev = POPi;
214 DEBUGf("getattr end: %i\n",rv);
219 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
224 DEBUGf("readlink begin\n");
228 XPUSHs(sv_2mortal(newSVpv(file,0)));
230 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
236 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
239 strncpy(buf,SvPV_nolen(mysv),buflen);
247 DEBUGf("readlink end: %i\n",rv);
252 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
256 DEBUGf("getdir begin\n");
260 XPUSHs(sv_2mortal(newSVpv(file,0)));
262 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
265 /* Should yield the bottom of the current stack... */
266 swp = &TOPs - prv + 1;
268 /* Sort of a hack to walk the stack in order, instead of reverse
269 * order - trying to explain to potential users why they need to
270 * reverse the order of this array would be confusing, at best. */
272 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
274 fprintf(stderr,"getdir() handler returned nothing!\n");
280 DEBUGf("getdir end: %i\n",rv);
285 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
288 DEBUGf("mknod begin\n");
292 XPUSHs(sv_2mortal(newSVpv(file,0)));
293 XPUSHs(sv_2mortal(newSViv(mode)));
294 XPUSHs(sv_2mortal(newSViv(dev)));
296 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
298 rv = (rv ? POPi : 0);
302 DEBUGf("mknod end: %i\n",rv);
307 int _PLfuse_mkdir (const char *file, mode_t mode) {
310 DEBUGf("mkdir begin\n");
314 XPUSHs(sv_2mortal(newSVpv(file,0)));
315 XPUSHs(sv_2mortal(newSViv(mode)));
317 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
319 rv = (rv ? POPi : 0);
323 DEBUGf("mkdir end: %i\n",rv);
329 int _PLfuse_unlink (const char *file) {
332 DEBUGf("unlink begin\n");
336 XPUSHs(sv_2mortal(newSVpv(file,0)));
338 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
340 rv = (rv ? POPi : 0);
344 DEBUGf("unlink end: %i\n",rv);
349 int _PLfuse_rmdir (const char *file) {
352 DEBUGf("rmdir begin\n");
356 XPUSHs(sv_2mortal(newSVpv(file,0)));
358 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
360 rv = (rv ? POPi : 0);
364 DEBUGf("rmdir end: %i\n",rv);
369 int _PLfuse_symlink (const char *file, const char *new) {
372 DEBUGf("symlink begin\n");
376 XPUSHs(sv_2mortal(newSVpv(file,0)));
377 XPUSHs(sv_2mortal(newSVpv(new,0)));
379 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
381 rv = (rv ? POPi : 0);
385 DEBUGf("symlink end: %i\n",rv);
390 int _PLfuse_rename (const char *file, const char *new) {
393 DEBUGf("rename begin\n");
397 XPUSHs(sv_2mortal(newSVpv(file,0)));
398 XPUSHs(sv_2mortal(newSVpv(new,0)));
400 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
402 rv = (rv ? POPi : 0);
406 DEBUGf("rename end: %i\n",rv);
411 int _PLfuse_link (const char *file, const char *new) {
414 DEBUGf("link begin\n");
418 XPUSHs(sv_2mortal(newSVpv(file,0)));
419 XPUSHs(sv_2mortal(newSVpv(new,0)));
421 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
423 rv = (rv ? POPi : 0);
427 DEBUGf("link end: %i\n",rv);
432 int _PLfuse_chmod (const char *file, mode_t mode) {
435 DEBUGf("chmod begin\n");
439 XPUSHs(sv_2mortal(newSVpv(file,0)));
440 XPUSHs(sv_2mortal(newSViv(mode)));
442 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
444 rv = (rv ? POPi : 0);
448 DEBUGf("chmod end: %i\n",rv);
453 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
456 DEBUGf("chown begin\n");
460 XPUSHs(sv_2mortal(newSVpv(file,0)));
461 XPUSHs(sv_2mortal(newSViv(uid)));
462 XPUSHs(sv_2mortal(newSViv(gid)));
464 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
466 rv = (rv ? POPi : 0);
470 DEBUGf("chown end: %i\n",rv);
475 int _PLfuse_truncate (const char *file, off_t off) {
477 #ifndef PERL_HAS_64BITINT
481 DEBUGf("truncate begin\n");
485 XPUSHs(sv_2mortal(newSVpv(file,0)));
486 #ifdef PERL_HAS_64BITINT
487 XPUSHs(sv_2mortal(newSViv(off)));
489 if (asprintf(&temp, "%llu", off) == -1)
490 croak("Memory allocation failure!");
491 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
495 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
497 rv = (rv ? POPi : 0);
501 DEBUGf("truncate end: %i\n",rv);
506 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
509 DEBUGf("utime begin\n");
513 XPUSHs(sv_2mortal(newSVpv(file,0)));
514 XPUSHs(sv_2mortal(newSViv(uti->actime)));
515 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
517 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
519 rv = (rv ? POPi : 0);
523 DEBUGf("utime end: %i\n",rv);
528 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
530 int flags = fi->flags;
533 DEBUGf("open begin\n");
537 XPUSHs(sv_2mortal(newSVpv(file,0)));
538 XPUSHs(sv_2mortal(newSViv(flags)));
539 /* Create a hashref containing the details from fi
540 * which we can look at or modify.
542 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
544 #if FUSE_VERSION >= 24
545 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
546 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
548 #if FUSE_VERSION >= 28
549 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
551 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
552 /* All hashref things done */
555 /* Open called with filename, flags */
556 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
560 FH_STOREHANDLE(fi,POPs);
568 /* Success, so copy the file handle which they returned */
569 #if FUSE_VERSION >= 24
571 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
572 fi->direct_io = SvIV(*svp);
573 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
574 fi->keep_cache = SvIV(*svp);
576 #if FUSE_VERSION >= 28
577 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
578 fi->nonseekable = SvIV(*svp);
584 DEBUGf("open end: %i\n",rv);
589 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
590 struct fuse_file_info *fi) {
592 #ifndef PERL_HAS_64BITINT
596 DEBUGf("read begin\n");
600 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
601 XPUSHs(sv_2mortal(newSViv(buflen)));
602 #ifdef PERL_HAS_64BITINT
603 XPUSHs(sv_2mortal(newSViv(off)));
605 if (asprintf(&temp, "%llu", off) == -1)
606 croak("Memory allocation failure!");
607 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
610 XPUSHs(FH_GETHANDLE(fi));
612 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
618 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
627 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
629 memcpy(buf,SvPV_nolen(mysv),rv);
635 DEBUGf("read end: %i\n",rv);
640 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
642 #ifndef PERL_HAS_64BITINT
646 DEBUGf("write begin\n");
650 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
651 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
652 #ifdef PERL_HAS_64BITINT
653 XPUSHs(sv_2mortal(newSViv(off)));
655 if (asprintf(&temp, "%llu", off) == -1)
656 croak("Memory allocation failure!");
657 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
660 XPUSHs(FH_GETHANDLE(fi));
662 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
664 rv = (rv ? POPi : 0);
668 DEBUGf("write end: %i\n",rv);
673 int _PLfuse_statfs (const char *file, struct statvfs *st) {
676 DEBUGf("statfs begin\n");
681 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
683 DEBUGf("statfs got %i params\n",rv);
684 if(rv == 6 || rv == 7) {
690 st->f_namemax = POPi;
691 /* zero and fill-in other */
695 st->f_bavail = st->f_bfree;
696 st->f_favail = st->f_ffree;
704 croak("inappropriate number of returned values from statfs");
713 DEBUGf("statfs end: %i\n",rv);
718 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
721 DEBUGf("flush begin\n");
725 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
726 XPUSHs(FH_GETHANDLE(fi));
728 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
730 rv = (rv ? POPi : 0);
734 DEBUGf("flush end: %i\n",rv);
739 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
741 int flags = fi->flags;
743 DEBUGf("release begin\n");
747 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
748 XPUSHs(sv_2mortal(newSViv(flags)));
749 XPUSHs(FH_GETHANDLE(fi));
751 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
753 rv = (rv ? POPi : 0);
754 FH_RELEASEHANDLE(fi);
758 DEBUGf("release end: %i\n",rv);
763 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
765 int flags = fi->flags;
767 DEBUGf("fsync begin\n");
771 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
772 XPUSHs(sv_2mortal(newSViv(flags)));
773 XPUSHs(FH_GETHANDLE(fi));
775 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
777 rv = (rv ? POPi : 0);
781 DEBUGf("fsync end: %i\n",rv);
786 #if __FreeBSD__ >= 10
787 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
789 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
793 DEBUGf("setxattr begin\n");
797 XPUSHs(sv_2mortal(newSVpv(file,0)));
798 XPUSHs(sv_2mortal(newSVpv(name,0)));
799 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
800 XPUSHs(sv_2mortal(newSViv(flags)));
802 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
804 rv = (rv ? POPi : 0);
808 DEBUGf("setxattr end: %i\n",rv);
813 #if __FreeBSD__ >= 10
814 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
816 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
820 DEBUGf("getxattr begin\n");
824 XPUSHs(sv_2mortal(newSVpv(file,0)));
825 XPUSHs(sv_2mortal(newSVpv(name,0)));
827 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
835 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
843 if ((rv > 0) && (buflen > 0))
848 memcpy(buf,SvPV_nolen(mysv),rv);
855 DEBUGf("getxattr end: %i\n",rv);
860 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
863 DEBUGf("listxattr begin\n");
867 XPUSHs(sv_2mortal(newSVpv(file,0)));
869 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
882 /* Always nul terminate */
883 if (list && (size > 0))
893 int s = SvCUR(mysv) + 1;
896 if (p && (size > 0) && (spc >= s))
898 memcpy(p,SvPV_nolen(mysv),s);
906 * If the Perl returned an error, return that.
907 * Otherwise check that the buffer was big enough.
912 if ((size > 0) && (size < total_len))
919 DEBUGf("listxattr end: %i\n",rv);
924 int _PLfuse_removexattr (const char *file, const char *name) {
927 DEBUGf("removexattr begin\n");
931 XPUSHs(sv_2mortal(newSVpv(file,0)));
932 XPUSHs(sv_2mortal(newSVpv(name,0)));
934 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
936 rv = (rv ? POPi : 0);
940 DEBUGf("removexattr end: %i\n",rv);
945 #if FUSE_VERSION >= 23
946 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
949 DEBUGf("opendir begin\n");
953 XPUSHs(sv_2mortal(newSVpv(file,0)));
954 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
956 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
960 FH_STOREHANDLE(fi, POPs);
968 DEBUGf("opendir end: %i\n",rv);
974 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
975 off_t off, struct fuse_file_info *fi) {
977 SV *sv, **svp, **swp;
981 #ifndef PERL_HAS_64BITINT
985 DEBUGf("readdir begin\n");
989 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
990 #ifdef PERL_HAS_64BITINT
991 XPUSHs(sv_2mortal(newSViv(off)));
993 if (asprintf(&temp, "%llu", off) == -1)
994 croak("Memory allocation failure!");
995 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
998 XPUSHs(FH_GETHANDLE(fi));
1000 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
1003 /* Should yield the bottom of the current stack... */
1004 swp = &TOPs - prv + 1;
1006 memset(&st, 0, sizeof(struct stat));
1007 /* Sort of a hack to walk the stack in order, instead of reverse
1008 * order - trying to explain to potential users why they need to
1009 * reverse the order of this array would be confusing, at best. */
1010 while (swp <= &TOPs) {
1012 if (!SvROK(sv) && SvPOK(sv))
1013 /* Just a bare SV (probably a string; hopefully a string) */
1014 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
1015 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
1016 if (av_len(av) >= 2) {
1017 /* The third element of the array should be the args that
1018 * would otherwise go to getattr(); a lot of filesystems
1019 * will, or at least can, return that info as part of the
1020 * enumeration process... */
1021 svp = av_fetch(av, 2, FALSE);
1023 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1024 av_len(av2) == 12) {
1025 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1026 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1027 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1028 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1029 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1030 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1031 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1032 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1033 PULL_TIME(&st, st_atim, *(av_fetch(av2, 8, FALSE)));
1034 PULL_TIME(&st, st_mtim, *(av_fetch(av2, 9, FALSE)));
1035 PULL_TIME(&st, st_ctim, *(av_fetch(av2, 10, FALSE)));
1036 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1037 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1041 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1042 /* For now if the element isn't what we want, just
1043 * quietly ignore it... */
1045 if (av_len(av) >= 1) {
1046 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1047 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1048 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1051 memset(&st, 0, sizeof(struct stat));
1056 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1059 fprintf(stderr,"readdir() handler returned nothing!\n");
1065 DEBUGf("readdir end: %i\n",rv);
1070 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1073 DEBUGf("releasedir begin\n");
1077 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1078 XPUSHs(FH_GETHANDLE(fi));
1080 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1082 rv = (rv ? POPi : 0);
1083 FH_RELEASEHANDLE(fi);
1087 DEBUGf("releasedir end: %i\n",rv);
1092 int _PLfuse_fsyncdir(const char *file, int datasync,
1093 struct fuse_file_info *fi) {
1096 DEBUGf("fsyncdir begin\n");
1100 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1101 XPUSHs(sv_2mortal(newSViv(datasync)));
1102 XPUSHs(FH_GETHANDLE(fi));
1104 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1106 rv = (rv ? POPi : 0);
1110 DEBUGf("fsyncdir end: %i\n",rv);
1115 #if FUSE_VERSION >= 26
1116 void *_PLfuse_init(struct fuse_conn_info *fc)
1117 #else /* FUSE_VERSION < 26 */
1118 void *_PLfuse_init(void)
1119 #endif /* FUSE_VERSION >= 26 */
1124 DEBUGf("init begin\n");
1129 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1133 if (rv == &PL_sv_undef)
1136 rv = SvREFCNT_inc((SV *)rv);
1141 DEBUGf("init end: %p\n", rv);
1146 void _PLfuse_destroy(void *private_data) {
1148 DEBUGf("destroy begin\n");
1152 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1154 call_sv(MY_CXT.callback[30], G_VOID);
1157 SvREFCNT_dec((SV *)private_data);
1161 DEBUGf("init end\n");
1164 #endif /* FUSE_VERSION >= 23 */
1166 #if FUSE_VERSION >= 25
1167 int _PLfuse_access(const char *file, int mask) {
1170 DEBUGf("access begin\n");
1174 XPUSHs(sv_2mortal(newSVpv(file,0)));
1175 XPUSHs(sv_2mortal(newSViv(mask)));
1177 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1179 rv = (rv ? POPi : 0);
1183 DEBUGf("access end: %d\n", rv);
1188 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1192 DEBUGf("create begin\n");
1196 XPUSHs(sv_2mortal(newSVpv(file,0)));
1197 XPUSHs(sv_2mortal(newSViv(mode)));
1198 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1199 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1200 /* Create a hashref containing the details from fi
1201 * which we can look at or modify.
1204 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1205 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1206 #if FUSE_VERSION >= 28
1207 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1209 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1210 /* All hashref things done */
1213 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1217 FH_STOREHANDLE(fi,POPs);
1222 fprintf(stderr, "create() handler returned nothing!\n");
1226 /* Success, so copy the file handle which they returned */
1228 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1229 fi->direct_io = SvIV(*svp);
1230 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1231 fi->keep_cache = SvIV(*svp);
1232 #if FUSE_VERSION >= 28
1233 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1234 fi->nonseekable = SvIV(*svp);
1240 DEBUGf("create end: %d\n",rv);
1245 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1247 #ifndef PERL_HAS_64BITINT
1251 DEBUGf("ftruncate begin\n");
1255 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1256 #ifdef PERL_HAS_64BITINT
1257 XPUSHs(sv_2mortal(newSViv(off)));
1259 if (asprintf(&temp, "%llu", off) == -1)
1260 croak("Memory allocation failure!");
1261 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1264 XPUSHs(FH_GETHANDLE(fi));
1266 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1268 rv = (rv ? POPi : 0);
1272 DEBUGf("ftruncate end: %i\n",rv);
1277 int _PLfuse_fgetattr(const char *file, struct stat *result,
1278 struct fuse_file_info *fi) {
1281 DEBUGf("fgetattr begin: %s\n",file);
1285 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1286 XPUSHs(FH_GETHANDLE(fi));
1288 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1292 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1299 result->st_blocks = POPi;
1300 result->st_blksize = POPi;
1301 PULL_TIME(result, st_ctim, POPs);
1302 PULL_TIME(result, st_mtim, POPs);
1303 PULL_TIME(result, st_atim, POPs);
1304 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1305 result->st_rdev = POPi;
1306 result->st_gid = POPi;
1307 result->st_uid = POPi;
1308 result->st_nlink = POPi;
1309 result->st_mode = POPi;
1310 result->st_ino = POPi;
1311 result->st_dev = POPi;
1317 DEBUGf("fgetattr end: %i\n",rv);
1321 #endif /* FUSE_VERSION >= 25 */
1323 #if FUSE_VERSION >= 26
1324 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1325 struct flock *lockinfo) {
1329 #ifndef PERL_HAS_64BITINT
1333 DEBUGf("lock begin\n");
1337 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1338 XPUSHs(sv_2mortal(newSViv(cmd)));
1341 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1342 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1343 #ifdef PERL_HAS_64BITINT
1344 sv = newSViv(lockinfo->l_start);
1346 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1347 croak("Memory allocation failure!");
1348 sv = newSVpv(temp, 0);
1351 (void) hv_store(lihash, "l_start", 7, sv, 0);
1352 #ifdef PERL_HAS_64BITINT
1353 sv = newSViv(lockinfo->l_len);
1355 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1356 croak("Memory allocation failure!");
1357 sv = newSVpv(temp, 0);
1360 (void) hv_store(lihash, "l_len", 5, sv, 0);
1361 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1363 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1364 XPUSHs(FH_GETHANDLE(fi));
1367 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1369 rv = (rv ? POPi : 0);
1370 if (lockinfo && !rv) {
1371 /* Need to copy back any altered values from the hash into
1374 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1375 lockinfo->l_type = SvIV(*svp);
1376 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1377 lockinfo->l_whence = SvIV(*svp);
1378 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1379 lockinfo->l_start = SvNV(*svp);
1380 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1381 lockinfo->l_len = SvNV(*svp);
1382 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1383 lockinfo->l_pid = SvIV(*svp);
1388 DEBUGf("lock end: %i\n",rv);
1393 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1396 DEBUGf("utimens begin\n");
1400 XPUSHs(sv_2mortal(newSVpv(file,0)));
1401 if (MY_CXT.utimens_as_array) {
1402 /* Pushing timespecs as 2-element arrays (if tv is present). */
1406 av_push(av, newSViv(tv[0].tv_sec));
1407 av_push(av, newSViv(tv[0].tv_nsec));
1408 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1410 av_push(av, newSViv(tv[1].tv_sec));
1411 av_push(av, newSViv(tv[1].tv_nsec));
1412 XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
1415 XPUSHs(&PL_sv_undef);
1416 XPUSHs(&PL_sv_undef);
1421 /* Pushing timespecs as floating point (double) values. */
1422 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1423 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1426 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1428 rv = (rv ? POPi : 0);
1432 DEBUGf("utimens end: %i\n",rv);
1437 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1439 #ifndef PERL_HAS_64BITINT
1443 DEBUGf("bmap begin\n");
1447 XPUSHs(sv_2mortal(newSVpv(file,0)));
1448 XPUSHs(sv_2mortal(newSViv(blocksize)));
1449 #ifdef PERL_HAS_64BITINT
1450 XPUSHs(sv_2mortal(newSViv(*idx)));
1452 if (asprintf(&temp, "%llu", *idx) == -1)
1453 croak("Memory allocation failure!");
1454 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1458 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1460 if (rv > 0 && rv < 3) {
1466 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1472 DEBUGf("bmap end: %i\n",rv);
1476 #endif /* FUSE_VERSION >= 26 */
1478 #if FUSE_VERSION >= 28
1479 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
1480 struct fuse_file_info *fi, unsigned int flags, void *data) {
1484 DEBUGf("ioctl begin\n");
1488 XPUSHs(sv_2mortal(newSVpv(file,0)));
1489 /* I don't know why cmd is a signed int in the first place;
1490 * casting as unsigned so stupid tricks don't have to be done on
1492 XPUSHs(sv_2mortal(newSViv((unsigned int)cmd)));
1493 XPUSHs(sv_2mortal(newSViv(flags)));
1494 if (_IOC_DIR(cmd) & _IOC_WRITE)
1495 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1497 XPUSHs(&PL_sv_undef);
1498 XPUSHs(FH_GETHANDLE(fi));
1500 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1502 if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
1510 if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
1513 char *rdata = SvPV(sv, len);
1515 if (len > _IOC_SIZE(cmd)) {
1516 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1520 memset(data, 0, _IOC_SIZE(cmd));
1521 memcpy(data, rdata, len);
1525 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1532 DEBUGf("ioctl end: %i\n",rv);
1537 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1538 struct fuse_pollhandle *ph, unsigned *reventsp) {
1542 DEBUGf("poll begin\n");
1546 XPUSHs(sv_2mortal(newSVpv(file,0)));
1548 /* Still gotta figure out how to do this right... */
1549 sv = newSViv(PTR2IV(ph));
1555 XPUSHs(&PL_sv_undef);
1556 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1557 XPUSHs(FH_GETHANDLE(fi));
1559 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1565 rv = (rv ? POPi : 0);
1569 DEBUGf("poll end: %i\n", rv);
1573 #endif /* FUSE_VERSION >= 28 */
1575 struct fuse_operations _available_ops = {
1576 getattr: _PLfuse_getattr,
1577 readlink: _PLfuse_readlink,
1578 getdir: _PLfuse_getdir,
1579 mknod: _PLfuse_mknod,
1580 mkdir: _PLfuse_mkdir,
1581 unlink: _PLfuse_unlink,
1582 rmdir: _PLfuse_rmdir,
1583 symlink: _PLfuse_symlink,
1584 rename: _PLfuse_rename,
1586 chmod: _PLfuse_chmod,
1587 chown: _PLfuse_chown,
1588 truncate: _PLfuse_truncate,
1589 utime: _PLfuse_utime,
1592 write: _PLfuse_write,
1593 statfs: _PLfuse_statfs,
1594 flush: _PLfuse_flush,
1595 release: _PLfuse_release,
1596 fsync: _PLfuse_fsync,
1597 setxattr: _PLfuse_setxattr,
1598 getxattr: _PLfuse_getxattr,
1599 listxattr: _PLfuse_listxattr,
1600 removexattr: _PLfuse_removexattr,
1601 #if FUSE_VERSION >= 23
1602 opendir: _PLfuse_opendir,
1603 readdir: _PLfuse_readdir,
1604 releasedir: _PLfuse_releasedir,
1605 fsyncdir: _PLfuse_fsyncdir,
1607 destroy: _PLfuse_destroy,
1608 #endif /* FUSE_VERSION >= 23 */
1609 #if FUSE_VERSION >= 25
1610 access: _PLfuse_access,
1611 create: _PLfuse_create,
1612 ftruncate: _PLfuse_ftruncate,
1613 fgetattr: _PLfuse_fgetattr,
1614 #endif /* FUSE_VERSION >= 25 */
1615 #if FUSE_VERSION >= 26
1617 utimens: _PLfuse_utimens,
1619 #endif /* FUSE_VERSION >= 26 */
1620 #if FUSE_VERSION >= 28
1621 ioctl: _PLfuse_ioctl,
1623 #endif /* FUSE_VERSION >= 28 */
1626 MODULE = Fuse PACKAGE = Fuse
1645 tTHX parent = MY_CXT.self;
1646 MY_CXT.self = my_perl;
1647 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1648 /* CLONE entered without a pointer table, so we can't safely clone static data */
1650 for(i=0;i<N_CALLBACKS;i++) {
1651 MY_CXT.callback[i] = NULL;
1653 MY_CXT.handles = newHV();
1657 CLONE_PARAMS *clone_param;
1658 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1659 clone_param = Perl_clone_params_new(parent, aTHX);
1661 CLONE_PARAMS raw_param;
1662 raw_param.flags = 0;
1663 raw_param.proto_perl = parent;
1664 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1665 clone_param = &raw_param;
1667 for(i=0;i<N_CALLBACKS;i++) {
1668 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1670 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1671 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1672 Perl_clone_params_del(clone_param);
1680 struct fuse_context *fc;
1682 fc = fuse_get_context();
1685 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1686 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1687 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1688 if (fc->private_data)
1689 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1690 #if FUSE_VERSION >= 28
1691 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1692 #endif /* FUSE_VERSION >= 28 */
1693 RETVAL = newRV_noinc((SV*)hash);
1703 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1710 RETVAL = newSViv(XATTR_CREATE);
1717 RETVAL = newSViv(XATTR_REPLACE);
1724 struct fuse_operations fops;
1728 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1729 struct fuse_chan *fc;
1732 if(items != N_CALLBACKS + 6) {
1733 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1736 memset(&fops, 0, sizeof(struct fuse_operations));
1738 debug = SvIV(ST(0));
1739 MY_CXT.threaded = SvIV(ST(1));
1740 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1741 if(MY_CXT.threaded) {
1742 #ifdef FUSE_USE_ITHREADS
1743 master_interp = aTHX;
1744 MUTEX_INIT(&MY_CXT.mutex);
1745 SvSHARE((SV*)(MY_CXT.handles));
1747 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1748 "mode, but your perl was not built with a supported "
1749 "thread model. Threads are disabled.\n");
1750 MY_CXT.threaded = 0;
1753 mountpoint = SvPV_nolen(ST(2));
1754 mountopts = SvPV_nolen(ST(3));
1755 #if FUSE_VERSION >= 28
1756 fops.flag_nullpath_ok = SvIV(ST(4));
1757 #endif /* FUSE_VERSION >= 28 */
1758 MY_CXT.utimens_as_array = SvIV(ST(5));
1759 for(i=0;i<N_CALLBACKS;i++) {
1761 /* allow symbolic references, or real code references. */
1762 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1763 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1764 /* Dirty hack, to keep anything from overwriting the
1765 * flag area with a pointer. There should never be
1766 * anything passed as 'junk', but this prevents
1767 * someone from doing it and screwing things up... */
1771 MY_CXT.callback[i] = var;
1772 } else if(SvOK(var)) {
1773 croak("invalid callback (%i) passed to perl_fuse_main "
1774 "(%s is not a string, code ref, or undef).\n",
1775 i+6,SvPVbyte_nolen(var));
1777 MY_CXT.callback[i] = NULL;
1781 * XXX: What comes here is just a ridiculous use of the option parsing API
1782 * to hack on compatibility with other parts of the new API. First and
1783 * foremost, real C argc/argv would be good to get at...
1785 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1786 fuse_opt_free_args(&args);
1787 croak("out of memory\n");
1789 if (mountopts && strcmp("", mountopts) &&
1790 (fuse_opt_add_arg(&args, "-o") == -1 ||
1791 fuse_opt_add_arg(&args, mountopts) == -1)) {
1792 fuse_opt_free_args(&args);
1793 croak("out of memory\n");
1795 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1796 fuse_opt_free_args(&args);
1797 croak("out of memory\n");
1799 fc = fuse_mount(mountpoint,&args);
1801 croak("could not mount fuse filesystem!\n");
1803 if(MY_CXT.threaded) {
1804 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1807 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1808 fuse_unmount(mountpoint,fc);
1809 fuse_opt_free_args(&args);
1811 #if FUSE_VERSION >= 28
1814 pollhandle_destroy(...)
1816 struct fuse_pollhandle *ph;
1819 fprintf(stderr, "No pollhandle passed?\n");
1823 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1824 fuse_pollhandle_destroy(ph);
1829 struct fuse_pollhandle *ph;
1832 fprintf(stderr, "No pollhandle passed?\n");
1836 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1837 RETVAL = fuse_notify_poll(ph);