+ DEBUGf("opendir end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+
+}
+
+int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
+ off_t off, struct fuse_file_info *fi) {
+ int prv = 0, rv;
+ SV *sv, **svp, **swp;
+ AV *av, *av2;
+ struct stat st;
+ bool st_filled = 0;
+#ifndef PERL_HAS_64BITINT
+ char *temp;
+#endif
+ FUSE_CONTEXT_PRE;
+ DEBUGf("readdir begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
+#ifdef PERL_HAS_64BITINT
+ XPUSHs(sv_2mortal(newSViv(off)));
+#else
+ if (asprintf(&temp, "%llu", off) == -1)
+ croak("Memory allocation failure!");
+ XPUSHs(sv_2mortal(newSVpv(temp, 0)));
+ free(temp);
+#endif
+ XPUSHs(FH_GETHANDLE(fi));
+ PUTBACK;
+ prv = call_sv(MY_CXT.callback[26],G_ARRAY);
+ SPAGAIN;
+ if (prv) {
+ /* Should yield the bottom of the current stack... */
+ swp = &TOPs - prv + 1;
+ rv = POPi;
+ memset(&st, 0, sizeof(struct stat));
+ /* Sort of a hack to walk the stack in order, instead of reverse
+ * order - trying to explain to potential users why they need to
+ * reverse the order of this array would be confusing, at best. */
+ while (swp <= &TOPs) {
+ sv = *(swp++);
+ if (!SvROK(sv) && SvPOK(sv))
+ /* Just a bare SV (probably a string; hopefully a string) */
+ dirfil(dirh, SvPVx_nolen(sv), NULL, 0);
+ else if (SvROK(sv) && SvTYPE(av = (AV *)SvRV(sv)) == SVt_PVAV) {
+ if (av_len(av) >= 2) {
+ /* The third element of the array should be the args that
+ * would otherwise go to getattr(); a lot of filesystems
+ * will, or at least can, return that info as part of the
+ * enumeration process... */
+ svp = av_fetch(av, 2, FALSE);
+ if (SvROK(*svp) &&
+ SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
+ av_len(av2) == 12) {
+ st.st_dev = SvIV(*(av_fetch(av2, 0, FALSE)));
+ st.st_ino = SvIV(*(av_fetch(av2, 1, FALSE)));
+ st.st_mode = SvIV(*(av_fetch(av2, 2, FALSE)));
+ st.st_nlink = SvIV(*(av_fetch(av2, 3, FALSE)));
+ st.st_uid = SvIV(*(av_fetch(av2, 4, FALSE)));
+ st.st_gid = SvIV(*(av_fetch(av2, 5, FALSE)));
+ st.st_rdev = SvIV(*(av_fetch(av2, 6, FALSE)));
+ st.st_size = SvNV(*(av_fetch(av2, 7, FALSE)));
+ st.st_atime = SvIV(*(av_fetch(av2, 8, FALSE)));
+ st.st_mtime = SvIV(*(av_fetch(av2, 9, FALSE)));
+ st.st_ctime = SvIV(*(av_fetch(av2, 10, FALSE)));
+ st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
+ st.st_blocks = SvIV(*(av_fetch(av2, 12, FALSE)));
+ st_filled = 1;
+ }
+ else
+ fprintf(stderr,"Extra SV didn't appear to be correct, ignoring\n");
+ /* For now if the element isn't what we want, just
+ * quietly ignore it... */
+ }
+ if (av_len(av) >= 1) {
+ char *entryname = SvPVx_nolen(*(av_fetch(av, 1, FALSE)));
+ off_t elemnum = SvNV(*(av_fetch(av, 0, FALSE)));
+ dirfil(dirh, entryname, st_filled ? &st : NULL, elemnum);
+ }
+ if (st_filled) {
+ memset(&st, 0, sizeof(struct stat));
+ st_filled = 0;
+ }
+ }
+ else
+ fprintf(stderr, "ERROR: Unknown entry passed via readdir\n");
+ }
+ } else {
+ fprintf(stderr,"readdir() handler returned nothing!\n");
+ rv = -ENOSYS;
+ }
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("readdir end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+int _PLfuse_releasedir(const char *file, struct fuse_file_info *fi) {
+ int rv;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("releasedir begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
+ XPUSHs(FH_GETHANDLE(fi));
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[27], G_SCALAR);
+ SPAGAIN;
+ rv = (rv ? POPi : 0);
+ FH_RELEASEHANDLE(fi);
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("releasedir end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+int _PLfuse_fsyncdir(const char *file, int datasync,
+ struct fuse_file_info *fi) {
+ int rv;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("fsyncdir begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
+ XPUSHs(sv_2mortal(newSViv(datasync)));
+ XPUSHs(FH_GETHANDLE(fi));
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[28], G_SCALAR);
+ SPAGAIN;
+ rv = (rv ? POPi : 0);
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("fsyncdir end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+#if FUSE_VERSION >= 26
+void *_PLfuse_init(struct fuse_conn_info *fc)
+#else /* FUSE_VERSION < 26 */
+void *_PLfuse_init(void)
+#endif /* FUSE_VERSION >= 26 */
+{
+ void *rv = NULL;
+ int prv;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("init begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ PUTBACK;
+ prv = call_sv(MY_CXT.callback[29], G_SCALAR);
+ SPAGAIN;
+ if (prv) {
+ rv = POPs;
+ if (rv == &PL_sv_undef)
+ rv = NULL;
+ else
+ rv = SvREFCNT_inc((SV *)rv);
+ }
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("init end: %p\n", rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+void _PLfuse_destroy(void *private_data) {
+ FUSE_CONTEXT_PRE;
+ DEBUGf("destroy begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(private_data ? (SV *)private_data : &PL_sv_undef);
+ PUTBACK;
+ call_sv(MY_CXT.callback[30], G_VOID);
+ SPAGAIN;
+ if (private_data)
+ SvREFCNT_dec((SV *)private_data);
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("init end\n");
+ FUSE_CONTEXT_POST;
+}
+#endif /* FUSE_VERSION >= 23 */
+
+#if FUSE_VERSION >= 25
+int _PLfuse_access(const char *file, int mask) {
+ int rv;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("access begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(sv_2mortal(newSVpv(file,0)));
+ XPUSHs(sv_2mortal(newSViv(mask)));
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[31], G_SCALAR);
+ SPAGAIN;
+ rv = (rv ? POPi : 0);
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("access end: %d\n", rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
+ int rv;
+ HV *fihash;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("create begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(sv_2mortal(newSVpv(file,0)));
+ XPUSHs(sv_2mortal(newSViv(mode)));
+ XPUSHs(sv_2mortal(newSViv(fi->flags)));
+ fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
+ /* Create a hashref containing the details from fi
+ * which we can look at or modify.
+ */
+ fihash = newHV();
+ (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
+ (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
+#if FUSE_VERSION >= 29
+ (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
+#endif
+ XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
+ /* All hashref things done */
+
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[32], G_ARRAY);
+ SPAGAIN;
+ if (rv) {
+ if (rv > 1) {
+ FH_STOREHANDLE(fi,POPs);
+ }
+ rv = POPi;
+ }
+ else {
+ fprintf(stderr, "create() handler returned nothing!\n");
+ rv = -ENOSYS;
+ }
+ if (rv == 0) {
+ /* Success, so copy the file handle which they returned */
+ SV **svp;
+ if ((svp = hv_fetch(fihash, "direct_io", 9, 0)) != NULL)
+ fi->direct_io = SvIV(*svp);
+ if ((svp = hv_fetch(fihash, "keep_cache", 10, 0)) != NULL)
+ fi->keep_cache = SvIV(*svp);
+#if FUSE_VERSION >= 29
+ if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
+ fi->nonseekable = SvIV(*svp);
+#endif
+ }
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("create end: %d\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+int _PLfuse_ftruncate(const char *file, off_t off, struct fuse_file_info *fi) {
+ int rv;
+#ifndef PERL_HAS_64BITINT
+ char *temp;
+#endif
+ FUSE_CONTEXT_PRE;
+ DEBUGf("ftruncate begin\n");
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
+#ifdef PERL_HAS_64BITINT
+ XPUSHs(sv_2mortal(newSViv(off)));
+#else
+ if (asprintf(&temp, "%llu", off) == -1)
+ croak("Memory allocation failure!");
+ XPUSHs(sv_2mortal(newSVpv(temp, 0)));
+ free(temp);
+#endif
+ XPUSHs(FH_GETHANDLE(fi));
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[33],G_SCALAR);
+ SPAGAIN;
+ rv = (rv ? POPi : 0);
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("ftruncate end: %i\n",rv);
+ FUSE_CONTEXT_POST;
+ return rv;
+}
+
+int _PLfuse_fgetattr(const char *file, struct stat *result,
+ struct fuse_file_info *fi) {
+ int rv;
+ FUSE_CONTEXT_PRE;
+ DEBUGf("fgetattr begin: %s\n",file);
+ ENTER;
+ SAVETMPS;
+ PUSHMARK(SP);
+ XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
+ XPUSHs(FH_GETHANDLE(fi));
+ PUTBACK;
+ rv = call_sv(MY_CXT.callback[34],G_ARRAY);
+ SPAGAIN;
+ if(rv != 13) {
+ if(rv > 1) {
+ fprintf(stderr,"inappropriate number of returned values from getattr\n");
+ rv = -ENOSYS;
+ } else if(rv)
+ rv = POPi;
+ else
+ rv = -ENOENT;
+ } else {
+ result->st_blocks = POPi;
+ result->st_blksize = POPi;
+ result->st_ctime = POPi;
+ result->st_mtime = POPi;
+ result->st_atime = POPi;
+ result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
+ result->st_rdev = POPi;
+ result->st_gid = POPi;
+ result->st_uid = POPi;
+ result->st_nlink = POPi;
+ result->st_mode = POPi;
+ result->st_ino = POPi;
+ result->st_dev = POPi;
+ rv = 0;
+ }
+ FREETMPS;
+ LEAVE;
+ PUTBACK;
+ DEBUGf("fgetattr end: %i\n",rv);