2 #include <attr/xattr.h>
4 typedef enum {T_FD, T_PATH, T_LINK} target_e;
14 /** Converts from a string, file or int argument to what we need. */
15 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
17 if(PyString_Check(myobj)) {
18 tgt->type = nofollow ? T_LINK : T_PATH;
19 tgt->name = PyString_AS_STRING(myobj);
20 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
24 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
30 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
32 return flistxattr(tgt->fd, list, size);
33 else if (tgt->type == T_LINK)
34 return llistxattr(tgt->name, list, size);
36 return listxattr(tgt->name, list, size);
39 static ssize_t _get_obj(target_t *tgt, char *name, void *value, size_t size) {
41 return fgetxattr(tgt->fd, name, value, size);
42 else if (tgt->type == T_LINK)
43 return lgetxattr(tgt->name, name, value, size);
45 return getxattr(tgt->name, name, value, size);
48 static ssize_t _set_obj(target_t *tgt, char *name, void *value, size_t size,
51 return fsetxattr(tgt->fd, name, value, size, flags);
52 else if (tgt->type == T_LINK)
53 return lsetxattr(tgt->name, name, value, size, flags);
55 return setxattr(tgt->name, name, value, size, flags);
58 static ssize_t _remove_obj(target_t *tgt, char *name) {
60 return fremovexattr(tgt->fd, name);
61 else if (tgt->type == T_LINK)
62 return lremovexattr(tgt->name, name);
64 return removexattr(tgt->name, name);
67 /* Wrapper for getxattr */
68 static char __pygetxattr_doc__[] =
69 "Get the value of a given extended attribute.\n"
72 " - a string representing filename, or a file-like object,\n"
73 " or a file descriptor; this represents the file on \n"
75 " - a string, representing the attribute whose value to retrieve;\n"
76 " usually in form of system.posix_acl or user.mime_type\n"
77 " - (optional) a boolean value (defaults to false), which, if\n"
78 " the file name given is a symbolic link, makes the\n"
79 " function operate on the symbolic link itself instead\n"
84 pygetxattr(PyObject *self, PyObject *args)
94 /* Parse the arguments */
95 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
97 if(!convertObj(myarg, &tgt, nofollow))
100 /* Find out the needed size of the buffer */
101 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
102 return PyErr_SetFromErrno(PyExc_IOError);
105 /* Try to allocate the memory, using Python's allocator */
106 if((buf = PyMem_Malloc(nalloc)) == NULL) {
111 /* Now retrieve the attribute value */
112 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
114 return PyErr_SetFromErrno(PyExc_IOError);
117 /* Create the string which will hold the result */
118 res = PyString_FromStringAndSize(buf, nret);
120 /* Free the buffer, now it is no longer needed */
123 /* Return the result */
127 static char __pysetxattr_doc__[] =
128 "Set the value of a given extended attribute.\n"
129 "Be carefull in case you want to set attributes on symbolic\n"
130 "links, you have to use all the 5 parameters; use 0 for the \n"
131 "flags value if you want the default behavior (create or "
135 " - a string representing filename, or a file-like object,\n"
136 " or a file descriptor; this represents the file on \n"
138 " - a string, representing the attribute whose value to set;\n"
139 " usually in form of system.posix_acl or user.mime_type\n"
140 " - a string, possibly with embedded NULLs; note that there\n"
141 " are restrictions regarding the size of the value, for\n"
142 " example, for ext2/ext3, maximum size is the block size\n"
143 " - (optional) flags; if 0 or ommited the attribute will be \n"
144 " created or replaced; if XATTR_CREATE, the attribute \n"
145 " will be created, giving an error if it already exists;\n"
146 " of XATTR_REPLACE, the attribute will be replaced,\n"
147 " giving an error if it doesn't exists;\n"
148 " - (optional) a boolean value (defaults to false), which, if\n"
149 " the file name given is a symbolic link, makes the\n"
150 " function operate on the symbolic link itself instead\n"
154 /* Wrapper for setxattr */
156 pysetxattr(PyObject *self, PyObject *args)
166 /* Parse the arguments */
167 if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname,
168 &buf, &bufsize, &flags, &nofollow))
170 if(!convertObj(myarg, &tgt, nofollow))
173 /* Set the attribute's value */
174 if((nret = _set_obj(&tgt, attrname, buf, bufsize, flags)) == -1) {
175 return PyErr_SetFromErrno(PyExc_IOError);
178 /* Return the result */
182 static char __pyremovexattr_doc__[] =
183 "Remove an attribute from a file\n"
186 " - a string representing filename, or a file-like object,\n"
187 " or a file descriptor; this represents the file on \n"
189 " - a string, representing the attribute to be removed;\n"
190 " usually in form of system.posix_acl or user.mime_type\n"
191 " - (optional) a boolean value (defaults to false), which, if\n"
192 " the file name given is a symbolic link, makes the\n"
193 " function operate on the symbolic link itself instead\n"
197 /* Wrapper for removexattr */
199 pyremovexattr(PyObject *self, PyObject *args)
207 /* Parse the arguments */
208 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
211 if(!convertObj(myarg, &tgt, nofollow))
214 /* Remove the attribute */
215 if((nret = _remove_obj(&tgt, attrname)) == -1) {
216 return PyErr_SetFromErrno(PyExc_IOError);
219 /* Return the result */
223 static char __pylistxattr_doc__[] =
224 "Return the list of attribute names for a file\n"
227 " - a string representing filename, or a file-like object,\n"
228 " or a file descriptor; this represents the file to \n"
230 " - (optional) a boolean value (defaults to false), which, if\n"
231 " the file name given is a symbolic link, makes the\n"
232 " function operate on the symbolic link itself instead\n"
236 /* Wrapper for listxattr */
238 pylistxattr(PyObject *self, PyObject *args)
249 /* Parse the arguments */
250 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
252 if(!convertObj(myarg, &tgt, nofollow))
255 /* Find out the needed size of the buffer */
256 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
257 return PyErr_SetFromErrno(PyExc_IOError);
260 /* Try to allocate the memory, using Python's allocator */
261 if((buf = PyMem_Malloc(nalloc)) == NULL) {
266 /* Now retrieve the list of attributes */
267 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
269 return PyErr_SetFromErrno(PyExc_IOError);
272 /* Compute the number of attributes in the list */
273 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
277 /* Create the list which will hold the result */
278 mylist = PyList_New(nattrs);
280 /* Create and insert the attributes as strings in the list */
281 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
282 PyList_SET_ITEM(mylist, nattrs, PyString_FromString(s));
286 /* Free the buffer, now it is no longer needed */
289 /* Return the result */
293 static PyMethodDef xattr_methods[] = {
294 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
295 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
296 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
297 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
298 {NULL, NULL, 0, NULL} /* Sentinel */
301 static char __xattr_doc__[] = \
302 "Access extended filesystem attributes\n"
304 "This module gives access to the extended attributes present\n"
305 "in some operating systems/filesystems. You can list attributes,\n"
306 "get, set and remove them.\n"
307 "The last and optional parameter for all functions is a boolean \n"
308 "value which enables the 'l-' version of the functions - acting\n"
309 "on symbolic links and not their destination.\n"
312 " >>> import xattr\n"
313 " >>> xattr.listxattr(\"file.txt\")\n"
314 " ['user.mime_type']\n"
315 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
317 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
318 "\"Simple text file\")\n"
319 " >>> xattr.listxattr(\"file.txt\")\n"
320 " ['user.mime_type', 'user.comment']\n"
321 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
328 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
330 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
331 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
333 /* namespace constants */
334 PyModule_AddStringConstant(m, "NS_SECURITY", "security");
335 PyModule_AddStringConstant(m, "NS_SYSTEM", "system");
336 PyModule_AddStringConstant(m, "NS_TRUSTED", "trusted");
337 PyModule_AddStringConstant(m, "NS_USER", "user");