When testing, check if '/proc/mounts' exists.
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
index f6756b3..01fcafb 100755 (executable)
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -3,6 +3,17 @@
 #include "perl.h"
 #include "XSUB.h"
 
+/*
+ * XXX: Fuse on FreeBSD does not support extended attributes (see
+ * /usr/local/share/doc/fusefs/kmod/doc.text). Also, 'extattr_set_file' syscall
+ * declared in 'sys/extattr.h' does not accept any flags. We could either skip
+ * defining XATTR_CREATE and XATTR_REPLACE constants (as further below, and
+ * which breaks their usage if Perl module exports them), set them to some
+ * dummy values, or not allow them to be exported at all on FreeBSD.
+ */
+#ifndef __FreeBSD__
+# include <sys/xattr.h>
+#endif
 #include <fuse.h>
 
 /* Determine if threads support should be included */
@@ -485,8 +496,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,23 +522,14 @@ 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;
-               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);
-               }
+               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);
 #endif
 #if FUSE_VERSION >= 29
-               svp = hv_fetch(fihash, "nonseekable", 11, 0);
-               if (svp != NULL)
-               {
-                       fi->nonseekable = SvIV(*svp);
-               }
+               if ((svp = hv_fetch(fihash, "nonseekable", 11, 0)) != NULL)
+                       fi->nonseekable = SvIV(*svp);
 #endif
        }
        FREETMPS;
@@ -538,7 +540,8 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        return rv;
 }
 
-int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, 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;
 #ifndef PERL_HAS_64BITINT
        char *temp;
@@ -548,7 +551,7 @@ int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
-       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
        XPUSHs(sv_2mortal(newSViv(buflen)));
 #ifdef PERL_HAS_64BITINT
        XPUSHs(sv_2mortal(newSViv(off)));
@@ -598,7 +601,7 @@ int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off,
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
-       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
        XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
 #ifdef PERL_HAS_64BITINT
        XPUSHs(sv_2mortal(newSViv(off)));
@@ -673,7 +676,7 @@ int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
-       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
        XPUSHs(FH_GETHANDLE(fi));
        PUTBACK;
        rv = call_sv(MY_CXT.callback[18],G_SCALAR);
@@ -695,7 +698,7 @@ int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
-       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
        XPUSHs(sv_2mortal(newSViv(flags)));
        XPUSHs(FH_GETHANDLE(fi));
        PUTBACK;
@@ -719,7 +722,7 @@ int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
-       XPUSHs(sv_2mortal(newSVpv(file,0)));
+       XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
        XPUSHs(sv_2mortal(newSViv(flags)));
        XPUSHs(FH_GETHANDLE(fi));
        PUTBACK;
@@ -900,7 +903,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
@@ -1144,10 +1147,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
@@ -1169,18 +1170,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
        }
@@ -1321,15 +1317,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;
@@ -1470,6 +1466,7 @@ mknod:                    _PLfuse_mknod,
 mkdir:                 _PLfuse_mkdir,
 unlink:                        _PLfuse_unlink,
 rmdir:                 _PLfuse_rmdir,
+symlink:               _PLfuse_symlink,
 rename:                        _PLfuse_rename,
 link:                  _PLfuse_link,
 chmod:                 _PLfuse_chmod,
@@ -1567,15 +1564,14 @@ fuse_get_context()
        fc = fuse_get_context();
        if(fc) {
                HV *hash = newHV();
-               (void) hv_store(hash, "uid", 3, newSViv(fc->uid), 0);
-               (void) hv_store(hash, "gid", 3, newSViv(fc->gid), 0);
-               (void) hv_store(hash, "pid", 3, newSViv(fc->pid), 0);
+               (void) hv_store(hash, "uid",   3, newSViv(fc->uid), 0);
+               (void) hv_store(hash, "gid",   3, newSViv(fc->gid), 0);
+               (void) hv_store(hash, "pid",   3, newSViv(fc->pid), 0);
                if (fc->private_data)
                        (void) hv_store(hash, "private", 7, fc->private_data, 0);
 #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;
@@ -1590,6 +1586,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:
@@ -1643,7 +1656,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;
                }
@@ -1653,9 +1666,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");