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 8cfa88b..6203eca
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -1,3 +1,7 @@
+/*
+ * vim:ts=8:noet
+ */
+
 #include "EXTERN.h"
 #include "perl.h"
 #include "XSUB.h"
@@ -17,14 +21,14 @@ static inline void create_perl_context() {
                }
        }
 }
-#  define FUSE_CONTEXT_PRE create_perl_context(); {
+#  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
+# define FUSE_CONTEXT_PRE dSP
 # define FUSE_CONTEXT_POST
 #endif
 #include <fuse.h>
@@ -42,7 +46,6 @@ SV *_PLfuse_callbacks[N_CALLBACKS];
 int _PLfuse_getattr(const char *file, struct stat *result) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("getattr begin: %s\n",file);
        ENTER;
        SAVETMPS;
@@ -65,7 +68,7 @@ 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;
@@ -86,7 +89,6 @@ int _PLfuse_getattr(const char *file, struct stat *result) {
 int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        if(buflen < 1)
                return EINVAL;
        DEBUGf("readlink begin\n");
@@ -127,7 +129,6 @@ int _PLfuse_readlink(const char *file,char *buf,size_t buflen) {
 int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
        int prv, rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("getdir begin\n");
        ENTER;
        SAVETMPS;
@@ -155,7 +156,6 @@ int _PLfuse_getdir(const char *file, fuse_dirh_t dirh, fuse_dirfil_t dirfil) {
 int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("mknod begin\n");
        ENTER;
        SAVETMPS;
@@ -181,7 +181,6 @@ int _PLfuse_mknod (const char *file, mode_t mode, dev_t dev) {
 int _PLfuse_mkdir (const char *file, mode_t mode) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("mkdir begin\n");
        ENTER;
        SAVETMPS;
@@ -207,7 +206,6 @@ int _PLfuse_mkdir (const char *file, mode_t mode) {
 int _PLfuse_unlink (const char *file) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("unlink begin\n");
        ENTER;
        SAVETMPS;
@@ -231,7 +229,6 @@ int _PLfuse_unlink (const char *file) {
 int _PLfuse_rmdir (const char *file) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("rmdir begin\n");
        ENTER;
        SAVETMPS;
@@ -255,7 +252,6 @@ int _PLfuse_rmdir (const char *file) {
 int _PLfuse_symlink (const char *file, const char *new) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("symlink begin\n");
        ENTER;
        SAVETMPS;
@@ -280,7 +276,6 @@ int _PLfuse_symlink (const char *file, const char *new) {
 int _PLfuse_rename (const char *file, const char *new) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("rename begin\n");
        ENTER;
        SAVETMPS;
@@ -305,7 +300,6 @@ int _PLfuse_rename (const char *file, const char *new) {
 int _PLfuse_link (const char *file, const char *new) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("link begin\n");
        ENTER;
        SAVETMPS;
@@ -330,7 +324,6 @@ int _PLfuse_link (const char *file, const char *new) {
 int _PLfuse_chmod (const char *file, mode_t mode) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("chmod begin\n");
        ENTER;
        SAVETMPS;
@@ -355,7 +348,6 @@ int _PLfuse_chmod (const char *file, mode_t mode) {
 int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("chown begin\n");
        ENTER;
        SAVETMPS;
@@ -381,7 +373,6 @@ int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
 int _PLfuse_truncate (const char *file, off_t off) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("truncate begin\n");
        ENTER;
        SAVETMPS;
@@ -406,7 +397,6 @@ int _PLfuse_truncate (const char *file, off_t off) {
 int _PLfuse_utime (const char *file, struct utimbuf *uti) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("utime begin\n");
        ENTER;
        SAVETMPS;
@@ -432,21 +422,76 @@ int _PLfuse_utime (const char *file, struct utimbuf *uti) {
 int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        int rv;
        int flags = fi->flags;
+       HV *fihash;
        FUSE_CONTEXT_PRE;
-       dSP;
        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;
@@ -458,7 +503,6 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
 int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("read begin\n");
        ENTER;
        SAVETMPS;
@@ -466,6 +510,7 @@ int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct
        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;
@@ -498,7 +543,6 @@ int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct
 int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off, struct fuse_file_info *fi) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("write begin\n");
        ENTER;
        SAVETMPS;
@@ -506,6 +550,7 @@ int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off,
        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;
@@ -524,7 +569,6 @@ int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off,
 int _PLfuse_statfs (const char *file, struct statvfs *st) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("statfs begin\n");
        ENTER;
        SAVETMPS;
@@ -570,12 +614,12 @@ int _PLfuse_statfs (const char *file, struct statvfs *st) {
 int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        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;
@@ -595,13 +639,13 @@ int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
        int rv;
        int flags = fi->flags;
        FUSE_CONTEXT_PRE;
-       dSP;
        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;
@@ -609,6 +653,14 @@ int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
                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;
@@ -621,13 +673,13 @@ int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
        int rv;
        int flags = fi->flags;
        FUSE_CONTEXT_PRE;
-       dSP;
        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;
@@ -646,7 +698,6 @@ int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("setxattr begin\n");
        ENTER;
        SAVETMPS;
@@ -673,7 +724,6 @@ int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_
 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("getxattr begin\n");
        ENTER;
        SAVETMPS;
@@ -717,7 +767,6 @@ int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t bufl
 int _PLfuse_listxattr (const char *file, char *list, size_t size) {
        int prv, rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("listxattr begin\n");
        ENTER;
        SAVETMPS;
@@ -782,7 +831,6 @@ int _PLfuse_listxattr (const char *file, char *list, size_t size) {
 int _PLfuse_removexattr (const char *file, const char *name) {
        int rv;
        FUSE_CONTEXT_PRE;
-       dSP;
        DEBUGf("removexattr begin\n");
        ENTER;
        SAVETMPS;
@@ -838,6 +886,24 @@ 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:
@@ -847,10 +913,11 @@ perl_fuse_main(...)
        int i, fd, debug, threaded;
        char *mountpoint;
        char *mountopts;
+       char *fuseopts = NULL;
        struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
        struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
        INIT:
-       if(items != 29) {
+       if(items != 30) {
                fprintf(stderr,"Perl<->C inconsistency or internal error\n");
                XSRETURN_UNDEF;
        }
@@ -859,7 +926,7 @@ perl_fuse_main(...)
        threaded = SvIV(ST(1));
        if(threaded) {
 #ifdef FUSE_USE_ITHREADS
-               master_interp = PERL_GET_INTERP;
+               master_interp = PERL_GET_CONTEXT;
 #else
                fprintf(stderr,"FUSE warning: Your script has requested multithreaded "
                               "mode, but your perl was not built with -Dusethreads.  "
@@ -869,8 +936,9 @@ perl_fuse_main(...)
        }
        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+4);
+               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;
@@ -883,7 +951,7 @@ perl_fuse_main(...)
                        _PLfuse_callbacks[i] = var;
                } else
                if(SvOK(var)) {
-                       croak("invalid callback passed to perl_fuse_main "
+                       croak("invalid callback (%i) passed to perl_fuse_main "
                              "(%s is not a string, code ref, or undef).\n",
                              i+4,SvPVbyte_nolen(var));
                }
@@ -900,23 +968,28 @@ perl_fuse_main(...)
                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!\n");
-        if (debug) {
-               if ( fuse_opt_add_arg(&fargs, "") == -1 ||
-                       fuse_opt_add_arg(&fargs, "-d") == -1) {
-                       fuse_opt_free_args(&fargs);
-                       croak("out of memory\n");
-               }
-       } else {
-               if (fuse_opt_add_arg(&fargs, "") == -1)
-                       croak("out of memory\n");
-       }
 
        if(threaded) {
-               fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)/sizeof(void*)));
+               fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)));
        } else
-               fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)/sizeof(void*)));
+               fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)));
        fuse_opt_free_args(&fargs);