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];
92 #ifdef FUSE_USE_ITHREADS
93 tTHX master_interp = NULL;
95 #define CLONE_INTERP(parent) S_clone_interp(parent)
96 tTHX S_clone_interp(tTHX parent) {
97 # if (PERL_VERSION < 10)
98 tTHX my_perl = parent;
100 dMY_CXT_INTERP(parent);
101 if(MY_CXT.threaded) {
102 MUTEX_LOCK(&MY_CXT.mutex);
103 PERL_SET_CONTEXT(parent);
105 #if (PERL_VERSION > 10) || (PERL_VERSION == 10 && PERL_SUBVERSION >= 1)
106 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST);
108 tTHX child = perl_clone(parent, CLONEf_CLONE_HOST | CLONEf_KEEP_PTR_TABLE);
109 ptr_table_free(PL_ptr_table);
112 MUTEX_UNLOCK(&MY_CXT.mutex);
118 # define FUSE_CONTEXT_PRE dTHX; if(!aTHX) aTHX = CLONE_INTERP(master_interp); { dMY_CXT; dSP;
119 # define FUSE_CONTEXT_POST }
121 # define FUSE_CONTEXT_PRE dTHX; dMY_CXT; dSP;
122 # define FUSE_CONTEXT_POST
127 #define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
132 #define FH_KEY(fi) sv_2mortal(newSViv((fi)->fh))
133 #define FH_GETHANDLE(fi) S_fh_get_handle(aTHX_ aMY_CXT_ fi)
134 #define FH_STOREHANDLE(fi,sv) S_fh_store_handle(aTHX_ aMY_CXT_ fi, sv)
135 #define FH_RELEASEHANDLE(fi) S_fh_release_handle(aTHX_ aMY_CXT_ fi)
137 SV *S_fh_get_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
142 if((he = hv_fetch_ent(MY_CXT.handles, FH_KEY(fi), 0, 0))) {
150 void S_fh_release_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi) {
152 (void)hv_delete_ent(MY_CXT.handles, FH_KEY(fi), G_DISCARD, 0);
157 void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
159 #ifdef FUSE_USE_ITHREADS
160 if(MY_CXT.threaded) {
164 /* This seems to be screwing things up... */
165 // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
166 // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
168 if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
175 int _PLfuse_getattr(const char *file, struct stat *result) {
178 DEBUGf("getattr begin: %s\n",file);
182 XPUSHs(sv_2mortal(newSVpv(file,strlen(file))));
184 rv = call_sv(MY_CXT.callback[0],G_ARRAY);
188 fprintf(stderr,"inappropriate number of returned values from getattr\n");
195 result->st_blocks = POPi;
196 result->st_blksize = POPi;
197 PULL_TIME(result, st_ctim, POPs);
198 PULL_TIME(result, st_mtim, POPs);
199 PULL_TIME(result, st_atim, POPs);
200 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
201 result->st_rdev = POPi;
202 result->st_gid = POPi;
203 result->st_uid = POPi;
204 result->st_nlink = POPi;
205 result->st_mode = POPi;
206 result->st_ino = POPi;
207 result->st_dev = POPi;
213 DEBUGf("getattr end: %i\n",rv);
218 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
223 DEBUGf("readlink begin\n");
227 XPUSHs(sv_2mortal(newSVpv(file,0)));
229 rv = call_sv(MY_CXT.callback[1],G_SCALAR);
235 if(SvTYPE(mysv) == SVt_IV || SvTYPE(mysv) == SVt_NV)
238 strncpy(buf,SvPV_nolen(mysv),buflen);
246 DEBUGf("readlink end: %i\n",rv);
251 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
255 DEBUGf("getdir begin\n");
259 XPUSHs(sv_2mortal(newSVpv(file,0)));
261 prv = call_sv(MY_CXT.callback[2],G_ARRAY);
264 /* Should yield the bottom of the current stack... */
265 swp = &TOPs - prv + 1;
267 /* Sort of a hack to walk the stack in order, instead of reverse
268 * order - trying to explain to potential users why they need to
269 * reverse the order of this array would be confusing, at best. */
271 dirfil(dirh,SvPVx_nolen(*(swp++)),0,0);
273 fprintf(stderr,"getdir() handler returned nothing!\n");
279 DEBUGf("getdir end: %i\n",rv);
284 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
287 DEBUGf("mknod begin\n");
291 XPUSHs(sv_2mortal(newSVpv(file,0)));
292 XPUSHs(sv_2mortal(newSViv(mode)));
293 XPUSHs(sv_2mortal(newSViv(dev)));
295 rv = call_sv(MY_CXT.callback[3],G_SCALAR);
297 rv = (rv ? POPi : 0);
301 DEBUGf("mknod end: %i\n",rv);
306 int _PLfuse_mkdir (const char *file, mode_t mode) {
309 DEBUGf("mkdir begin\n");
313 XPUSHs(sv_2mortal(newSVpv(file,0)));
314 XPUSHs(sv_2mortal(newSViv(mode)));
316 rv = call_sv(MY_CXT.callback[4],G_SCALAR);
318 rv = (rv ? POPi : 0);
322 DEBUGf("mkdir end: %i\n",rv);
328 int _PLfuse_unlink (const char *file) {
331 DEBUGf("unlink begin\n");
335 XPUSHs(sv_2mortal(newSVpv(file,0)));
337 rv = call_sv(MY_CXT.callback[5],G_SCALAR);
339 rv = (rv ? POPi : 0);
343 DEBUGf("unlink end: %i\n",rv);
348 int _PLfuse_rmdir (const char *file) {
351 DEBUGf("rmdir begin\n");
355 XPUSHs(sv_2mortal(newSVpv(file,0)));
357 rv = call_sv(MY_CXT.callback[6],G_SCALAR);
359 rv = (rv ? POPi : 0);
363 DEBUGf("rmdir end: %i\n",rv);
368 int _PLfuse_symlink (const char *file, const char *new) {
371 DEBUGf("symlink begin\n");
375 XPUSHs(sv_2mortal(newSVpv(file,0)));
376 XPUSHs(sv_2mortal(newSVpv(new,0)));
378 rv = call_sv(MY_CXT.callback[7],G_SCALAR);
380 rv = (rv ? POPi : 0);
384 DEBUGf("symlink end: %i\n",rv);
389 int _PLfuse_rename (const char *file, const char *new) {
392 DEBUGf("rename begin\n");
396 XPUSHs(sv_2mortal(newSVpv(file,0)));
397 XPUSHs(sv_2mortal(newSVpv(new,0)));
399 rv = call_sv(MY_CXT.callback[8],G_SCALAR);
401 rv = (rv ? POPi : 0);
405 DEBUGf("rename end: %i\n",rv);
410 int _PLfuse_link (const char *file, const char *new) {
413 DEBUGf("link begin\n");
417 XPUSHs(sv_2mortal(newSVpv(file,0)));
418 XPUSHs(sv_2mortal(newSVpv(new,0)));
420 rv = call_sv(MY_CXT.callback[9],G_SCALAR);
422 rv = (rv ? POPi : 0);
426 DEBUGf("link end: %i\n",rv);
431 int _PLfuse_chmod (const char *file, mode_t mode) {
434 DEBUGf("chmod begin\n");
438 XPUSHs(sv_2mortal(newSVpv(file,0)));
439 XPUSHs(sv_2mortal(newSViv(mode)));
441 rv = call_sv(MY_CXT.callback[10],G_SCALAR);
443 rv = (rv ? POPi : 0);
447 DEBUGf("chmod end: %i\n",rv);
452 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
455 DEBUGf("chown begin\n");
459 XPUSHs(sv_2mortal(newSVpv(file,0)));
460 XPUSHs(sv_2mortal(newSViv(uid)));
461 XPUSHs(sv_2mortal(newSViv(gid)));
463 rv = call_sv(MY_CXT.callback[11],G_SCALAR);
465 rv = (rv ? POPi : 0);
469 DEBUGf("chown end: %i\n",rv);
474 int _PLfuse_truncate (const char *file, off_t off) {
476 #ifndef PERL_HAS_64BITINT
480 DEBUGf("truncate begin\n");
484 XPUSHs(sv_2mortal(newSVpv(file,0)));
485 #ifdef PERL_HAS_64BITINT
486 XPUSHs(sv_2mortal(newSViv(off)));
488 if (asprintf(&temp, "%llu", off) == -1)
489 croak("Memory allocation failure!");
490 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
494 rv = call_sv(MY_CXT.callback[12],G_SCALAR);
496 rv = (rv ? POPi : 0);
500 DEBUGf("truncate end: %i\n",rv);
505 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
508 DEBUGf("utime begin\n");
512 XPUSHs(sv_2mortal(newSVpv(file,0)));
513 XPUSHs(sv_2mortal(newSViv(uti->actime)));
514 XPUSHs(sv_2mortal(newSViv(uti->modtime)));
516 rv = call_sv(MY_CXT.callback[13],G_SCALAR);
518 rv = (rv ? POPi : 0);
522 DEBUGf("utime end: %i\n",rv);
527 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
529 int flags = fi->flags;
532 DEBUGf("open begin\n");
536 XPUSHs(sv_2mortal(newSVpv(file,0)));
537 XPUSHs(sv_2mortal(newSViv(flags)));
538 /* Create a hashref containing the details from fi
539 * which we can look at or modify.
541 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
543 #if FUSE_VERSION >= 24
544 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
545 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
547 #if FUSE_VERSION >= 28
548 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
550 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
551 /* All hashref things done */
554 /* Open called with filename, flags */
555 rv = call_sv(MY_CXT.callback[14],G_ARRAY);
559 FH_STOREHANDLE(fi,POPs);
567 /* Success, so copy the file handle which they returned */
568 #if FUSE_VERSION >= 24
570 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
571 fi->direct_io = SvIV(*svp);
572 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
573 fi->keep_cache = SvIV(*svp);
575 #if FUSE_VERSION >= 28
576 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
577 fi->nonseekable = SvIV(*svp);
583 DEBUGf("open end: %i\n",rv);
588 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off,
589 struct fuse_file_info *fi) {
591 #ifndef PERL_HAS_64BITINT
595 DEBUGf("read begin\n");
599 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
600 XPUSHs(sv_2mortal(newSViv(buflen)));
601 #ifdef PERL_HAS_64BITINT
602 XPUSHs(sv_2mortal(newSViv(off)));
604 if (asprintf(&temp, "%llu", off) == -1)
605 croak("Memory allocation failure!");
606 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
609 XPUSHs(FH_GETHANDLE(fi));
611 rv = call_sv(MY_CXT.callback[15],G_SCALAR);
617 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
626 croak("read() handler returned more than buflen! (%i > %i)",rv,buflen);
628 memcpy(buf,SvPV_nolen(mysv),rv);
634 DEBUGf("read end: %i\n",rv);
639 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
641 #ifndef PERL_HAS_64BITINT
645 DEBUGf("write begin\n");
649 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
650 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
651 #ifdef PERL_HAS_64BITINT
652 XPUSHs(sv_2mortal(newSViv(off)));
654 if (asprintf(&temp, "%llu", off) == -1)
655 croak("Memory allocation failure!");
656 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
659 XPUSHs(FH_GETHANDLE(fi));
661 rv = call_sv(MY_CXT.callback[16],G_SCALAR);
663 rv = (rv ? POPi : 0);
667 DEBUGf("write end: %i\n",rv);
672 int _PLfuse_statfs (const char *file, struct statvfs *st) {
675 DEBUGf("statfs begin\n");
680 rv = call_sv(MY_CXT.callback[17],G_ARRAY);
682 DEBUGf("statfs got %i params\n",rv);
683 if(rv == 6 || rv == 7) {
689 st->f_namemax = POPi;
690 /* zero and fill-in other */
694 st->f_bavail = st->f_bfree;
695 st->f_favail = st->f_ffree;
703 croak("inappropriate number of returned values from statfs");
712 DEBUGf("statfs end: %i\n",rv);
717 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
720 DEBUGf("flush begin\n");
724 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
725 XPUSHs(FH_GETHANDLE(fi));
727 rv = call_sv(MY_CXT.callback[18],G_SCALAR);
729 rv = (rv ? POPi : 0);
733 DEBUGf("flush end: %i\n",rv);
738 int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
740 int flags = fi->flags;
742 DEBUGf("release begin\n");
746 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
747 XPUSHs(sv_2mortal(newSViv(flags)));
748 XPUSHs(FH_GETHANDLE(fi));
750 rv = call_sv(MY_CXT.callback[19],G_SCALAR);
752 rv = (rv ? POPi : 0);
753 FH_RELEASEHANDLE(fi);
757 DEBUGf("release end: %i\n",rv);
762 int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
764 int flags = fi->flags;
766 DEBUGf("fsync begin\n");
770 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
771 XPUSHs(sv_2mortal(newSViv(flags)));
772 XPUSHs(FH_GETHANDLE(fi));
774 rv = call_sv(MY_CXT.callback[20],G_SCALAR);
776 rv = (rv ? POPi : 0);
780 DEBUGf("fsync end: %i\n",rv);
785 #if __FreeBSD__ >= 10
786 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
788 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
792 DEBUGf("setxattr begin\n");
796 XPUSHs(sv_2mortal(newSVpv(file,0)));
797 XPUSHs(sv_2mortal(newSVpv(name,0)));
798 XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
799 XPUSHs(sv_2mortal(newSViv(flags)));
801 rv = call_sv(MY_CXT.callback[21],G_SCALAR);
803 rv = (rv ? POPi : 0);
807 DEBUGf("setxattr end: %i\n",rv);
812 #if __FreeBSD__ >= 10
813 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
815 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
819 DEBUGf("getxattr begin\n");
823 XPUSHs(sv_2mortal(newSVpv(file,0)));
824 XPUSHs(sv_2mortal(newSVpv(name,0)));
826 rv = call_sv(MY_CXT.callback[22],G_SCALAR);
834 if(SvTYPE(mysv) == SVt_NV || SvTYPE(mysv) == SVt_IV)
842 if ((rv > 0) && (buflen > 0))
847 memcpy(buf,SvPV_nolen(mysv),rv);
854 DEBUGf("getxattr end: %i\n",rv);
859 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
862 DEBUGf("listxattr begin\n");
866 XPUSHs(sv_2mortal(newSVpv(file,0)));
868 prv = call_sv(MY_CXT.callback[23],G_ARRAY);
881 /* Always nul terminate */
882 if (list && (size > 0))
892 int s = SvCUR(mysv) + 1;
895 if (p && (size > 0) && (spc >= s))
897 memcpy(p,SvPV_nolen(mysv),s);
905 * If the Perl returned an error, return that.
906 * Otherwise check that the buffer was big enough.
911 if ((size > 0) && (size < total_len))
918 DEBUGf("listxattr end: %i\n",rv);
923 int _PLfuse_removexattr (const char *file, const char *name) {
926 DEBUGf("removexattr begin\n");
930 XPUSHs(sv_2mortal(newSVpv(file,0)));
931 XPUSHs(sv_2mortal(newSVpv(name,0)));
933 rv = call_sv(MY_CXT.callback[24],G_SCALAR);
935 rv = (rv ? POPi : 0);
939 DEBUGf("removexattr end: %i\n",rv);
944 #if FUSE_VERSION >= 23
945 int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
948 DEBUGf("opendir begin\n");
952 XPUSHs(sv_2mortal(newSVpv(file,0)));
953 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
955 rv = call_sv(MY_CXT.callback[25], G_ARRAY);
959 FH_STOREHANDLE(fi, POPs);
967 DEBUGf("opendir end: %i\n",rv);
973 int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
974 off_t off, struct fuse_file_info *fi) {
976 SV *sv, **svp, **swp;
980 #ifndef PERL_HAS_64BITINT
984 DEBUGf("readdir begin\n");
988 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
989 #ifdef PERL_HAS_64BITINT
990 XPUSHs(sv_2mortal(newSViv(off)));
992 if (asprintf(&temp, "%llu", off) == -1)
993 croak("Memory allocation failure!");
994 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
997 XPUSHs(FH_GETHANDLE(fi));
999 prv = call_sv(MY_CXT.callback[26],G_ARRAY);
1002 /* Should yield the bottom of the current stack... */
1003 swp = &TOPs - prv + 1;
1005 memset(&st, 0, sizeof(struct stat));
1006 /* Sort of a hack to walk the stack in order, instead of reverse
1007 * order - trying to explain to potential users why they need to
1008 * reverse the order of this array would be confusing, at best. */
1009 while (swp <= &TOPs) {
1011 if (!SvROK(sv) && SvPOK(sv))
1012 /* Just a bare SV (probably a string; hopefully a string) */
1013 dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
1014 else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
1015 if (av_len(av) >= 2) {
1016 /* The third element of the array should be the args that
1017 * would otherwise go to getattr(); a lot of filesystems
1018 * will, or at least can, return that info as part of the
1019 * enumeration process... */
1020 svp = av_fetch(av, 2, FALSE);
1022 SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
1023 av_len(av2) == 12) {
1024 st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
1025 st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
1026 st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
1027 st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
1028 st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
1029 st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
1030 st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
1031 st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
1032 PULL_TIME(&st, st_atim, *(av_fetch(av2, 8, FALSE)));
1033 PULL_TIME(&st, st_mtim, *(av_fetch(av2, 9, FALSE)));
1034 PULL_TIME(&st, st_ctim, *(av_fetch(av2, 10, FALSE)));
1035 st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
1036 st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
1040 fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
1041 /* For now if the element isn't what we want, just
1042 * quietly ignore it... */
1044 if (av_len(av) >= 1) {
1045 char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
1046 off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
1047 dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
1050 memset(&st, 0, sizeof(struct stat));
1055 fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
1058 fprintf(stderr,"readdir() handler returned nothing!\n");
1064 DEBUGf("readdir end: %i\n",rv);
1069 int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
1072 DEBUGf("releasedir begin\n");
1076 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1077 XPUSHs(FH_GETHANDLE(fi));
1079 rv = call_sv(MY_CXT.callback[27], G_SCALAR);
1081 rv = (rv ? POPi : 0);
1082 FH_RELEASEHANDLE(fi);
1086 DEBUGf("releasedir end: %i\n",rv);
1091 int _PLfuse_fsyncdir(const char *file, int datasync,
1092 struct fuse_file_info *fi) {
1095 DEBUGf("fsyncdir begin\n");
1099 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1100 XPUSHs(sv_2mortal(newSViv(datasync)));
1101 XPUSHs(FH_GETHANDLE(fi));
1103 rv = call_sv(MY_CXT.callback[28], G_SCALAR);
1105 rv = (rv ? POPi : 0);
1109 DEBUGf("fsyncdir end: %i\n",rv);
1114 #if FUSE_VERSION >= 26
1115 void *_PLfuse_init(struct fuse_conn_info *fc)
1116 #else /* FUSE_VERSION < 26 */
1117 void *_PLfuse_init(void)
1118 #endif /* FUSE_VERSION >= 26 */
1123 DEBUGf("init begin\n");
1128 prv = call_sv(MY_CXT.callback[29], G_SCALAR);
1132 if (rv == &PL_sv_undef)
1135 rv = SvREFCNT_inc((SV *)rv);
1140 DEBUGf("init end: %p\n", rv);
1145 void _PLfuse_destroy(void *private_data) {
1147 DEBUGf("destroy begin\n");
1151 XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
1153 call_sv(MY_CXT.callback[30], G_VOID);
1156 SvREFCNT_dec((SV *)private_data);
1160 DEBUGf("init end\n");
1163 #endif /* FUSE_VERSION >= 23 */
1165 #if FUSE_VERSION >= 25
1166 int _PLfuse_access(const char *file, int mask) {
1169 DEBUGf("access begin\n");
1173 XPUSHs(sv_2mortal(newSVpv(file,0)));
1174 XPUSHs(sv_2mortal(newSViv(mask)));
1176 rv = call_sv(MY_CXT.callback[31], G_SCALAR);
1178 rv = (rv ? POPi : 0);
1182 DEBUGf("access end: %d\n", rv);
1187 int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
1191 DEBUGf("create begin\n");
1195 XPUSHs(sv_2mortal(newSVpv(file,0)));
1196 XPUSHs(sv_2mortal(newSViv(mode)));
1197 XPUSHs(sv_2mortal(newSViv(fi->flags)));
1198 fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
1199 /* Create a hashref containing the details from fi
1200 * which we can look at or modify.
1203 (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
1204 (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
1205 #if FUSE_VERSION >= 28
1206 (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
1208 XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
1209 /* All hashref things done */
1212 rv = call_sv(MY_CXT.callback[32], G_ARRAY);
1216 FH_STOREHANDLE(fi,POPs);
1221 fprintf(stderr, "create() handler returned nothing!\n");
1225 /* Success, so copy the file handle which they returned */
1227 if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
1228 fi->direct_io = SvIV(*svp);
1229 if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
1230 fi->keep_cache = SvIV(*svp);
1231 #if FUSE_VERSION >= 28
1232 if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
1233 fi->nonseekable = SvIV(*svp);
1239 DEBUGf("create end: %d\n",rv);
1244 int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
1246 #ifndef PERL_HAS_64BITINT
1250 DEBUGf("ftruncate begin\n");
1254 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1255 #ifdef PERL_HAS_64BITINT
1256 XPUSHs(sv_2mortal(newSViv(off)));
1258 if (asprintf(&temp, "%llu", off) == -1)
1259 croak("Memory allocation failure!");
1260 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1263 XPUSHs(FH_GETHANDLE(fi));
1265 rv = call_sv(MY_CXT.callback[33],G_SCALAR);
1267 rv = (rv ? POPi : 0);
1271 DEBUGf("ftruncate end: %i\n",rv);
1276 int _PLfuse_fgetattr(const char *file, struct stat *result,
1277 struct fuse_file_info *fi) {
1280 DEBUGf("fgetattr begin: %s\n",file);
1284 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1285 XPUSHs(FH_GETHANDLE(fi));
1287 rv = call_sv(MY_CXT.callback[34],G_ARRAY);
1291 fprintf(stderr,"inappropriate number of returned values from getattr\n");
1298 result->st_blocks = POPi;
1299 result->st_blksize = POPi;
1300 PULL_TIME(result, st_ctim, POPs);
1301 PULL_TIME(result, st_mtim, POPs);
1302 PULL_TIME(result, st_atim, POPs);
1303 result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
1304 result->st_rdev = POPi;
1305 result->st_gid = POPi;
1306 result->st_uid = POPi;
1307 result->st_nlink = POPi;
1308 result->st_mode = POPi;
1309 result->st_ino = POPi;
1310 result->st_dev = POPi;
1316 DEBUGf("fgetattr end: %i\n",rv);
1320 #endif /* FUSE_VERSION >= 25 */
1322 #if FUSE_VERSION >= 26
1323 int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
1324 struct flock *lockinfo) {
1328 #ifndef PERL_HAS_64BITINT
1332 DEBUGf("lock begin\n");
1336 XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
1337 XPUSHs(sv_2mortal(newSViv(cmd)));
1340 (void) hv_store(lihash, "l_type", 6, newSViv(lockinfo->l_type), 0);
1341 (void) hv_store(lihash, "l_whence", 8, newSViv(lockinfo->l_whence), 0);
1342 #ifdef PERL_HAS_64BITINT
1343 sv = newSViv(lockinfo->l_start);
1345 if (asprintf(&temp, "%llu", lockinfo->l_start) == -1)
1346 croak("Memory allocation failure!");
1347 sv = newSVpv(temp, 0);
1350 (void) hv_store(lihash, "l_start", 7, sv, 0);
1351 #ifdef PERL_HAS_64BITINT
1352 sv = newSViv(lockinfo->l_len);
1354 if (asprintf(&temp, "%llu", lockinfo->l_len) == -1)
1355 croak("Memory allocation failure!");
1356 sv = newSVpv(temp, 0);
1359 (void) hv_store(lihash, "l_len", 5, sv, 0);
1360 (void) hv_store(lihash, "l_pid", 5, newSViv(lockinfo->l_pid), 0);
1362 XPUSHs(sv_2mortal(newRV_noinc((SV*) lihash)));
1363 XPUSHs(FH_GETHANDLE(fi));
1366 rv = call_sv(MY_CXT.callback[35],G_SCALAR);
1368 rv = (rv ? POPi : 0);
1369 if (lockinfo && !rv) {
1370 /* Need to copy back any altered values from the hash into
1373 if ((svp = hv_fetch(lihash, "l_type", 6, 0)) != NULL)
1374 lockinfo->l_type = SvIV(*svp);
1375 if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
1376 lockinfo->l_whence = SvIV(*svp);
1377 if ((svp = hv_fetch(lihash, "l_start", 7, 0)) != NULL)
1378 lockinfo->l_start = SvNV(*svp);
1379 if ((svp = hv_fetch(lihash, "l_len", 5, 0)) != NULL)
1380 lockinfo->l_len = SvNV(*svp);
1381 if ((svp = hv_fetch(lihash, "l_pid", 5, 0)) != NULL)
1382 lockinfo->l_pid = SvIV(*svp);
1387 DEBUGf("lock end: %i\n",rv);
1392 int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
1395 DEBUGf("utimens begin\n");
1399 XPUSHs(sv_2mortal(newSVpv(file,0)));
1400 XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1401 XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
1403 rv = call_sv(MY_CXT.callback[36],G_SCALAR);
1405 rv = (rv ? POPi : 0);
1409 DEBUGf("utimens end: %i\n",rv);
1414 int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
1416 #ifndef PERL_HAS_64BITINT
1420 DEBUGf("bmap begin\n");
1424 XPUSHs(sv_2mortal(newSVpv(file,0)));
1425 XPUSHs(sv_2mortal(newSViv(blocksize)));
1426 #ifdef PERL_HAS_64BITINT
1427 XPUSHs(sv_2mortal(newSViv(*idx)));
1429 if (asprintf(&temp, "%llu", *idx) == -1)
1430 croak("Memory allocation failure!");
1431 XPUSHs(sv_2mortal(newSVpv(temp, 0)));
1435 rv = call_sv(MY_CXT.callback[37],G_ARRAY);
1437 if (rv > 0 && rv < 3) {
1443 fprintf(stderr, "bmap(): wrong number of values returned?\n");
1449 DEBUGf("bmap end: %i\n",rv);
1453 #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) {
1461 DEBUGf("ioctl begin\n");
1465 XPUSHs(sv_2mortal(newSVpv(file,0)));
1466 /* I don't know why cmd is a signed int in the first place;
1467 * casting as unsigned so stupid tricks don't have to be done on
1469 XPUSHs(sv_2mortal(newSViv((unsigned int)cmd)));
1470 XPUSHs(sv_2mortal(newSViv(flags)));
1471 if (_IOC_DIR(cmd) & _IOC_WRITE)
1472 XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
1474 XPUSHs(&PL_sv_undef);
1475 XPUSHs(FH_GETHANDLE(fi));
1477 rv = call_sv(MY_CXT.callback[39],G_ARRAY);
1479 if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
1487 if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
1490 char *rdata = SvPV(sv, len);
1492 if (len > _IOC_SIZE(cmd)) {
1493 fprintf(stderr, "ioctl(): returned data was too large for data area\n");
1497 memset(data, 0, _IOC_SIZE(cmd));
1498 memcpy(data, rdata, len);
1502 fprintf(stderr, "ioctl(): ioctl was a read op, but no data was returned from call?\n");
1509 DEBUGf("ioctl end: %i\n",rv);
1514 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
1515 struct fuse_pollhandle *ph, unsigned *reventsp) {
1519 DEBUGf("poll begin\n");
1523 XPUSHs(sv_2mortal(newSVpv(file,0)));
1525 /* Still gotta figure out how to do this right... */
1526 sv = newSViv(PTR2IV(ph));
1532 XPUSHs(&PL_sv_undef);
1533 XPUSHs(sv_2mortal(newSViv(*reventsp)));
1534 XPUSHs(FH_GETHANDLE(fi));
1536 rv = call_sv(MY_CXT.callback[40],G_ARRAY);
1542 rv = (rv ? POPi : 0);
1546 DEBUGf("poll end: %i\n", rv);
1550 #endif /* FUSE_VERSION >= 28 */
1552 struct fuse_operations _available_ops = {
1553 getattr: _PLfuse_getattr,
1554 readlink: _PLfuse_readlink,
1555 getdir: _PLfuse_getdir,
1556 mknod: _PLfuse_mknod,
1557 mkdir: _PLfuse_mkdir,
1558 unlink: _PLfuse_unlink,
1559 rmdir: _PLfuse_rmdir,
1560 symlink: _PLfuse_symlink,
1561 rename: _PLfuse_rename,
1563 chmod: _PLfuse_chmod,
1564 chown: _PLfuse_chown,
1565 truncate: _PLfuse_truncate,
1566 utime: _PLfuse_utime,
1569 write: _PLfuse_write,
1570 statfs: _PLfuse_statfs,
1571 flush: _PLfuse_flush,
1572 release: _PLfuse_release,
1573 fsync: _PLfuse_fsync,
1574 setxattr: _PLfuse_setxattr,
1575 getxattr: _PLfuse_getxattr,
1576 listxattr: _PLfuse_listxattr,
1577 removexattr: _PLfuse_removexattr,
1578 #if FUSE_VERSION >= 23
1579 opendir: _PLfuse_opendir,
1580 readdir: _PLfuse_readdir,
1581 releasedir: _PLfuse_releasedir,
1582 fsyncdir: _PLfuse_fsyncdir,
1584 destroy: _PLfuse_destroy,
1585 #endif /* FUSE_VERSION >= 23 */
1586 #if FUSE_VERSION >= 25
1587 access: _PLfuse_access,
1588 create: _PLfuse_create,
1589 ftruncate: _PLfuse_ftruncate,
1590 fgetattr: _PLfuse_fgetattr,
1591 #endif /* FUSE_VERSION >= 25 */
1592 #if FUSE_VERSION >= 26
1594 utimens: _PLfuse_utimens,
1596 #endif /* FUSE_VERSION >= 26 */
1597 #if FUSE_VERSION >= 28
1598 ioctl: _PLfuse_ioctl,
1600 #endif /* FUSE_VERSION >= 28 */
1603 MODULE = Fuse PACKAGE = Fuse
1622 tTHX parent = MY_CXT.self;
1623 MY_CXT.self = my_perl;
1624 #if (PERL_VERSION < 10) || (PERL_VERSION == 10 && PERL_SUBVERSION <= 0)
1625 /* CLONE entered without a pointer table, so we can't safely clone static data */
1627 for(i=0;i<N_CALLBACKS;i++) {
1628 MY_CXT.callback[i] = NULL;
1630 MY_CXT.handles = newHV();
1634 CLONE_PARAMS *clone_param;
1635 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1636 clone_param = Perl_clone_params_new(parent, aTHX);
1638 CLONE_PARAMS raw_param;
1639 raw_param.flags = 0;
1640 raw_param.proto_perl = parent;
1641 raw_param.stashes = (AV*)sv_2mortal((SV*)newAV());
1642 clone_param = &raw_param;
1644 for(i=0;i<N_CALLBACKS;i++) {
1645 MY_CXT.callback[i] = sv_dup(MY_CXT.callback[i], clone_param);
1647 MY_CXT.handles = (HV*)sv_dup((SV*)MY_CXT.handles, clone_param);
1648 #if (PERL_VERSION > 13) || (PERL_VERSION == 13 && PERL_SUBVERSION >= 2)
1649 Perl_clone_params_del(clone_param);
1657 struct fuse_context *fc;
1659 fc = fuse_get_context();
1662 (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
1663 (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
1664 (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
1665 if (fc->private_data)
1666 (void) hv_store(hash, "private", 7, fc->private_data, 0);
1667 #if FUSE_VERSION >= 28
1668 (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
1669 #endif /* FUSE_VERSION >= 28 */
1670 RETVAL = newRV_noinc((SV*)hash);
1680 RETVAL = newSVpvf("%d.%d", FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION);
1687 RETVAL = newSViv(XATTR_CREATE);
1694 RETVAL = newSViv(XATTR_REPLACE);
1701 struct fuse_operations fops;
1705 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1706 struct fuse_chan *fc;
1709 if(items != N_CALLBACKS + 5) {
1710 fprintf(stderr,"Perl<->C inconsistency or internal error\n");
1713 memset(&fops, 0, sizeof(struct fuse_operations));
1715 debug = SvIV(ST(0));
1716 MY_CXT.threaded = SvIV(ST(1));
1717 MY_CXT.handles = (HV*)(sv_2mortal((SV*)(newHV())));
1718 if(MY_CXT.threaded) {
1719 #ifdef FUSE_USE_ITHREADS
1720 master_interp = aTHX;
1721 MUTEX_INIT(&MY_CXT.mutex);
1722 SvSHARE((SV*)(MY_CXT.handles));
1724 fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
1725 "mode, but your perl was not built with a supported "
1726 "thread model. Threads are disabled.\n");
1727 MY_CXT.threaded = 0;
1730 mountpoint = SvPV_nolen(ST(2));
1731 mountopts = SvPV_nolen(ST(3));
1732 #if FUSE_VERSION >= 28
1733 fops.flag_nullpath_ok = SvIV(ST(4));
1734 #endif /* FUSE_VERSION >= 28 */
1735 for(i=0;i<N_CALLBACKS;i++) {
1737 /* allow symbolic references, or real code references. */
1738 if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
1739 void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
1740 /* Dirty hack, to keep anything from overwriting the
1741 * flag area with a pointer. There should never be
1742 * anything passed as 'junk', but this prevents
1743 * someone from doing it and screwing things up... */
1747 MY_CXT.callback[i] = var;
1748 } else if(SvOK(var)) {
1749 croak("invalid callback (%i) passed to perl_fuse_main "
1750 "(%s is not a string, code ref, or undef).\n",
1751 i+5,SvPVbyte_nolen(var));
1753 MY_CXT.callback[i] = NULL;
1757 * XXX: What comes here is just a ridiculous use of the option parsing API
1758 * to hack on compatibility with other parts of the new API. First and
1759 * foremost, real C argc/argv would be good to get at...
1761 if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
1762 fuse_opt_free_args(&args);
1763 croak("out of memory\n");
1765 if (mountopts && strcmp("", mountopts) &&
1766 (fuse_opt_add_arg(&args, "-o") == -1 ||
1767 fuse_opt_add_arg(&args, mountopts) == -1)) {
1768 fuse_opt_free_args(&args);
1769 croak("out of memory\n");
1771 if (debug && fuse_opt_add_arg(&args, "-d") == -1) {
1772 fuse_opt_free_args(&args);
1773 croak("out of memory\n");
1775 fc = fuse_mount(mountpoint,&args);
1777 croak("could not mount fuse filesystem!\n");
1779 if(MY_CXT.threaded) {
1780 fuse_loop_mt(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1783 fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
1784 fuse_unmount(mountpoint,fc);
1785 fuse_opt_free_args(&args);
1787 #if FUSE_VERSION >= 28
1790 pollhandle_destroy(...)
1792 struct fuse_pollhandle *ph;
1795 fprintf(stderr, "No pollhandle passed?\n");
1799 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1800 fuse_pollhandle_destroy(ph);
1805 struct fuse_pollhandle *ph;
1808 fprintf(stderr, "No pollhandle passed?\n");
1812 ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
1813 RETVAL = fuse_notify_poll(ph);