64-bit support, 2.6 binding
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
old mode 100644 (file)
new mode 100755 (executable)
index b87d44a..9574fdd
--- a/Fuse.xs
+++ b/Fuse.xs
@@ -368,13 +368,22 @@ int _PLfuse_chown (const char *file, uid_t uid, gid_t gid) {
 
 int _PLfuse_truncate (const char *file, off_t off) {
        int rv;
+#ifndef PERL_HAS_64BITINT
+       char *temp;
+#endif
        FUSE_CONTEXT_PRE;
        DEBUGf("truncate begin\n");
        ENTER;
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
+#ifdef PERL_HAS_64BITINT
        XPUSHs(sv_2mortal(newSViv(off)));
+#else
+       asprintf(&temp, "%llu", off);
+       XPUSHs(sv_2mortal(newSVpv(temp, 0)));
+       free(temp);
+#endif
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[12],G_SCALAR);
        SPAGAIN;
@@ -418,6 +427,7 @@ 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;
        DEBUGf("open begin\n");
        ENTER;
@@ -425,13 +435,68 @@ int _PLfuse_open (const char *file, struct fuse_file_info *fi) {
        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;
@@ -442,6 +507,9 @@ 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;
+#ifndef PERL_HAS_64BITINT
+       char *temp;
+#endif
        FUSE_CONTEXT_PRE;
        DEBUGf("read begin\n");
        ENTER;
@@ -449,7 +517,14 @@ int _PLfuse_read (const char *file, char *buf, size_t buflen, off_t off, struct
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSViv(buflen)));
+#ifdef PERL_HAS_64BITINT
        XPUSHs(sv_2mortal(newSViv(off)));
+#else
+       asprintf(&temp, "%llu", off);
+       XPUSHs(sv_2mortal(newSVpv(temp, 0)));
+       free(temp);
+#endif
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[15],G_SCALAR);
        SPAGAIN;
@@ -481,6 +556,9 @@ 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;
+#ifndef PERL_HAS_64BITINT
+       char *temp;
+#endif
        FUSE_CONTEXT_PRE;
        DEBUGf("write begin\n");
        ENTER;
@@ -488,7 +566,14 @@ int _PLfuse_write (const char *file, const char *buf, size_t buflen, off_t off,
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
        XPUSHs(sv_2mortal(newSVpvn(buf,buflen)));
+#ifdef PERL_HAS_64BITINT
        XPUSHs(sv_2mortal(newSViv(off)));
+#else
+       asprintf(&temp, "%llu", off);
+       XPUSHs(sv_2mortal(newSVpv(temp, 0)));
+       free(temp);
+#endif
+       XPUSHs(fi->fh==0 ? &PL_sv_undef : (SV *)fi->fh);
        PUTBACK;
        rv = call_sv(_PLfuse_callbacks[16],G_SCALAR);
        SPAGAIN;
@@ -557,6 +642,7 @@ int _PLfuse_flush (const char *file, struct fuse_file_info *fi) {
        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;
@@ -582,6 +668,7 @@ int _PLfuse_release (const char *file, struct fuse_file_info *fi) {
        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;
@@ -589,6 +676,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;
@@ -607,6 +702,7 @@ int _PLfuse_fsync (const char *file, int datasync, struct fuse_file_info *fi) {
        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;
@@ -837,11 +933,12 @@ perl_fuse_main(...)
        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;
+       int i, debug, threaded;
        char *mountpoint;
        char *mountopts;
        struct fuse_args margs = FUSE_ARGS_INIT(0, NULL);
        struct fuse_args fargs = FUSE_ARGS_INIT(0, NULL);
+       struct fuse_chan *fc;
        INIT:
        if(items != 29) {
                fprintf(stderr,"Perl<->C inconsistency or internal error\n");
@@ -876,7 +973,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));
                }
@@ -893,9 +990,9 @@ perl_fuse_main(...)
                fuse_opt_free_args(&margs);
                croak("out of memory\n");
        }
-       fd = fuse_mount(mountpoint,&margs);
+       fc = fuse_mount(mountpoint,&margs);
        fuse_opt_free_args(&margs);        
-       if(fd < 0)
+       if (fc == NULL)
                croak("could not mount fuse filesystem!\n");
         if (debug) {
                if ( fuse_opt_add_arg(&fargs, "") == -1 ||
@@ -907,9 +1004,11 @@ perl_fuse_main(...)
                if (fuse_opt_add_arg(&fargs, "") == -1)
                        croak("out of memory\n");
        }
-
+#ifndef __NetBSD__
        if(threaded) {
-               fuse_loop_mt(fuse_new(fd,&fargs,&fops,sizeof(fops)));
+               fuse_loop_mt(fuse_new(fc,&fargs,&fops,sizeof(fops),NULL));
        } else
-               fuse_loop(fuse_new(fd,&fargs,&fops,sizeof(fops)));
+#endif
+               fuse_loop(fuse_new(fc,&fargs,&fops,sizeof(fops),NULL));
+       fuse_unmount(mountpoint,fc);
        fuse_opt_free_args(&fargs);