libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id()
[platform/upstream/btrfs-progs.git] / libbtrfsutil / python / qgroup.c
1 /*
2  * Copyright (C) 2018 Facebook
3  *
4  * This file is part of libbtrfsutil.
5  *
6  * libbtrfsutil is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * libbtrfsutil is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with libbtrfsutil.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "btrfsutilpy.h"
21
22 static void QgroupInherit_dealloc(QgroupInherit *self)
23 {
24         btrfs_util_destroy_qgroup_inherit(self->inherit);
25         Py_TYPE(self)->tp_free((PyObject *)self);
26 }
27
28 static int QgroupInherit_init(QgroupInherit *self, PyObject *args,
29                               PyObject *kwds)
30 {
31         static char *keywords[] = {NULL};
32         enum btrfs_util_error err;
33
34         if (!PyArg_ParseTupleAndKeywords(args, kwds, ":QgroupInherit",
35                                          keywords))
36                 return -1;
37
38         err = btrfs_util_create_qgroup_inherit(0, &self->inherit);
39         if (err) {
40                 SetFromBtrfsUtilError(err);
41                 return -1;
42         }
43
44         return 0;
45 }
46
47 static PyObject *QgroupInherit_getattro(QgroupInherit *self, PyObject *nameobj)
48 {
49     const char *name = "";
50
51     if (PyUnicode_Check(nameobj)) {
52             name = PyUnicode_AsUTF8(nameobj);
53             if (!name)
54                     return NULL;
55     }
56
57     if (strcmp(name, "groups") == 0) {
58             PyObject *ret, *tmp;
59             const uint64_t *arr;
60             size_t n, i;
61
62             btrfs_util_qgroup_inherit_get_groups(self->inherit, &arr, &n);
63             ret = PyList_New(n);
64             if (!ret)
65                     return NULL;
66
67             for (i = 0; i < n; i++) {
68                     tmp = PyLong_FromUnsignedLongLong(arr[i]);
69                     if (!tmp) {
70                             Py_DECREF(ret);
71                             return NULL;
72                     }
73                     PyList_SET_ITEM(ret, i, tmp);
74             }
75
76             return ret;
77     } else {
78             return PyObject_GenericGetAttr((PyObject *)self, nameobj);
79     }
80 }
81
82 static PyObject *QgroupInherit_add_group(QgroupInherit *self, PyObject *args,
83                                          PyObject *kwds)
84 {
85         static char *keywords[] = {"qgroupid", NULL};
86         enum btrfs_util_error err;
87         uint64_t qgroupid;
88
89         if (!PyArg_ParseTupleAndKeywords(args, kwds, "K:add_group", keywords,
90                                          &qgroupid))
91                 return NULL;
92
93         err = btrfs_util_qgroup_inherit_add_group(&self->inherit, qgroupid);
94         if (err) {
95                 SetFromBtrfsUtilError(err);
96                 return NULL;
97         }
98
99         Py_RETURN_NONE;
100 }
101
102 static PyMethodDef QgroupInherit_methods[] = {
103         {"add_group", (PyCFunction)QgroupInherit_add_group,
104          METH_VARARGS | METH_KEYWORDS,
105          "add_group(qgroupid)\n\n"
106          "Add a qgroup to inherit from.\n\n"
107          "Arguments:\n"
108          "qgroupid -- ID of qgroup to add"},
109         {},
110 };
111
112 #define QgroupInherit_DOC       \
113         "QgroupInherit() -> new qgroup inheritance specifier\n\n"       \
114         "Create a new object which specifies what qgroups to inherit\n" \
115         "from for create_subvolume() and create_snapshot()"
116
117 PyTypeObject QgroupInherit_type = {
118         PyVarObject_HEAD_INIT(NULL, 0)
119         "btrfsutil.QgroupInherit",              /* tp_name */
120         sizeof(QgroupInherit),                  /* tp_basicsize */
121         0,                                      /* tp_itemsize */
122         (destructor)QgroupInherit_dealloc,      /* tp_dealloc */
123         NULL,                                   /* tp_print */
124         NULL,                                   /* tp_getattr */
125         NULL,                                   /* tp_setattr */
126         NULL,                                   /* tp_as_async */
127         NULL,                                   /* tp_repr */
128         NULL,                                   /* tp_as_number */
129         NULL,                                   /* tp_as_sequence */
130         NULL,                                   /* tp_as_mapping */
131         NULL,                                   /* tp_hash  */
132         NULL,                                   /* tp_call */
133         NULL,                                   /* tp_str */
134         (getattrofunc)QgroupInherit_getattro,   /* tp_getattro */
135         NULL,                                   /* tp_setattro */
136         NULL,                                   /* tp_as_buffer */
137         Py_TPFLAGS_DEFAULT,                     /* tp_flags */
138         QgroupInherit_DOC,                      /* tp_doc */
139         NULL,                                   /* tp_traverse */
140         NULL,                                   /* tp_clear */
141         NULL,                                   /* tp_richcompare */
142         0,                                      /* tp_weaklistoffset */
143         NULL,                                   /* tp_iter */
144         NULL,                                   /* tp_iternext */
145         QgroupInherit_methods,                  /* tp_methods */
146         NULL,                                   /* tp_members */
147         NULL,                                   /* tp_getset */
148         NULL,                                   /* tp_base */
149         NULL,                                   /* tp_dict */
150         NULL,                                   /* tp_descr_get */
151         NULL,                                   /* tp_descr_set */
152         0,                                      /* tp_dictoffset */
153         (initproc)QgroupInherit_init,           /* tp_init */
154 };