2 #include <attr/xattr.h>
5 pygetxattr(PyObject *self, PyObject *args)
13 if (!PyArg_ParseTuple(args, "ss", &file, &attrname))
16 if((nalloc = getxattr(file, attrname, NULL, 0)) == -1) {
17 return PyErr_SetFromErrno(PyExc_IOError);
20 if((buf = PyMem_Malloc(nalloc)) == NULL) {
25 if((nret = getxattr(file, attrname, buf, nalloc)) == -1) {
26 return PyErr_SetFromErrno(PyExc_IOError);
29 res = PyString_FromStringAndSize(buf, nret);
35 pysetxattr(PyObject *self, PyObject *args)
44 if (!PyArg_ParseTuple(args, "sss#|b", &file, &attrname, &buf, &bufsize, &mode))
47 flags = mode == 1 ? XATTR_CREATE : mode == 2 ? XATTR_REPLACE : 0;
48 if((nret = setxattr(file, attrname, buf, bufsize, flags)) == -1) {
49 return PyErr_SetFromErrno(PyExc_IOError);
57 pyremovexattr(PyObject *self, PyObject *args)
62 if (!PyArg_ParseTuple(args, "ss", &file, &attrname))
65 if(removexattr(file, attrname) == -1) {
66 return PyErr_SetFromErrno(PyExc_IOError);
74 pylistxattr(PyObject *self, PyObject *args)
83 /* Parse the arguments */
84 if (!PyArg_ParseTuple(args, "s", &file))
87 /* Find out the needed size of the buffer */
88 if((nalloc = listxattr(file, NULL, 0)) == -1) {
89 return PyErr_SetFromErrno(PyExc_IOError);
92 /* Try to allocate the memory, using Python's allocator */
93 if((buf = PyMem_Malloc(nalloc)) == NULL) {
98 /* Now retrieve the list of attributes */
99 if((nret = listxattr(file, buf, nalloc)) == -1) {
100 return PyErr_SetFromErrno(PyExc_IOError);
103 /* Compute the number of attributes in the list */
104 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
108 /* Create the tuple which will hold the result */
109 mytuple = PyTuple_New(nattrs);
111 /* Create and insert the attributes as strings in the tuple */
112 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
113 PyTuple_SET_ITEM(mytuple, nattrs, PyString_FromString(s));
117 /* Free the buffer, now it is no longer needed */
120 /* Return the result */
124 static PyMethodDef AclMethods[] = {
125 {"getxattr", pygetxattr, METH_VARARGS,
126 "Get the value of a given extended attribute."},
127 {"setxattr", pysetxattr, METH_VARARGS,
128 "Set the value of a given extended attribute."},
129 {"removexattr", pyremovexattr, METH_VARARGS,
130 "Remove the a given extended attribute."},
131 {"listxattr", pylistxattr, METH_VARARGS,
132 "Retrieve the list of extened attributes."},
133 {NULL, NULL, 0, NULL} /* Sentinel */
139 (void) Py_InitModule("aclmodule", AclMethods);