+// Currently the Linux kernel and FUSE disagree over the type for
+// FS_IOC_GETFLAGS and FS_IOC_SETFLAGS. The kernel actually uses int,
+// but FUSE chose long (matching the declaration in linux/fs.h). So
+// if you use int, and then traverse a FUSE filesystem, you may
+// corrupt the stack. But if you use long, then you may get invalid
+// results on big-endian systems.
+//
+// For now, we just use long, and then disable Linux attrs entirely
+// (with a warning) in helpers.py on systems that are affected.
+
+#ifdef BUP_HAVE_FILE_ATTRS
+static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
+{
+ int rc;
+ unsigned long attr;
+ char *path;
+ int fd;
+
+ if (!PyArg_ParseTuple(args, "s", &path))
+ return NULL;
+
+ fd = _open_noatime(path, O_NONBLOCK);
+ if (fd == -1)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+
+ attr = 0; // Handle int/long mismatch (see above)
+ rc = ioctl(fd, FS_IOC_GETFLAGS, &attr);
+ 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 */
+
+
+
+#ifdef BUP_HAVE_FILE_ATTRS
+static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
+{
+ int rc;
+ unsigned long orig_attr;
+ unsigned int attr;
+ char *path;
+ PyObject *py_attr;
+ int fd;
+
+ 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);
+ if (fd == -1)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+
+ // Restrict attr to modifiable flags acdeijstuADST -- see
+ // chattr(1) and the e2fsprogs source. Letter to flag mapping is
+ // in pf.c flags_array[].
+ attr &= FS_APPEND_FL | FS_COMPR_FL | FS_NODUMP_FL | FS_EXTENT_FL
+ | FS_IMMUTABLE_FL | FS_JOURNAL_DATA_FL | FS_SECRM_FL | FS_NOTAIL_FL
+ | FS_UNRM_FL | FS_NOATIME_FL | FS_DIRSYNC_FL | FS_SYNC_FL
+ | FS_TOPDIR_FL | FS_NOCOW_FL;
+
+ // The extents flag can't be removed, so don't (see chattr(1) and chattr.c).
+ orig_attr = 0; // Handle int/long mismatch (see above)
+ rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
+ if (rc == -1)
+ {
+ close(fd);
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+ }
+ assert(orig_attr <= UINT_MAX); // Kernel type is actually int
+ attr |= ((unsigned int) orig_attr) & FS_EXTENT_FL;
+
+ rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
+ if (rc == -1)
+ {
+ close(fd);
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+ }
+
+ close(fd);
+ return Py_BuildValue("O", Py_None);
+}
+#endif /* def BUP_HAVE_FILE_ATTRS */
+
+
+#ifndef HAVE_UTIMENSAT
+#ifndef HAVE_UTIMES
+#error "cannot find utimensat or utimes()"
+#endif
+#ifndef HAVE_LUTIMES
+#error "cannot find utimensat or lutimes()"
+#endif
+#endif
+
+#define ASSIGN_PYLONG_TO_INTEGRAL(dest, pylong, overflow) \
+ ({ \
+ int result = 0; \
+ *(overflow) = 0; \
+ const long long lltmp = PyLong_AsLongLong(pylong); \
+ if (lltmp == -1 && PyErr_Occurred()) \
+ { \
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) \
+ { \
+ const unsigned long long ulltmp = PyLong_AsUnsignedLongLong(pylong); \
+ if (ulltmp == (unsigned long long) -1 && PyErr_Occurred()) \
+ { \
+ if (PyErr_ExceptionMatches(PyExc_OverflowError)) \
+ { \
+ PyErr_Clear(); \
+ *(overflow) = 1; \
+ } \
+ } \
+ if (INTEGRAL_ASSIGNMENT_FITS((dest), ulltmp)) \
+ result = 1; \
+ else \
+ *(overflow) = 1; \
+ } \
+ } \
+ else \
+ { \
+ if (INTEGRAL_ASSIGNMENT_FITS((dest), lltmp)) \
+ result = 1; \
+ else \
+ *(overflow) = 1; \
+ } \
+ result; \
+ })
+
+
+#ifdef HAVE_UTIMENSAT
+
+static PyObject *bup_utimensat(PyObject *self, PyObject *args)
+{
+ int rc;
+ int fd, flag;
+ char *path;
+ PyObject *access_py, *modification_py;
+ struct timespec ts[2];
+
+ if (!PyArg_ParseTuple(args, "is((Ol)(Ol))i",
+ &fd,
+ &path,
+ &access_py, &(ts[0].tv_nsec),
+ &modification_py, &(ts[1].tv_nsec),
+ &flag))
+ return NULL;
+
+ int overflow;
+ if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[0].tv_sec), access_py, &overflow))
+ {
+ if (overflow)
+ PyErr_SetString(PyExc_ValueError,
+ "unable to convert access time seconds for utimensat");
+ return NULL;
+ }
+ if (!ASSIGN_PYLONG_TO_INTEGRAL(&(ts[1].tv_sec), modification_py, &overflow))
+ {
+ if (overflow)
+ PyErr_SetString(PyExc_ValueError,
+ "unable to convert modification time seconds for utimensat");
+ return NULL;
+ }
+ rc = utimensat(fd, path, ts, flag);
+ if (rc != 0)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+
+ return Py_BuildValue("O", Py_None);
+}
+
+#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
+static PyObject *bup_utimes(PyObject *self, PyObject *args)
+{
+ char *path;
+ struct timeval tv[2];
+ 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
+static PyObject *bup_lutimes(PyObject *self, PyObject *args)
+{
+ char *path;
+ struct timeval tv[2];
+ if (!bup_parse_xutimes_args(&path, tv, args))
+ return NULL;
+ int rc = lutimes(path, tv);
+ if (rc != 0)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+
+ return Py_BuildValue("O", Py_None);
+}
+#endif /* def HAVE_LUTIMES */
+
+
+#ifdef HAVE_STAT_ST_ATIM
+# define BUP_STAT_ATIME_NS(st) (st)->st_atim.tv_nsec
+# define BUP_STAT_MTIME_NS(st) (st)->st_mtim.tv_nsec
+# define BUP_STAT_CTIME_NS(st) (st)->st_ctim.tv_nsec
+#elif defined HAVE_STAT_ST_ATIMENSEC
+# define BUP_STAT_ATIME_NS(st) (st)->st_atimespec.tv_nsec
+# define BUP_STAT_MTIME_NS(st) (st)->st_mtimespec.tv_nsec
+# define BUP_STAT_CTIME_NS(st) (st)->st_ctimespec.tv_nsec
+#else
+# define BUP_STAT_ATIME_NS(st) 0
+# define BUP_STAT_MTIME_NS(st) 0
+# define BUP_STAT_CTIME_NS(st) 0
+#endif
+
+
+#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)
+{
+ // 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(). 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),
+ INTEGER_TO_PY(st->st_nlink),
+ INTEGER_TO_PY(st->st_uid),
+ INTEGER_TO_PY(st->st_gid),
+ INTEGER_TO_PY(st->st_rdev),
+ (PY_LONG_LONG) st->st_size,
+ 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)
+{
+ int rc;
+ char *filename;
+
+ if (!PyArg_ParseTuple(args, "s", &filename))
+ return NULL;
+
+ struct stat st;
+ rc = stat(filename, &st);
+ if (rc != 0)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
+ return stat_struct_to_py(&st, filename, 0);
+}
+
+
+static PyObject *bup_lstat(PyObject *self, PyObject *args)
+{
+ int rc;
+ char *filename;
+
+ if (!PyArg_ParseTuple(args, "s", &filename))
+ return NULL;
+
+ struct stat st;
+ rc = lstat(filename, &st);
+ if (rc != 0)
+ return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
+ return stat_struct_to_py(&st, filename, 0);
+}
+
+
+static PyObject *bup_fstat(PyObject *self, PyObject *args)
+{
+ int rc, fd;
+
+ if (!PyArg_ParseTuple(args, "i", &fd))
+ return NULL;
+
+ struct stat st;
+ rc = fstat(fd, &st);
+ if (rc != 0)
+ return PyErr_SetFromErrno(PyExc_OSError);
+ return stat_struct_to_py(&st, NULL, fd);
+}
+
+
+#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)
+{
+ Py_buffer src, dest;
+ PyObject *py_src_n, *py_src_off, *py_dest_off;
+
+ if (!PyArg_ParseTuple(args, buf_argf "*OOw*O",
+ &src, &py_src_n, &py_src_off,
+ &dest, &py_dest_off))
+ return NULL;
+
+ PyObject *result = NULL;
+
+ unsigned long long src_n, src_off, 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")))
+ goto clean_and_return;
+
+ unsigned long long src_region_end;
+ if (!uadd(&src_region_end, src_off, src_n)) {
+ result = PyErr_Format(PyExc_OverflowError, "(src_off + src_n) too large");
+ goto clean_and_return;
+ }
+ if (src_region_end > src.len) {
+ result = PyErr_Format(PyExc_OverflowError, "region runs off end of src");
+ goto clean_and_return;
+ }
+
+ unsigned long long dest_size;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&dest_size, dest.len)) {
+ result = PyErr_Format(PyExc_OverflowError, "invalid dest size");
+ goto clean_and_return;
+ }
+ if (dest_off > dest_size) {
+ result = PyErr_Format(PyExc_OverflowError, "region runs off end of dest");
+ goto clean_and_return;
+ }
+
+ size_t length;
+ if (!INTEGRAL_ASSIGNMENT_FITS(&length, src_n)) {
+ result = PyErr_Format(PyExc_OverflowError, "src_n overflows size_t");
+ goto clean_and_return;
+ }
+ int rc = mincore((void *)(src.buf + src_off), src_n,
+ (BUP_MINCORE_BUF_TYPE *) (dest.buf + dest_off));
+ if (rc != 0) {
+ result = PyErr_SetFromErrno(PyExc_OSError);
+ goto clean_and_return;
+ }
+ result = Py_BuildValue("O", Py_None);
+
+ clean_and_return:
+ PyBuffer_Release(&src);
+ PyBuffer_Release(&dest);
+ return result;
+}
+#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." },