Added a daemonize() function to account for certain conditions where the forked proce...
[perl-fuse.git] / Fuse.xs
diff --git a/Fuse.xs b/Fuse.xs
index 3b1e23b..814fb47 100755 (executable)
--- a/Fuse.xs
+++ b/Fuse.xs
 # define STAT_NSEC(st, st_xtim) ((st)->st_xtim##espec.tv_nsec)
 #endif
 
+/* Implement a macro to handle multiple formats (integer, float, and array
+ * containing seconds and nanoseconds). */
+#define PULL_TIME(st, st_xtim, svp)                                    \
+{                                                                      \
+       SV *sv = svp;                                                   \
+       if (SvROK(sv)) {                                                \
+               AV *av = (AV *)SvRV(sv);                                \
+               if (SvTYPE((SV *)av) != SVt_PVAV) {                     \
+                       Perl_croak_nocontext("Reference was not array ref"); \
+               }                                                       \
+               if (av_len(av) != 1) {                                  \
+                       Perl_croak_nocontext("Array of incorrect dimension"); \
+               }                                                       \
+               (st)->st_xtim##e = SvIV(*(av_fetch(av, 0, FALSE)));     \
+               STAT_NSEC(st, st_xtim) = SvIV(*(av_fetch(av, 1, FALSE))); \
+       }                                                               \
+       else if (SvNOK(sv) || SvIOK(sv)) {                              \
+               double tm = SvNV(sv);                                   \
+               (st)->st_xtim##e = (int)tm;                             \
+               STAT_NSEC(st, st_xtim) = (tm - (int)tm) * 1000000000;   \
+       }                                                               \
+       else {                                                          \
+               Perl_croak_nocontext("Invalid data type passed");       \
+       }                                                               \
+}
+
 /* Determine if threads support should be included */
 #ifdef USE_ITHREADS
 # ifdef I_PTHREAD
@@ -60,6 +86,7 @@ typedef struct {
 #ifdef USE_ITHREADS
        perl_mutex mutex;
 #endif
+       int utimens_as_array;
 } my_cxt_t;
 START_MY_CXT;
 
@@ -166,19 +193,11 @@ int _PLfuse_getattr(const char *file, struct stat *result) {
                else
                        rv = -ENOENT;
        } else {
-        double tm;
                result->st_blocks = POPi;
                result->st_blksize = POPi;
-               /* Do a little gymnastics to transform the fractional part into nsec */
-               tm = POPn;
-               result->st_ctime = (int)tm;
-               STAT_NSEC(result, st_ctim) = (tm - (int)tm) * 1000000000;
-               tm = POPn;
-               result->st_mtime = (int)tm;
-               STAT_NSEC(result, st_mtim) = (tm - (int)tm) * 1000000000;
-               tm = POPn;
-               result->st_atime = (int)tm;
-               STAT_NSEC(result, st_atim) = (tm - (int)tm) * 1000000000;
+               PULL_TIME(result, st_ctim, POPs);
+               PULL_TIME(result, st_mtim, POPs);
+               PULL_TIME(result, st_atim, POPs);
                result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
                result->st_rdev = POPi;
                result->st_gid = POPi;
@@ -1003,7 +1022,6 @@ int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
                                        if (SvROK(*svp) &&
                                                        SvTYPE(av2 = (AV *)SvRV(*svp)) == SVt_PVAV &&
                                                        av_len(av2) == 12) {
-                        double tm;
                                                st.st_dev     = SvIV(*(av_fetch(av2,  0, FALSE)));
                                                st.st_ino     = SvIV(*(av_fetch(av2,  1, FALSE)));
                                                st.st_mode    = SvIV(*(av_fetch(av2,  2, FALSE)));
@@ -1012,15 +1030,9 @@ int _PLfuse_readdir(const char *file, void *dirh, fuse_fill_dir_t dirfil,
                                                st.st_gid     = SvIV(*(av_fetch(av2,  5, FALSE)));
                                                st.st_rdev    = SvIV(*(av_fetch(av2,  6, FALSE)));
                                                st.st_size    = SvNV(*(av_fetch(av2,  7, FALSE)));
-                                               tm            = SvNV(*(av_fetch(av2,  8, FALSE)));
-                                               st.st_atime   = (int)tm;
-                                               STAT_NSEC(&st, st_atim) = (tm - (int)tm) * 1000000000;
-                                               tm            = SvNV(*(av_fetch(av2,  9, FALSE)));
-                                               st.st_mtime   = (int)tm;
-                                               STAT_NSEC(&st, st_mtim) = (tm - (int)tm) * 1000000000;
-                                               tm            = SvNV(*(av_fetch(av2, 10, FALSE)));
-                                               st.st_ctime   = (int)tm;
-                                               STAT_NSEC(&st, st_ctim) = (tm - (int)tm) * 1000000000;
+                                               PULL_TIME(&st, st_atim, *(av_fetch(av2,  8, FALSE)));
+                                               PULL_TIME(&st, st_mtim, *(av_fetch(av2,  9, FALSE)));
+                                               PULL_TIME(&st, st_ctim, *(av_fetch(av2, 10, FALSE)));
                                                st.st_blksize = SvIV(*(av_fetch(av2, 11, FALSE)));
                                                st.st_blocks  = SvIV(*(av_fetch(av2, 12, FALSE)));
                                                st_filled = 1;
@@ -1284,19 +1296,11 @@ int _PLfuse_fgetattr(const char *file, struct stat *result,
                else
                        rv = -ENOENT;
        } else {
-        double tm;
                result->st_blocks = POPi;
                result->st_blksize = POPi;
-               /* Do a little gymnastics to transform the fractional part into nsec */
-               tm = POPn;
-               result->st_ctime = (int)tm;
-               STAT_NSEC(result, st_ctim) = (tm - (int)tm) * 1000000000;
-               tm = POPn;
-               result->st_mtime = (int)tm;
-               STAT_NSEC(result, st_mtim) = (tm - (int)tm) * 1000000000;
-               tm = POPn;
-               result->st_atime = (int)tm;
-               STAT_NSEC(result, st_atim) = (tm - (int)tm) * 1000000000;
+               PULL_TIME(result, st_ctim, POPs);
+               PULL_TIME(result, st_mtim, POPs);
+               PULL_TIME(result, st_atim, POPs);
                result->st_size = POPn; // we pop double here to support files larger than 4Gb (long limit)
                result->st_rdev = POPi;
                result->st_gid = POPi;
@@ -1394,8 +1398,30 @@ int _PLfuse_utimens(const char *file, const struct timespec tv[2]) {
        SAVETMPS;
        PUSHMARK(SP);
        XPUSHs(sv_2mortal(newSVpv(file,0)));
-       XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
-       XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
+       if (MY_CXT.utimens_as_array) {
+               /* Pushing timespecs as 2-element arrays (if tv is present). */
+               AV *av;
+               if (tv) {
+                       av = newAV();
+                       av_push(av, newSViv(tv[0].tv_sec));
+                       av_push(av, newSViv(tv[0].tv_nsec));
+                       XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
+                       av = newAV();
+                       av_push(av, newSViv(tv[1].tv_sec));
+                       av_push(av, newSViv(tv[1].tv_nsec));
+                       XPUSHs(sv_2mortal(newRV_noinc((SV *)av)));
+               }
+               else {
+                       XPUSHs(&PL_sv_undef);
+                       XPUSHs(&PL_sv_undef);
+               }
+
+       }
+       else {
+               /* Pushing timespecs as floating point (double) values. */
+               XPUSHs(tv ? sv_2mortal(newSVnv(tv[0].tv_sec + (tv[0].tv_nsec / 1000000000.0))) : &PL_sv_undef);
+               XPUSHs(tv ? sv_2mortal(newSVnv(tv[1].tv_sec + (tv[1].tv_nsec / 1000000000.0))) : &PL_sv_undef);
+       }
        PUTBACK;
        rv = call_sv(MY_CXT.callback[36],G_SCALAR);
        SPAGAIN;
@@ -1463,7 +1489,7 @@ int _PLfuse_ioctl(const char *file, int cmd, void *arg,
        /* 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(newSVuv((unsigned int)cmd)));
        XPUSHs(sv_2mortal(newSViv(flags)));
        if (_IOC_DIR(cmd) & _IOC_WRITE)
                XPUSHs(sv_2mortal(newSVpvn(data, _IOC_SIZE(cmd))));
@@ -1703,7 +1729,7 @@ perl_fuse_main(...)
        struct fuse_chan *fc;
        dMY_CXT;
        INIT:
-       if(items != N_CALLBACKS + 5) {
+       if(items != N_CALLBACKS + 6) {
                fprintf(stderr,"Perl<->C inconsistency or internal error\n");
                XSRETURN_UNDEF;
        }
@@ -1729,8 +1755,9 @@ perl_fuse_main(...)
 #if FUSE_VERSION >= 28
        fops.flag_nullpath_ok = SvIV(ST(4));
 #endif /* FUSE_VERSION >= 28 */
+       MY_CXT.utimens_as_array = SvIV(ST(5));
        for(i=0;i<N_CALLBACKS;i++) {
-               SV *var = ST(i+5);
+               SV *var = ST(i+6);
                /* 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;
@@ -1745,7 +1772,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+5,SvPVbyte_nolen(var));
+                             i+6,SvPVbyte_nolen(var));
                } else {
                        MY_CXT.callback[i] = NULL;
                }