6 staticforward PyTypeObject ACLType;
7 static PyObject* ACL_applyto(PyObject* obj, PyObject* args);
8 static PyObject* ACL_valid(PyObject* obj, PyObject* args);
10 static PyObject* ACL_get_state(PyObject *obj, PyObject* args);
11 static PyObject* ACL_set_state(PyObject *obj, PyObject* args);
19 /* ACL type methods */
20 static PyMethodDef ACL_methods[] = {
21 {"applyto", ACL_applyto, METH_VARARGS, "Apply the ACL to a file or filehandle."},
22 {"valid", ACL_valid, METH_NOARGS, "Test the ACL for validity."},
24 {"__getstate__", ACL_get_state, METH_NOARGS, "Dumps the ACL to an external format."},
25 {"__setstate__", ACL_set_state, METH_VARARGS, "Loads the ACL from an external format."},
30 /* Creation of a new ACL instance */
31 static PyObject* ACL_new(PyObject* self, PyObject* args, PyObject *keywds) {
33 static char *kwlist[] = { "file", "fd", "text", "acl", NULL };
37 ACLObject* thesrc = NULL;
40 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sisO!", kwlist,
41 &file, &fd, &text, &ACLType, &thesrc))
53 PyErr_SetString(PyExc_ValueError, "a maximum of one argument must be passed");
57 theacl = PyObject_New(ACLObject, &ACLType);
59 theacl->ob_acl = acl_get_file(file, ACL_TYPE_ACCESS);
61 theacl->ob_acl = acl_from_text(text);
63 theacl->ob_acl = acl_get_fd(fd);
64 else if(thesrc != NULL)
65 theacl->ob_acl = acl_dup(thesrc->ob_acl);
67 theacl->ob_acl = acl_init(0);
68 if(theacl->ob_acl == NULL) {
70 return PyErr_SetFromErrno(PyExc_IOError);
73 return (PyObject*)theacl;
76 /* Standard type functions */
77 static void ACL_dealloc(PyObject* obj) {
78 ACLObject *self = (ACLObject*) obj;
79 PyObject *err_type, *err_value, *err_traceback;
80 int have_error = PyErr_Occurred() ? 1 : 0;
83 PyErr_Fetch(&err_type, &err_value, &err_traceback);
84 if(acl_free(self->ob_acl) != 0)
85 PyErr_WriteUnraisable(obj);
87 PyErr_Restore(err_type, err_value, err_traceback);
91 /* Converts the acl to a text format */
92 static PyObject* ACL_repr(PyObject *obj) {
94 ACLObject *self = (ACLObject*) obj;
97 text = acl_to_text(self->ob_acl, NULL);
99 return PyErr_SetFromErrno(PyExc_IOError);
101 ret = PyString_FromString(text);
102 if(acl_free(text) != 0) {
104 return PyErr_SetFromErrno(PyExc_IOError);
110 /* Applyes the ACL to a file */
111 static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
112 ACLObject *self = (ACLObject*) obj;
114 int type_default = 0;
115 acl_type_t type = ACL_TYPE_ACCESS;
119 if (!PyArg_ParseTuple(args, "O|i", &myarg, &type_default))
122 type = ACL_TYPE_DEFAULT;
124 if(PyString_Check(myarg)) {
125 char *filename = PyString_AS_STRING(myarg);
126 nret = acl_set_file(filename, type, self->ob_acl);
127 } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
128 nret = acl_set_fd(fd, self->ob_acl);
130 PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int, or file-like object");
134 return PyErr_SetFromErrno(PyExc_IOError);
137 /* Return the result */
142 /* Checks the ACL for validity */
143 static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
144 ACLObject *self = (ACLObject*) obj;
146 if(acl_valid(self->ob_acl) == -1) {
147 return PyErr_SetFromErrno(PyExc_IOError);
150 /* Return the result */
157 static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
158 ACLObject *self = (ACLObject*) obj;
163 size = acl_size(self->ob_acl);
165 return PyErr_SetFromErrno(PyExc_IOError);
167 if((ret = PyString_FromStringAndSize(NULL, size)) == NULL)
169 buf = PyString_AsString(ret);
171 if((nsize = acl_copy_ext(buf, self->ob_acl, size)) == -1) {
173 return PyErr_SetFromErrno(PyExc_IOError);
179 static PyObject* ACL_set_state(PyObject *obj, PyObject* args) {
180 ACLObject *self = (ACLObject*) obj;
185 /* Parse the argument */
186 if (!PyArg_ParseTuple(args, "s#", &buf, &bufsize))
189 /* Try to import the external representation */
190 if((ptr = acl_copy_int(buf)) == NULL)
191 return PyErr_SetFromErrno(PyExc_IOError);
193 /* Free the old acl. Should we ignore errors here? */
194 if(self->ob_acl != NULL) {
195 if(acl_free(self->ob_acl) == -1)
196 return PyErr_SetFromErrno(PyExc_IOError);
201 /* Return the result */
208 /* The definition of the ACL Type */
209 static PyTypeObject ACLType = {
210 PyObject_HEAD_INIT(NULL)
215 ACL_dealloc,/*tp_dealloc*/
220 ACL_repr, /*tp_repr*/
222 0, /*tp_as_sequence*/
231 "Type which represents a POSIX ACL", /*tp_doc*/
234 0, /*tp_richcompare*/
235 0, /*tp_weaklistoffset*/
238 ACL_methods, /*tp_methods*/
243 /* Deletes the default ACL from a directory */
244 static PyObject* aclmodule_delete_default(PyObject* obj, PyObject* args) {
247 /* Parse the arguments */
248 if (!PyArg_ParseTuple(args, "s", &filename))
251 if(acl_delete_def_file(filename) == -1) {
252 return PyErr_SetFromErrno(PyExc_IOError);
255 /* Return the result */
260 /* The module methods */
261 static PyMethodDef aclmodule_methods[] = {
262 {"ACL", (PyCFunction)ACL_new, METH_VARARGS|METH_KEYWORDS, "Create a new ACL object."},
263 {"delete_default", aclmodule_delete_default,
264 METH_VARARGS, "Delete the default ACL from a directory."},
265 {NULL, NULL, 0, NULL}
268 DL_EXPORT(void) initposixacl(void) {
269 ACLType.ob_type = &PyType_Type;
271 if(PyType_Ready(&ACLType) < 0)
273 Py_InitModule("posixacl", aclmodule_methods);