Changes to improve compatibility with Perl 5.8.
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
index 7f31e93..9e2504e 100755 (executable)
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -5,10 +5,25 @@
 
 #include <fuse.h>
 
+#if defined(__FreeBSD__) || defined(__NetBSD__)
+# define XATTR_CREATE 1
+# define XATTR_REPLACE 2
+#else
+# include <sys/xattr.h>
+#endif
+
 /* Determine if threads support should be included */
 #ifdef USE_ITHREADS
 # ifdef I_PTHREAD
 #  define FUSE_USE_ITHREADS
+#  if (PERL_VERSION < 10)
+#    define tTHX PerlInterpreter*
+#    define STR_WITH_LEN(s)  ("" s ""), (sizeof(s)-1)
+#    define hv_fetchs(hv,key,lval) Perl_hv_fetch(aTHX_ hv, STR_WITH_LEN(key), lval)
+#    define dMY_CXT_INTERP(interp) \
+       SV *my_cxt_sv = *hv_fetchs(interp->Imodglobal, MY_CXT_KEY, TRUE); \
+       my_cxt_t *my_cxtp = INT2PTR(my_cxt_t*, SvUV(my_cxt_sv))
+#  endif
 # else
 #  warning "Sorry, I don't know how to handle ithreads on this architecture. Building non-threaded version"
 # endif
 typedef struct {
        SV *callback[N_CALLBACKS];
        HV *handles;
+#ifdef USE_ITHREADS
        tTHX self;
+#endif
        int threaded;
+#ifdef USE_ITHREADS
        perl_mutex mutex;
+#endif
 } my_cxt_t;
 START_MY_CXT;
 
@@ -43,6 +62,9 @@ tTHX master_interp = NULL;
 
 #define CLONE_INTERP(parent) S_clone_interp(parent)
 tTHX S_clone_interp(tTHX parent) {
+#if (PERL_VERSION < 10)
+       tTHX my_perl = parent;
+#endif
        dMY_CXT_INTERP(parent);
        if(MY_CXT.threaded) {
                MUTEX_LOCK(&MY_CXT.mutex);
@@ -485,8 +507,8 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        fi->fh = 0; /* Ensure it starts with 0 - important if they don't set it */
        fihash = newHV();
 #if FUSE_VERSION >= 24
-       (void) hv_store(fihash, "direct_io", 9, newSViv(fi->direct_io), 0);
-       (void) hv_store(fihash, "keep_cache", 10, newSViv(fi->keep_cache), 0);
+       (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
        (void) hv_store(fihash, "nonseekable", 11, newSViv(fi->nonseekable), 0);
@@ -511,13 +533,13 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
                /* Success, so copy the file handle which they returned */
 #if FUSE_VERSION >= 24
                SV **svp;
-               if ((svp = hv_fetch(fihash, "direct_io",    9, 0)))
+               if ((svp = hv_fetch(fihash, "direct_io",    9, 0)) != NULL)
                        fi->direct_io   = SvIV(*svp);
-               if ((svp = hv_fetch(fihash, "keep_cache",  10, 0)))
+               if ((svp = hv_fetch(fihash, "keep_cache",  10, 0)) != NULL)
                        fi->keep_cache  = SvIV(*svp);
 #endif
 #if FUSE_VERSION >= 29
-               if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)))
+               if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
                        fi->nonseekable = SvIV(*svp);
 #endif
        }
@@ -726,7 +748,11 @@ int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
        return rv;
 }
 
+#if __FreeBSD__ >= 10
+int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags, uint32_t position) {
+#else
 int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_t buflen, int flags) {
+#endif
        int rv;
        FUSE_CONTEXT_PRE;
        DEBUGf("setxattr begin\n");
@@ -749,7 +775,11 @@ int _PLfuse_setxattr (const char *file, const char *name, const char *buf, size_
        return rv;
 }
 
+#if __FreeBSD__ >= 10
+int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen, uint32_t position) {
+#else
 int _PLfuse_getxattr (const char *file, const char *name, char *buf, size_t buflen) {
+#endif
        int rv;
        FUSE_CONTEXT_PRE;
        DEBUGf("getxattr begin\n");
@@ -892,7 +922,7 @@ int _PLfuse_opendir(const char *file, struct fuse_file_info *fi) {
        SPAGAIN;
        if (rv) {
                if (rv > 1) {
-                        FH_STOREHANDLE(fi, POPs);
+                       FH_STOREHANDLE(fi, POPs);
                }
                rv = POPi;
        } else
@@ -1136,10 +1166,8 @@ int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
         * which we can look at or modify.
         */
        fihash = newHV();
-#if FUSE_VERSION >= 24
-       (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
+       (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
@@ -1161,18 +1189,13 @@ int _PLfuse_create(const char *file, mode_t mode, struct fuse_file_info *fi) {
        }
        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 ((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
-               svp = hv_fetch(fihash, "nonseekable", 11, 0);
-               if (svp != NULL)
+               if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
                        fi->nonseekable = SvIV(*svp);
 #endif
        }
@@ -1313,15 +1336,15 @@ int _PLfuse_lock(const char *file, struct fuse_file_info *fi, int cmd,
                /* Need to copy back any altered values from the hash into
                 * the struct... */
                SV **svp;
-               if ((svp = hv_fetch(lihash, "l_type",   6, 0)))
+               if ((svp = hv_fetch(lihash, "l_type",   6, 0)) != NULL)
                        lockinfo->l_type   = SvIV(*svp);
-               if ((svp = hv_fetch(lihash, "l_whence", 8, 0)))
+               if ((svp = hv_fetch(lihash, "l_whence", 8, 0)) != NULL)
                        lockinfo->l_whence = SvIV(*svp);
-               if ((svp = hv_fetch(lihash, "l_start",  7, 0)))
+               if ((svp = hv_fetch(lihash, "l_start",  7, 0)) != NULL)
                        lockinfo->l_start  = SvNV(*svp);
-               if ((svp = hv_fetch(lihash, "l_len",    5, 0)))
+               if ((svp = hv_fetch(lihash, "l_len",    5, 0)) != NULL)
                        lockinfo->l_len    = SvNV(*svp);
-               if ((svp = hv_fetch(lihash, "l_pid",    5, 0)))
+               if ((svp = hv_fetch(lihash, "l_pid",    5, 0)) != NULL)
                        lockinfo->l_pid    = SvIV(*svp);
        }
        FREETMPS;
@@ -1512,14 +1535,19 @@ PROTOTYPES: DISABLE
 
 BOOT:
        MY_CXT_INIT;
+#ifdef USE_ITHREADS
        MY_CXT.self = aTHX;
+#endif
 
 void
 CLONE(...)
        PREINIT:
+#ifdef USE_ITHREADS
                int i;
                dTHX;
+#endif
        CODE:
+#ifdef USE_ITHREADS
                MY_CXT_CLONE;
                tTHX parent = MY_CXT.self;
                MY_CXT.self = my_perl;
@@ -1551,6 +1579,7 @@ CLONE(...)
                        clone_params_del(clone_param);
 #endif
                }
+#endif
 
 SV*
 fuse_get_context()
@@ -1568,7 +1597,6 @@ fuse_get_context()
 #if FUSE_VERSION >= 28
                (void) hv_store(hash, "umask", 5, newSViv(fc->umask), 0);
 #endif /* FUSE_VERSION >= 28 */
-
                RETVAL = newRV_noinc((SV*)hash);
        } else {
                XSRETURN_UNDEF;
@@ -1583,6 +1611,23 @@ fuse_version()
        OUTPUT:
        RETVAL
 
+#ifndef __FreeBSD__
+SV *
+XATTR_CREATE()
+       CODE:
+       RETVAL = newSViv(XATTR_CREATE);
+       OUTPUT:
+       RETVAL
+
+SV *
+XATTR_REPLACE()
+       CODE:
+       RETVAL = newSViv(XATTR_REPLACE);
+       OUTPUT:
+       RETVAL
+
+#endif
+
 void
 perl_fuse_main(...)
        PREINIT:
@@ -1636,7 +1681,7 @@ perl_fuse_main(...)
                } 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));
+                             i+5,SvPVbyte_nolen(var));
                } else {
                        MY_CXT.callback[i] = NULL;
                }
@@ -1646,9 +1691,12 @@ perl_fuse_main(...)
         * 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(&args, "") == -1 ||
-            fuse_opt_add_arg(&args, "-o") == -1 ||
+       if ((mountopts || debug) && fuse_opt_add_arg(&args, "") == -1) {
+               fuse_opt_free_args(&args);
+               croak("out of memory\n");
+       }
+       if (mountopts && strcmp("", mountopts) &&
+            (fuse_opt_add_arg(&args, "-o") == -1 ||
             fuse_opt_add_arg(&args, mountopts) == -1)) {
                fuse_opt_free_args(&args);
                croak("out of memory\n");