2 xattr - a python module for manipulating filesystem extended attributes
4 Copyright (C) 2002, 2003, 2006, 2008, 2012 Iustin Pop <iusty@k1024.org>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #define PY_SSIZE_T_CLEAN
25 #include <attr/xattr.h>
28 /* Compatibility with python 2.4 regarding python size type (PEP 353) */
29 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
30 typedef int Py_ssize_t;
31 #define PY_SSIZE_T_MAX INT_MAX
32 #define PY_SSIZE_T_MIN INT_MIN
35 #if PY_MAJOR_VERSION >= 3
38 #define PyBytes_Check PyString_Check
39 #define PyBytes_AS_STRING PyString_AS_STRING
40 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
41 #define PyBytes_FromString PyString_FromString
44 /* the estimated (startup) attribute buffer size in
46 #define ESTIMATE_ATTR_SIZE 256
48 typedef enum {T_FD, T_PATH, T_LINK} target_e;
59 /* Cleans up a tgt structure */
60 static void free_tgt(target_t *tgt) {
61 if (tgt->tmp != NULL) {
66 /** Converts from a string, file or int argument to what we need.
68 * Returns -1 on failure, 0 on success.
70 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
73 if(PyBytes_Check(myobj)) {
74 tgt->type = nofollow ? T_LINK : T_PATH;
75 tgt->name = PyBytes_AS_STRING(myobj);
76 } else if(PyUnicode_Check(myobj)) {
77 tgt->type = nofollow ? T_LINK : T_PATH;
79 PyUnicode_AsEncodedString(myobj,
80 Py_FileSystemDefaultEncoding, "strict");
83 tgt->name = PyBytes_AS_STRING(tgt->tmp);
84 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
88 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
94 /* Combine a namespace string and an attribute name into a
95 fully-qualified name */
96 static const char* merge_ns(const char *ns, const char *name, char **buf) {
99 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
100 if((*buf = PyMem_Malloc(new_size)) == NULL) {
104 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
105 if(cnt > new_size || cnt < 0) {
106 PyErr_SetString(PyExc_ValueError,
107 "can't format the attribute name");
118 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
119 if(tgt->type == T_FD)
120 return flistxattr(tgt->fd, list, size);
121 else if (tgt->type == T_LINK)
122 return llistxattr(tgt->name, list, size);
124 return listxattr(tgt->name, list, size);
127 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
129 if(tgt->type == T_FD)
130 return fgetxattr(tgt->fd, name, value, size);
131 else if (tgt->type == T_LINK)
132 return lgetxattr(tgt->name, name, value, size);
134 return getxattr(tgt->name, name, value, size);
137 static int _set_obj(target_t *tgt, const char *name,
138 const void *value, size_t size, int flags) {
139 if(tgt->type == T_FD)
140 return fsetxattr(tgt->fd, name, value, size, flags);
141 else if (tgt->type == T_LINK)
142 return lsetxattr(tgt->name, name, value, size, flags);
144 return setxattr(tgt->name, name, value, size, flags);
147 static int _remove_obj(target_t *tgt, const char *name) {
148 if(tgt->type == T_FD)
149 return fremovexattr(tgt->fd, name);
150 else if (tgt->type == T_LINK)
151 return lremovexattr(tgt->name, name);
153 return removexattr(tgt->name, name);
157 Checks if an attribute name matches an optional namespace.
159 If the namespace is NULL, it will return the name itself. If the
160 namespace is non-NULL and the name matches, it will return a
161 pointer to the offset in the name after the namespace and the
162 separator. If however the name doesn't match the namespace, it will
165 const char *matches_ns(const char *ns, const char *name) {
169 ns_size = strlen(ns);
171 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
172 name[ns_size] == '.')
173 return name + ns_size + 1;
177 /* Wrapper for getxattr */
178 static char __pygetxattr_doc__[] =
179 "Get the value of a given extended attribute (deprecated).\n"
182 " - a string representing filename, or a file-like object,\n"
183 " or a file descriptor; this represents the file on \n"
185 " - a string, representing the attribute whose value to retrieve;\n"
186 " usually in form of system.posix_acl or user.mime_type\n"
187 " - (optional) a boolean value (defaults to false), which, if\n"
188 " the file name given is a symbolic link, makes the\n"
189 " function operate on the symbolic link itself instead\n"
191 "@deprecated: since version 0.4, this function has been deprecated\n"
192 " by the L{get} function\n"
196 pygetxattr(PyObject *self, PyObject *args)
201 char *attrname = NULL;
203 ssize_t nalloc, nret;
206 /* Parse the arguments */
207 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
209 if(convertObj(myarg, &tgt, nofollow) < 0) {
214 /* Find out the needed size of the buffer */
215 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
216 res = PyErr_SetFromErrno(PyExc_IOError);
220 /* Try to allocate the memory, using Python's allocator */
221 if((buf = PyMem_Malloc(nalloc)) == NULL) {
222 res = PyErr_NoMemory();
226 /* Now retrieve the attribute value */
227 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
228 res = PyErr_SetFromErrno(PyExc_IOError);
232 /* Create the string which will hold the result */
233 res = PyBytes_FromStringAndSize(buf, nret);
236 /* Free the buffer, now it is no longer needed */
241 PyMem_Free(attrname);
243 /* Return the result */
247 /* Wrapper for getxattr */
248 static char __get_doc__[] =
249 "Get the value of a given extended attribute.\n"
252 " >>> xattr.get('/path/to/file', 'user.comment')\n"
254 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
257 "@param item: the item to query; either a string representing the\n"
258 " filename, or a file-like object, or a file descriptor\n"
259 "@param name: the attribute whose value to set; usually in form of\n"
260 " system.posix_acl or user.mime_type\n"
261 "@type name: string\n"
262 "@param nofollow: if given and True, and the function is passed a\n"
263 " filename that points to a symlink, the function will act on the\n"
264 " symlink itself instead of its target\n"
265 "@type nofollow: boolean\n"
266 "@param namespace: if given, the attribute must not contain the\n"
267 " namespace itself, but instead the namespace will be taken from\n"
269 "@type namespace: string\n"
270 "@return: the value of the extended attribute (can contain NULLs)\n"
272 "@raise EnvironmentError: caused by any system errors\n"
277 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
282 char *attrname = NULL, *namebuf;
283 const char *fullname;
285 const char *ns = NULL;
286 ssize_t nalloc, nret;
288 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
290 /* Parse the arguments */
291 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
292 &myarg, NULL, &attrname, &nofollow, &ns))
294 if(convertObj(myarg, &tgt, nofollow) < 0) {
299 fullname = merge_ns(ns, attrname, &namebuf);
301 /* Find out the needed size of the buffer */
302 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
303 res = PyErr_SetFromErrno(PyExc_IOError);
307 /* Try to allocate the memory, using Python's allocator */
308 if((buf = PyMem_Malloc(nalloc)) == NULL) {
309 res = PyErr_NoMemory();
313 /* Now retrieve the attribute value */
314 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
315 res = PyErr_SetFromErrno(PyExc_IOError);
319 /* Create the string which will hold the result */
320 res = PyBytes_FromStringAndSize(buf, nret);
322 /* Free the buffers, they are no longer needed */
330 PyMem_Free(attrname);
332 /* Return the result */
336 /* Wrapper for getxattr */
337 static char __get_all_doc__[] =
338 "Get all the extended attributes of an item.\n"
340 "This function performs a bulk-get of all extended attribute names\n"
341 "and the corresponding value.\n"
343 " >>> xattr.get_all('/path/to/file')\n"
344 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
345 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
346 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
347 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
349 "@param item: the item to query; either a string representing the\n"
350 " filename, or a file-like object, or a file descriptor\n"
351 "@keyword namespace: an optional namespace for filtering the\n"
352 " attributes; for example, querying all user attributes can be\n"
353 " accomplished by passing namespace=L{NS_USER}\n"
354 "@type namespace: string\n"
355 "@keyword nofollow: if passed and true, if the target file is a\n"
356 " symbolic link, the attributes for the link itself will be\n"
357 " returned, instead of the attributes of the target\n"
358 "@type nofollow: boolean\n"
359 "@return: list of tuples (name, value); note that if a namespace\n"
360 " argument was passed, it (and the separator) will be stripped from\n"
361 " the names returned\n"
363 "@raise EnvironmentError: caused by any system errors\n"
364 "@note: Since reading the whole attribute list is not an atomic\n"
365 " operation, it might be possible that attributes are added\n"
366 " or removed between the initial query and the actual reading\n"
367 " of the attributes; the returned list will contain only the\n"
368 " attributes that were present at the initial listing of the\n"
369 " attribute names and that were still present when the read\n"
370 " attempt for the value is made.\n"
375 get_all(PyObject *self, PyObject *args, PyObject *keywds)
377 PyObject *myarg, *res;
379 const char *ns = NULL;
380 char *buf_list, *buf_val;
382 ssize_t nalloc, nlist, nval;
385 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
387 /* Parse the arguments */
388 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
389 &myarg, &dolink, &ns))
391 if(convertObj(myarg, &tgt, dolink) < 0)
394 /* Compute first the list of attributes */
396 /* Find out the needed size of the buffer for the attribute list */
397 nalloc = _list_obj(&tgt, NULL, 0);
400 res = PyErr_SetFromErrno(PyExc_IOError);
404 /* Try to allocate the memory, using Python's allocator */
405 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
406 res = PyErr_NoMemory();
410 /* Now retrieve the list of attributes */
411 nlist = _list_obj(&tgt, buf_list, nalloc);
414 res = PyErr_SetFromErrno(PyExc_IOError);
418 /* Create the list which will hold the result */
419 mylist = PyList_New(0);
425 nalloc = ESTIMATE_ATTR_SIZE;
426 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
428 res = PyErr_NoMemory();
432 /* Create and insert the attributes as strings in the list */
433 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
438 if((name=matches_ns(ns, s))==NULL)
440 /* Now retrieve the attribute value */
443 nval = _get_obj(&tgt, s, buf_val, nalloc);
446 if(errno == ERANGE) {
447 nval = _get_obj(&tgt, s, NULL, 0);
448 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
455 } else if(errno == ENODATA || errno == ENOATTR) {
456 /* this attribute has gone away since we queried
457 the attribute list */
461 /* else we're dealing with a different error, which we
462 don't know how to handle nicely, so we abort */
464 res = PyErr_SetFromErrno(PyExc_IOError);
472 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
474 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
476 if (my_tuple == NULL) {
481 PyList_Append(mylist, my_tuple);
485 /* Successful exit */
492 PyMem_Free(buf_list);
497 /* Return the result */
502 static char __pysetxattr_doc__[] =
503 "Set the value of a given extended attribute (deprecated).\n"
505 "Be carefull in case you want to set attributes on symbolic\n"
506 "links, you have to use all the 5 parameters; use 0 for the \n"
507 "flags value if you want the default behavior (create or "
511 " - a string representing filename, or a file-like object,\n"
512 " or a file descriptor; this represents the file on \n"
514 " - a string, representing the attribute whose value to set;\n"
515 " usually in form of system.posix_acl or user.mime_type\n"
516 " - a string, possibly with embedded NULLs; note that there\n"
517 " are restrictions regarding the size of the value, for\n"
518 " example, for ext2/ext3, maximum size is the block size\n"
519 " - (optional) flags; if 0 or ommited the attribute will be \n"
520 " created or replaced; if XATTR_CREATE, the attribute \n"
521 " will be created, giving an error if it already exists;\n"
522 " of XATTR_REPLACE, the attribute will be replaced,\n"
523 " giving an error if it doesn't exists;\n"
524 " - (optional) a boolean value (defaults to false), which, if\n"
525 " the file name given is a symbolic link, makes the\n"
526 " function operate on the symbolic link itself instead\n"
528 "@deprecated: since version 0.4, this function has been deprecated\n"
529 " by the L{set} function\n"
532 /* Wrapper for setxattr */
534 pysetxattr(PyObject *self, PyObject *args)
536 PyObject *myarg, *res;
538 char *attrname = NULL;
545 /* Parse the arguments */
546 if (!PyArg_ParseTuple(args, "Oetet#|bi", &myarg, NULL, &attrname,
547 NULL, &buf, &bufsize, &flags, &nofollow))
549 if(convertObj(myarg, &tgt, nofollow) < 0) {
554 /* Set the attribute's value */
555 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
560 res = PyErr_SetFromErrno(PyExc_IOError);
568 PyMem_Free(attrname);
571 /* Return the result */
575 static char __set_doc__[] =
576 "Set the value of a given extended attribute.\n"
579 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
580 " >>> xattr.set('/path/to/file', 'comment', 'test',"
581 " namespace=xattr.NS_USER)\n"
583 "@param item: the item to query; either a string representing the\n"
584 " filename, or a file-like object, or a file descriptor\n"
585 "@param name: the attribute whose value to set; usually in form of\n"
586 " system.posix_acl or user.mime_type\n"
587 "@type name: string\n"
588 "@param value: a string, possibly with embedded NULLs; note that there\n"
589 " are restrictions regarding the size of the value, for\n"
590 " example, for ext2/ext3, maximum size is the block size\n"
591 "@type value: string\n"
592 "@param flags: if 0 or ommited the attribute will be\n"
593 " created or replaced; if L{XATTR_CREATE}, the attribute\n"
594 " will be created, giving an error if it already exists;\n"
595 " if L{XATTR_REPLACE}, the attribute will be replaced,\n"
596 " giving an error if it doesn't exists;\n"
597 "@type flags: integer\n"
598 "@param nofollow: if given and True, and the function is passed a\n"
599 " filename that points to a symlink, the function will act on the\n"
600 " symlink itself instead of its target\n"
601 "@type nofollow: boolean\n"
602 "@param namespace: if given, the attribute must not contain the\n"
603 " namespace itself, but instead the namespace will be taken from\n"
605 "@type namespace: string\n"
607 "@raise EnvironmentError: caused by any system errors\n"
611 /* Wrapper for setxattr */
613 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
615 PyObject *myarg, *res;
617 char *attrname = NULL;
623 const char *ns = NULL;
625 const char *full_name;
626 static char *kwlist[] = {"item", "name", "value", "flags",
627 "nofollow", "namespace", NULL};
629 /* Parse the arguments */
630 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|iiz", kwlist,
631 &myarg, NULL, &attrname, NULL,
632 &buf, &bufsize, &flags, &nofollow, &ns))
634 if(convertObj(myarg, &tgt, nofollow) < 0) {
639 full_name = merge_ns(ns, attrname, &newname);
641 /* Set the attribute's value */
642 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
650 res = PyErr_SetFromErrno(PyExc_IOError);
658 PyMem_Free(attrname);
661 /* Return the result */
666 static char __pyremovexattr_doc__[] =
667 "Remove an attribute from a file (deprecated).\n"
670 " - a string representing filename, or a file-like object,\n"
671 " or a file descriptor; this represents the file on \n"
673 " - a string, representing the attribute to be removed;\n"
674 " usually in form of system.posix_acl or user.mime_type\n"
675 " - (optional) a boolean value (defaults to false), which, if\n"
676 " the file name given is a symbolic link, makes the\n"
677 " function operate on the symbolic link itself instead\n"
679 "@deprecated: since version 0.4, this function has been deprecated\n"
684 /* Wrapper for removexattr */
686 pyremovexattr(PyObject *self, PyObject *args)
688 PyObject *myarg, *res;
690 char *attrname = NULL;
694 /* Parse the arguments */
695 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
698 if(convertObj(myarg, &tgt, nofollow) < 0) {
703 /* Remove the attribute */
704 nret = _remove_obj(&tgt, attrname);
709 res = PyErr_SetFromErrno(PyExc_IOError);
717 PyMem_Free(attrname);
719 /* Return the result */
723 static char __remove_doc__[] =
724 "Remove an attribute from a file.\n"
727 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
729 "@param item: the item to query; either a string representing the\n"
730 " filename, or a file-like object, or a file descriptor\n"
731 "@param name: the attribute whose value to set; usually in form of\n"
732 " system.posix_acl or user.mime_type\n"
733 "@type name: string\n"
734 "@param nofollow: if given and True, and the function is passed a\n"
735 " filename that points to a symlink, the function will act on the\n"
736 " symlink itself instead of its target\n"
737 "@type nofollow: boolean\n"
738 "@param namespace: if given, the attribute must not contain the\n"
739 " namespace itself, but instead the namespace will be taken from\n"
741 "@type namespace: string\n"
744 "@raise EnvironmentError: caused by any system errors\n"
747 /* Wrapper for removexattr */
749 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
751 PyObject *myarg, *res;
753 char *attrname = NULL, *name_buf;
754 const char *ns = NULL;
755 const char *full_name;
758 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
760 /* Parse the arguments */
761 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
762 &myarg, NULL, &attrname, &nofollow, &ns))
765 if(convertObj(myarg, &tgt, nofollow) < 0) {
770 full_name = merge_ns(ns, attrname, &name_buf);
771 if(full_name == NULL) {
776 /* Remove the attribute */
777 nret = _remove_obj(&tgt, full_name);
779 PyMem_Free(name_buf);
784 res = PyErr_SetFromErrno(PyExc_IOError);
792 PyMem_Free(attrname);
794 /* Return the result */
798 static char __pylistxattr_doc__[] =
799 "Return the list of attribute names for a file (deprecated).\n"
802 " - a string representing filename, or a file-like object,\n"
803 " or a file descriptor; this represents the file to \n"
805 " - (optional) a boolean value (defaults to false), which, if\n"
806 " the file name given is a symbolic link, makes the\n"
807 " function operate on the symbolic link itself instead\n"
809 "@deprecated: since version 0.4, this function has been deprecated\n"
814 /* Wrapper for listxattr */
816 pylistxattr(PyObject *self, PyObject *args)
820 ssize_t nalloc, nret;
827 /* Parse the arguments */
828 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
830 if(convertObj(myarg, &tgt, nofollow) < 0)
833 /* Find out the needed size of the buffer */
834 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
835 mylist = PyErr_SetFromErrno(PyExc_IOError);
839 /* Try to allocate the memory, using Python's allocator */
840 if((buf = PyMem_Malloc(nalloc)) == NULL) {
841 mylist = PyErr_NoMemory();
845 /* Now retrieve the list of attributes */
846 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
847 mylist = PyErr_SetFromErrno(PyExc_IOError);
851 /* Compute the number of attributes in the list */
852 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
856 /* Create the list which will hold the result */
857 mylist = PyList_New(nattrs);
861 /* Create and insert the attributes as strings in the list */
862 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
863 PyObject *item = PyBytes_FromString(s);
869 PyList_SET_ITEM(mylist, nattrs, item);
874 /* Free the buffer, now it is no longer needed */
880 /* Return the result */
884 static char __list_doc__[] =
885 "Return the list of attribute names for a file.\n"
888 " >>> xattr.list('/path/to/file')\n"
889 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
890 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
891 " ['test', 'comment']\n"
893 "@param item: the item to query; either a string representing the\n"
894 " filename, or a file-like object, or a file descriptor\n"
895 "@param nofollow: if given and True, and the function is passed a\n"
896 " filename that points to a symlink, the function will act on the\n"
897 " symlink itself instead of its target\n"
898 "@type nofollow: boolean\n"
899 "@param namespace: if given, the attribute must not contain the\n"
900 " namespace itself, but instead the namespace will be taken from\n"
902 "@type namespace: string\n"
903 "@return: list of strings; note that if a namespace argument was\n"
904 " passed, it (and the separator) will be stripped from the names\n"
907 "@raise EnvironmentError: caused by any system errors\n"
911 /* Wrapper for listxattr */
913 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
917 ssize_t nalloc, nret;
924 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
926 /* Parse the arguments */
927 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iet", kwlist,
928 &myarg, &nofollow, NULL, &ns))
930 if(convertObj(myarg, &tgt, nofollow) < 0) {
935 /* Find out the needed size of the buffer */
936 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
937 res = PyErr_SetFromErrno(PyExc_IOError);
941 /* Try to allocate the memory, using Python's allocator */
942 if((buf = PyMem_Malloc(nalloc)) == NULL) {
943 res = PyErr_NoMemory();
947 /* Now retrieve the list of attributes */
948 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
949 res = PyErr_SetFromErrno(PyExc_IOError);
953 /* Compute the number of attributes in the list */
954 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
955 if(matches_ns(ns, s) != NULL)
958 /* Create the list which will hold the result */
959 res = PyList_New(nattrs);
963 /* Create and insert the attributes as strings in the list */
964 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
965 const char *name = matches_ns(ns, s);
967 PyObject *item = PyBytes_FromString(name);
973 PyList_SET_ITEM(res, nattrs, item);
979 /* Free the buffer, now it is no longer needed */
987 /* Return the result */
991 static PyMethodDef xattr_methods[] = {
992 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
993 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
995 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
997 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
998 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
1000 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
1001 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
1003 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
1004 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
1006 {NULL, NULL, 0, NULL} /* Sentinel */
1009 static char __xattr_doc__[] = \
1010 "Interface to extended filesystem attributes.\n"
1012 "This module gives access to the extended attributes present\n"
1013 "in some operating systems/filesystems. You can list attributes,\n"
1014 "get, set and remove them.\n"
1016 "The module exposes two sets of functions:\n"
1017 " - the 'old' L{listxattr}, L{getxattr}, L{setxattr}, L{removexattr}\n"
1018 " functions which are deprecated since version 0.4\n"
1019 " - the new L{list}, L{get}, L{get_all}, L{set}, L{remove} functions\n"
1020 " which expose a namespace-aware API and simplify a bit the calling\n"
1021 " model by using keyword arguments\n"
1024 " >>> import xattr\n"
1025 " >>> xattr.listxattr(\"file.txt\")\n"
1026 " ['user.mime_type']\n"
1027 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1029 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1030 "\"Simple text file\")\n"
1031 " >>> xattr.listxattr(\"file.txt\")\n"
1032 " ['user.mime_type', 'user.comment']\n"
1033 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1035 "@note: Most or all errors reported by the system while using the xattr\n"
1036 "library will be reported by raising a L{EnvironmentError}; under Linux,\n"
1037 "the following C{errno} values are used:\n"
1038 " - C{ENOATTR} and C{ENODATA} mean that the attribute name is invalid\n"
1039 " - C{ENOTSUP} and C{EOPNOTSUPP} mean that the filesystem does not\n"
1040 " support extended attributes, or that the namespace is invalid\n"
1041 " - C{E2BIG} mean that the attribute value is too big\n"
1042 " - C{ERANGE} mean that the attribute name is too big (it might also\n"
1043 " mean an error in the xattr module itself)\n"
1044 " - C{ENOSPC} and C{EDQUOT} are documented as meaning out of disk space\n"
1045 " or out of disk space because of quota limits\n"
1047 "@group Deprecated API: *xattr\n"
1048 "@group Namespace constants: NS_*\n"
1049 "@group set function flags: XATTR_CREATE, XATTR_REPLACE\n"
1050 "@sort: list, get, get_all, set, remove, listxattr, getxattr, setxattr\n"
1056 static struct PyModuleDef xattrmodule = {
1057 PyModuleDef_HEAD_INIT,
1064 #define INITERROR return NULL
1070 #define INITERROR return
1076 PyObject *m = PyModule_Create(&xattrmodule);
1078 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1083 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1084 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1085 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1086 PyModule_AddStringConstant(m, "__license__",
1087 "GNU Lesser General Public License (LGPL)");
1088 PyModule_AddStringConstant(m, "__docformat__", "epytext en");
1090 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1091 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1093 /* namespace constants */
1094 PyModule_AddObject(m, "NS_SECURITY", PyBytes_FromString("security"));
1095 PyModule_AddObject(m, "NS_SYSTEM", PyBytes_FromString("system"));
1096 PyModule_AddObject(m, "NS_TRUSTED", PyBytes_FromString("trusted"));
1097 PyModule_AddObject(m, "NS_USER", PyBytes_FromString("user"));