#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
-#include <sys/mman.h>
+#ifdef HAVE_SYS_MMAN_H
+#include <sys/mman.h>
+#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <sys/ioctl.h>
#endif
+#ifdef HAVE_TM_TM_GMTOFF
+#include <time.h>
+#endif
+
#include "bupsplit.h"
#if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
#endif
+#define INTEGRAL_ASSIGNMENT_FITS(dest, src) \
+ ({ \
+ *(dest) = (src); \
+ *(dest) == (src) && (*(dest) < 1) == ((src) < 1); \
+ })
+
+
// At the moment any code that calls INTGER_TO_PY() will have to
// disable -Wtautological-compare for clang. See below.
#endif // not __WIN32__ or __CYGWIN__
+static unsigned long long count_leading_zeros(const unsigned char * const buf,
+ unsigned long long len)
+{
+ const unsigned char *cur = buf;
+ while(len-- && *cur == 0)
+ cur++;
+ return cur - buf;
+}
+
+
+static int write_all(int fd, const void *buf, const size_t count)
+{
+ size_t written = 0;
+ while (written < count)
+ {
+ const ssize_t rc = write(fd, buf + written, count - written);
+ if (rc == -1)
+ return -1;
+ written += rc;
+ }
+ return 0;
+}
+
+
+static int uadd(unsigned long long *dest,
+ const unsigned long long x,
+ const unsigned long long y)
+{
+ const unsigned long long result = x + y;
+ if (result < x || result < y)
+ return 0;
+ *dest = result;
+ return 1;
+}
+
+static PyObject *append_sparse_region(const int fd, unsigned long long n)
+{
+ while(n)
+ {
+ off_t new_off;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&new_off, n))
+ new_off = INT_MAX;
+ const off_t off = lseek(fd, new_off, SEEK_CUR);
+ if (off == (off_t) -1)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ n -= new_off;
+ }
+ return NULL;
+}
+
+
+static PyObject *bup_write_sparsely(PyObject *self, PyObject *args)
+{
+ int fd;
+ unsigned char *buf = NULL;
+ Py_ssize_t sbuf_len;
+ PyObject *py_min_sparse_len, *py_prev_sparse_len;
+ if (!PyArg_ParseTuple(args, "it#OO",
+ &fd, &buf, &sbuf_len,
+ &py_min_sparse_len, &py_prev_sparse_len))
+ return NULL;
+ unsigned long long min_sparse_len, prev_sparse_len, buf_len;
+ if (!bup_ullong_from_py(&min_sparse_len, py_min_sparse_len, "min_sparse_len"))
+ return NULL;
+ if (!bup_ullong_from_py(&prev_sparse_len, py_prev_sparse_len, "prev_sparse_len"))
+ return NULL;
+ if (sbuf_len < 0)
+ return PyErr_Format(PyExc_ValueError, "negative bufer length");
+ if (!INTEGRAL_ASSIGNMENT_FITS(&buf_len, sbuf_len))
+ return PyErr_Format(PyExc_OverflowError, "buffer length too large");
+
+ // The value of zeros_read indicates the number of zeros read from
+ // buf that haven't been accounted for yet (with respect to cur),
+ // while zeros indicates the total number of pending zeros, which
+ // could be larger in the first iteration if prev_sparse_len
+ // wasn't zero.
+ int rc;
+ unsigned long long unexamined = buf_len;
+ unsigned char *block_start = buf, *cur = buf;
+ unsigned long long zeros, zeros_read = count_leading_zeros(cur, unexamined);
+ assert(zeros_read <= unexamined);
+ unexamined -= zeros_read;
+ if (!uadd(&zeros, prev_sparse_len, zeros_read))
+ {
+ PyObject *err = append_sparse_region(fd, prev_sparse_len);
+ if (err != NULL)
+ return err;
+ zeros = zeros_read;
+ }
+
+ while(unexamined)
+ {
+ if (zeros < min_sparse_len)
+ cur += zeros_read;
+ else
+ {
+ rc = write_all(fd, block_start, cur - block_start);
+ if (rc)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ PyObject *err = append_sparse_region(fd, zeros);
+ if (err != NULL)
+ return err;
+ cur += zeros_read;
+ block_start = cur;
+ }
+ // Pending zeros have ether been made sparse, or are going to
+ // be rolled into the next non-sparse block since we know we
+ // now have at least one unexamined non-zero byte.
+ assert(unexamined && *cur != 0);
+ zeros = zeros_read = 0;
+ while (unexamined && *cur != 0)
+ {
+ cur++; unexamined--;
+ }
+ if (unexamined)
+ {
+ zeros_read = count_leading_zeros(cur, unexamined);
+ assert(zeros_read <= unexamined);
+ unexamined -= zeros_read;
+ zeros = zeros_read;
+ }
+ }
+ rc = write_all(fd, block_start, cur - block_start);
+ if (rc)
+ return PyErr_SetFromErrno(PyExc_IOError);
+ return PyLong_FromUnsignedLongLong(zeros);
+}
+
+
static PyObject *selftest(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
static PyObject *fadvise_done(PyObject *self, PyObject *args)
{
int fd = -1;
- long long ofs = 0;
- if (!PyArg_ParseTuple(args, "iL", &fd, &ofs))
+ long long llofs, lllen = 0;
+ if (!PyArg_ParseTuple(args, "iLL", &fd, &llofs, &lllen))
return NULL;
+ off_t ofs, len;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&ofs, llofs))
+ return PyErr_Format(PyExc_OverflowError,
+ "fadvise offset overflows off_t");
+ if (!INTEGRAL_ASSIGNMENT_FITS(&len, lllen))
+ return PyErr_Format(PyExc_OverflowError,
+ "fadvise length overflows off_t");
#ifdef POSIX_FADV_DONTNEED
- posix_fadvise(fd, 0, ofs, POSIX_FADV_DONTNEED);
+ posix_fadvise(fd, ofs, len, POSIX_FADV_DONTNEED);
#endif
return Py_BuildValue("");
}
attr = 0; // Handle int/long mismatch (see above)
rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
- assert(attr <= UINT_MAX); // Kernel type is actually int
if (rc == -1)
{
close(fd);
return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
}
-
close(fd);
+ assert(attr <= UINT_MAX); // Kernel type is actually int
return PyLong_FromUnsignedLong(attr);
}
#endif /* def BUP_HAVE_FILE_ATTRS */
#endif
#endif
-
-#define INTEGRAL_ASSIGNMENT_FITS(dest, src) \
- ({ \
- *(dest) = (src); \
- *(dest) == (src) && (*(dest) < 1) == ((src) < 1); \
- })
-
-
#define ASSIGN_PYLONG_TO_INTEGRAL(dest, pylong, overflow) \
({ \
int result = 0; \
}
+#ifdef HAVE_TM_TM_GMTOFF
+static PyObject *bup_localtime(PyObject *self, PyObject *args)
+{
+ long long lltime;
+ time_t ttime;
+ if (!PyArg_ParseTuple(args, "L", &lltime))
+ return NULL;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&ttime, lltime))
+ return PyErr_Format(PyExc_OverflowError, "time value too large");
+
+ struct tm tm;
+ tzset();
+ if(localtime_r(&ttime, &tm) == NULL)
+ return PyErr_SetFromErrno(PyExc_OSError);
+
+ // Match the Python struct_time values.
+ return Py_BuildValue("[i,i,i,i,i,i,i,i,i,i,s]",
+ 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
+ tm.tm_hour, tm.tm_min, tm.tm_sec,
+ tm.tm_wday, tm.tm_yday + 1,
+ tm.tm_isdst, tm.tm_gmtoff, tm.tm_zone);
+}
+#endif /* def HAVE_TM_TM_GMTOFF */
+
+
+#ifdef BUP_MINCORE_BUF_TYPE
+static PyObject *bup_mincore(PyObject *self, PyObject *args)
+{
+ const char *src;
+ Py_ssize_t src_ssize;
+ Py_buffer dest;
+ PyObject *py_src_n, *py_src_off, *py_dest_off;
+ if (!PyArg_ParseTuple(args, "s#OOw*O",
+ &src, &src_ssize, &py_src_n, &py_src_off,
+ &dest, &py_dest_off))
+ return NULL;
+
+ unsigned long long src_size, src_n, src_off, dest_size, dest_off;
+ if (!(bup_ullong_from_py(&src_n, py_src_n, "src_n")
+ && bup_ullong_from_py(&src_off, py_src_off, "src_off")
+ && bup_ullong_from_py(&dest_off, py_dest_off, "dest_off")))
+ return NULL;
+
+ if (!INTEGRAL_ASSIGNMENT_FITS(&src_size, src_ssize))
+ return PyErr_Format(PyExc_OverflowError, "invalid src size");
+ unsigned long long src_region_end;
+
+ if (!uadd(&src_region_end, src_off, src_n))
+ return PyErr_Format(PyExc_OverflowError, "(src_off + src_n) too large");
+ if (src_region_end > src_size)
+ return PyErr_Format(PyExc_OverflowError, "region runs off end of src");
+
+ if (!INTEGRAL_ASSIGNMENT_FITS(&dest_size, dest.len))
+ return PyErr_Format(PyExc_OverflowError, "invalid dest size");
+ if (dest_off > dest_size)
+ return PyErr_Format(PyExc_OverflowError, "region runs off end of dest");
+
+ size_t length;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&length, src_n))
+ return PyErr_Format(PyExc_OverflowError, "src_n overflows size_t");
+ int rc = mincore((void *)(src + src_off), src_n,
+ (BUP_MINCORE_BUF_TYPE *) (dest.buf + dest_off));
+ if (rc != 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+ return Py_BuildValue("O", Py_None);
+}
+#endif /* def BUP_MINCORE_BUF_TYPE */
+
+
static PyMethodDef helper_methods[] = {
+ { "write_sparsely", bup_write_sparsely, METH_VARARGS,
+ "Write buf excepting zeros at the end. Return trailing zero count." },
{ "selftest", selftest, METH_VARARGS,
"Check that the rolling checksum rolls correctly (for unit tests)." },
{ "blobbits", blobbits, METH_VARARGS,
"Extended version of lstat." },
{ "fstat", bup_fstat, METH_VARARGS,
"Extended version of fstat." },
+#ifdef HAVE_TM_TM_GMTOFF
+ { "localtime", bup_localtime, METH_VARARGS,
+ "Return struct_time elements plus the timezone offset and name." },
+#endif
+#ifdef BUP_MINCORE_BUF_TYPE
+ { "mincore", bup_mincore, METH_VARARGS,
+ "For mincore(src, src_n, src_off, dest, dest_off)"
+ " call the system mincore(src + src_off, src_n, &dest[dest_off])." },
+#endif
{ NULL, NULL, 0, NULL }, // sentinel
};
assert(sizeof(PY_LONG_LONG) <= sizeof(long long));
assert(sizeof(unsigned PY_LONG_LONG) <= sizeof(unsigned long long));
+ if (sizeof(off_t) < sizeof(int))
+ {
+ // Originally required by append_sparse_region().
+ fprintf(stderr, "sizeof(off_t) < sizeof(int); please report.\n");
+ exit(1);
+ }
+
char *e;
PyObject *m = Py_InitModule("_helpers", helper_methods);
if (m == NULL)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wtautological-compare" // For INTEGER_TO_PY().
+ {
+ PyObject *value;
+ value = INTEGER_TO_PY(INT_MAX);
+ PyObject_SetAttrString(m, "INT_MAX", value);
+ Py_DECREF(value);
+ value = INTEGER_TO_PY(UINT_MAX);
+ PyObject_SetAttrString(m, "UINT_MAX", value);
+ Py_DECREF(value);
+ }
#ifdef HAVE_UTIMENSAT
{
PyObject *value;
Py_DECREF(value);
}
#endif
+#ifdef BUP_HAVE_MINCORE_INCORE
{
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);
+ value = INTEGER_TO_PY(MINCORE_INCORE);
+ PyObject_SetAttrString(m, "MINCORE_INCORE", value);
Py_DECREF(value);
}
+#endif
#pragma clang diagnostic pop // ignored "-Wtautological-compare"
e = getenv("BUP_FORCE_TTY");