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 /* Used for cpychecker: */
67 /* The checker automatically defines this preprocessor name when creating
68 the custom attribute: */
69 #if defined(WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE)
70 #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
71 __attribute__((cpychecker_negative_result_sets_exception))
73 #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
76 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow)
77 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
79 /** Converts from a string, file or int argument to what we need.
81 * Returns -1 on failure, 0 on success.
83 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
86 if(PyBytes_Check(myobj)) {
87 tgt->type = nofollow ? T_LINK : T_PATH;
88 tgt->name = PyBytes_AS_STRING(myobj);
89 } else if(PyUnicode_Check(myobj)) {
90 tgt->type = nofollow ? T_LINK : T_PATH;
92 PyUnicode_AsEncodedString(myobj,
93 Py_FileSystemDefaultEncoding, "strict");
96 tgt->name = PyBytes_AS_STRING(tgt->tmp);
97 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
101 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
107 /* Combine a namespace string and an attribute name into a
108 fully-qualified name */
109 static int merge_ns(const char *ns, const char *name,
110 const char **result, char **buf) {
113 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
114 if((*buf = PyMem_Malloc(new_size)) == NULL) {
118 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
119 if(cnt > new_size || cnt < 0) {
120 PyErr_SetString(PyExc_ValueError,
121 "can't format the attribute name");
133 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
134 if(tgt->type == T_FD)
135 return flistxattr(tgt->fd, list, size);
136 else if (tgt->type == T_LINK)
137 return llistxattr(tgt->name, list, size);
139 return listxattr(tgt->name, list, size);
142 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
144 if(tgt->type == T_FD)
145 return fgetxattr(tgt->fd, name, value, size);
146 else if (tgt->type == T_LINK)
147 return lgetxattr(tgt->name, name, value, size);
149 return getxattr(tgt->name, name, value, size);
152 static int _set_obj(target_t *tgt, const char *name,
153 const void *value, size_t size, int flags) {
154 if(tgt->type == T_FD)
155 return fsetxattr(tgt->fd, name, value, size, flags);
156 else if (tgt->type == T_LINK)
157 return lsetxattr(tgt->name, name, value, size, flags);
159 return setxattr(tgt->name, name, value, size, flags);
162 static int _remove_obj(target_t *tgt, const char *name) {
163 if(tgt->type == T_FD)
164 return fremovexattr(tgt->fd, name);
165 else if (tgt->type == T_LINK)
166 return lremovexattr(tgt->name, name);
168 return removexattr(tgt->name, name);
172 Checks if an attribute name matches an optional namespace.
174 If the namespace is NULL, it will return the name itself. If the
175 namespace is non-NULL and the name matches, it will return a
176 pointer to the offset in the name after the namespace and the
177 separator. If however the name doesn't match the namespace, it will
180 const char *matches_ns(const char *ns, const char *name) {
184 ns_size = strlen(ns);
186 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
187 name[ns_size] == '.')
188 return name + ns_size + 1;
192 /* Wrapper for getxattr */
193 static char __pygetxattr_doc__[] =
194 "Get the value of a given extended attribute (deprecated).\n"
197 " - a string representing filename, or a file-like object,\n"
198 " or a file descriptor; this represents the file on \n"
200 " - a string, representing the attribute whose value to retrieve;\n"
201 " usually in form of system.posix_acl or user.mime_type\n"
202 " - (optional) a boolean value (defaults to false), which, if\n"
203 " the file name given is a symbolic link, makes the\n"
204 " function operate on the symbolic link itself instead\n"
206 "@deprecated: since version 0.4, this function has been deprecated\n"
207 " by the L{get} function\n"
211 pygetxattr(PyObject *self, PyObject *args)
216 char *attrname = NULL;
218 ssize_t nalloc, nret;
221 /* Parse the arguments */
222 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
224 if(convertObj(myarg, &tgt, nofollow) < 0) {
229 /* Find out the needed size of the buffer */
230 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
231 res = PyErr_SetFromErrno(PyExc_IOError);
235 /* Try to allocate the memory, using Python's allocator */
236 if((buf = PyMem_Malloc(nalloc)) == NULL) {
237 res = PyErr_NoMemory();
241 /* Now retrieve the attribute value */
242 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
243 res = PyErr_SetFromErrno(PyExc_IOError);
247 /* Create the string which will hold the result */
248 res = PyBytes_FromStringAndSize(buf, nret);
251 /* Free the buffer, now it is no longer needed */
256 PyMem_Free(attrname);
258 /* Return the result */
262 /* Wrapper for getxattr */
263 static char __get_doc__[] =
264 "Get the value of a given extended attribute.\n"
267 " >>> xattr.get('/path/to/file', 'user.comment')\n"
269 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
272 "@param item: the item to query; either a string representing the\n"
273 " filename, or a file-like object, or a file descriptor\n"
274 "@param name: the attribute whose value to set; usually in form of\n"
275 " system.posix_acl or user.mime_type\n"
276 "@type name: string\n"
277 "@param nofollow: if given and True, and the function is passed a\n"
278 " filename that points to a symlink, the function will act on the\n"
279 " symlink itself instead of its target\n"
280 "@type nofollow: boolean\n"
281 "@param namespace: if given, the attribute must not contain the\n"
282 " namespace itself, but instead the namespace will be taken from\n"
284 "@type namespace: string\n"
285 "@return: the value of the extended attribute (can contain NULLs)\n"
287 "@raise EnvironmentError: caused by any system errors\n"
292 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
297 char *attrname = NULL, *namebuf;
298 const char *fullname;
300 const char *ns = NULL;
301 ssize_t nalloc, nret;
303 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
305 /* Parse the arguments */
306 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
307 &myarg, NULL, &attrname, &nofollow, &ns))
309 if(convertObj(myarg, &tgt, nofollow) < 0) {
314 merge_ns(ns, attrname, &fullname, &namebuf);
316 /* Find out the needed size of the buffer */
317 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
318 res = PyErr_SetFromErrno(PyExc_IOError);
322 /* Try to allocate the memory, using Python's allocator */
323 if((buf = PyMem_Malloc(nalloc)) == NULL) {
324 res = PyErr_NoMemory();
328 /* Now retrieve the attribute value */
329 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
330 res = PyErr_SetFromErrno(PyExc_IOError);
334 /* Create the string which will hold the result */
335 res = PyBytes_FromStringAndSize(buf, nret);
337 /* Free the buffers, they are no longer needed */
345 PyMem_Free(attrname);
347 /* Return the result */
351 /* Wrapper for getxattr */
352 static char __get_all_doc__[] =
353 "Get all the extended attributes of an item.\n"
355 "This function performs a bulk-get of all extended attribute names\n"
356 "and the corresponding value.\n"
358 " >>> xattr.get_all('/path/to/file')\n"
359 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
360 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
361 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
362 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
364 "@param item: the item to query; either a string representing the\n"
365 " filename, or a file-like object, or a file descriptor\n"
366 "@keyword namespace: an optional namespace for filtering the\n"
367 " attributes; for example, querying all user attributes can be\n"
368 " accomplished by passing namespace=L{NS_USER}\n"
369 "@type namespace: string\n"
370 "@keyword nofollow: if passed and true, if the target file is a\n"
371 " symbolic link, the attributes for the link itself will be\n"
372 " returned, instead of the attributes of the target\n"
373 "@type nofollow: boolean\n"
374 "@return: list of tuples (name, value); note that if a namespace\n"
375 " argument was passed, it (and the separator) will be stripped from\n"
376 " the names returned\n"
378 "@raise EnvironmentError: caused by any system errors\n"
379 "@note: Since reading the whole attribute list is not an atomic\n"
380 " operation, it might be possible that attributes are added\n"
381 " or removed between the initial query and the actual reading\n"
382 " of the attributes; the returned list will contain only the\n"
383 " attributes that were present at the initial listing of the\n"
384 " attribute names and that were still present when the read\n"
385 " attempt for the value is made.\n"
390 get_all(PyObject *self, PyObject *args, PyObject *keywds)
392 PyObject *myarg, *res;
394 const char *ns = NULL;
395 char *buf_list, *buf_val;
397 ssize_t nalloc, nlist, nval;
400 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
402 /* Parse the arguments */
403 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
404 &myarg, &nofollow, &ns))
406 if(convertObj(myarg, &tgt, nofollow) < 0)
409 /* Compute first the list of attributes */
411 /* Find out the needed size of the buffer for the attribute list */
412 nalloc = _list_obj(&tgt, NULL, 0);
415 res = PyErr_SetFromErrno(PyExc_IOError);
419 /* Try to allocate the memory, using Python's allocator */
420 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
421 res = PyErr_NoMemory();
425 /* Now retrieve the list of attributes */
426 nlist = _list_obj(&tgt, buf_list, nalloc);
429 res = PyErr_SetFromErrno(PyExc_IOError);
433 /* Create the list which will hold the result */
434 mylist = PyList_New(0);
440 nalloc = ESTIMATE_ATTR_SIZE;
441 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
443 res = PyErr_NoMemory();
447 /* Create and insert the attributes as strings in the list */
448 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
453 if((name=matches_ns(ns, s))==NULL)
455 /* Now retrieve the attribute value */
458 nval = _get_obj(&tgt, s, buf_val, nalloc);
461 if(errno == ERANGE) {
462 nval = _get_obj(&tgt, s, NULL, 0);
463 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
464 res = PyErr_NoMemory();
470 } else if(errno == ENODATA || errno == ENOATTR) {
471 /* this attribute has gone away since we queried
472 the attribute list */
476 /* else we're dealing with a different error, which we
477 don't know how to handle nicely, so we abort */
479 res = PyErr_SetFromErrno(PyExc_IOError);
487 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
489 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
491 if (my_tuple == NULL) {
496 PyList_Append(mylist, my_tuple);
500 /* Successful exit */
507 PyMem_Free(buf_list);
512 /* Return the result */
517 static char __pysetxattr_doc__[] =
518 "Set the value of a given extended attribute (deprecated).\n"
520 "Be carefull in case you want to set attributes on symbolic\n"
521 "links, you have to use all the 5 parameters; use 0 for the \n"
522 "flags value if you want the default behavior (create or "
526 " - a string representing filename, or a file-like object,\n"
527 " or a file descriptor; this represents the file on \n"
529 " - a string, representing the attribute whose value to set;\n"
530 " usually in form of system.posix_acl or user.mime_type\n"
531 " - a string, possibly with embedded NULLs; note that there\n"
532 " are restrictions regarding the size of the value, for\n"
533 " example, for ext2/ext3, maximum size is the block size\n"
534 " - (optional) flags; if 0 or ommited the attribute will be \n"
535 " created or replaced; if XATTR_CREATE, the attribute \n"
536 " will be created, giving an error if it already exists;\n"
537 " of XATTR_REPLACE, the attribute will be replaced,\n"
538 " giving an error if it doesn't exists;\n"
539 " - (optional) a boolean value (defaults to false), which, if\n"
540 " the file name given is a symbolic link, makes the\n"
541 " function operate on the symbolic link itself instead\n"
543 "@deprecated: since version 0.4, this function has been deprecated\n"
544 " by the L{set} function\n"
547 /* Wrapper for setxattr */
549 pysetxattr(PyObject *self, PyObject *args)
551 PyObject *myarg, *res;
553 char *attrname = NULL;
560 /* Parse the arguments */
561 if (!PyArg_ParseTuple(args, "Oetet#|bi", &myarg, NULL, &attrname,
562 NULL, &buf, &bufsize, &flags, &nofollow))
564 if(convertObj(myarg, &tgt, nofollow) < 0) {
569 /* Set the attribute's value */
570 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
575 res = PyErr_SetFromErrno(PyExc_IOError);
583 PyMem_Free(attrname);
586 /* Return the result */
590 static char __set_doc__[] =
591 "Set the value of a given extended attribute.\n"
594 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
595 " >>> xattr.set('/path/to/file', 'comment', 'test',"
596 " namespace=xattr.NS_USER)\n"
598 "@param item: the item to query; either a string representing the\n"
599 " filename, or a file-like object, or a file descriptor\n"
600 "@param name: the attribute whose value to set; usually in form of\n"
601 " system.posix_acl or user.mime_type\n"
602 "@type name: string\n"
603 "@param value: a string, possibly with embedded NULLs; note that there\n"
604 " are restrictions regarding the size of the value, for\n"
605 " example, for ext2/ext3, maximum size is the block size\n"
606 "@type value: string\n"
607 "@param flags: if 0 or ommited the attribute will be\n"
608 " created or replaced; if L{XATTR_CREATE}, the attribute\n"
609 " will be created, giving an error if it already exists;\n"
610 " if L{XATTR_REPLACE}, the attribute will be replaced,\n"
611 " giving an error if it doesn't exists;\n"
612 "@type flags: integer\n"
613 "@param nofollow: if given and True, and the function is passed a\n"
614 " filename that points to a symlink, the function will act on the\n"
615 " symlink itself instead of its target\n"
616 "@type nofollow: boolean\n"
617 "@param namespace: if given, the attribute must not contain the\n"
618 " namespace itself, but instead the namespace will be taken from\n"
620 "@type namespace: string\n"
622 "@raise EnvironmentError: caused by any system errors\n"
626 /* Wrapper for setxattr */
628 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
630 PyObject *myarg, *res;
632 char *attrname = NULL;
638 const char *ns = NULL;
640 const char *full_name;
641 static char *kwlist[] = {"item", "name", "value", "flags",
642 "nofollow", "namespace", NULL};
644 /* Parse the arguments */
645 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|iiz", kwlist,
646 &myarg, NULL, &attrname, NULL,
647 &buf, &bufsize, &flags, &nofollow, &ns))
649 if(convertObj(myarg, &tgt, nofollow) < 0) {
654 merge_ns(ns, attrname, &full_name, &newname);
656 /* Set the attribute's value */
657 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
665 res = PyErr_SetFromErrno(PyExc_IOError);
673 PyMem_Free(attrname);
676 /* Return the result */
681 static char __pyremovexattr_doc__[] =
682 "Remove an attribute from a file (deprecated).\n"
685 " - a string representing filename, or a file-like object,\n"
686 " or a file descriptor; this represents the file on \n"
688 " - a string, representing the attribute to be removed;\n"
689 " usually in form of system.posix_acl or user.mime_type\n"
690 " - (optional) a boolean value (defaults to false), which, if\n"
691 " the file name given is a symbolic link, makes the\n"
692 " function operate on the symbolic link itself instead\n"
694 "@deprecated: since version 0.4, this function has been deprecated\n"
699 /* Wrapper for removexattr */
701 pyremovexattr(PyObject *self, PyObject *args)
703 PyObject *myarg, *res;
705 char *attrname = NULL;
709 /* Parse the arguments */
710 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
713 if(convertObj(myarg, &tgt, nofollow) < 0) {
718 /* Remove the attribute */
719 nret = _remove_obj(&tgt, attrname);
724 res = PyErr_SetFromErrno(PyExc_IOError);
732 PyMem_Free(attrname);
734 /* Return the result */
738 static char __remove_doc__[] =
739 "Remove an attribute from a file.\n"
742 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
744 "@param item: the item to query; either a string representing the\n"
745 " filename, or a file-like object, or a file descriptor\n"
746 "@param name: the attribute whose value to set; usually in form of\n"
747 " system.posix_acl or user.mime_type\n"
748 "@type name: string\n"
749 "@param nofollow: if given and True, and the function is passed a\n"
750 " filename that points to a symlink, the function will act on the\n"
751 " symlink itself instead of its target\n"
752 "@type nofollow: boolean\n"
753 "@param namespace: if given, the attribute must not contain the\n"
754 " namespace itself, but instead the namespace will be taken from\n"
756 "@type namespace: string\n"
759 "@raise EnvironmentError: caused by any system errors\n"
762 /* Wrapper for removexattr */
764 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
766 PyObject *myarg, *res;
768 char *attrname = NULL, *name_buf;
769 const char *ns = NULL;
770 const char *full_name;
773 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
775 /* Parse the arguments */
776 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
777 &myarg, NULL, &attrname, &nofollow, &ns))
780 if(convertObj(myarg, &tgt, nofollow) < 0) {
785 if(merge_ns(ns, attrname, &full_name, &name_buf) < 0) {
790 /* Remove the attribute */
791 nret = _remove_obj(&tgt, full_name);
793 PyMem_Free(name_buf);
798 res = PyErr_SetFromErrno(PyExc_IOError);
806 PyMem_Free(attrname);
808 /* Return the result */
812 static char __pylistxattr_doc__[] =
813 "Return the list of attribute names for a file (deprecated).\n"
816 " - a string representing filename, or a file-like object,\n"
817 " or a file descriptor; this represents the file to \n"
819 " - (optional) a boolean value (defaults to false), which, if\n"
820 " the file name given is a symbolic link, makes the\n"
821 " function operate on the symbolic link itself instead\n"
823 "@deprecated: since version 0.4, this function has been deprecated\n"
828 /* Wrapper for listxattr */
830 pylistxattr(PyObject *self, PyObject *args)
834 ssize_t nalloc, nret;
841 /* Parse the arguments */
842 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
844 if(convertObj(myarg, &tgt, nofollow) < 0)
847 /* Find out the needed size of the buffer */
848 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
849 mylist = PyErr_SetFromErrno(PyExc_IOError);
853 /* Try to allocate the memory, using Python's allocator */
854 if((buf = PyMem_Malloc(nalloc)) == NULL) {
855 mylist = PyErr_NoMemory();
859 /* Now retrieve the list of attributes */
860 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
861 mylist = PyErr_SetFromErrno(PyExc_IOError);
865 /* Compute the number of attributes in the list */
866 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
870 /* Create the list which will hold the result */
871 mylist = PyList_New(nattrs);
875 /* Create and insert the attributes as strings in the list */
876 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
877 PyObject *item = PyBytes_FromString(s);
883 PyList_SET_ITEM(mylist, nattrs, item);
888 /* Free the buffer, now it is no longer needed */
894 /* Return the result */
898 static char __list_doc__[] =
899 "Return the list of attribute names for a file.\n"
902 " >>> xattr.list('/path/to/file')\n"
903 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
904 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
905 " ['test', 'comment']\n"
907 "@param item: the item to query; either a string representing the\n"
908 " filename, or a file-like object, or a file descriptor\n"
909 "@param nofollow: if given and True, and the function is passed a\n"
910 " filename that points to a symlink, the function will act on the\n"
911 " symlink itself instead of its target\n"
912 "@type nofollow: boolean\n"
913 "@param namespace: if given, the attribute must not contain the\n"
914 " namespace itself, but instead the namespace will be taken from\n"
916 "@type namespace: string\n"
917 "@return: list of strings; note that if a namespace argument was\n"
918 " passed, it (and the separator) will be stripped from the names\n"
921 "@raise EnvironmentError: caused by any system errors\n"
925 /* Wrapper for listxattr */
927 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
931 ssize_t nalloc, nret;
938 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
940 /* Parse the arguments */
941 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iet", kwlist,
942 &myarg, &nofollow, NULL, &ns))
944 if(convertObj(myarg, &tgt, nofollow) < 0) {
949 /* Find out the needed size of the buffer */
950 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
951 res = PyErr_SetFromErrno(PyExc_IOError);
955 /* Try to allocate the memory, using Python's allocator */
956 if((buf = PyMem_Malloc(nalloc)) == NULL) {
957 res = PyErr_NoMemory();
961 /* Now retrieve the list of attributes */
962 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
963 res = PyErr_SetFromErrno(PyExc_IOError);
967 /* Compute the number of attributes in the list */
968 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
969 if(matches_ns(ns, s) != NULL)
972 /* Create the list which will hold the result */
973 res = PyList_New(nattrs);
977 /* Create and insert the attributes as strings in the list */
978 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
979 const char *name = matches_ns(ns, s);
981 PyObject *item = PyBytes_FromString(name);
987 PyList_SET_ITEM(res, nattrs, item);
993 /* Free the buffer, now it is no longer needed */
1001 /* Return the result */
1005 static PyMethodDef xattr_methods[] = {
1006 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
1007 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
1009 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
1011 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
1012 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
1014 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
1015 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
1017 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
1018 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
1020 {NULL, NULL, 0, NULL} /* Sentinel */
1023 static char __xattr_doc__[] = \
1024 "Interface to extended filesystem attributes.\n"
1026 "This module gives access to the extended attributes present\n"
1027 "in some operating systems/filesystems. You can list attributes,\n"
1028 "get, set and remove them.\n"
1030 "The module exposes two sets of functions:\n"
1031 " - the 'old' L{listxattr}, L{getxattr}, L{setxattr}, L{removexattr}\n"
1032 " functions which are deprecated since version 0.4\n"
1033 " - the new L{list}, L{get}, L{get_all}, L{set}, L{remove} functions\n"
1034 " which expose a namespace-aware API and simplify a bit the calling\n"
1035 " model by using keyword arguments\n"
1038 " >>> import xattr\n"
1039 " >>> xattr.listxattr(\"file.txt\")\n"
1040 " ['user.mime_type']\n"
1041 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1043 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1044 "\"Simple text file\")\n"
1045 " >>> xattr.listxattr(\"file.txt\")\n"
1046 " ['user.mime_type', 'user.comment']\n"
1047 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1049 "@note: Most or all errors reported by the system while using the xattr\n"
1050 "library will be reported by raising a L{EnvironmentError}; under Linux,\n"
1051 "the following C{errno} values are used:\n"
1052 " - C{ENOATTR} and C{ENODATA} mean that the attribute name is invalid\n"
1053 " - C{ENOTSUP} and C{EOPNOTSUPP} mean that the filesystem does not\n"
1054 " support extended attributes, or that the namespace is invalid\n"
1055 " - C{E2BIG} mean that the attribute value is too big\n"
1056 " - C{ERANGE} mean that the attribute name is too big (it might also\n"
1057 " mean an error in the xattr module itself)\n"
1058 " - C{ENOSPC} and C{EDQUOT} are documented as meaning out of disk space\n"
1059 " or out of disk space because of quota limits\n"
1061 "@group Deprecated API: *xattr\n"
1062 "@group Namespace constants: NS_*\n"
1063 "@group set function flags: XATTR_CREATE, XATTR_REPLACE\n"
1064 "@sort: list, get, get_all, set, remove, listxattr, getxattr, setxattr\n"
1070 static struct PyModuleDef xattrmodule = {
1071 PyModuleDef_HEAD_INIT,
1078 #define INITERROR return NULL
1084 #define INITERROR return
1090 PyObject *m = PyModule_Create(&xattrmodule);
1092 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1097 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1098 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1099 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1100 PyModule_AddStringConstant(m, "__license__",
1101 "GNU Lesser General Public License (LGPL)");
1102 PyModule_AddStringConstant(m, "__docformat__", "epytext en");
1104 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1105 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1107 /* namespace constants */
1108 PyModule_AddObject(m, "NS_SECURITY", PyBytes_FromString("security"));
1109 PyModule_AddObject(m, "NS_SYSTEM", PyBytes_FromString("system"));
1110 PyModule_AddObject(m, "NS_TRUSTED", PyBytes_FromString("trusted"));
1111 PyModule_AddObject(m, "NS_USER", PyBytes_FromString("user"));