2 #include <attr/xattr.h>
5 /* the estimated (startup) attribute buffer size in
7 #define ESTIMATE_ATTR_SIZE 256
9 typedef enum {T_FD, T_PATH, T_LINK} target_e;
19 /** Converts from a string, file or int argument to what we need. */
20 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
22 if(PyString_Check(myobj)) {
23 tgt->type = nofollow ? T_LINK : T_PATH;
24 tgt->name = PyString_AS_STRING(myobj);
25 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
29 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
35 /* Combine a namespace string and an attribute name into a
36 fully-qualified name */
37 static const char* merge_ns(const char *ns, const char *name, char **buf) {
40 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
41 if((*buf = PyMem_Malloc(new_size)) == NULL) {
45 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
46 if(cnt > new_size || cnt < 0) {
47 PyErr_SetString(PyExc_ValueError,
48 "can't format the attribute name");
59 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
61 return flistxattr(tgt->fd, list, size);
62 else if (tgt->type == T_LINK)
63 return llistxattr(tgt->name, list, size);
65 return listxattr(tgt->name, list, size);
68 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
71 return fgetxattr(tgt->fd, name, value, size);
72 else if (tgt->type == T_LINK)
73 return lgetxattr(tgt->name, name, value, size);
75 return getxattr(tgt->name, name, value, size);
78 static ssize_t _set_obj(target_t *tgt, const char *name,
79 const void *value, size_t size,
82 return fsetxattr(tgt->fd, name, value, size, flags);
83 else if (tgt->type == T_LINK)
84 return lsetxattr(tgt->name, name, value, size, flags);
86 return setxattr(tgt->name, name, value, size, flags);
89 static ssize_t _remove_obj(target_t *tgt, const char *name) {
91 return fremovexattr(tgt->fd, name);
92 else if (tgt->type == T_LINK)
93 return lremovexattr(tgt->name, name);
95 return removexattr(tgt->name, name);
98 /* Checks if an attribute name matches an optional namespace */
99 static int matches_ns(const char *name, const char *ns) {
103 ns_size = strlen(ns);
105 if (strlen(name) > ns_size && !strncmp(name, ns, ns_size) &&
106 name[ns_size] == '.')
111 /* Wrapper for getxattr */
112 static char __pygetxattr_doc__[] =
113 "Get the value of a given extended attribute.\n"
116 " - a string representing filename, or a file-like object,\n"
117 " or a file descriptor; this represents the file on \n"
119 " - a string, representing the attribute whose value to retrieve;\n"
120 " usually in form of system.posix_acl or user.mime_type\n"
121 " - (optional) a boolean value (defaults to false), which, if\n"
122 " the file name given is a symbolic link, makes the\n"
123 " function operate on the symbolic link itself instead\n"
125 "@deprecated: this function has been replace with the L{get} function"
126 " which replaces the positional parameters with keyword ones\n"
130 pygetxattr(PyObject *self, PyObject *args)
140 /* Parse the arguments */
141 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
143 if(!convertObj(myarg, &tgt, nofollow))
146 /* Find out the needed size of the buffer */
147 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
148 return PyErr_SetFromErrno(PyExc_IOError);
151 /* Try to allocate the memory, using Python's allocator */
152 if((buf = PyMem_Malloc(nalloc)) == NULL) {
157 /* Now retrieve the attribute value */
158 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
160 return PyErr_SetFromErrno(PyExc_IOError);
163 /* Create the string which will hold the result */
164 res = PyString_FromStringAndSize(buf, nret);
166 /* Free the buffer, now it is no longer needed */
169 /* Return the result */
173 /* Wrapper for getxattr */
174 static char __get_doc__[] =
175 "Get the value of a given extended attribute.\n"
178 " - a string representing filename, or a file-like object,\n"
179 " or a file descriptor; this represents the file on \n"
181 " - a string, representing the attribute whose value to retrieve;\n"
182 " usually in form of system.posix_acl or user.mime_type\n"
183 " - (optional) a boolean value (defaults to false), which, if\n"
184 " the file name given is a symbolic link, makes the\n"
185 " function operate on the symbolic link itself instead\n"
190 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
195 char *attrname, *namebuf;
196 const char *fullname;
201 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
203 /* Parse the arguments */
204 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|iz", kwlist,
205 &myarg, &attrname, &nofollow, &ns))
207 if(!convertObj(myarg, &tgt, nofollow))
210 fullname = merge_ns(ns, attrname, &namebuf);
212 /* Find out the needed size of the buffer */
213 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
214 return PyErr_SetFromErrno(PyExc_IOError);
217 /* Try to allocate the memory, using Python's allocator */
218 if((buf = PyMem_Malloc(nalloc)) == NULL) {
224 /* Now retrieve the attribute value */
225 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
228 return PyErr_SetFromErrno(PyExc_IOError);
231 /* Create the string which will hold the result */
232 res = PyString_FromStringAndSize(buf, nret);
234 /* Free the buffers, they are no longer needed */
238 /* Return the result */
242 /* Wrapper for getxattr */
243 static char __get_all_doc__[] =
244 "Get all the extended attributes of an item.\n"
246 "This function performs a bulk-get of all extended attribute names\n"
247 "and the corresponding value.\n"
249 " >>> xattr.get_all('/path/to/file')\n"
250 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),"
251 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
252 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
253 " [('user.mime-type', 'plain/text'), ('user.comment', 'test')]\n"
255 "@param item: the item to query; either a string representing the"
256 " filename, or a file-like object, or a file descriptor\n"
257 "@keyword namespace: an optional namespace for filtering the"
258 " attributes; for example, querying all user attributes can be"
259 " accomplished by passing namespace=L{NS_USER}\n"
260 "@type namespace: string\n"
261 "@keyword nofollow: if passed and true, if the target file is a symbolic"
263 " the attributes for the link itself will be returned, instead of the"
264 " attributes of the target\n"
265 "@type nofollow: boolean\n"
266 "@return: list of tuples (name, value)\n"
270 get_all(PyObject *self, PyObject *args, PyObject *keywds)
275 char *buf_list, *buf_val;
277 size_t nalloc, nlist, nval;
280 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
282 /* Parse the arguments */
283 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
284 &myarg, &dolink, &ns))
286 if(!convertObj(myarg, &tgt, dolink))
289 /* Compute first the list of attributes */
291 /* Find out the needed size of the buffer for the attribute list */
292 nalloc = _list_obj(&tgt, NULL, 0);
295 return PyErr_SetFromErrno(PyExc_IOError);
298 /* Try to allocate the memory, using Python's allocator */
299 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
304 /* Now retrieve the list of attributes */
305 nlist = _list_obj(&tgt, buf_list, nalloc);
308 PyErr_SetFromErrno(PyExc_IOError);
312 /* Create the list which will hold the result */
313 mylist = PyList_New(0);
314 nalloc = ESTIMATE_ATTR_SIZE;
315 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
320 /* Create and insert the attributes as strings in the list */
321 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
325 if(!matches_ns(s, ns))
327 /* Now retrieve the attribute value */
330 nval = _get_obj(&tgt, s, buf_val, nalloc);
333 if(errno == ERANGE) {
334 nval = _get_obj(&tgt, s, NULL, 0);
335 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL)
339 } else if(errno == ENODATA || errno == ENOATTR) {
340 /* this attribute has gone away since we queried
341 the attribute list */
351 my_tuple = Py_BuildValue("ss#", s, buf_val, nval);
353 PyList_Append(mylist, my_tuple);
357 /* Free the buffers, now they are no longer needed */
359 PyMem_Free(buf_list);
361 /* Return the result */
364 PyErr_SetFromErrno(PyExc_IOError);
369 PyMem_Free(buf_list);
374 static char __pysetxattr_doc__[] =
375 "Set the value of a given extended attribute.\n"
376 "Be carefull in case you want to set attributes on symbolic\n"
377 "links, you have to use all the 5 parameters; use 0 for the \n"
378 "flags value if you want the default behavior (create or "
382 " - a string representing filename, or a file-like object,\n"
383 " or a file descriptor; this represents the file on \n"
385 " - a string, representing the attribute whose value to set;\n"
386 " usually in form of system.posix_acl or user.mime_type\n"
387 " - a string, possibly with embedded NULLs; note that there\n"
388 " are restrictions regarding the size of the value, for\n"
389 " example, for ext2/ext3, maximum size is the block size\n"
390 " - (optional) flags; if 0 or ommited the attribute will be \n"
391 " created or replaced; if XATTR_CREATE, the attribute \n"
392 " will be created, giving an error if it already exists;\n"
393 " of XATTR_REPLACE, the attribute will be replaced,\n"
394 " giving an error if it doesn't exists;\n"
395 " - (optional) a boolean value (defaults to false), which, if\n"
396 " the file name given is a symbolic link, makes the\n"
397 " function operate on the symbolic link itself instead\n"
399 "@deprecated: this function has been deprecated by the new L{set}"
403 /* Wrapper for setxattr */
405 pysetxattr(PyObject *self, PyObject *args)
415 /* Parse the arguments */
416 if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname,
417 &buf, &bufsize, &flags, &nofollow))
419 if(!convertObj(myarg, &tgt, nofollow))
422 /* Set the attribute's value */
423 if((nret = _set_obj(&tgt, attrname, buf, bufsize, flags)) == -1) {
424 return PyErr_SetFromErrno(PyExc_IOError);
427 /* Return the result */
431 static char __set_doc__[] =
432 "Set the value of a given extended attribute.\n"
434 "@param item: the item to query; either a string representing the"
435 " filename, or a file-like object, or a file descriptor\n"
436 "@param name: the attribute whose value to set; usually in form of"
437 " system.posix_acl or user.mime_type\n"
438 "@type name: string\n"
439 "@param value: a string, possibly with embedded NULLs; note that there"
440 " are restrictions regarding the size of the value, for"
441 " example, for ext2/ext3, maximum size is the block size\n"
442 "@type value: string\n"
443 "@param flags: if 0 or ommited the attribute will be"
444 " created or replaced; if L{XATTR_CREATE}, the attribute"
445 " will be created, giving an error if it already exists;"
446 " if L{XATTR_REPLACE}, the attribute will be replaced,"
447 " giving an error if it doesn't exists;\n"
448 "@type flags: integer\n"
449 "@param nofollow: if given and True, and the function is passed a"
450 " filename that points to a symlink, the function will act on the symlink"
451 " itself instead of its target\n"
452 "@type nofollow: boolean\n"
453 "@param namespace: if given, the attribute must not contain the namespace"
454 " itself, but instead the namespace will be taken from this parameter\n"
455 "@type namespace: string\n"
458 /* Wrapper for setxattr */
460 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
471 const char *full_name;
472 static char *kwlist[] = {"item", "name", "value", "flags",
473 "nofollow", "namespace", NULL};
475 /* Parse the arguments */
476 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oss#|iiz", kwlist,
478 &buf, &bufsize, &flags, &nofollow, &ns))
480 if(!convertObj(myarg, &tgt, nofollow))
483 full_name = merge_ns(ns, attrname, &newname);
484 /* Set the attribute's value */
485 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
489 return PyErr_SetFromErrno(PyExc_IOError);
492 /* Return the result */
497 static char __pyremovexattr_doc__[] =
498 "Remove an attribute from a file\n"
501 " - a string representing filename, or a file-like object,\n"
502 " or a file descriptor; this represents the file on \n"
504 " - a string, representing the attribute to be removed;\n"
505 " usually in form of system.posix_acl or user.mime_type\n"
506 " - (optional) a boolean value (defaults to false), which, if\n"
507 " the file name given is a symbolic link, makes the\n"
508 " function operate on the symbolic link itself instead\n"
512 /* Wrapper for removexattr */
514 pyremovexattr(PyObject *self, PyObject *args)
522 /* Parse the arguments */
523 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
526 if(!convertObj(myarg, &tgt, nofollow))
529 /* Remove the attribute */
530 if((nret = _remove_obj(&tgt, attrname)) == -1) {
531 return PyErr_SetFromErrno(PyExc_IOError);
534 /* Return the result */
538 static char __remove_doc__[] =
539 "Remove an attribute from a file\n"
541 "@param item: the item to query; either a string representing the"
542 " filename, or a file-like object, or a file descriptor\n"
543 "@param name: the attribute whose value to set; usually in form of"
544 " system.posix_acl or user.mime_type\n"
545 "@type name: string\n"
546 "@param nofollow: if given and True, and the function is passed a"
547 " filename that points to a symlink, the function will act on the symlink"
548 " itself instead of its target\n"
549 "@type nofollow: boolean\n"
550 "@param namespace: if given, the attribute must not contain the namespace"
551 " itself, but instead the namespace will be taken from this parameter\n"
552 "@type namespace: string\n"
555 /* Wrapper for removexattr */
557 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
561 char *attrname, *name_buf;
563 const char *full_name;
566 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
568 /* Parse the arguments */
569 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|iz", kwlist,
570 &myarg, &attrname, &nofollow, &ns))
573 if(!convertObj(myarg, &tgt, nofollow))
575 full_name = merge_ns(ns, attrname, &name_buf);
576 if(full_name == NULL)
579 /* Remove the attribute */
580 nret = _remove_obj(&tgt, full_name);
581 PyMem_Free(name_buf);
583 return PyErr_SetFromErrno(PyExc_IOError);
586 /* Return the result */
590 static char __pylistxattr_doc__[] =
591 "Return the list of attribute names for a file\n"
594 " - a string representing filename, or a file-like object,\n"
595 " or a file descriptor; this represents the file to \n"
597 " - (optional) a boolean value (defaults to false), which, if\n"
598 " the file name given is a symbolic link, makes the\n"
599 " function operate on the symbolic link itself instead\n"
603 /* Wrapper for listxattr */
605 pylistxattr(PyObject *self, PyObject *args)
616 /* Parse the arguments */
617 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
619 if(!convertObj(myarg, &tgt, nofollow))
622 /* Find out the needed size of the buffer */
623 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
624 return PyErr_SetFromErrno(PyExc_IOError);
627 /* Try to allocate the memory, using Python's allocator */
628 if((buf = PyMem_Malloc(nalloc)) == NULL) {
633 /* Now retrieve the list of attributes */
634 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
636 return PyErr_SetFromErrno(PyExc_IOError);
639 /* Compute the number of attributes in the list */
640 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
644 /* Create the list which will hold the result */
645 mylist = PyList_New(nattrs);
647 /* Create and insert the attributes as strings in the list */
648 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
649 PyList_SET_ITEM(mylist, nattrs, PyString_FromString(s));
653 /* Free the buffer, now it is no longer needed */
656 /* Return the result */
660 static char __list_doc__[] =
661 "Return the list of attribute names for a file\n"
663 "@param item: the item to query; either a string representing the"
664 " filename, or a file-like object, or a file descriptor\n"
665 "@param nofollow: if given and True, and the function is passed a"
666 " filename that points to a symlink, the function will act on the symlink"
667 " itself instead of its target\n"
668 "@type nofollow: boolean\n"
669 "@param namespace: if given, the attribute must not contain the namespace"
670 " itself, but instead the namespace will be taken from this parameter\n"
671 "@type namespace: string\n"
674 /* Wrapper for listxattr */
676 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
687 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
689 /* Parse the arguments */
690 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
691 &myarg, &nofollow, &ns))
693 if(!convertObj(myarg, &tgt, nofollow))
696 /* Find out the needed size of the buffer */
697 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
698 return PyErr_SetFromErrno(PyExc_IOError);
701 /* Try to allocate the memory, using Python's allocator */
702 if((buf = PyMem_Malloc(nalloc)) == NULL) {
707 /* Now retrieve the list of attributes */
708 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
710 return PyErr_SetFromErrno(PyExc_IOError);
713 /* Compute the number of attributes in the list */
714 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
715 if(matches_ns(s, ns))
718 /* Create the list which will hold the result */
719 mylist = PyList_New(nattrs);
721 /* Create and insert the attributes as strings in the list */
722 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
723 if(matches_ns(s, ns)) {
724 char *short_form = ns == NULL ? s : s + strlen(ns) + 1;
725 PyList_SET_ITEM(mylist, nattrs, PyString_FromString(short_form));
730 /* Free the buffer, now it is no longer needed */
733 /* Return the result */
737 static PyMethodDef xattr_methods[] = {
738 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
739 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
741 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
743 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
744 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
746 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
747 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
749 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
750 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
752 {NULL, NULL, 0, NULL} /* Sentinel */
755 static char __xattr_doc__[] = \
756 "Access extended filesystem attributes\n"
758 "This module gives access to the extended attributes present\n"
759 "in some operating systems/filesystems. You can list attributes,\n"
760 "get, set and remove them.\n"
761 "The last and optional parameter for all functions is a boolean \n"
762 "value which enables the 'l-' version of the functions - acting\n"
763 "on symbolic links and not their destination.\n"
766 " >>> import xattr\n"
767 " >>> xattr.listxattr(\"file.txt\")\n"
768 " ['user.mime_type']\n"
769 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
771 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
772 "\"Simple text file\")\n"
773 " >>> xattr.listxattr(\"file.txt\")\n"
774 " ['user.mime_type', 'user.comment']\n"
775 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
782 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
784 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
785 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
787 /* namespace constants */
788 PyModule_AddStringConstant(m, "NS_SECURITY", "security");
789 PyModule_AddStringConstant(m, "NS_SYSTEM", "system");
790 PyModule_AddStringConstant(m, "NS_TRUSTED", "trusted");
791 PyModule_AddStringConstant(m, "NS_USER", "user");