]> git.ipfire.org Git - pakfire.git/blob - src/_pakfire/capabilities.c
Cleanup database and add indexes.
[pakfire.git] / src / _pakfire / capabilities.c
1 /*#############################################################################
2 # #
3 # Pakfire - The IPFire package management system #
4 # Copyright (C) 2011 Pakfire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU 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 # This program 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 General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #include <Python.h>
22
23 #include <errno.h>
24 #include <sys/capability.h>
25
26 #include "constants.h"
27
28 PyObject *
29 get_capabilities(PyObject *self, PyObject *args) {
30 const char *filename;
31 cap_t cap_d;
32 char exception[STRING_SIZE];
33
34 if (!PyArg_ParseTuple(args, "s", &filename)) {
35 /* XXX raise exception */
36 return NULL;
37 }
38
39 cap_d = cap_get_file(filename);
40 if (cap_d == NULL) {
41 if (errno == EOPNOTSUPP) {
42 /* Return nothing, if the operation is not supported. */
43 Py_RETURN_NONE;
44 } else if (errno != ENODATA) {
45 snprintf(exception, STRING_SIZE - 1, "Failed to get capabilities of file %s (%s).",
46 filename, strerror(errno));
47 PyErr_SetString(PyExc_RuntimeError, exception);
48 return NULL;
49 }
50 Py_RETURN_NONE;
51 }
52
53 char *result = cap_to_text(cap_d, NULL);
54 cap_free(cap_d);
55
56 if (!result) {
57 snprintf(exception, STRING_SIZE - 1, "Failed to get capabilities of human readable format at %s (%s).",
58 filename, strerror(errno));
59 PyErr_SetString(PyExc_RuntimeError, exception);
60 return NULL;
61 }
62
63 // Remove leading two characters '= '.
64 int i;
65 for (i = 0; i < 2; i++) {
66 result++;
67 }
68
69 PyObject * ret = Py_BuildValue("s", result);
70 cap_free(result);
71
72 return ret;
73 }
74
75 PyObject *
76 set_capabilities(PyObject *self, PyObject *args) {
77 const char *filename;
78 const char *input;
79 char *exception = NULL;
80 char buf[STRING_SIZE];
81 cap_t cap_d;
82 int ret;
83
84 if (!PyArg_ParseTuple(args, "ss", &filename, &input)) {
85 /* XXX raise exception */
86 return NULL;
87 }
88
89 snprintf(buf, STRING_SIZE - 1, "= %s", input);
90 cap_d = cap_from_text(buf);
91 if (cap_d == NULL) {
92 PyErr_SetString(PyExc_ValueError, "Could not read capability string.");
93 return NULL;
94 }
95
96 ret = cap_set_file(filename, cap_d);
97 cap_free(cap_d);
98
99 if (ret != 0) {
100 snprintf(exception, STRING_SIZE - 1, "Failed to set capabilities on file %s (%s).",
101 filename, strerror(errno));
102 PyErr_SetString(PyExc_RuntimeError, exception);
103 return NULL;
104 }
105
106 return Py_BuildValue("i", ret);
107 }