]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/_helpers.c
Find SC_ARG_MAX via C sysconf().
[bup.git] / lib / bup / _helpers.c
index d9dcbed64451a9aada0ddea07d6aa238523a601f..ea49c0e7571045484a35c73a7e07344607f822eb 100644 (file)
 
 static int istty2 = 0;
 
+// At the moment any code that calls INTGER_TO_PY() will have to
+// disable -Wtautological-compare for clang.  See below.
+
+#define INTEGER_TO_PY(x) \
+    (((x) >= 0) ? PyLong_FromUnsignedLongLong(x) : PyLong_FromLongLong(x))
+
+
+static int bup_ulong_from_pyint(unsigned long *x, PyObject *py,
+                                const char *name)
+{
+    const long tmp = PyInt_AsLong(py);
+    if (tmp == -1 && PyErr_Occurred())
+    {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_OverflowError, "%s too big for unsigned long",
+                         name);
+        return 0;
+    }
+    if (tmp < 0)
+    {
+        PyErr_Format(PyExc_OverflowError,
+                     "negative %s cannot be converted to unsigned long", name);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
+
+
+static int bup_ulong_from_py(unsigned long *x, PyObject *py, const char *name)
+{
+    if (PyInt_Check(py))
+        return bup_ulong_from_pyint(x, py, name);
+
+    if (!PyLong_Check(py))
+    {
+        PyErr_Format(PyExc_TypeError, "expected integer %s", name);
+        return 0;
+    }
+
+    const unsigned long tmp = PyLong_AsUnsignedLong(py);
+    if (PyErr_Occurred())
+    {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_OverflowError, "%s too big for unsigned long",
+                         name);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
+
+
+static int bup_uint_from_py(unsigned int *x, PyObject *py, const char *name)
+{
+    unsigned long tmp;
+    if (!bup_ulong_from_py(&tmp, py, name))
+        return 0;
+
+    if (tmp > UINT_MAX)
+    {
+        PyErr_Format(PyExc_OverflowError, "%s too big for unsigned int", name);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
+
+static int bup_ullong_from_py(unsigned PY_LONG_LONG *x, PyObject *py,
+                              const char *name)
+{
+    if (PyInt_Check(py))
+    {
+        unsigned long tmp;
+        if (bup_ulong_from_pyint(&tmp, py, name))
+        {
+            *x = tmp;
+            return 1;
+        }
+        return 0;
+    }
+
+    if (!PyLong_Check(py))
+    {
+        PyErr_Format(PyExc_TypeError, "integer argument expected for %s", name);
+        return 0;
+    }
+
+    const unsigned PY_LONG_LONG tmp = PyLong_AsUnsignedLongLong(py);
+    if (tmp == (unsigned long long) -1 && PyErr_Occurred())
+    {
+        if (PyErr_ExceptionMatches(PyExc_OverflowError))
+            PyErr_Format(PyExc_OverflowError,
+                         "%s too big for unsigned long long", name);
+        return 0;
+    }
+    *x = tmp;
+    return 1;
+}
+
+
 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
 #if __WIN32__ || __CYGWIN__
 
@@ -403,7 +504,7 @@ static uint32_t _get_idx_i(struct idx *idx)
 
 static PyObject *merge_into(PyObject *self, PyObject *args)
 {
-    PyObject *ilist = NULL;
+    PyObject *py_total, *ilist = NULL;
     unsigned char *fmap = NULL;
     struct sha *sha_ptr, *sha_start = NULL;
     uint32_t *table_ptr, *name_ptr, *name_start;
@@ -415,9 +516,13 @@ static PyObject *merge_into(PyObject *self, PyObject *args)
     int num_i;
     int last_i;
 
-    if (!PyArg_ParseTuple(args, "w#iIO", &fmap, &flen, &bits, &total, &ilist))
+    if (!PyArg_ParseTuple(args, "w#iOO",
+                          &fmap, &flen, &bits, &py_total, &ilist))
        return NULL;
 
+    if (!bup_uint_from_py(&total, py_total, "total"))
+        return NULL;
+
     num_i = PyList_Size(ilist);
     idxs = (struct idx **)PyMem_Malloc(num_i * sizeof(struct idx *));
 
@@ -490,7 +595,7 @@ static uint64_t htonll(uint64_t value)
 static PyObject *write_idx(PyObject *self, PyObject *args)
 {
     char *filename = NULL;
-    PyObject *idx = NULL;
+    PyObject *py_total, *idx = NULL;
     PyObject *part;
     unsigned char *fmap = NULL;
     Py_ssize_t flen = 0;
@@ -501,9 +606,13 @@ static PyObject *write_idx(PyObject *self, PyObject *args)
     uint64_t *ofs64_ptr;
     struct sha *sha_ptr;
 
-    if (!PyArg_ParseTuple(args, "sw#OI", &filename, &fmap, &flen, &idx, &total))
+    if (!PyArg_ParseTuple(args, "sw#OO",
+                          &filename, &fmap, &flen, &idx, &py_total))
        return NULL;
 
+    if (!bup_uint_from_py(&total, py_total, "total"))
+        return NULL;
+
     if (PyList_Size (idx) != FAN_ENTRIES) // Check for list of the right length.
         return PyErr_Format (PyExc_TypeError, "idx must contain %d entries",
                              FAN_ENTRIES);
@@ -531,15 +640,20 @@ static PyObject *write_idx(PyObject *self, PyObject *args)
        {
            unsigned char *sha = NULL;
            Py_ssize_t sha_len = 0;
-           unsigned int crc = 0;
-            unsigned PY_LONG_LONG ofs_py = 0;
+            PyObject *crc_py, *ofs_py;
+           unsigned int crc;
+            unsigned PY_LONG_LONG ofs_ull;
            uint64_t ofs;
-           if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#IK",
-                                 &sha, &sha_len, &crc, &ofs_py))
+           if (!PyArg_ParseTuple(PyList_GET_ITEM(part, j), "t#OO",
+                                 &sha, &sha_len, &crc_py, &ofs_py))
                return NULL;
+            if(!bup_uint_from_py(&crc, crc_py, "crc"))
+                return NULL;
+            if(!bup_ullong_from_py(&ofs_ull, ofs_py, "ofs"))
+                return NULL;
             assert(crc <= UINT32_MAX);
-            assert(ofs_py <= UINT64_MAX);
-           ofs = ofs_py;
+            assert(ofs_ull <= UINT64_MAX);
+           ofs = ofs_ull;
            if (sha_len != sizeof(struct sha))
                return NULL;
            memcpy(sha_ptr++, sha, sizeof(struct sha));
@@ -718,15 +832,20 @@ static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
 #endif /* def BUP_HAVE_FILE_ATTRS */
 
 
+
 #ifdef BUP_HAVE_FILE_ATTRS
 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
 {
     int rc;
     unsigned int orig_attr, attr;
     char *path;
+    PyObject *py_attr;
     int fd;
 
-    if (!PyArg_ParseTuple(args, "sI", &path, &attr))
+    if (!PyArg_ParseTuple(args, "sO", &path, &py_attr))
+        return NULL;
+
+    if (!bup_uint_from_py(&attr, py_attr, "attr"))
         return NULL;
 
     fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
@@ -773,70 +892,6 @@ static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
 #endif
 
 
-#if defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES) || defined(HAVE_LUTIMES)
-
-static int bup_parse_xutime_args(char **path,
-                                 long *access,
-                                 long *access_ns,
-                                 long *modification,
-                                 long *modification_ns,
-                                 PyObject *self, PyObject *args)
-{
-    if (!PyArg_ParseTuple(args, "s((ll)(ll))",
-                          path,
-                          access, access_ns,
-                          modification, modification_ns))
-        return 0;
-
-    if (isnan(*access))
-    {
-        PyErr_SetString(PyExc_ValueError, "access time is NaN");
-        return 0;
-    }
-    else if (isinf(*access))
-    {
-        PyErr_SetString(PyExc_ValueError, "access time is infinite");
-        return 0;
-    }
-    else if (isnan(*modification))
-    {
-        PyErr_SetString(PyExc_ValueError, "modification time is NaN");
-        return 0;
-    }
-    else if (isinf(*modification))
-    {
-        PyErr_SetString(PyExc_ValueError, "modification time is infinite");
-        return 0;
-    }
-
-    if (isnan(*access_ns))
-    {
-        PyErr_SetString(PyExc_ValueError, "access time ns is NaN");
-        return 0;
-    }
-    else if (isinf(*access_ns))
-    {
-        PyErr_SetString(PyExc_ValueError, "access time ns is infinite");
-        return 0;
-    }
-    else if (isnan(*modification_ns))
-    {
-        PyErr_SetString(PyExc_ValueError, "modification time ns is NaN");
-        return 0;
-    }
-    else if (isinf(*modification_ns))
-    {
-        PyErr_SetString(PyExc_ValueError, "modification time ns is infinite");
-        return 0;
-    }
-
-    return 1;
-}
-
-#endif /* defined(HAVE_UTIMENSAT) || defined(HAVE_FUTIMES)
-          || defined(HAVE_LUTIMES) */
-
-
 #define INTEGRAL_ASSIGNMENT_FITS(dest, src)                             \
     ({                                                                  \
         *(dest) = (src);                                                \
@@ -848,13 +903,13 @@ static int bup_parse_xutime_args(char **path,
     ({                                                     \
         int result = 0;                                                 \
         *(overflow) = 0;                                                \
-        const long long ltmp = PyLong_AsLongLong(pylong);               \
-        if (ltmp == -1 && PyErr_Occurred())                             \
+        const long long lltmp = PyLong_AsLongLong(pylong);              \
+        if (lltmp == -1 && PyErr_Occurred())                            \
         {                                                               \
             if (PyErr_ExceptionMatches(PyExc_OverflowError))            \
             {                                                           \
-                const unsigned long ultmp = PyLong_AsUnsignedLongLong(pylong); \
-                if (ultmp == (unsigned long long) -1 && PyErr_Occurred()) \
+                const unsigned long long ulltmp = PyLong_AsUnsignedLongLong(pylong); \
+                if (ulltmp == (unsigned long long) -1 && PyErr_Occurred()) \
                 {                                                       \
                     if (PyErr_ExceptionMatches(PyExc_OverflowError))    \
                     {                                                   \
@@ -862,7 +917,7 @@ static int bup_parse_xutime_args(char **path,
                         *(overflow) = 1;                                \
                     }                                                   \
                 }                                                       \
-                if (INTEGRAL_ASSIGNMENT_FITS((dest), ultmp))            \
+                if (INTEGRAL_ASSIGNMENT_FITS((dest), ulltmp))           \
                     result = 1;                                         \
                 else                                                    \
                     *(overflow) = 1;                                    \
@@ -870,7 +925,7 @@ static int bup_parse_xutime_args(char **path,
         }                                                               \
         else                                                            \
         {                                                               \
-            if (INTEGRAL_ASSIGNMENT_FITS((dest), ltmp))                 \
+            if (INTEGRAL_ASSIGNMENT_FITS((dest), lltmp))                \
                 result = 1;                                             \
             else                                                        \
                 *(overflow) = 1;                                        \
@@ -922,52 +977,73 @@ static PyObject *bup_utimensat(PyObject *self, PyObject *args)
 #endif /* def HAVE_UTIMENSAT */
 
 
+#if defined(HAVE_UTIMES) || defined(HAVE_LUTIMES)
+
+static int bup_parse_xutimes_args(char **path,
+                                  struct timeval tv[2],
+                                  PyObject *args)
+{
+    PyObject *access_py, *modification_py;
+    long long access_us, modification_us; // POSIX guarantees tv_usec is signed.
+
+    if (!PyArg_ParseTuple(args, "s((OL)(OL))",
+                          path,
+                          &access_py, &access_us,
+                          &modification_py, &modification_us))
+        return 0;
+
+    int overflow;
+    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[0].tv_sec), access_py, &overflow))
+    {
+        if (overflow)
+            PyErr_SetString(PyExc_ValueError, "unable to convert access time seconds to timeval");
+        return 0;
+    }
+    if (!INTEGRAL_ASSIGNMENT_FITS(&(tv[0].tv_usec), access_us))
+    {
+        PyErr_SetString(PyExc_ValueError, "unable to convert access time nanoseconds to timeval");
+        return 0;
+    }
+    if (!ASSIGN_PYLONG_TO_INTEGRAL(&(tv[1].tv_sec), modification_py, &overflow))
+    {
+        if (overflow)
+            PyErr_SetString(PyExc_ValueError, "unable to convert modification time seconds to timeval");
+        return 0;
+    }
+    if (!INTEGRAL_ASSIGNMENT_FITS(&(tv[1].tv_usec), modification_us))
+    {
+        PyErr_SetString(PyExc_ValueError, "unable to convert modification time nanoseconds to timeval");
+        return 0;
+    }
+    return 1;
+}
+
+#endif /* defined(HAVE_UTIMES) || defined(HAVE_LUTIMES) */
+
+
 #ifdef HAVE_UTIMES
-#define BUP_HAVE_BUP_UTIME_NS 1
-static PyObject *bup_utime_ns(PyObject *self, PyObject *args)
+static PyObject *bup_utimes(PyObject *self, PyObject *args)
 {
-    int rc;
     char *path;
-    long access, access_ns, modification, modification_ns;
     struct timeval tv[2];
-
-    if (!bup_parse_xutime_args(&path, &access, &access_ns,
-                               &modification, &modification_ns,
-                               self, args))
-       return NULL;
-
-    tv[0].tv_sec = access;
-    tv[0].tv_usec = access_ns / 1000;
-    tv[1].tv_sec = modification;
-    tv[1].tv_usec = modification_ns / 1000;
-    rc = utimes(path, tv);
+    if (!bup_parse_xutimes_args(&path, tv, args))
+        return NULL;
+    int rc = utimes(path, tv);
     if (rc != 0)
         return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
-
     return Py_BuildValue("O", Py_None);
 }
 #endif /* def HAVE_UTIMES */
 
 
 #ifdef HAVE_LUTIMES
-#define BUP_HAVE_BUP_LUTIME_NS 1
-static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
+static PyObject *bup_lutimes(PyObject *self, PyObject *args)
 {
-    int rc;
     char *path;
-    long access, access_ns, modification, modification_ns;
     struct timeval tv[2];
-
-    if (!bup_parse_xutime_args(&path, &access, &access_ns,
-                               &modification, &modification_ns,
-                               self, args))
-       return NULL;
-
-    tv[0].tv_sec = access;
-    tv[0].tv_usec = access_ns / 1000;
-    tv[1].tv_sec = modification;
-    tv[1].tv_usec = modification_ns / 1000;
-    rc = lutimes(path, tv);
+    if (!bup_parse_xutimes_args(&path, tv, args))
+        return NULL;
+    int rc = lutimes(path, tv);
     if (rc != 0)
         return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
 
@@ -991,74 +1067,18 @@ static PyObject *bup_lutime_ns(PyObject *self, PyObject *args)
 #endif
 
 
-static void set_invalid_timespec_msg(const char *field,
-                                     const long long sec,
-                                     const long nsec,
-                                     const char *filename,
-                                     int fd)
-{
-    if (filename != NULL)
-        PyErr_Format(PyExc_ValueError,
-                     "invalid %s timespec (%lld %ld) for file \"%s\"",
-                     field, sec, nsec, filename);
-    else
-        PyErr_Format(PyExc_ValueError,
-                     "invalid %s timespec (%lld %ld) for file descriptor %d",
-                     field, sec, nsec, fd);
-}
-
-
-static int normalize_timespec_values(const char *name,
-                                     long long *sec,
-                                     long *nsec,
-                                     const char *filename,
-                                     int fd)
-{
-    if (*nsec < -999999999 || *nsec > 999999999)
-    {
-        set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
-        return 0;
-    }
-    if (*nsec < 0)
-    {
-        if (*sec == LONG_MIN)
-        {
-            set_invalid_timespec_msg(name, *sec, *nsec, filename, fd);
-            return 0;
-        }
-        *nsec += 1000000000;
-        *sec -= 1;
-    }
-    return 1;
-}
-
-
-#define INTEGER_TO_PY(x) \
-    (((x) >= 0) ? PyLong_FromUnsignedLongLong(x) : PyLong_FromLongLong(x))
-
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wtautological-compare" // For INTEGER_TO_PY().
 
 static PyObject *stat_struct_to_py(const struct stat *st,
                                    const char *filename,
                                    int fd)
 {
-    long long atime = st->st_atime;
-    long long mtime = st->st_mtime;
-    long long ctime = st->st_ctime;
-    long atime_ns = BUP_STAT_ATIME_NS(st);
-    long mtime_ns = BUP_STAT_MTIME_NS(st);
-    long ctime_ns = BUP_STAT_CTIME_NS(st);
-
-    if (!normalize_timespec_values("atime", &atime, &atime_ns, filename, fd))
-        return NULL;
-    if (!normalize_timespec_values("mtime", &mtime, &mtime_ns, filename, fd))
-        return NULL;
-    if (!normalize_timespec_values("ctime", &ctime, &ctime_ns, filename, fd))
-        return NULL;
-
     // We can check the known (via POSIX) signed and unsigned types at
     // compile time, but not (easily) the unspecified types, so handle
-    // those via INTEGER_TO_PY().
-    return Py_BuildValue("OKOOOOOL(Ll)(Ll)(Ll)",
+    // those via INTEGER_TO_PY().  Assumes ns values will fit in a
+    // long.
+    return Py_BuildValue("OKOOOOOL(Ol)(Ol)(Ol)",
                          INTEGER_TO_PY(st->st_mode),
                          (unsigned PY_LONG_LONG) st->st_ino,
                          INTEGER_TO_PY(st->st_dev),
@@ -1067,14 +1087,15 @@ static PyObject *stat_struct_to_py(const struct stat *st,
                          INTEGER_TO_PY(st->st_gid),
                          INTEGER_TO_PY(st->st_rdev),
                          (PY_LONG_LONG) st->st_size,
-                         (PY_LONG_LONG) atime,
-                         (long) atime_ns,
-                         (PY_LONG_LONG) mtime,
-                         (long) mtime_ns,
-                         (PY_LONG_LONG) ctime,
-                         (long) ctime_ns);
+                         INTEGER_TO_PY(st->st_atime),
+                         (long) BUP_STAT_ATIME_NS(st),
+                         INTEGER_TO_PY(st->st_mtime),
+                         (long) BUP_STAT_MTIME_NS(st),
+                         INTEGER_TO_PY(st->st_ctime),
+                         (long) BUP_STAT_CTIME_NS(st));
 }
 
+#pragma clang diagnostic pop  // ignored "-Wtautological-compare"
 
 static PyObject *bup_stat(PyObject *self, PyObject *args)
 {
@@ -1164,13 +1185,13 @@ static PyMethodDef helper_methods[] = {
     { "bup_utimensat", bup_utimensat, METH_VARARGS,
       "Change path timestamps with nanosecond precision (POSIX)." },
 #endif
-#ifdef BUP_HAVE_BUP_UTIME_NS
-    { "bup_utime_ns", bup_utime_ns, METH_VARARGS,
-      "Change path timestamps with up to nanosecond precision." },
+#ifdef HAVE_UTIMES
+    { "bup_utimes", bup_utimes, METH_VARARGS,
+      "Change path timestamps with microsecond precision." },
 #endif
-#ifdef BUP_HAVE_BUP_LUTIME_NS
-    { "bup_lutime_ns", bup_lutime_ns, METH_VARARGS,
-      "Change path timestamps with up to nanosecond precision;"
+#ifdef HAVE_LUTIMES
+    { "bup_lutimes", bup_lutimes, METH_VARARGS,
+      "Change path timestamps with microsecond precision;"
       " don't follow symlinks." },
 #endif
     { "stat", bup_stat, METH_VARARGS,
@@ -1194,12 +1215,15 @@ PyMODINIT_FUNC init_helpers(void)
     assert(sizeof(blkcnt_t) <= sizeof(PY_LONG_LONG));
     // Just be sure (relevant when passing timestamps back to Python above).
     assert(sizeof(PY_LONG_LONG) <= sizeof(long long));
+    assert(sizeof(unsigned PY_LONG_LONG) <= sizeof(unsigned long long));
 
     char *e;
     PyObject *m = Py_InitModule("_helpers", helper_methods);
     if (m == NULL)
         return;
 
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wtautological-compare" // For INTEGER_TO_PY().
 #ifdef HAVE_UTIMENSAT
     {
         PyObject *value;
@@ -1214,6 +1238,19 @@ PyMODINIT_FUNC init_helpers(void)
         Py_DECREF(value);
     }
 #endif
+    {
+        PyObject *value;
+        const long arg_max = sysconf(_SC_ARG_MAX);
+        if (arg_max == -1)
+        {
+            fprintf(stderr, "Cannot find SC_ARG_MAX, please report a bug.\n");
+            exit(1);
+        }
+        value = INTEGER_TO_PY(arg_max);
+        PyObject_SetAttrString(m, "SC_ARG_MAX", value);
+        Py_DECREF(value);
+    }
+#pragma clang diagnostic pop  // ignored "-Wtautological-compare"
 
     e = getenv("BUP_FORCE_TTY");
     istty2 = isatty(2) || (atoi(e ? e : "0") & 2);