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 static int merge_ns(const char *ns, const char *name,
80 const char **result, char **buf)
81 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
84 /** Converts from a string, file or int argument to what we need.
86 * Returns -1 on failure, 0 on success.
88 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
91 if(PyBytes_Check(myobj)) {
92 tgt->type = nofollow ? T_LINK : T_PATH;
93 tgt->name = PyBytes_AS_STRING(myobj);
94 } else if(PyUnicode_Check(myobj)) {
95 tgt->type = nofollow ? T_LINK : T_PATH;
97 PyUnicode_AsEncodedString(myobj,
98 Py_FileSystemDefaultEncoding, "strict");
101 tgt->name = PyBytes_AS_STRING(tgt->tmp);
102 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
106 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
112 /* Combine a namespace string and an attribute name into a
113 fully-qualified name */
114 static int merge_ns(const char *ns, const char *name,
115 const char **result, char **buf) {
118 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
119 if((*buf = PyMem_Malloc(new_size)) == NULL) {
123 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
124 if(cnt > new_size || cnt < 0) {
125 PyErr_SetString(PyExc_ValueError,
126 "can't format the attribute name");
138 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
139 if(tgt->type == T_FD)
140 return flistxattr(tgt->fd, list, size);
141 else if (tgt->type == T_LINK)
142 return llistxattr(tgt->name, list, size);
144 return listxattr(tgt->name, list, size);
147 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
149 if(tgt->type == T_FD)
150 return fgetxattr(tgt->fd, name, value, size);
151 else if (tgt->type == T_LINK)
152 return lgetxattr(tgt->name, name, value, size);
154 return getxattr(tgt->name, name, value, size);
157 static int _set_obj(target_t *tgt, const char *name,
158 const void *value, size_t size, int flags) {
159 if(tgt->type == T_FD)
160 return fsetxattr(tgt->fd, name, value, size, flags);
161 else if (tgt->type == T_LINK)
162 return lsetxattr(tgt->name, name, value, size, flags);
164 return setxattr(tgt->name, name, value, size, flags);
167 static int _remove_obj(target_t *tgt, const char *name) {
168 if(tgt->type == T_FD)
169 return fremovexattr(tgt->fd, name);
170 else if (tgt->type == T_LINK)
171 return lremovexattr(tgt->name, name);
173 return removexattr(tgt->name, name);
177 Checks if an attribute name matches an optional namespace.
179 If the namespace is NULL, it will return the name itself. If the
180 namespace is non-NULL and the name matches, it will return a
181 pointer to the offset in the name after the namespace and the
182 separator. If however the name doesn't match the namespace, it will
185 const char *matches_ns(const char *ns, const char *name) {
189 ns_size = strlen(ns);
191 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
192 name[ns_size] == '.')
193 return name + ns_size + 1;
197 /* Wrapper for getxattr */
198 static char __pygetxattr_doc__[] =
199 "Get the value of a given extended attribute (deprecated).\n"
202 " - a string representing filename, or a file-like object,\n"
203 " or a file descriptor; this represents the file on \n"
205 " - a string, representing the attribute whose value to retrieve;\n"
206 " usually in form of system.posix_acl or user.mime_type\n"
207 " - (optional) a boolean value (defaults to false), which, if\n"
208 " the file name given is a symbolic link, makes the\n"
209 " function operate on the symbolic link itself instead\n"
211 "@deprecated: since version 0.4, this function has been deprecated\n"
212 " by the L{get} function\n"
216 pygetxattr(PyObject *self, PyObject *args)
221 char *attrname = NULL;
223 ssize_t nalloc, nret;
226 /* Parse the arguments */
227 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
229 if(convertObj(myarg, &tgt, nofollow) < 0) {
234 /* Find out the needed size of the buffer */
235 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
236 res = PyErr_SetFromErrno(PyExc_IOError);
240 /* Try to allocate the memory, using Python's allocator */
241 if((buf = PyMem_Malloc(nalloc)) == NULL) {
242 res = PyErr_NoMemory();
246 /* Now retrieve the attribute value */
247 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
248 res = PyErr_SetFromErrno(PyExc_IOError);
252 /* Create the string which will hold the result */
253 res = PyBytes_FromStringAndSize(buf, nret);
256 /* Free the buffer, now it is no longer needed */
261 PyMem_Free(attrname);
263 /* Return the result */
267 /* Wrapper for getxattr */
268 static char __get_doc__[] =
269 "Get the value of a given extended attribute.\n"
272 " >>> xattr.get('/path/to/file', 'user.comment')\n"
274 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
277 "@param item: the item to query; either a string representing the\n"
278 " filename, or a file-like object, or a file descriptor\n"
279 "@param name: the attribute whose value to set; usually in form of\n"
280 " system.posix_acl or user.mime_type\n"
281 "@type name: string\n"
282 "@param nofollow: if given and True, and the function is passed a\n"
283 " filename that points to a symlink, the function will act on the\n"
284 " symlink itself instead of its target\n"
285 "@type nofollow: boolean\n"
286 "@param namespace: if given, the attribute must not contain the\n"
287 " namespace itself, but instead the namespace will be taken from\n"
289 "@type namespace: string\n"
290 "@return: the value of the extended attribute (can contain NULLs)\n"
292 "@raise EnvironmentError: caused by any system errors\n"
297 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
302 char *attrname = NULL, *namebuf;
303 const char *fullname;
305 const char *ns = NULL;
306 ssize_t nalloc, nret;
308 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
310 /* Parse the arguments */
311 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
312 &myarg, NULL, &attrname, &nofollow, &ns))
314 if(convertObj(myarg, &tgt, nofollow) < 0) {
319 if(merge_ns(ns, attrname, &fullname, &namebuf) < 0) {
324 /* Find out the needed size of the buffer */
325 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
326 res = PyErr_SetFromErrno(PyExc_IOError);
330 /* Try to allocate the memory, using Python's allocator */
331 if((buf = PyMem_Malloc(nalloc)) == NULL) {
332 res = PyErr_NoMemory();
336 /* Now retrieve the attribute value */
337 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
338 res = PyErr_SetFromErrno(PyExc_IOError);
342 /* Create the string which will hold the result */
343 res = PyBytes_FromStringAndSize(buf, nret);
345 /* Free the buffers, they are no longer needed */
353 PyMem_Free(attrname);
355 /* Return the result */
359 /* Wrapper for getxattr */
360 static char __get_all_doc__[] =
361 "Get all the extended attributes of an item.\n"
363 "This function performs a bulk-get of all extended attribute names\n"
364 "and the corresponding value.\n"
366 " >>> xattr.get_all('/path/to/file')\n"
367 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
368 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
369 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
370 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
372 "@param item: the item to query; either a string representing the\n"
373 " filename, or a file-like object, or a file descriptor\n"
374 "@keyword namespace: an optional namespace for filtering the\n"
375 " attributes; for example, querying all user attributes can be\n"
376 " accomplished by passing namespace=L{NS_USER}\n"
377 "@type namespace: string\n"
378 "@keyword nofollow: if passed and true, if the target file is a\n"
379 " symbolic link, the attributes for the link itself will be\n"
380 " returned, instead of the attributes of the target\n"
381 "@type nofollow: boolean\n"
382 "@return: list of tuples (name, value); note that if a namespace\n"
383 " argument was passed, it (and the separator) will be stripped from\n"
384 " the names returned\n"
386 "@raise EnvironmentError: caused by any system errors\n"
387 "@note: Since reading the whole attribute list is not an atomic\n"
388 " operation, it might be possible that attributes are added\n"
389 " or removed between the initial query and the actual reading\n"
390 " of the attributes; the returned list will contain only the\n"
391 " attributes that were present at the initial listing of the\n"
392 " attribute names and that were still present when the read\n"
393 " attempt for the value is made.\n"
398 get_all(PyObject *self, PyObject *args, PyObject *keywds)
400 PyObject *myarg, *res;
402 const char *ns = NULL;
403 char *buf_list, *buf_val;
405 ssize_t nalloc, nlist, nval;
408 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
410 /* Parse the arguments */
411 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
412 &myarg, &nofollow, &ns))
414 if(convertObj(myarg, &tgt, nofollow) < 0)
417 /* Compute first the list of attributes */
419 /* Find out the needed size of the buffer for the attribute list */
420 nalloc = _list_obj(&tgt, NULL, 0);
423 res = PyErr_SetFromErrno(PyExc_IOError);
427 /* Try to allocate the memory, using Python's allocator */
428 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
429 res = PyErr_NoMemory();
433 /* Now retrieve the list of attributes */
434 nlist = _list_obj(&tgt, buf_list, nalloc);
437 res = PyErr_SetFromErrno(PyExc_IOError);
441 /* Create the list which will hold the result */
442 mylist = PyList_New(0);
448 nalloc = ESTIMATE_ATTR_SIZE;
449 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
451 res = PyErr_NoMemory();
455 /* Create and insert the attributes as strings in the list */
456 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
461 if((name=matches_ns(ns, s))==NULL)
463 /* Now retrieve the attribute value */
466 nval = _get_obj(&tgt, s, buf_val, nalloc);
469 if(errno == ERANGE) {
470 nval = _get_obj(&tgt, s, NULL, 0);
471 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
472 res = PyErr_NoMemory();
478 } else if(errno == ENODATA || errno == ENOATTR) {
479 /* this attribute has gone away since we queried
480 the attribute list */
484 /* else we're dealing with a different error, which we
485 don't know how to handle nicely, so we abort */
487 res = PyErr_SetFromErrno(PyExc_IOError);
495 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
497 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
499 if (my_tuple == NULL) {
504 PyList_Append(mylist, my_tuple);
508 /* Successful exit */
515 PyMem_Free(buf_list);
520 /* Return the result */
525 static char __pysetxattr_doc__[] =
526 "Set the value of a given extended attribute (deprecated).\n"
528 "Be carefull in case you want to set attributes on symbolic\n"
529 "links, you have to use all the 5 parameters; use 0 for the \n"
530 "flags value if you want the default behavior (create or "
534 " - a string representing filename, or a file-like object,\n"
535 " or a file descriptor; this represents the file on \n"
537 " - a string, representing the attribute whose value to set;\n"
538 " usually in form of system.posix_acl or user.mime_type\n"
539 " - a string, possibly with embedded NULLs; note that there\n"
540 " are restrictions regarding the size of the value, for\n"
541 " example, for ext2/ext3, maximum size is the block size\n"
542 " - (optional) flags; if 0 or ommited the attribute will be \n"
543 " created or replaced; if XATTR_CREATE, the attribute \n"
544 " will be created, giving an error if it already exists;\n"
545 " of XATTR_REPLACE, the attribute will be replaced,\n"
546 " giving an error if it doesn't exists;\n"
547 " - (optional) a boolean value (defaults to false), which, if\n"
548 " the file name given is a symbolic link, makes the\n"
549 " function operate on the symbolic link itself instead\n"
551 "@deprecated: since version 0.4, this function has been deprecated\n"
552 " by the L{set} function\n"
555 /* Wrapper for setxattr */
557 pysetxattr(PyObject *self, PyObject *args)
559 PyObject *myarg, *res;
561 char *attrname = NULL;
568 /* Parse the arguments */
569 if (!PyArg_ParseTuple(args, "Oetet#|ii", &myarg, NULL, &attrname,
570 NULL, &buf, &bufsize, &flags, &nofollow))
572 if(convertObj(myarg, &tgt, nofollow) < 0) {
577 /* Set the attribute's value */
578 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
583 res = PyErr_SetFromErrno(PyExc_IOError);
591 PyMem_Free(attrname);
594 /* Return the result */
598 static char __set_doc__[] =
599 "Set the value of a given extended attribute.\n"
602 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
603 " >>> xattr.set('/path/to/file', 'comment', 'test',"
604 " namespace=xattr.NS_USER)\n"
606 "@param item: the item to query; either a string representing the\n"
607 " filename, or a file-like object, or a file descriptor\n"
608 "@param name: the attribute whose value to set; usually in form of\n"
609 " system.posix_acl or user.mime_type\n"
610 "@type name: string\n"
611 "@param value: a string, possibly with embedded NULLs; note that there\n"
612 " are restrictions regarding the size of the value, for\n"
613 " example, for ext2/ext3, maximum size is the block size\n"
614 "@type value: string\n"
615 "@param flags: if 0 or ommited the attribute will be\n"
616 " created or replaced; if L{XATTR_CREATE}, the attribute\n"
617 " will be created, giving an error if it already exists;\n"
618 " if L{XATTR_REPLACE}, the attribute will be replaced,\n"
619 " giving an error if it doesn't exists;\n"
620 "@type flags: integer\n"
621 "@param nofollow: if given and True, and the function is passed a\n"
622 " filename that points to a symlink, the function will act on the\n"
623 " symlink itself instead of its target\n"
624 "@type nofollow: boolean\n"
625 "@param namespace: if given, the attribute must not contain the\n"
626 " namespace itself, but instead the namespace will be taken from\n"
628 "@type namespace: string\n"
630 "@raise EnvironmentError: caused by any system errors\n"
634 /* Wrapper for setxattr */
636 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
638 PyObject *myarg, *res;
640 char *attrname = NULL;
646 const char *ns = NULL;
648 const char *full_name;
649 static char *kwlist[] = {"item", "name", "value", "flags",
650 "nofollow", "namespace", NULL};
652 /* Parse the arguments */
653 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|iiz", kwlist,
654 &myarg, NULL, &attrname, NULL,
655 &buf, &bufsize, &flags, &nofollow, &ns))
657 if(convertObj(myarg, &tgt, nofollow) < 0) {
662 if(merge_ns(ns, attrname, &full_name, &newname) < 0) {
667 /* Set the attribute's value */
668 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
675 res = PyErr_SetFromErrno(PyExc_IOError);
683 PyMem_Free(attrname);
686 /* Return the result */
691 static char __pyremovexattr_doc__[] =
692 "Remove an attribute from a file (deprecated).\n"
695 " - a string representing filename, or a file-like object,\n"
696 " or a file descriptor; this represents the file on \n"
698 " - a string, representing the attribute to be removed;\n"
699 " usually in form of system.posix_acl or user.mime_type\n"
700 " - (optional) a boolean value (defaults to false), which, if\n"
701 " the file name given is a symbolic link, makes the\n"
702 " function operate on the symbolic link itself instead\n"
704 "@deprecated: since version 0.4, this function has been deprecated\n"
709 /* Wrapper for removexattr */
711 pyremovexattr(PyObject *self, PyObject *args)
713 PyObject *myarg, *res;
715 char *attrname = NULL;
719 /* Parse the arguments */
720 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
723 if(convertObj(myarg, &tgt, nofollow) < 0) {
728 /* Remove the attribute */
729 nret = _remove_obj(&tgt, attrname);
734 res = PyErr_SetFromErrno(PyExc_IOError);
742 PyMem_Free(attrname);
744 /* Return the result */
748 static char __remove_doc__[] =
749 "Remove an attribute from a file.\n"
752 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
754 "@param item: the item to query; either a string representing the\n"
755 " filename, or a file-like object, or a file descriptor\n"
756 "@param name: the attribute whose value to set; usually in form of\n"
757 " system.posix_acl or user.mime_type\n"
758 "@type name: string\n"
759 "@param nofollow: if given and True, and the function is passed a\n"
760 " filename that points to a symlink, the function will act on the\n"
761 " symlink itself instead of its target\n"
762 "@type nofollow: boolean\n"
763 "@param namespace: if given, the attribute must not contain the\n"
764 " namespace itself, but instead the namespace will be taken from\n"
766 "@type namespace: string\n"
769 "@raise EnvironmentError: caused by any system errors\n"
772 /* Wrapper for removexattr */
774 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
776 PyObject *myarg, *res;
778 char *attrname = NULL, *name_buf;
779 const char *ns = NULL;
780 const char *full_name;
783 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
785 /* Parse the arguments */
786 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
787 &myarg, NULL, &attrname, &nofollow, &ns))
790 if(convertObj(myarg, &tgt, nofollow) < 0) {
795 if(merge_ns(ns, attrname, &full_name, &name_buf) < 0) {
800 /* Remove the attribute */
801 nret = _remove_obj(&tgt, full_name);
803 PyMem_Free(name_buf);
808 res = PyErr_SetFromErrno(PyExc_IOError);
816 PyMem_Free(attrname);
818 /* Return the result */
822 static char __pylistxattr_doc__[] =
823 "Return the list of attribute names for a file (deprecated).\n"
826 " - a string representing filename, or a file-like object,\n"
827 " or a file descriptor; this represents the file to \n"
829 " - (optional) a boolean value (defaults to false), which, if\n"
830 " the file name given is a symbolic link, makes the\n"
831 " function operate on the symbolic link itself instead\n"
833 "@deprecated: since version 0.4, this function has been deprecated\n"
838 /* Wrapper for listxattr */
840 pylistxattr(PyObject *self, PyObject *args)
844 ssize_t nalloc, nret;
851 /* Parse the arguments */
852 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
854 if(convertObj(myarg, &tgt, nofollow) < 0)
857 /* Find out the needed size of the buffer */
858 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
859 mylist = PyErr_SetFromErrno(PyExc_IOError);
863 /* Try to allocate the memory, using Python's allocator */
864 if((buf = PyMem_Malloc(nalloc)) == NULL) {
865 mylist = PyErr_NoMemory();
869 /* Now retrieve the list of attributes */
870 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
871 mylist = PyErr_SetFromErrno(PyExc_IOError);
875 /* Compute the number of attributes in the list */
876 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
880 /* Create the list which will hold the result */
881 mylist = PyList_New(nattrs);
885 /* Create and insert the attributes as strings in the list */
886 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
887 PyObject *item = PyBytes_FromString(s);
893 PyList_SET_ITEM(mylist, nattrs, item);
898 /* Free the buffer, now it is no longer needed */
904 /* Return the result */
908 static char __list_doc__[] =
909 "Return the list of attribute names for a file.\n"
912 " >>> xattr.list('/path/to/file')\n"
913 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
914 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
915 " ['test', 'comment']\n"
917 "@param item: the item to query; either a string representing the\n"
918 " filename, or a file-like object, or a file descriptor\n"
919 "@param nofollow: if given and True, and the function is passed a\n"
920 " filename that points to a symlink, the function will act on the\n"
921 " symlink itself instead of its target\n"
922 "@type nofollow: boolean\n"
923 "@param namespace: if given, the attribute must not contain the\n"
924 " namespace itself, but instead the namespace will be taken from\n"
926 "@type namespace: string\n"
927 "@return: list of strings; note that if a namespace argument was\n"
928 " passed, it (and the separator) will be stripped from the names\n"
931 "@raise EnvironmentError: caused by any system errors\n"
935 /* Wrapper for listxattr */
937 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
941 ssize_t nalloc, nret;
948 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
950 /* Parse the arguments */
951 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iet", kwlist,
952 &myarg, &nofollow, NULL, &ns))
954 if(convertObj(myarg, &tgt, nofollow) < 0) {
959 /* Find out the needed size of the buffer */
960 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
961 res = PyErr_SetFromErrno(PyExc_IOError);
965 /* Try to allocate the memory, using Python's allocator */
966 if((buf = PyMem_Malloc(nalloc)) == NULL) {
967 res = PyErr_NoMemory();
971 /* Now retrieve the list of attributes */
972 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
973 res = PyErr_SetFromErrno(PyExc_IOError);
977 /* Compute the number of attributes in the list */
978 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
979 if(matches_ns(ns, s) != NULL)
982 /* Create the list which will hold the result */
983 res = PyList_New(nattrs);
987 /* Create and insert the attributes as strings in the list */
988 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
989 const char *name = matches_ns(ns, s);
991 PyObject *item = PyBytes_FromString(name);
997 PyList_SET_ITEM(res, nattrs, item);
1003 /* Free the buffer, now it is no longer needed */
1011 /* Return the result */
1015 static PyMethodDef xattr_methods[] = {
1016 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
1017 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
1019 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
1021 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
1022 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
1024 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
1025 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
1027 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
1028 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
1030 {NULL, NULL, 0, NULL} /* Sentinel */
1033 static char __xattr_doc__[] = \
1034 "Interface to extended filesystem attributes.\n"
1036 "This module gives access to the extended attributes present\n"
1037 "in some operating systems/filesystems. You can list attributes,\n"
1038 "get, set and remove them.\n"
1040 "The module exposes two sets of functions:\n"
1041 " - the 'old' L{listxattr}, L{getxattr}, L{setxattr}, L{removexattr}\n"
1042 " functions which are deprecated since version 0.4\n"
1043 " - the new L{list}, L{get}, L{get_all}, L{set}, L{remove} functions\n"
1044 " which expose a namespace-aware API and simplify a bit the calling\n"
1045 " model by using keyword arguments\n"
1048 " >>> import xattr\n"
1049 " >>> xattr.listxattr(\"file.txt\")\n"
1050 " ['user.mime_type']\n"
1051 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1053 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1054 "\"Simple text file\")\n"
1055 " >>> xattr.listxattr(\"file.txt\")\n"
1056 " ['user.mime_type', 'user.comment']\n"
1057 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1059 "@note: Most or all errors reported by the system while using the xattr\n"
1060 "library will be reported by raising a L{EnvironmentError}; under Linux,\n"
1061 "the following C{errno} values are used:\n"
1062 " - C{ENOATTR} and C{ENODATA} mean that the attribute name is invalid\n"
1063 " - C{ENOTSUP} and C{EOPNOTSUPP} mean that the filesystem does not\n"
1064 " support extended attributes, or that the namespace is invalid\n"
1065 " - C{E2BIG} mean that the attribute value is too big\n"
1066 " - C{ERANGE} mean that the attribute name is too big (it might also\n"
1067 " mean an error in the xattr module itself)\n"
1068 " - C{ENOSPC} and C{EDQUOT} are documented as meaning out of disk space\n"
1069 " or out of disk space because of quota limits\n"
1071 "@group Deprecated API: *xattr\n"
1072 "@group Namespace constants: NS_*\n"
1073 "@group set function flags: XATTR_CREATE, XATTR_REPLACE\n"
1074 "@sort: list, get, get_all, set, remove, listxattr, getxattr, setxattr\n"
1080 static struct PyModuleDef xattrmodule = {
1081 PyModuleDef_HEAD_INIT,
1088 #define INITERROR return NULL
1094 #define INITERROR return
1100 PyObject *m = PyModule_Create(&xattrmodule);
1102 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1107 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1108 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1109 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1110 PyModule_AddStringConstant(m, "__license__",
1111 "GNU Lesser General Public License (LGPL)");
1112 PyModule_AddStringConstant(m, "__docformat__", "epytext en");
1114 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1115 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1117 /* namespace constants */
1118 PyModule_AddObject(m, "NS_SECURITY", PyBytes_FromString("security"));
1119 PyModule_AddObject(m, "NS_SYSTEM", PyBytes_FromString("system"));
1120 PyModule_AddObject(m, "NS_TRUSTED", PyBytes_FromString("trusted"));
1121 PyModule_AddObject(m, "NS_USER", PyBytes_FromString("user"));