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);
18 /* ACL type methods */
19 static PyMethodDef ACL_methods[] = {
20 {"applyto", ACL_applyto, METH_VARARGS, "Apply the ACL to a file or filehandle."},
21 {"valid", ACL_valid, METH_NOARGS, "Test the ACL for validity."},
23 {"__getstate__", ACL_get_state, METH_NOARGS, "Dumps the ACL to an external format."},
28 /* Creation of a new ACL instance */
29 static PyObject* ACL_new(PyObject* self, PyObject* args, PyObject *keywds) {
31 static char *kwlist[] = { "file", "fd", "text", "acl", NULL };
35 ACLObject* thesrc = NULL;
38 if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sisO!", kwlist,
39 &file, &fd, &text, &ACLType, &thesrc))
51 PyErr_SetString(PyExc_ValueError, "a maximum of one argument must be passed");
55 theacl = PyObject_New(ACLObject, &ACLType);
57 theacl->ob_acl = acl_get_file(file, ACL_TYPE_ACCESS);
59 theacl->ob_acl = acl_from_text(text);
61 theacl->ob_acl = acl_get_fd(fd);
62 else if(thesrc != NULL)
63 theacl->ob_acl = acl_dup(thesrc->ob_acl);
65 theacl->ob_acl = acl_init(0);
66 if(theacl->ob_acl == NULL) {
68 return PyErr_SetFromErrno(PyExc_IOError);
71 return (PyObject*)theacl;
74 /* Standard type functions */
75 static void ACL_dealloc(PyObject* obj) {
76 ACLObject *self = (ACLObject*) obj;
77 PyObject *err_type, *err_value, *err_traceback;
78 int have_error = PyErr_Occurred() ? 1 : 0;
81 PyErr_Fetch(&err_type, &err_value, &err_traceback);
82 if(acl_free(self->ob_acl) != 0)
83 PyErr_WriteUnraisable(obj);
85 PyErr_Restore(err_type, err_value, err_traceback);
89 /* Converts the acl to a text format */
90 static PyObject* ACL_repr(PyObject *obj) {
92 ACLObject *self = (ACLObject*) obj;
95 text = acl_to_text(self->ob_acl, NULL);
97 return PyErr_SetFromErrno(PyExc_IOError);
99 ret = PyString_FromString(text);
100 if(acl_free(text) != 0) {
102 return PyErr_SetFromErrno(PyExc_IOError);
108 /* Applyes the ACL to a file */
109 static PyObject* ACL_applyto(PyObject* obj, PyObject* args) {
110 ACLObject *self = (ACLObject*) obj;
112 int type_default = 0;
113 acl_type_t type = ACL_TYPE_ACCESS;
117 if (!PyArg_ParseTuple(args, "O|i", &myarg, &type_default))
120 type = ACL_TYPE_DEFAULT;
122 if(PyString_Check(myarg)) {
123 char *filename = PyString_AS_STRING(myarg);
124 nret = acl_set_file(filename, type, self->ob_acl);
125 } else if((fd = PyObject_AsFileDescriptor(myarg)) != -1) {
126 nret = acl_set_fd(fd, self->ob_acl);
128 PyErr_SetString(PyExc_TypeError, "argument 1 must be string, int, or file-like object");
132 return PyErr_SetFromErrno(PyExc_IOError);
135 /* Return the result */
140 /* Checks the ACL for validity */
141 static PyObject* ACL_valid(PyObject* obj, PyObject* args) {
142 ACLObject *self = (ACLObject*) obj;
144 if(acl_valid(self->ob_acl) == -1) {
145 return PyErr_SetFromErrno(PyExc_IOError);
148 /* Return the result */
155 static PyObject* ACL_get_state(PyObject *obj, PyObject* args) {
156 ACLObject *self = (ACLObject*) obj;
161 size = acl_size(self->ob_acl);
163 return PyErr_SetFromErrno(PyExc_IOError);
165 if((ret = PyString_FromStringAndSize(NULL, size)) == NULL)
167 buf = PyString_AsString(ret);
169 if((nsize = acl_copy_ext(buf, self->ob_acl, size)) == -1) {
171 return PyErr_SetFromErrno(PyExc_IOError);
179 /* The definition of the ACL Type */
180 static PyTypeObject ACLType = {
181 PyObject_HEAD_INIT(NULL)
186 ACL_dealloc,/*tp_dealloc*/
191 ACL_repr, /*tp_repr*/
193 0, /*tp_as_sequence*/
202 "Type which represents a POSIX ACL", /*tp_doc*/
205 0, /*tp_richcompare*/
206 0, /*tp_weaklistoffset*/
209 ACL_methods, /*tp_methods*/
214 /* Deletes the default ACL from a directory */
215 static PyObject* aclmodule_delete_default(PyObject* obj, PyObject* args) {
218 /* Parse the arguments */
219 if (!PyArg_ParseTuple(args, "s", &filename))
222 if(acl_delete_def_file(filename) == -1) {
223 return PyErr_SetFromErrno(PyExc_IOError);
226 /* Return the result */
231 /* The module methods */
232 static PyMethodDef aclmodule_methods[] = {
233 {"ACL", (PyCFunction)ACL_new, METH_VARARGS|METH_KEYWORDS, "Create a new ACL object."},
234 {"delete_default", aclmodule_delete_default,
235 METH_VARARGS, "Delete the default ACL from a directory."},
236 {NULL, NULL, 0, NULL}
239 DL_EXPORT(void) initacl(void) {
240 ACLType.ob_type = &PyType_Type;
242 if(PyType_Ready(&ACLType) < 0)
244 Py_InitModule("acl", aclmodule_methods);