libbtrfsutil: add btrfs_util_deleted_subvolumes()
[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             const uint64_t *arr;
59             size_t n;
60
61             btrfs_util_qgroup_inherit_get_groups(self->inherit, &arr, &n);
62
63             return list_from_uint64_array(arr, n);
64     } else {
65             return PyObject_GenericGetAttr((PyObject *)self, nameobj);
66     }
67 }
68
69 static PyObject *QgroupInherit_add_group(QgroupInherit *self, PyObject *args,
70                                          PyObject *kwds)
71 {
72         static char *keywords[] = {"qgroupid", NULL};
73         enum btrfs_util_error err;
74         uint64_t qgroupid;
75
76         if (!PyArg_ParseTupleAndKeywords(args, kwds, "K:add_group", keywords,
77                                          &qgroupid))
78                 return NULL;
79
80         err = btrfs_util_qgroup_inherit_add_group(&self->inherit, qgroupid);
81         if (err) {
82                 SetFromBtrfsUtilError(err);
83                 return NULL;
84         }
85
86         Py_RETURN_NONE;
87 }
88
89 static PyMethodDef QgroupInherit_methods[] = {
90         {"add_group", (PyCFunction)QgroupInherit_add_group,
91          METH_VARARGS | METH_KEYWORDS,
92          "add_group(qgroupid)\n\n"
93          "Add a qgroup to inherit from.\n\n"
94          "Arguments:\n"
95          "qgroupid -- ID of qgroup to add"},
96         {},
97 };
98
99 #define QgroupInherit_DOC       \
100         "QgroupInherit() -> new qgroup inheritance specifier\n\n"       \
101         "Create a new object which specifies what qgroups to inherit\n" \
102         "from for create_subvolume() and create_snapshot()"
103
104 PyTypeObject QgroupInherit_type = {
105         PyVarObject_HEAD_INIT(NULL, 0)
106         "btrfsutil.QgroupInherit",              /* tp_name */
107         sizeof(QgroupInherit),                  /* tp_basicsize */
108         0,                                      /* tp_itemsize */
109         (destructor)QgroupInherit_dealloc,      /* tp_dealloc */
110         NULL,                                   /* tp_print */
111         NULL,                                   /* tp_getattr */
112         NULL,                                   /* tp_setattr */
113         NULL,                                   /* tp_as_async */
114         NULL,                                   /* tp_repr */
115         NULL,                                   /* tp_as_number */
116         NULL,                                   /* tp_as_sequence */
117         NULL,                                   /* tp_as_mapping */
118         NULL,                                   /* tp_hash  */
119         NULL,                                   /* tp_call */
120         NULL,                                   /* tp_str */
121         (getattrofunc)QgroupInherit_getattro,   /* tp_getattro */
122         NULL,                                   /* tp_setattro */
123         NULL,                                   /* tp_as_buffer */
124         Py_TPFLAGS_DEFAULT,                     /* tp_flags */
125         QgroupInherit_DOC,                      /* tp_doc */
126         NULL,                                   /* tp_traverse */
127         NULL,                                   /* tp_clear */
128         NULL,                                   /* tp_richcompare */
129         0,                                      /* tp_weaklistoffset */
130         NULL,                                   /* tp_iter */
131         NULL,                                   /* tp_iternext */
132         QgroupInherit_methods,                  /* tp_methods */
133         NULL,                                   /* tp_members */
134         NULL,                                   /* tp_getset */
135         NULL,                                   /* tp_base */
136         NULL,                                   /* tp_dict */
137         NULL,                                   /* tp_descr_get */
138         NULL,                                   /* tp_descr_set */
139         0,                                      /* tp_dictoffset */
140         (initproc)QgroupInherit_init,           /* tp_init */
141 };