]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/_helpers.c
Remove dependency on linux/ext2_fs.h
[bup.git] / lib / bup / _helpers.c
index 04a3bb9ce9a928ac3154a7d95a1f88202143efce..ab10fc8485ac98033e27d08dc46f0be00676776f 100644 (file)
 #ifdef HAVE_SYS_IOCTL_H
 #include <sys/ioctl.h>
 #endif
-#ifdef HAVE_EXT2FS_EXT2_FS_H
-#include <ext2fs/ext2_fs.h>
-#endif
 
-#if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) \
-    && defined(HAVE_EXT2FS_EXT2_FS_H)
+#if defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS)
 #define BUP_HAVE_FILE_ATTRS 1
 #endif
 
+#ifndef FS_NOCOW_FL
+// Of course, this assumes it's a bitfield value.
+#define FS_NOCOW_FL 0
+#endif
+
 static int istty2 = 0;
 
 // Probably we should use autoconf or something and set HAVE_PY_GETARGCARGV...
@@ -601,13 +602,10 @@ static PyObject *random_sha(PyObject *self, PyObject *args)
 }
 
 
-static PyObject *open_noatime(PyObject *self, PyObject *args)
+static int _open_noatime(const char *filename, int attrs)
 {
-    char *filename = NULL;
-    int attrs, attrs_noatime, fd;
-    if (!PyArg_ParseTuple(args, "s", &filename))
-       return NULL;
-    attrs = O_RDONLY;
+    int attrs_noatime, fd;
+    attrs |= O_RDONLY;
 #ifdef O_NOFOLLOW
     attrs |= O_NOFOLLOW;
 #endif
@@ -628,6 +626,17 @@ static PyObject *open_noatime(PyObject *self, PyObject *args)
        // just harmlessly ignore it, so this branch won't trigger)
        fd = open(filename, attrs);
     }
+    return fd;
+}
+
+
+static PyObject *open_noatime(PyObject *self, PyObject *args)
+{
+    char *filename = NULL;
+    int fd;
+    if (!PyArg_ParseTuple(args, "s", &filename))
+       return NULL;
+    fd = _open_noatime(filename, 0);
     if (fd < 0)
        return PyErr_SetFromErrnoWithFilename(PyExc_OSError, filename);
     return Py_BuildValue("i", fd);
@@ -658,7 +667,7 @@ static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
     if (!PyArg_ParseTuple(args, "s", &path))
         return NULL;
 
-    fd = open(path, O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_NOFOLLOW);
+    fd = _open_noatime(path, O_NONBLOCK);
     if (fd == -1)
         return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
 
@@ -680,7 +689,7 @@ static PyObject *bup_get_linux_file_attr(PyObject *self, PyObject *args)
 static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
 {
     int rc;
-    unsigned long attr;
+    unsigned long orig_attr, attr;
     char *path;
     int fd;
 
@@ -694,10 +703,19 @@ static PyObject *bup_set_linux_file_attr(PyObject *self, PyObject *args)
     // Restrict attr to modifiable flags acdeijstuADST -- see
     // chattr(1) and the e2fsprogs source.  Letter to flag mapping is
     // in pf.c flags_array[].
-    attr &= EXT2_APPEND_FL | EXT2_COMPR_FL | EXT2_NODUMP_FL | EXT4_EXTENTS_FL
-    | EXT2_IMMUTABLE_FL | EXT3_JOURNAL_DATA_FL | EXT2_SECRM_FL | EXT2_NOTAIL_FL
-    | EXT2_UNRM_FL | EXT2_NOATIME_FL | EXT2_DIRSYNC_FL | EXT2_SYNC_FL
-    | EXT2_TOPDIR_FL;
+    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).
+    rc = ioctl(fd, FS_IOC_GETFLAGS, &orig_attr);
+    if (rc == -1)
+    {
+        close(fd);
+        return PyErr_SetFromErrnoWithFilename(PyExc_OSError, path);
+    }
+    attr |= (orig_attr & FS_EXTENT_FL);
 
     rc = ioctl(fd, FS_IOC_SETFLAGS, &attr);
     if (rc == -1)