Adding working but somewhat preliminary poll() support.
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
index b74b429..639adf8 100755 (executable)
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -5,7 +5,7 @@
 
 #include <fuse.h>
 
-#if defined(__FreeBSD__) || defined(__NetBSD__)
+#if (defined(__FreeBSD__) && __FreeBSD__ < 10) || defined(__NetBSD__)
 # define XATTR_CREATE 1
 # define XATTR_REPLACE 2
 #else
@@ -33,8 +33,7 @@
 
 #define MY_CXT_KEY "Fuse::_guts" XS_VERSION
 #if FUSE_VERSION >= 28
-# define N_CALLBACKS 40
-/* # define N_CALLBACKS 41 */
+# define N_CALLBACKS 41
 #elif FUSE_VERSION >= 26
 # define N_CALLBACKS 38
 #elif FUSE_VERSION >= 25
@@ -130,8 +129,10 @@ void S_fh_store_handle(pTHX_ pMY_CXT_ struct fuse_file_info *fi, SV *sv) {
                        SvSHARE(sv);
                }
 #endif
-               MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
-               fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
+        /* This seems to be screwing things up... */
+               // MAGIC *mg = (SvTYPE(sv) == SVt_PVMG) ? mg_find(sv, PERL_MAGIC_shared_scalar) : NULL;
+               // fi->fh = mg ? PTR2IV(mg->mg_ptr) : PTR2IV(sv);
+               fi->fh = PTR2IV(sv);
                if(hv_store_ent(MY_CXT.handles, FH_KEY(fi), SvREFCNT_inc(sv), 0) == NULL) {
                        SvREFCNT_dec(sv);
                }
@@ -511,7 +512,7 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        (void) hv_store(fihash, "direct_io",    9, newSViv(fi->direct_io),   0);
        (void) hv_store(fihash, "keep_cache",  10, newSViv(fi->keep_cache),  0);
 #endif
-#if FUSE_VERSION >= 29
+#if FUSE_VERSION >= 28
        (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
 #endif
        XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
@@ -539,7 +540,7 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
                if ((svp = hv_fetch(fihash, "keep_cache",  10, 0)) != NULL)
                        fi->keep_cache  = SvIV(*svp);
 #endif
-#if FUSE_VERSION >= 29
+#if FUSE_VERSION >= 28
                if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
                        fi->nonseekable = SvIV(*svp);
 #endif
@@ -1169,7 +1170,7 @@ int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
        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
+#if FUSE_VERSION >= 28
        (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
 #endif
        XPUSHs(sv_2mortal(newRV_noinc((SV*) fihash)));
@@ -1195,7 +1196,7 @@ int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
                        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 FUSE_VERSION >= 28
                if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
                        fi->nonseekable = SvIV(*svp);
 #endif
@@ -1423,13 +1424,17 @@ int _PLfuse_bmap(const char *file, size_t blocksize, uint64_t *idx) {
 int _PLfuse_ioctl(const char *file, int cmd, void *arg,
                   struct fuse_file_info *fi, unsigned int flags, void *data) {
        int rv;
+       SV *sv = NULL;
        FUSE_CONTEXT_PRE;
        DEBUGf("ioctl begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
-       XPUSHs(sv_2mortal(newSViv(cmd)));
+       /* I don't know why cmd is a signed int in the first place;
+        * casting as unsigned so stupid tricks don't have to be done on
+        * the perl side */
+       XPUSHs(sv_2mortal(newSViv((unsigned int)cmd)));
        XPUSHs(sv_2mortal(newSViv(flags)));
        if (_IOC_DIR(cmd) & _IOC_WRITE)
                XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
@@ -1439,12 +1444,18 @@ int _PLfuse_ioctl(const char *file, int cmd, void *arg,
        PUTBACK;
        rv = call_sv(MY_CXT.callback[39],G_ARRAY);
        SPAGAIN;
-       if (_IOC_DIR(cmd) & _IOC_READ) {
-               if (rv == 2) {
-                       SV *sv = POPs;
+       if ((_IOC_DIR(cmd) & _IOC_READ) && (rv == 2)) {
+               sv = POPs;
+               rv--;
+       }
+
+       if (rv > 0)
+               rv = POPi;
+
+       if ((_IOC_DIR(cmd) & _IOC_READ) && !rv) {
+               if (sv) {
                        size_t len;
                        char *rdata = SvPV(sv, len);
-            rv--;
 
                        if (len > _IOC_SIZE(cmd)) {
                                fprintf(stderr, "ioctl(): returned data was too large for data area\n");
@@ -1460,8 +1471,6 @@ int _PLfuse_ioctl(const char *file, int cmd, void *arg,
                        rv = -EFAULT;
                }
        }
-       if (rv > 0)
-               rv = POPi;
        FREETMPS;
        LEAVE;
        PUTBACK;
@@ -1470,12 +1479,42 @@ int _PLfuse_ioctl(const char *file, int cmd, void *arg,
        return rv;
 }
 
-#if 0
 int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
                  struct fuse_pollhandle *ph, unsigned *reventsp) {
-
+       int rv;
+       SV *sv = NULL;
+       FUSE_CONTEXT_PRE;
+       DEBUGf("poll begin\n");
+       ENTER;
+       SAVETMPS;
+       PUSHMARK(SP);
+       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       if (ph) {
+        /* Still gotta figure out how to do this right... */
+               sv = newSViv(PTR2IV(ph));
+        SvREADONLY_on(sv);
+               SvSHARE(sv);
+               XPUSHs(sv);
+       }
+       else
+               XPUSHs(&PL_sv_undef);
+       XPUSHs(sv_2mortal(newSViv(*reventsp)));
+       XPUSHs(FH_GETHANDLE(fi));
+       PUTBACK;
+       rv = call_sv(MY_CXT.callback[40],G_ARRAY);
+       SPAGAIN;
+       if (rv > 1) {
+               *reventsp = POPi;
+        rv--;
+    }
+       rv = (rv ? POPi : 0);
+       FREETMPS;
+       LEAVE;
+       PUTBACK;
+       DEBUGf("poll end: %i\n", rv);
+       FUSE_CONTEXT_POST;
+       return rv;
 }
-#endif
 #endif /* FUSE_VERSION >= 28 */
 
 struct fuse_operations _available_ops = {
@@ -1525,9 +1564,7 @@ bmap:                     _PLfuse_bmap,
 #endif /* FUSE_VERSION >= 26 */
 #if FUSE_VERSION >= 28
 ioctl:                 _PLfuse_ioctl,
-#if 0
 poll:                  _PLfuse_poll,
-#endif
 #endif /* FUSE_VERSION >= 28 */
 };
 
@@ -1612,7 +1649,6 @@ fuse_version()
        OUTPUT:
        RETVAL
 
-#ifndef __FreeBSD__
 SV *
 XATTR_CREATE()
        CODE:
@@ -1627,8 +1663,6 @@ XATTR_REPLACE()
        OUTPUT:
        RETVAL
 
-#endif
-
 void
 perl_fuse_main(...)
        PREINIT:
@@ -1717,3 +1751,35 @@ perl_fuse_main(...)
                fuse_loop(fuse_new(fc,&args,&fops,sizeof(fops),NULL));
        fuse_unmount(mountpoint,fc);
        fuse_opt_free_args(&args);
+
+#if FUSE_VERSION >= 28
+
+void
+pollhandle_destroy(...)
+    PREINIT:
+       struct fuse_pollhandle *ph;
+    INIT:
+        if (items != 1) {
+            fprintf(stderr, "No pollhandle passed?\n");
+            XSRETURN_UNDEF;
+        }
+       CODE:
+        ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
+               fuse_pollhandle_destroy(ph);
+
+int 
+notify_poll(...)
+    PREINIT:
+        struct fuse_pollhandle *ph;
+    INIT:
+        if (items != 1) {
+            fprintf(stderr, "No pollhandle passed?\n");
+            XSRETURN_UNDEF;
+        }
+       CODE:
+        ph = INT2PTR(struct fuse_pollhandle*, SvIV(ST(0)));
+               RETVAL = fuse_notify_poll(ph);
+       OUTPUT:
+               RETVAL
+
+#endif