summaryrefslogtreecommitdiff
path: root/libbtrfsutil/python
diff options
context:
space:
mode:
authorOmar Sandoval <osandov@fb.com>2018-01-18 13:09:32 -0800
committerDavid Sterba <dsterba@suse.com>2018-02-24 01:37:17 +0100
commitbad4208da33f3ef2adf9af2b06a597f2ce23d228 (patch)
tree691e65ddd839a3cb80fb79f288a4eb55b896f007 /libbtrfsutil/python
parent23c01b3c1b0803950657f3f29914cd59f3554a84 (diff)
libbtrfsutil: add qgroup inheritance helpers
We want to hide struct btrfs_qgroup_inherit from the user because that comes from the Btrfs UAPI headers. Instead, wrap it in a struct btrfs_util_qgroup_inherit and provide helpers to manipulate it. This will be used for subvolume and snapshot creation. 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.h6
-rw-r--r--libbtrfsutil/python/module.c8
-rw-r--r--libbtrfsutil/python/qgroup.c154
-rwxr-xr-xlibbtrfsutil/python/setup.py1
-rw-r--r--libbtrfsutil/python/tests/test_qgroup.py36
5 files changed, 205 insertions, 0 deletions
diff --git a/libbtrfsutil/python/btrfsutilpy.h b/libbtrfsutil/python/btrfsutilpy.h
index 6d82f7e1..3b5d7849 100644
--- a/libbtrfsutil/python/btrfsutilpy.h
+++ b/libbtrfsutil/python/btrfsutilpy.h
@@ -29,7 +29,13 @@
#include <btrfsutil.h>
+typedef struct {
+ PyObject_HEAD
+ struct btrfs_util_qgroup_inherit *inherit;
+} QgroupInherit;
+
extern PyTypeObject BtrfsUtilError_type;
+extern PyTypeObject QgroupInherit_type;
/*
* Helpers for path arguments based on posixmodule.c in CPython.
diff --git a/libbtrfsutil/python/module.c b/libbtrfsutil/python/module.c
index d7398808..24d962dc 100644
--- a/libbtrfsutil/python/module.c
+++ b/libbtrfsutil/python/module.c
@@ -152,6 +152,10 @@ PyInit_btrfsutil(void)
if (PyType_Ready(&BtrfsUtilError_type) < 0)
return NULL;
+ QgroupInherit_type.tp_new = PyType_GenericNew;
+ if (PyType_Ready(&QgroupInherit_type) < 0)
+ return NULL;
+
m = PyModule_Create(&btrfsutilmodule);
if (!m)
return NULL;
@@ -160,6 +164,10 @@ PyInit_btrfsutil(void)
PyModule_AddObject(m, "BtrfsUtilError",
(PyObject *)&BtrfsUtilError_type);
+ Py_INCREF(&QgroupInherit_type);
+ PyModule_AddObject(m, "QgroupInherit",
+ (PyObject *)&QgroupInherit_type);
+
add_module_constants(m);
return m;
diff --git a/libbtrfsutil/python/qgroup.c b/libbtrfsutil/python/qgroup.c
new file mode 100644
index 00000000..69716d92
--- /dev/null
+++ b/libbtrfsutil/python/qgroup.c
@@ -0,0 +1,154 @@
+/*
+ * 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"
+
+static void QgroupInherit_dealloc(QgroupInherit *self)
+{
+ btrfs_util_destroy_qgroup_inherit(self->inherit);
+ Py_TYPE(self)->tp_free((PyObject *)self);
+}
+
+static int QgroupInherit_init(QgroupInherit *self, PyObject *args,
+ PyObject *kwds)
+{
+ static char *keywords[] = {NULL};
+ enum btrfs_util_error err;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, ":QgroupInherit",
+ keywords))
+ return -1;
+
+ err = btrfs_util_create_qgroup_inherit(0, &self->inherit);
+ if (err) {
+ SetFromBtrfsUtilError(err);
+ return -1;
+ }
+
+ return 0;
+}
+
+static PyObject *QgroupInherit_getattro(QgroupInherit *self, PyObject *nameobj)
+{
+ const char *name = "";
+
+ if (PyUnicode_Check(nameobj)) {
+ name = PyUnicode_AsUTF8(nameobj);
+ if (!name)
+ return NULL;
+ }
+
+ if (strcmp(name, "groups") == 0) {
+ PyObject *ret, *tmp;
+ const uint64_t *arr;
+ size_t n, i;
+
+ btrfs_util_qgroup_inherit_get_groups(self->inherit, &arr, &n);
+ ret = PyList_New(n);
+ if (!ret)
+ return NULL;
+
+ for (i = 0; i < n; i++) {
+ tmp = PyLong_FromUnsignedLongLong(arr[i]);
+ if (!tmp) {
+ Py_DECREF(ret);
+ return NULL;
+ }
+ PyList_SET_ITEM(ret, i, tmp);
+ }
+
+ return ret;
+ } else {
+ return PyObject_GenericGetAttr((PyObject *)self, nameobj);
+ }
+}
+
+static PyObject *QgroupInherit_add_group(QgroupInherit *self, PyObject *args,
+ PyObject *kwds)
+{
+ static char *keywords[] = {"qgroupid", NULL};
+ enum btrfs_util_error err;
+ uint64_t qgroupid;
+
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "K:add_group", keywords,
+ &qgroupid))
+ return NULL;
+
+ err = btrfs_util_qgroup_inherit_add_group(&self->inherit, qgroupid);
+ if (err) {
+ SetFromBtrfsUtilError(err);
+ return NULL;
+ }
+
+ Py_RETURN_NONE;
+}
+
+static PyMethodDef QgroupInherit_methods[] = {
+ {"add_group", (PyCFunction)QgroupInherit_add_group,
+ METH_VARARGS | METH_KEYWORDS,
+ "add_group(qgroupid)\n\n"
+ "Add a qgroup to inherit from.\n\n"
+ "Arguments:\n"
+ "qgroupid -- ID of qgroup to add"},
+ {},
+};
+
+#define QgroupInherit_DOC \
+ "QgroupInherit() -> new qgroup inheritance specifier\n\n" \
+ "Create a new object which specifies what qgroups to inherit\n" \
+ "from for create_subvolume() and create_snapshot()"
+
+PyTypeObject QgroupInherit_type = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "btrfsutil.QgroupInherit", /* tp_name */
+ sizeof(QgroupInherit), /* tp_basicsize */
+ 0, /* tp_itemsize */
+ (destructor)QgroupInherit_dealloc, /* tp_dealloc */
+ NULL, /* tp_print */
+ NULL, /* tp_getattr */
+ NULL, /* tp_setattr */
+ NULL, /* tp_as_async */
+ NULL, /* tp_repr */
+ NULL, /* tp_as_number */
+ NULL, /* tp_as_sequence */
+ NULL, /* tp_as_mapping */
+ NULL, /* tp_hash */
+ NULL, /* tp_call */
+ NULL, /* tp_str */
+ (getattrofunc)QgroupInherit_getattro, /* tp_getattro */
+ NULL, /* tp_setattro */
+ NULL, /* tp_as_buffer */
+ Py_TPFLAGS_DEFAULT, /* tp_flags */
+ QgroupInherit_DOC, /* tp_doc */
+ NULL, /* tp_traverse */
+ NULL, /* tp_clear */
+ NULL, /* tp_richcompare */
+ 0, /* tp_weaklistoffset */
+ NULL, /* tp_iter */
+ NULL, /* tp_iternext */
+ QgroupInherit_methods, /* tp_methods */
+ NULL, /* tp_members */
+ NULL, /* tp_getset */
+ NULL, /* tp_base */
+ NULL, /* tp_dict */
+ NULL, /* tp_descr_get */
+ NULL, /* tp_descr_set */
+ 0, /* tp_dictoffset */
+ (initproc)QgroupInherit_init, /* tp_init */
+};
diff --git a/libbtrfsutil/python/setup.py b/libbtrfsutil/python/setup.py
index 6f04a6fe..478510ce 100755
--- a/libbtrfsutil/python/setup.py
+++ b/libbtrfsutil/python/setup.py
@@ -91,6 +91,7 @@ module = Extension(
'constants.c',
'error.c',
'module.c',
+ 'qgroup.c',
],
include_dirs=['..'],
library_dirs=['../..'],
diff --git a/libbtrfsutil/python/tests/test_qgroup.py b/libbtrfsutil/python/tests/test_qgroup.py
new file mode 100644
index 00000000..a590464b
--- /dev/null
+++ b/libbtrfsutil/python/tests/test_qgroup.py
@@ -0,0 +1,36 @@
+# 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 os
+import unittest
+
+import btrfsutil
+
+
+class TestQgroupInherit(unittest.TestCase):
+ def test_new(self):
+ inherit = btrfsutil.QgroupInherit()
+ self.assertEqual(inherit.groups, [])
+
+ def test_add_group(self):
+ inherit = btrfsutil.QgroupInherit()
+ inherit.add_group(1)
+ self.assertEqual(inherit.groups, [1])
+ inherit.add_group(2)
+ self.assertEqual(inherit.groups, [1, 2])
+ inherit.add_group(3)
+ self.assertEqual(inherit.groups, [1, 2, 3])