]> git.k1024.org Git - pylibacl.git/blob - acl.c
Initial revision
[pylibacl.git] / acl.c
1 #include <sys/types.h>
2 #include <sys/acl.h>
3
4 #include <Python.h>
5
6 staticforward PyTypeObject ACLType;
7
8 typedef struct {
9     PyObject_HEAD
10     acl_t ob_acl;
11 } ACLObject;
12
13 static PyObject* new_ACL(PyObject* self, PyObject* args) {
14     ACLObject* theacl;
15
16     if (!PyArg_ParseTuple(args,"")) 
17         return NULL;
18
19     theacl = PyObject_New(ACLObject, &ACLType);
20     theacl->ob_acl = acl_init(0);
21     if(theacl->ob_acl == NULL) {
22         Py_DECREF(theacl);
23         return PyErr_SetFromErrno(PyExc_IOError);
24     }
25
26     return (PyObject*)theacl;
27 }
28
29 static void ACL_dealloc(PyObject* obj) {
30     ACLObject *self = (ACLObject*) obj;
31     PyObject *err_type, *err_value, *err_traceback;
32     int have_error = PyErr_Occurred() ? 1 : 0;
33
34     if (have_error)
35         PyErr_Fetch(&err_type, &err_value, &err_traceback);
36     if(acl_free(self->ob_acl) != 0)
37         PyErr_WriteUnraisable(obj);
38     if (have_error)
39         PyErr_Restore(err_type, err_value, err_traceback);
40     PyObject_DEL(self);
41 }
42
43 static PyObject* ACL_repr(PyObject *obj) {
44     char *text;
45     ACLObject *self = (ACLObject*) obj;
46     PyObject *ret;
47
48     text = acl_to_text(self->ob_acl, NULL);
49     if(text == NULL) {
50         return PyErr_SetFromErrno(PyExc_IOError);
51     }
52     ret = PyString_FromString(text);
53     if(acl_free(text) != 0) {
54         Py_DECREF(ret);
55         return PyErr_SetFromErrno(PyExc_IOError);
56     }
57     return ret;
58 }
59
60 static PyTypeObject ACLType = {
61     PyObject_HEAD_INIT(NULL)
62     0,
63     "ACL",
64     sizeof(ACLObject),
65     0,
66     ACL_dealloc, /*tp_dealloc*/
67     0,          /*tp_print*/
68     0,          /*tp_getattr*/
69     0,          /*tp_setattr*/
70     0,          /*tp_compare*/
71     ACL_repr,   /*tp_repr*/
72     0,          /*tp_as_number*/
73     0,          /*tp_as_sequence*/
74     0,          /*tp_as_mapping*/
75     0,          /*tp_hash */
76 };
77
78 static PyMethodDef acl_methods[] = {
79     {"ACL", new_ACL, METH_VARARGS, "Create a new ACL object."},
80     {NULL, NULL, 0, NULL}
81 };
82
83 DL_EXPORT(void) initacl(void) {
84     ACLType.ob_type = &PyType_Type;
85
86     Py_InitModule("acl", acl_methods);
87 }