2 #include <attr/xattr.h>
4 /** Converts from a string, file or int argument to what we need. */
5 static int convertObj(PyObject *myobj, int *ishandle, int *filehandle,
7 if(PyString_Check(myobj)) {
9 *filename = PyString_AS_STRING(myobj);
10 } else if((*filehandle = PyObject_AsFileDescriptor(myobj)) != -1) {
13 PyErr_SetString(PyExc_TypeError, "argument 1 must be string or int");
19 /* Checks if an attribute name matches an optional namespace */
20 static int matches_ns(const char *name, const char *ns) {
26 if (strlen(name) > ns_size && !strncmp(name, ns, ns_size) &&
32 /* Wrapper for getxattr */
33 static char __pygetxattr_doc__[] =
34 "Get the value of a given extended attribute.\n"
37 " - a string representing filename, or a file-like object,\n"
38 " or a file descriptor; this represents the file on \n"
40 " - a string, representing the attribute whose value to retrieve;\n"
41 " usually in form of system.posix_acl or user.mime_type\n"
42 " - (optional) a boolean value (defaults to false), which, if\n"
43 " the file name given is a symbolic link, makes the\n"
44 " function operate on the symbolic link itself instead\n"
46 "@deprecated: this function has been replace with the L{get_all} function"
47 " which replaces the positional parameters with keyword ones\n"
51 pygetxattr(PyObject *self, PyObject *args)
55 int filedes = -1, ishandle, dolink=0;
61 /* Parse the arguments */
62 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
64 if(!convertObj(myarg, &ishandle, &filedes, &file))
67 /* Find out the needed size of the buffer */
69 fgetxattr(filedes, attrname, NULL, 0) :
71 lgetxattr(file, attrname, NULL, 0) :
72 getxattr(file, attrname, NULL, 0);
74 return PyErr_SetFromErrno(PyExc_IOError);
77 /* Try to allocate the memory, using Python's allocator */
78 if((buf = PyMem_Malloc(nalloc)) == NULL) {
83 /* Now retrieve the attribute value */
85 fgetxattr(filedes, attrname, buf, nalloc) :
87 lgetxattr(file, attrname, buf, nalloc) :
88 getxattr(file, attrname, buf, nalloc);
91 return PyErr_SetFromErrno(PyExc_IOError);
94 /* Create the string which will hold the result */
95 res = PyString_FromStringAndSize(buf, nret);
97 /* Free the buffer, now it is no longer needed */
100 /* Return the result */
104 /* Wrapper for getxattr */
105 static char __get_all_doc__[] =
106 "Get all the extended attributes of an item.\n"
108 "This function performs a bulk-get of all extended attribute names\n"
109 "and the corresponding value.\n"
111 " >>> xattr.get_all('/path/to/file')\n"
112 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),"
113 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
114 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
115 " [('user.mime-type', 'plain/text'), ('user.comment', 'test')]\n"
117 "@param item: the item to query; either a string representing the"
118 " filename, or a file-like object, or a file descriptor\n"
119 "@keyword namespace: an optional namespace for filtering the"
120 " attributes; for example, querying all user attributes can be"
121 " accomplished by passing namespace=L{NS_USER}\n"
122 "@type namespace: string\n"
123 "@keyword nofollow: if passed and true, if the target file is a symbolic"
125 " the attributes for the link itself will be returned, instead of the"
126 " attributes of the target\n"
127 "@type nofollow: boolean\n"
128 "@return: list of tuples (name, value)\n"
132 get_all(PyObject *self, PyObject *args, PyObject *keywds)
136 int filedes = -1, ishandle, dolink=0;
138 char *buf_list, *buf_val;
140 size_t nalloc, nlist, nval;
142 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
144 /* Parse the arguments */
145 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
146 &myarg, &dolink, &ns))
148 if(!convertObj(myarg, &ishandle, &filedes, &file))
151 /* Compute first the list of attributes */
153 /* Find out the needed size of the buffer for the attribute list */
155 flistxattr(filedes, NULL, 0) :
157 llistxattr(file, NULL, 0) :
158 listxattr(file, NULL, 0);
161 return PyErr_SetFromErrno(PyExc_IOError);
164 /* Try to allocate the memory, using Python's allocator */
165 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
170 /* Now retrieve the list of attributes */
172 flistxattr(filedes, buf_list, nalloc) :
174 llistxattr(file, buf_list, nalloc) :
175 listxattr(file, buf_list, nalloc);
178 return PyErr_SetFromErrno(PyExc_IOError);
181 /* Create the list which will hold the result */
182 mylist = PyList_New(0);
184 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
185 PyMem_Free(buf_list);
190 /* Create and insert the attributes as strings in the list */
191 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
195 if(!matches_ns(s, ns))
197 /* Now retrieve the attribute value */
201 fgetxattr(filedes, s, buf_val, nalloc) :
203 lgetxattr(file, s, buf_val, nalloc) :
204 getxattr(file, s, buf_val, nalloc);
207 if(errno == ERANGE) {
209 fgetxattr(filedes, s, NULL, 0) :
211 lgetxattr(file, s, NULL, 0) :
212 getxattr(file, s, NULL, 0);
213 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
214 PyMem_Free(buf_list);
220 } else if(errno == ENODATA || errno == ENOATTR) {
221 /* this attribute has gone away since we queried
222 the attribute list */
226 PyMem_Free(buf_list);
228 return PyErr_SetFromErrno(PyExc_IOError);
234 my_tuple = Py_BuildValue("ss#", s, buf_val, nval);
236 PyList_Append(mylist, my_tuple);
240 /* Free the buffers, now they are no longer needed */
242 PyMem_Free(buf_list);
244 /* Return the result */
250 static char __pysetxattr_doc__[] =
251 "Set the value of a given extended attribute.\n"
252 "Be carefull in case you want to set attributes on symbolic\n"
253 "links, you have to use all the 5 parameters; use 0 for the \n"
254 "flags value if you want the default behavior (create or "
258 " - a string representing filename, or a file-like object,\n"
259 " or a file descriptor; this represents the file on \n"
261 " - a string, representing the attribute whose value to set;\n"
262 " usually in form of system.posix_acl or user.mime_type\n"
263 " - a string, possibly with embedded NULLs; note that there\n"
264 " are restrictions regarding the size of the value, for\n"
265 " example, for ext2/ext3, maximum size is the block size\n"
266 " - (optional) flags; if 0 or ommited the attribute will be \n"
267 " created or replaced; if XATTR_CREATE, the attribute \n"
268 " will be created, giving an error if it already exists;\n"
269 " of XATTR_REPLACE, the attribute will be replaced,\n"
270 " giving an error if it doesn't exists;\n"
271 " - (optional) a boolean value (defaults to false), which, if\n"
272 " the file name given is a symbolic link, makes the\n"
273 " function operate on the symbolic link itself instead\n"
277 /* Wrapper for setxattr */
279 pysetxattr(PyObject *self, PyObject *args)
283 int ishandle, filedes, dolink=0;
289 /* Parse the arguments */
290 if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname,
291 &buf, &bufsize, &flags, &dolink))
293 if(!convertObj(myarg, &ishandle, &filedes, &file))
296 /* Set the attribute's value */
298 fsetxattr(filedes, attrname, buf, bufsize, flags) :
300 lsetxattr(file, attrname, buf, bufsize, flags) :
301 setxattr(file, attrname, buf, bufsize, flags);
304 return PyErr_SetFromErrno(PyExc_IOError);
307 /* Return the result */
312 static char __pyremovexattr_doc__[] =
313 "Remove an attribute from a file\n"
316 " - a string representing filename, or a file-like object,\n"
317 " or a file descriptor; this represents the file on \n"
319 " - a string, representing the attribute to be removed;\n"
320 " usually in form of system.posix_acl or user.mime_type\n"
321 " - (optional) a boolean value (defaults to false), which, if\n"
322 " the file name given is a symbolic link, makes the\n"
323 " function operate on the symbolic link itself instead\n"
327 /* Wrapper for removexattr */
329 pyremovexattr(PyObject *self, PyObject *args)
333 int ishandle, filedes, dolink=0;
337 /* Parse the arguments */
338 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
341 if(!convertObj(myarg, &ishandle, &filedes, &file))
344 /* Remove the attribute */
346 fremovexattr(filedes, attrname) :
348 lremovexattr(file, attrname) :
349 removexattr(file, attrname);
352 return PyErr_SetFromErrno(PyExc_IOError);
354 /* Return the result */
359 static char __pylistxattr_doc__[] =
360 "Return the list of attribute names for a file\n"
363 " - a string representing filename, or a file-like object,\n"
364 " or a file descriptor; this represents the file to \n"
366 " - (optional) a boolean value (defaults to false), which, if\n"
367 " the file name given is a symbolic link, makes the\n"
368 " function operate on the symbolic link itself instead\n"
372 /* Wrapper for listxattr */
374 pylistxattr(PyObject *self, PyObject *args)
379 int ishandle, dolink=0;
386 /* Parse the arguments */
387 if (!PyArg_ParseTuple(args, "O|i", &myarg, &dolink))
389 if(!convertObj(myarg, &ishandle, &filedes, &file))
392 /* Find out the needed size of the buffer */
394 flistxattr(filedes, NULL, 0) :
396 llistxattr(file, NULL, 0) :
397 listxattr(file, NULL, 0);
400 return PyErr_SetFromErrno(PyExc_IOError);
403 /* Try to allocate the memory, using Python's allocator */
404 if((buf = PyMem_Malloc(nalloc)) == NULL) {
409 /* Now retrieve the list of attributes */
411 flistxattr(filedes, buf, nalloc) :
413 llistxattr(file, buf, nalloc) :
414 listxattr(file, buf, nalloc);
417 return PyErr_SetFromErrno(PyExc_IOError);
420 /* Compute the number of attributes in the list */
421 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
425 /* Create the list which will hold the result */
426 mylist = PyList_New(nattrs);
428 /* Create and insert the attributes as strings in the list */
429 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
430 PyList_SET_ITEM(mylist, nattrs, PyString_FromString(s));
434 /* Free the buffer, now it is no longer needed */
437 /* Return the result */
441 static PyMethodDef xattr_methods[] = {
442 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
443 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
445 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
446 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
447 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
448 {NULL, NULL, 0, NULL} /* Sentinel */
451 static char __xattr_doc__[] = \
452 "Access extended filesystem attributes\n"
454 "This module gives access to the extended attributes present\n"
455 "in some operating systems/filesystems. You can list attributes,\n"
456 "get, set and remove them.\n"
457 "The last and optional parameter for all functions is a boolean \n"
458 "value which enables the 'l-' version of the functions - acting\n"
459 "on symbolic links and not their destination.\n"
462 " >>> import xattr\n"
463 " >>> xattr.listxattr(\"file.txt\")\n"
464 " ['user.mime_type']\n"
465 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
467 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
468 "\"Simple text file\")\n"
469 " >>> xattr.listxattr(\"file.txt\")\n"
470 " ['user.mime_type', 'user.comment']\n"
471 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
478 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
480 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
481 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
483 /* namespace constants */
484 PyModule_AddStringConstant(m, "NS_SECURITY", "security");
485 PyModule_AddStringConstant(m, "NS_SYSTEM", "system");
486 PyModule_AddStringConstant(m, "NS_TRUSTED", "trusted");
487 PyModule_AddStringConstant(m, "NS_USER", "user");