summaryrefslogtreecommitdiff
path: root/libbtrfsutil/python
diff options
context:
space:
mode:
authorOmar Sandoval <osandov@fb.com>2018-01-18 12:49:55 -0800
committerDavid Sterba <dsterba@suse.com>2018-03-06 11:28:36 +0100
commit92d4035074dd5234f1c3fd263947dde1f9bb288e (patch)
treed54816a235d76725e36e5efdebc67752ed51b25b /libbtrfsutil/python
parent1b2775bdb068a1faee631a81aab72ba5aeda7d4f (diff)
libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id()
These are the most trivial helpers in the library and will be used to implement several of the more involved functions. Signed-off-by: Omar Sandoval <osandov@fb.com> Signed-off-by: David Sterba <dsterba@suse.com>
Diffstat (limited to 'libbtrfsutil/python')
-rw-r--r--libbtrfsutil/python/btrfsutilpy.h2
-rw-r--r--libbtrfsutil/python/module.c12
-rwxr-xr-xlibbtrfsutil/python/setup.py1
-rw-r--r--libbtrfsutil/python/subvolume.c73
-rw-r--r--libbtrfsutil/python/tests/test_subvolume.py57
5 files changed, 145 insertions, 0 deletions
diff --git a/libbtrfsutil/python/btrfsutilpy.h b/libbtrfsutil/python/btrfsutilpy.h
index 02039ba1..47125d85 100644
--- a/libbtrfsutil/python/btrfsutilpy.h
+++ b/libbtrfsutil/python/btrfsutilpy.h
@@ -61,6 +61,8 @@ void SetFromBtrfsUtilErrorWithPaths(enum btrfs_util_error err,
PyObject *filesystem_sync(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *start_sync(PyObject *self, PyObject *args, PyObject *kwds);
PyObject *wait_sync(PyObject *self, PyObject *args, PyObject *kwds);
+PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds);
+PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds);
void add_module_constants(PyObject *m);
diff --git a/libbtrfsutil/python/module.c b/libbtrfsutil/python/module.c
index 9c1a864a..18e8c5aa 100644
--- a/libbtrfsutil/python/module.c
+++ b/libbtrfsutil/python/module.c
@@ -153,6 +153,18 @@ static PyMethodDef btrfsutil_methods[] = {
"path -- string, bytes, path-like object, or open file descriptor\n"
"transid -- int transaction ID to wait for, or zero for the current\n"
"transaction"},
+ {"is_subvolume", (PyCFunction)is_subvolume,
+ METH_VARARGS | METH_KEYWORDS,
+ "is_subvolume(path) -> bool\n\n"
+ "Get whether a file is a subvolume.\n\n"
+ "Arguments:\n"
+ "path -- string, bytes, path-like object, or open file descriptor"},
+ {"subvolume_id", (PyCFunction)subvolume_id,
+ METH_VARARGS | METH_KEYWORDS,
+ "subvolume_id(path) -> int\n\n"
+ "Get the ID of the subvolume containing a file.\n\n"
+ "Arguments:\n"
+ "path -- string, bytes, path-like object, or open file descriptor"},
{},
};
diff --git a/libbtrfsutil/python/setup.py b/libbtrfsutil/python/setup.py
index df44809f..1cd3bc00 100755
--- a/libbtrfsutil/python/setup.py
+++ b/libbtrfsutil/python/setup.py
@@ -93,6 +93,7 @@ module = Extension(
'filesystem.c',
'module.c',
'qgroup.c',
+ 'subvolume.c',
],
include_dirs=['..'],
library_dirs=['../..'],
diff --git a/libbtrfsutil/python/subvolume.c b/libbtrfsutil/python/subvolume.c
new file mode 100644
index 00000000..4aab06a5
--- /dev/null
+++ b/libbtrfsutil/python/subvolume.c
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2018 Facebook
+ *
+ * This file is part of libbtrfsutil.
+ *
+ * libbtrfsutil is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * libbtrfsutil is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "btrfsutilpy.h"
+
+PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
+{
+ static char *keywords[] = {"path", NULL};
+ struct path_arg path = {.allow_fd = true};
+ enum btrfs_util_error err;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:is_subvolume",
+ keywords, &path_converter, &path))
+ return NULL;
+
+ if (path.path)
+ err = btrfs_util_is_subvolume(path.path);
+ else
+ err = btrfs_util_is_subvolume_fd(path.fd);
+ if (err == BTRFS_UTIL_OK) {
+ path_cleanup(&path);
+ Py_RETURN_TRUE;
+ } else if (err == BTRFS_UTIL_ERROR_NOT_BTRFS ||
+ err == BTRFS_UTIL_ERROR_NOT_SUBVOLUME) {
+ path_cleanup(&path);
+ Py_RETURN_FALSE;
+ } else {
+ SetFromBtrfsUtilErrorWithPath(err, &path);
+ path_cleanup(&path);
+ return NULL;
+ }
+}
+
+PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds)
+{
+ static char *keywords[] = {"path", NULL};
+ struct path_arg path = {.allow_fd = true};
+ enum btrfs_util_error err;
+ uint64_t id;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:subvolume_id",
+ keywords, &path_converter, &path))
+ return NULL;
+
+ if (path.path)
+ err = btrfs_util_subvolume_id(path.path, &id);
+ else
+ err = btrfs_util_subvolume_id_fd(path.fd, &id);
+ if (err) {
+ SetFromBtrfsUtilErrorWithPath(err, &path);
+ path_cleanup(&path);
+ return NULL;
+ }
+
+ path_cleanup(&path);
+ return PyLong_FromUnsignedLongLong(id);
+}
diff --git a/libbtrfsutil/python/tests/test_subvolume.py b/libbtrfsutil/python/tests/test_subvolume.py
new file mode 100644
index 00000000..44b1d7f0
--- /dev/null
+++ b/libbtrfsutil/python/tests/test_subvolume.py
@@ -0,0 +1,57 @@
+# Copyright (C) 2018 Facebook
+#
+# This file is part of libbtrfsutil.
+#
+# libbtrfsutil is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# libbtrfsutil is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
+
+import fcntl
+import errno
+import os
+import os.path
+from pathlib import PurePath
+import traceback
+
+import btrfsutil
+from tests import BtrfsTestCase
+
+
+class TestSubvolume(BtrfsTestCase):
+ def test_is_subvolume(self):
+ dir = os.path.join(self.mountpoint, 'foo')
+ os.mkdir(dir)
+
+ for arg in self.path_or_fd(self.mountpoint):
+ with self.subTest(type=type(arg)):
+ self.assertTrue(btrfsutil.is_subvolume(arg))
+ for arg in self.path_or_fd(dir):
+ with self.subTest(type=type(arg)):
+ self.assertFalse(btrfsutil.is_subvolume(arg))
+
+ with self.assertRaises(btrfsutil.BtrfsUtilError) as e:
+ btrfsutil.is_subvolume(os.path.join(self.mountpoint, 'bar'))
+ # This is a bit of an implementation detail, but really this is testing
+ # that the exception is initialized correctly.
+ self.assertEqual(e.exception.btrfsutilerror, btrfsutil.ERROR_STATFS_FAILED)
+ self.assertEqual(e.exception.errno, errno.ENOENT)
+
+ def test_subvolume_id(self):
+ dir = os.path.join(self.mountpoint, 'foo')
+ os.mkdir(dir)
+
+ for arg in self.path_or_fd(self.mountpoint):
+ with self.subTest(type=type(arg)):
+ self.assertEqual(btrfsutil.subvolume_id(arg), 5)
+ for arg in self.path_or_fd(dir):
+ with self.subTest(type=type(arg)):
+ self.assertEqual(btrfsutil.subvolume_id(arg), 5)