fuseopts, RT #50000
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
old mode 100644 (file)
new mode 100755 (executable)
index 318d355..6203eca
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -1,12 +1,41 @@
+/*
+ * vim:ts=8:noet
+ */
+
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
 
-#include <fuse/fuse.h>
+#ifdef USE_ITHREADS
+# ifdef I_PTHREAD
+/* perl implements threads with pthread.  So, we use the pthread API for
+ * handling thread-local storage. */
+#  include <pthread.h>
+PerlInterpreter *master_interp = NULL;
+static inline void create_perl_context() {
+       if(master_interp) {
+               PerlInterpreter *me = PERL_GET_CONTEXT;
+               if(!me) {
+                       PERL_SET_CONTEXT(master_interp);
+                       me = perl_clone(master_interp, CLONEf_CLONE_HOST);
+               }
+       }
+}
+#  define FUSE_CONTEXT_PRE create_perl_context(); { dSP
+#  define FUSE_CONTEXT_POST }
+#  define FUSE_USE_ITHREADS
+# else
+#  error "Sorry, I don't know how to handle ithreads on this architecture."
+# endif
+#else
+# define FUSE_CONTEXT_PRE dSP
+# define FUSE_CONTEXT_POST
+#endif
+#include <fuse.h>
 
 #undef DEBUGf
 #if 0
-#define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,PL_stack_sp-PL_stack_base ,##a )
+#define DEBUGf(f, a...) fprintf(stderr, "%s:%d (%i): " f,__BASE_FILE__,__LINE__,sp-PL_stack_base ,##a )
 #else
 #define DEBUGf(a...)
 #endif
@@ -15,8 +44,9 @@
 SV *_PLfuse_callbacks[N_CALLBACKS];
 
 int _PLfuse_getattr(const char *file, struct stat *result) {
-       dSP;
-       int rv, statcount;
+       int rv;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("getattr begin: %s\n",file);
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -38,29 +68,30 @@ int _PLfuse_getattr(const char *file, struct stat *result) {
                result->st_ctime = POPi;
                result->st_mtime = POPi;
                result->st_atime = POPi;
-               result->st_size = 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_ino   = POPi;
                result->st_dev = POPi;
                rv = 0;
        }
        FREETMPS;
        LEAVE;
        PUTBACK;
+       DEBUGf("getattr end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
        int rv;
-       char *rvstr;
-       dSP;
-       I32 ax;
+       FUSE_CONTEXT_PRE;
        if(buflen < 1)
                return EINVAL;
+       DEBUGf("readlink begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -83,12 +114,22 @@ int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
        LEAVE;
        buf[buflen-1] = 0;
        PUTBACK;
+       DEBUGf("readlink end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
+#if 0
+/*
+ * This doesn't yet work... we alwas get ENOSYS when trying to use readdir().
+ * Well, of course, getdir() is fine as well.
+ */
+ int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil, off_t off, struct fuse_file_info *fi) {
+#endif
 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
        int prv, rv;
-       dSP;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("getdir begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -99,7 +140,7 @@ int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
        if(prv) {
                rv = POPi;
                while(--prv)
-                       dirfil(dirh,POPp,0);
+                       dirfil(dirh,POPp,0,0);
        } else {
                fprintf(stderr,"getdir() handler returned nothing!\n");
                rv = -ENOSYS;
@@ -107,14 +148,15 @@ int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
        FREETMPS;
        LEAVE;
        PUTBACK;
+       DEBUGf("getdir end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("mknod begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -131,15 +173,15 @@ int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
        FREETMPS;
        LEAVE;
        PUTBACK;
+       DEBUGf("mknod end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_mkdir (const char *file, mode_t mode) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("mkdir begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("mkdir begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -155,17 +197,16 @@ int _PLfuse_mkdir (const char *file, mode_t mode) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("mkdir end: %i %i\n",sp-PL_stack_base,rv);
+       DEBUGf("mkdir end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 
 int _PLfuse_unlink (const char *file) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("unlink begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("unlink begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -180,16 +221,15 @@ int _PLfuse_unlink (const char *file) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("unlink end: %i\n",sp-PL_stack_base);
+       DEBUGf("unlink end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_rmdir (const char *file) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("rmdir begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("rmdir begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -204,16 +244,15 @@ int _PLfuse_rmdir (const char *file) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("rmdir end: %i %i\n",sp-PL_stack_base,rv);
+       DEBUGf("rmdir end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_symlink (const char *file, const char *new) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("symlink begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("symlink begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -229,16 +268,15 @@ int _PLfuse_symlink (const char *file, const char *new) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("symlink end: %i\n",sp-PL_stack_base);
+       DEBUGf("symlink end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_rename (const char *file, const char *new) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("rename begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("rename begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -254,16 +292,15 @@ int _PLfuse_rename (const char *file, const char *new) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("rename end: %i\n",sp-PL_stack_base);
+       DEBUGf("rename end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_link (const char *file, const char *new) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("link begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("link begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -279,16 +316,15 @@ int _PLfuse_link (const char *file, const char *new) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("link end: %i\n",sp-PL_stack_base);
+       DEBUGf("link end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_chmod (const char *file, mode_t mode) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("chmod begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("chmod begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -304,16 +340,15 @@ int _PLfuse_chmod (const char *file, mode_t mode) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("chmod end: %i\n",sp-PL_stack_base);
+       DEBUGf("chmod end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("chown begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("chown begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -330,16 +365,15 @@ int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("chown end: %i\n",sp-PL_stack_base);
+       DEBUGf("chown end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_truncate (const char *file, off_t off) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("truncate begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("truncate begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -355,16 +389,15 @@ int _PLfuse_truncate (const char *file, off_t off) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("truncate end: %i\n",sp-PL_stack_base);
+       DEBUGf("truncate end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("utime begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("utime begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -381,46 +414,103 @@ int _PLfuse_utime (const char *file, struct utimbuf *uti) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("utime end: %i\n",sp-PL_stack_base);
+       DEBUGf("utime end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_open (const char *file, int flags) {
+int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        int rv;
-       SV *rvsv;
-       char *rvstr;
-       dSP;
-       DEBUGf("open begin: %i\n",sp-PL_stack_base);
+       int flags = fi->flags;
+       HV *fihash;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("open begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSViv(flags)));
+       /* Create a hashref containing the details from fi
+        * which we can look at or modify.
+        */
+       fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
+       fihash = newHV();
+#if FUSE_VERSION >= 24
+       hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
+       hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
+#endif
+#if FUSE_VERSION >= 29
+       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(_PLfuse_callbacks[14],G_SCALAR);
+       /* Open called with filename, flags */
+       rv = call_sv(_PLfuse_callbacks[14],G_ARRAY);
        SPAGAIN;
        if(rv)
+       {
+               SV *sv;
+               if (rv > 1)
+               {
+                       sv = POPs;
+                       if (SvOK(sv))
+                       {
+                               /* We're holding on to the sv reference until
+                                * after exit of this function, so we need to
+                                * increment its reference count
+                                */
+                               fi->fh = SvREFCNT_inc(sv);
+                       }
+               }
                rv = POPi;
+       }
        else
                rv = 0;
+       if (rv == 0)
+       {
+               /* Success, so copy the file handle which they returned */
+#if FUSE_VERSION >= 24
+               SV **svp;
+               svp = hv_fetch(fihash, "direct_io", 9, 0);
+               if (svp != NULL)
+               {
+                       fi->direct_io = SvIV(*svp);
+               }
+               svp = hv_fetch(fihash, "keep_cache", 10, 0);
+               if (svp != NULL)
+               {
+                       fi->keep_cache = SvIV(*svp);
+               }
+#endif
+#if FUSE_VERSION >= 29
+               svp = hv_fetch(fihash, "nonseekable", 11, 0);
+               if (svp != NULL)
+               {
+                       fi->nonseekable = SvIV(*svp);
+               }
+#endif
+       }
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("open end: %i %i\n",sp-PL_stack_base,rv);
+       DEBUGf("open end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off) {
+int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("read begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("read begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSViv(buflen)));
        XPUSHs(sv_2mortal(newSViv(off)));
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[15],G_SCALAR);
        SPAGAIN;
@@ -445,21 +535,22 @@ int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("read end: %i %i\n",sp-PL_stack_base,rv);
+       DEBUGf("read end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off) {
+int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("write begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("write begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
        XPUSHs(sv_2mortal(newSViv(off)));
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[16],G_SCALAR);
        SPAGAIN;
@@ -470,29 +561,37 @@ int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off)
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("write end: %i\n",sp-PL_stack_base);
+       DEBUGf("write end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_statfs (const char *file, struct statfs *st) {
+int _PLfuse_statfs (const char *file, struct statvfs *st) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("statfs begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("statfs begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[17],G_ARRAY);
        SPAGAIN;
-       if(rv > 5) {
-               st->f_bsize    = POPi;
-               st->f_bfree    = POPi;
-               st->f_blocks   = POPi;
-               st->f_ffree    = POPi;
-               st->f_files    = POPi;
-               st->f_namelen  = POPi;
-               if(rv > 6)
+       DEBUGf("statfs got %i params\n",rv);
+       if(rv == 6 || rv == 7) {
+               st->f_bsize     = POPi;
+               st->f_bfree     = POPi;
+               st->f_blocks    = POPi;
+               st->f_ffree     = POPi;
+               st->f_files     = POPi;
+               st->f_namemax   = POPi;
+               /* zero and fill-in other */
+               st->f_fsid = 0;
+               st->f_frsize = 4096;
+               st->f_flag = 0;
+               st->f_bavail = st->f_bfree;
+               st->f_favail = st->f_ffree;
+
+               if(rv == 7)
                        rv = POPi;
                else
                        rv = 0;
@@ -507,19 +606,20 @@ int _PLfuse_statfs (const char *file, struct statfs *st) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("statfs end: %i\n",sp-PL_stack_base);
+       DEBUGf("statfs end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_flush (const char *file) {
+int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("flush begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("flush begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[18],G_SCALAR);
        SPAGAIN;
@@ -530,20 +630,22 @@ int _PLfuse_flush (const char *file) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("flush end: %i\n",sp-PL_stack_base);
+       DEBUGf("flush end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_release (const char *file, int flags) {
+int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("release begin: %i\n",sp-PL_stack_base);
+       int flags = fi->flags;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("release begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSViv(flags)));
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[19],G_SCALAR);
        SPAGAIN;
@@ -551,23 +653,33 @@ int _PLfuse_release (const char *file, int flags) {
                rv = POPi;
        else
                rv = 0;
+       /* We're now finished with the handle that we were given, so
+        * we should decrement its count so that it can be freed.
+        */
+       if (fi->fh != 0)
+       {
+               SvREFCNT_dec((SV *)fi->fh);
+               fi->fh = 0;
+       }
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("release end: %i\n",sp-PL_stack_base);
+       DEBUGf("release end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
-int _PLfuse_fsync (const char *file, int flags) {
+int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("fsync begin: %i\n",sp-PL_stack_base);
+       int flags = fi->flags;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("fsync begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSViv(flags)));
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[20],G_SCALAR);
        SPAGAIN;
@@ -578,15 +690,15 @@ int _PLfuse_fsync (const char *file, int flags) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("fsync end: %i\n",sp-PL_stack_base);
+       DEBUGf("fsync end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("setxattr begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("setxattr begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -604,15 +716,15 @@ int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("setxattr end: %i\n",sp-PL_stack_base);
+       DEBUGf("setxattr end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("getxattr begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("getxattr begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -647,15 +759,15 @@ int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t bufl
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("getxattr end: %i\n",sp-PL_stack_base);
+       DEBUGf("getxattr end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
        int prv, rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("listxattr begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("listxattr begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -670,7 +782,6 @@ int _PLfuse_listxattr (const char *file, char *list, size_t size) {
                char *p = list;
                int spc = size;
                int total_len = 0;
-               int i;
 
                rv = POPi;
                prv--;
@@ -712,15 +823,15 @@ int _PLfuse_listxattr (const char *file, char *list, size_t size) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("listxattr end: %i\n",sp-PL_stack_base);
+       DEBUGf("listxattr end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
 int _PLfuse_removexattr (const char *file, const char *name) {
        int rv;
-       char *rvstr;
-       dSP;
-       DEBUGf("removexattr begin: %i\n",sp-PL_stack_base);
+       FUSE_CONTEXT_PRE;
+       DEBUGf("removexattr begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
@@ -736,7 +847,8 @@ int _PLfuse_removexattr (const char *file, const char *name) {
        FREETMPS;
        LEAVE;
        PUTBACK;
-       DEBUGf("removexattr end: %i\n",sp-PL_stack_base);
+       DEBUGf("removexattr end: %i\n",rv);
+       FUSE_CONTEXT_POST;
        return rv;
 }
 
@@ -744,6 +856,9 @@ struct fuse_operations _available_ops = {
 getattr:               _PLfuse_getattr,
 readlink:              _PLfuse_readlink,
 getdir:                        _PLfuse_getdir,
+#if 0
+readdir:               _PLfuse_readdir,
+#endif
 mknod:                 _PLfuse_mknod,
 mkdir:                 _PLfuse_mkdir,
 unlink:                        _PLfuse_unlink,
@@ -771,38 +886,110 @@ removexattr:             _PLfuse_removexattr,
 MODULE = Fuse          PACKAGE = Fuse
 PROTOTYPES: DISABLE
 
+SV*
+fuse_get_context()
+       PREINIT:
+       struct fuse_context *fc;
+       CODE:
+       fc = fuse_get_context();
+       if(fc) {
+               HV *hash = newHV();
+               hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
+               hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
+               hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
+               RETVAL = newRV_noinc((SV*)hash);
+       } else {
+               XSRETURN_UNDEF;
+       }
+       OUTPUT:
+       RETVAL
+
 void
 perl_fuse_main(...)
        PREINIT:
-       struct fuse_operations fops = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
-       int i, fd, varnum = 0, debug, have_mnt;
+       struct fuse_operations fops = 
+               {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,
+                NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
+       int i, fd, debug, threaded;
        char *mountpoint;
        char *mountopts;
-       STRLEN n_a;
-       STRLEN l;
+       char *fuseopts = NULL;
+       struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
+       struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
        INIT:
-       if(items != 28) {
+       if(items != 30) {
                fprintf(stderr,"Perl<->C inconsistency or internal error\n");
                XSRETURN_UNDEF;
        }
        CODE:
        debug = SvIV(ST(0));
-       mountpoint = SvPV_nolen(ST(1));
-       mountopts = SvPV_nolen(ST(2));
-       /* FIXME: reevaluate multithreading support when perl6 arrives */
+       threaded = SvIV(ST(1));
+       if(threaded) {
+#ifdef FUSE_USE_ITHREADS
+               master_interp = PERL_GET_CONTEXT;
+#else
+               fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
+                              "mode, but your perl was not built with -Dusethreads.  "
+                              "Threads are disabled.\n");
+               threaded = 0;
+#endif
+       }
+       mountpoint = SvPV_nolen(ST(2));
+       mountopts = SvPV_nolen(ST(3));
+       if (SvCUR(ST(4))) fuseopts = SvPV_nolen(ST(4));
        for(i=0;i<N_CALLBACKS;i++) {
-               SV *var = ST(i+3);
-               if((var != &PL_sv_undef) && SvROK(var)) {
-                       if(SvTYPE(SvRV(var)) == SVt_PVCV) {
-                               void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
-                               tmp2[i] = tmp1[i];
-                               _PLfuse_callbacks[i] = var;
-                       } else
-                               croak("arg is not a code reference!");
+               SV *var = ST(i+5);
+               /* allow symbolic references, or real code references. */
+               if(SvOK(var) && (SvPOK(var) || (SvROK(var) && SvTYPE(SvRV(var)) == SVt_PVCV))) {
+                       void **tmp1 = (void**)&_available_ops, **tmp2 = (void**)&fops;
+                       tmp2[i] = tmp1[i];
+#ifdef FUSE_USE_ITHREADS
+                       if(threaded)
+                /* note: under 5.8.7, this croaks for code references. */
+                SvSHARE(var);
+#endif
+                       _PLfuse_callbacks[i] = var;
+               } else
+               if(SvOK(var)) {
+                       croak("invalid callback (%i) passed to perl_fuse_main "
+                             "(%s is not a string, code ref, or undef).\n",
+                             i+4,SvPVbyte_nolen(var));
                }
        }
-       /* FIXME: need to pass fusermount arguments */
-       fd = fuse_mount(mountpoint,mountopts);
+       /*
+        * XXX: What comes here is just a ridiculous use of the option parsing API
+        * to hack on compatibility with other parts of the new API. First and
+        * foremost, real C argc/argv would be good to get at...
+        */
+       if (mountopts &&
+           (fuse_opt_add_arg(&margs, "") == -1 ||
+            fuse_opt_add_arg(&margs, "-o") == -1 ||
+            fuse_opt_add_arg(&margs, mountopts) == -1)) {
+               fuse_opt_free_args(&margs);
+               croak("out of memory\n");
+       }
+       if (fuse_opt_add_arg(&fargs, "") == -1) {
+               fuse_opt_free_args(&fargs);
+               croak("out of memory\n");
+       }
+       if (fuseopts &&
+           (fuse_opt_add_arg(&fargs, "-o") == -1 ||
+            fuse_opt_add_arg(&fargs, fuseopts) == -1)) {
+               fuse_opt_free_args(&fargs);
+               croak("out of memory\n");
+       }
+       if (debug && (fuse_opt_add_arg(&fargs, "-d") == -1)) {
+               fuse_opt_free_args(&fargs);
+               croak("out of memory\n");
+       }
+
+       fd = fuse_mount(mountpoint,&margs);
+       fuse_opt_free_args(&margs);        
        if(fd < 0)
-               croak("could not mount fuse filesystem!");
-       fuse_loop(fuse_new(fd,debug ? "debug" : NULL,&fops));
+               croak("could not mount fuse filesystem!\n");
+
+       if(threaded) {
+               fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)));
+       } else
+               fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)));
+       fuse_opt_free_args(&fargs);