2 xattr - a python module for manipulating filesystem extended attributes
4 Copyright (C) 2002, 2003, 2006, 2008 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. */
67 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
70 if(PyBytes_Check(myobj)) {
71 tgt->type = nofollow ? T_LINK : T_PATH;
72 tgt->name = PyBytes_AS_STRING(myobj);
73 } else if(PyUnicode_Check(myobj)) {
74 tgt->type = nofollow ? T_LINK : T_PATH;
76 PyUnicode_AsEncodedString(myobj,
77 Py_FileSystemDefaultEncoding, "strict");
80 tgt->name = PyBytes_AS_STRING(tgt->tmp);
81 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
85 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
91 /* Combine a namespace string and an attribute name into a
92 fully-qualified name */
93 static const char* merge_ns(const char *ns, const char *name, char **buf) {
96 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
97 if((*buf = PyMem_Malloc(new_size)) == NULL) {
101 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
102 if(cnt > new_size || cnt < 0) {
103 PyErr_SetString(PyExc_ValueError,
104 "can't format the attribute name");
115 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
116 if(tgt->type == T_FD)
117 return flistxattr(tgt->fd, list, size);
118 else if (tgt->type == T_LINK)
119 return llistxattr(tgt->name, list, size);
121 return listxattr(tgt->name, list, size);
124 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
126 if(tgt->type == T_FD)
127 return fgetxattr(tgt->fd, name, value, size);
128 else if (tgt->type == T_LINK)
129 return lgetxattr(tgt->name, name, value, size);
131 return getxattr(tgt->name, name, value, size);
134 static int _set_obj(target_t *tgt, const char *name,
135 const void *value, size_t size, int flags) {
136 if(tgt->type == T_FD)
137 return fsetxattr(tgt->fd, name, value, size, flags);
138 else if (tgt->type == T_LINK)
139 return lsetxattr(tgt->name, name, value, size, flags);
141 return setxattr(tgt->name, name, value, size, flags);
144 static int _remove_obj(target_t *tgt, const char *name) {
145 if(tgt->type == T_FD)
146 return fremovexattr(tgt->fd, name);
147 else if (tgt->type == T_LINK)
148 return lremovexattr(tgt->name, name);
150 return removexattr(tgt->name, name);
154 Checks if an attribute name matches an optional namespace.
156 If the namespace is NULL, it will return the name itself. If the
157 namespace is non-NULL and the name matches, it will return a
158 pointer to the offset in the name after the namespace and the
159 separator. If however the name doesn't match the namespace, it will
162 const char *matches_ns(const char *ns, const char *name) {
166 ns_size = strlen(ns);
168 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
169 name[ns_size] == '.')
170 return name + ns_size + 1;
174 /* Wrapper for getxattr */
175 static char __pygetxattr_doc__[] =
176 "Get the value of a given extended attribute (deprecated).\n"
179 " - a string representing filename, or a file-like object,\n"
180 " or a file descriptor; this represents the file on \n"
182 " - a string, representing the attribute whose value to retrieve;\n"
183 " usually in form of system.posix_acl or user.mime_type\n"
184 " - (optional) a boolean value (defaults to false), which, if\n"
185 " the file name given is a symbolic link, makes the\n"
186 " function operate on the symbolic link itself instead\n"
188 "@deprecated: since version 0.4, this function has been deprecated\n"
189 " by the L{get} function\n"
193 pygetxattr(PyObject *self, PyObject *args)
198 char *attrname = NULL;
200 ssize_t nalloc, nret;
203 /* Parse the arguments */
204 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
206 if(!convertObj(myarg, &tgt, nofollow)) {
211 /* Find out the needed size of the buffer */
212 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
213 res = 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, attrname, buf, nalloc)) == -1) {
226 res = PyErr_SetFromErrno(PyExc_IOError);
230 /* Create the string which will hold the result */
231 res = PyBytes_FromStringAndSize(buf, nret);
234 /* Free the buffer, now it is no longer needed */
239 PyMem_Free(attrname);
241 /* Return the result */
245 /* Wrapper for getxattr */
246 static char __get_doc__[] =
247 "Get the value of a given extended attribute.\n"
250 " >>> xattr.get('/path/to/file', 'user.comment')\n"
252 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
255 "@param item: the item to query; either a string representing the\n"
256 " filename, or a file-like object, or a file descriptor\n"
257 "@param name: the attribute whose value to set; usually in form of\n"
258 " system.posix_acl or user.mime_type\n"
259 "@type name: string\n"
260 "@param nofollow: if given and True, and the function is passed a\n"
261 " filename that points to a symlink, the function will act on the\n"
262 " symlink itself instead of its target\n"
263 "@type nofollow: boolean\n"
264 "@param namespace: if given, the attribute must not contain the\n"
265 " namespace itself, but instead the namespace will be taken from\n"
267 "@type namespace: string\n"
268 "@return: the value of the extended attribute (can contain NULLs)\n"
270 "@raise EnvironmentError: caused by any system errors\n"
275 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
280 char *attrname = NULL, *namebuf;
281 const char *fullname;
284 ssize_t nalloc, nret;
286 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
288 /* Parse the arguments */
289 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
290 &myarg, NULL, &attrname, &nofollow, &ns))
292 if(!convertObj(myarg, &tgt, nofollow)) {
297 fullname = merge_ns(ns, attrname, &namebuf);
299 /* Find out the needed size of the buffer */
300 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
301 res = PyErr_SetFromErrno(PyExc_IOError);
305 /* Try to allocate the memory, using Python's allocator */
306 if((buf = PyMem_Malloc(nalloc)) == NULL) {
312 /* Now retrieve the attribute value */
313 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
314 res = PyErr_SetFromErrno(PyExc_IOError);
318 /* Create the string which will hold the result */
319 res = PyBytes_FromStringAndSize(buf, nret);
321 /* Free the buffers, they are no longer needed */
329 PyMem_Free(attrname);
331 /* Return the result */
335 /* Wrapper for getxattr */
336 static char __get_all_doc__[] =
337 "Get all the extended attributes of an item.\n"
339 "This function performs a bulk-get of all extended attribute names\n"
340 "and the corresponding value.\n"
342 " >>> xattr.get_all('/path/to/file')\n"
343 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
344 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
345 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
346 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
348 "@param item: the item to query; either a string representing the\n"
349 " filename, or a file-like object, or a file descriptor\n"
350 "@keyword namespace: an optional namespace for filtering the\n"
351 " attributes; for example, querying all user attributes can be\n"
352 " accomplished by passing namespace=L{NS_USER}\n"
353 "@type namespace: string\n"
354 "@keyword nofollow: if passed and true, if the target file is a\n"
355 " symbolic link, the attributes for the link itself will be\n"
356 " returned, instead of the attributes of the target\n"
357 "@type nofollow: boolean\n"
358 "@return: list of tuples (name, value); note that if a namespace\n"
359 " argument was passed, it (and the separator) will be stripped from\n"
360 " the names returned\n"
362 "@raise EnvironmentError: caused by any system errors\n"
363 "@note: Since reading the whole attribute list is not an atomic\n"
364 " operation, it might be possible that attributes are added\n"
365 " or removed between the initial query and the actual reading\n"
366 " of the attributes; the returned list will contain only the\n"
367 " attributes that were present at the initial listing of the\n"
368 " attribute names and that were still present when the read\n"
369 " attempt for the value is made.\n"
374 get_all(PyObject *self, PyObject *args, PyObject *keywds)
376 PyObject *myarg, *res;
379 char *buf_list, *buf_val;
381 size_t nalloc, nlist, nval;
384 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
386 /* Parse the arguments */
387 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
388 &myarg, &dolink, &ns))
390 if(!convertObj(myarg, &tgt, dolink))
393 /* Compute first the list of attributes */
395 /* Find out the needed size of the buffer for the attribute list */
396 nalloc = _list_obj(&tgt, NULL, 0);
399 res = PyErr_SetFromErrno(PyExc_IOError);
403 /* Try to allocate the memory, using Python's allocator */
404 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
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);
420 nalloc = ESTIMATE_ATTR_SIZE;
421 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
428 /* Create and insert the attributes as strings in the list */
429 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
434 if((name=matches_ns(ns, s))==NULL)
436 /* Now retrieve the attribute value */
439 nval = _get_obj(&tgt, s, buf_val, nalloc);
442 if(errno == ERANGE) {
443 nval = _get_obj(&tgt, s, NULL, 0);
444 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
451 } else if(errno == ENODATA || errno == ENOATTR) {
452 /* this attribute has gone away since we queried
453 the attribute list */
457 res = PyErr_SetFromErrno(PyExc_IOError);
465 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
467 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
470 PyList_Append(mylist, my_tuple);
474 /* Successfull exit */
481 PyMem_Free(buf_list);
486 /* Return the result */
491 static char __pysetxattr_doc__[] =
492 "Set the value of a given extended attribute (deprecated).\n"
494 "Be carefull in case you want to set attributes on symbolic\n"
495 "links, you have to use all the 5 parameters; use 0 for the \n"
496 "flags value if you want the default behavior (create or "
500 " - a string representing filename, or a file-like object,\n"
501 " or a file descriptor; this represents the file on \n"
503 " - a string, representing the attribute whose value to set;\n"
504 " usually in form of system.posix_acl or user.mime_type\n"
505 " - a string, possibly with embedded NULLs; note that there\n"
506 " are restrictions regarding the size of the value, for\n"
507 " example, for ext2/ext3, maximum size is the block size\n"
508 " - (optional) flags; if 0 or ommited the attribute will be \n"
509 " created or replaced; if XATTR_CREATE, the attribute \n"
510 " will be created, giving an error if it already exists;\n"
511 " of XATTR_REPLACE, the attribute will be replaced,\n"
512 " giving an error if it doesn't exists;\n"
513 " - (optional) a boolean value (defaults to false), which, if\n"
514 " the file name given is a symbolic link, makes the\n"
515 " function operate on the symbolic link itself instead\n"
517 "@deprecated: since version 0.4, this function has been deprecated\n"
518 " by the L{set} function\n"
521 /* Wrapper for setxattr */
523 pysetxattr(PyObject *self, PyObject *args)
525 PyObject *myarg, *res;
527 char *attrname = NULL;
534 /* Parse the arguments */
535 if (!PyArg_ParseTuple(args, "Oetet#|bi", &myarg, NULL, &attrname,
536 NULL, &buf, &bufsize, &flags, &nofollow))
538 if(!convertObj(myarg, &tgt, nofollow)) {
543 /* Set the attribute's value */
544 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
549 res = PyErr_SetFromErrno(PyExc_IOError);
557 PyMem_Free(attrname);
560 /* Return the result */
564 static char __set_doc__[] =
565 "Set the value of a given extended attribute.\n"
568 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
569 " >>> xattr.set('/path/to/file', 'comment', 'test',"
570 " namespace=xattr.NS_USER)\n"
572 "@param item: the item to query; either a string representing the\n"
573 " filename, or a file-like object, or a file descriptor\n"
574 "@param name: the attribute whose value to set; usually in form of\n"
575 " system.posix_acl or user.mime_type\n"
576 "@type name: string\n"
577 "@param value: a string, possibly with embedded NULLs; note that there\n"
578 " are restrictions regarding the size of the value, for\n"
579 " example, for ext2/ext3, maximum size is the block size\n"
580 "@type value: string\n"
581 "@param flags: if 0 or ommited the attribute will be\n"
582 " created or replaced; if L{XATTR_CREATE}, the attribute\n"
583 " will be created, giving an error if it already exists;\n"
584 " if L{XATTR_REPLACE}, the attribute will be replaced,\n"
585 " giving an error if it doesn't exists;\n"
586 "@type flags: integer\n"
587 "@param nofollow: if given and True, and the function is passed a\n"
588 " filename that points to a symlink, the function will act on the\n"
589 " symlink itself instead of its target\n"
590 "@type nofollow: boolean\n"
591 "@param namespace: if given, the attribute must not contain the\n"
592 " namespace itself, but instead the namespace will be taken from\n"
594 "@type namespace: string\n"
596 "@raise EnvironmentError: caused by any system errors\n"
600 /* Wrapper for setxattr */
602 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
604 PyObject *myarg, *res;
606 char *attrname = NULL;
614 const char *full_name;
615 static char *kwlist[] = {"item", "name", "value", "flags",
616 "nofollow", "namespace", NULL};
618 /* Parse the arguments */
619 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|iiz", kwlist,
620 &myarg, NULL, &attrname, NULL,
621 &buf, &bufsize, &flags, &nofollow, &ns))
623 if(!convertObj(myarg, &tgt, nofollow)) {
628 full_name = merge_ns(ns, attrname, &newname);
630 /* Set the attribute's value */
631 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
639 res = PyErr_SetFromErrno(PyExc_IOError);
647 PyMem_Free(attrname);
650 /* Return the result */
655 static char __pyremovexattr_doc__[] =
656 "Remove an attribute from a file (deprecated).\n"
659 " - a string representing filename, or a file-like object,\n"
660 " or a file descriptor; this represents the file on \n"
662 " - a string, representing the attribute to be removed;\n"
663 " usually in form of system.posix_acl or user.mime_type\n"
664 " - (optional) a boolean value (defaults to false), which, if\n"
665 " the file name given is a symbolic link, makes the\n"
666 " function operate on the symbolic link itself instead\n"
668 "@deprecated: since version 0.4, this function has been deprecated\n"
673 /* Wrapper for removexattr */
675 pyremovexattr(PyObject *self, PyObject *args)
677 PyObject *myarg, *res;
679 char *attrname = NULL;
683 /* Parse the arguments */
684 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
687 if(!convertObj(myarg, &tgt, nofollow)) {
692 /* Remove the attribute */
693 nret = _remove_obj(&tgt, attrname);
698 res = PyErr_SetFromErrno(PyExc_IOError);
706 PyMem_Free(attrname);
708 /* Return the result */
712 static char __remove_doc__[] =
713 "Remove an attribute from a file.\n"
716 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
718 "@param item: the item to query; either a string representing the\n"
719 " filename, or a file-like object, or a file descriptor\n"
720 "@param name: the attribute whose value to set; usually in form of\n"
721 " system.posix_acl or user.mime_type\n"
722 "@type name: string\n"
723 "@param nofollow: if given and True, and the function is passed a\n"
724 " filename that points to a symlink, the function will act on the\n"
725 " symlink itself instead of its target\n"
726 "@type nofollow: boolean\n"
727 "@param namespace: if given, the attribute must not contain the\n"
728 " namespace itself, but instead the namespace will be taken from\n"
730 "@type namespace: string\n"
733 "@raise EnvironmentError: caused by any system errors\n"
736 /* Wrapper for removexattr */
738 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
740 PyObject *myarg, *res;
742 char *attrname = NULL, *name_buf;
744 const char *full_name;
747 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
749 /* Parse the arguments */
750 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|iz", kwlist,
751 &myarg, NULL, &attrname, &nofollow, &ns))
754 if(!convertObj(myarg, &tgt, nofollow)) {
759 full_name = merge_ns(ns, attrname, &name_buf);
760 if(full_name == NULL) {
765 /* Remove the attribute */
766 nret = _remove_obj(&tgt, full_name);
768 PyMem_Free(name_buf);
773 res = PyErr_SetFromErrno(PyExc_IOError);
781 PyMem_Free(attrname);
783 /* Return the result */
787 static char __pylistxattr_doc__[] =
788 "Return the list of attribute names for a file (deprecated).\n"
791 " - a string representing filename, or a file-like object,\n"
792 " or a file descriptor; this represents the file to \n"
794 " - (optional) a boolean value (defaults to false), which, if\n"
795 " the file name given is a symbolic link, makes the\n"
796 " function operate on the symbolic link itself instead\n"
798 "@deprecated: since version 0.4, this function has been deprecated\n"
803 /* Wrapper for listxattr */
805 pylistxattr(PyObject *self, PyObject *args)
809 ssize_t nalloc, nret;
816 /* Parse the arguments */
817 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
819 if(!convertObj(myarg, &tgt, nofollow))
822 /* Find out the needed size of the buffer */
823 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
824 mylist = PyErr_SetFromErrno(PyExc_IOError);
828 /* Try to allocate the memory, using Python's allocator */
829 if((buf = PyMem_Malloc(nalloc)) == NULL) {
835 /* Now retrieve the list of attributes */
836 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
837 mylist = PyErr_SetFromErrno(PyExc_IOError);
841 /* Compute the number of attributes in the list */
842 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
846 /* Create the list which will hold the result */
847 mylist = PyList_New(nattrs);
849 /* Create and insert the attributes as strings in the list */
850 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
851 PyList_SET_ITEM(mylist, nattrs, PyBytes_FromString(s));
856 /* Free the buffer, now it is no longer needed */
862 /* Return the result */
866 static char __list_doc__[] =
867 "Return the list of attribute names for a file.\n"
870 " >>> xattr.list('/path/to/file')\n"
871 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
872 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
873 " ['test', 'comment']\n"
875 "@param item: the item to query; either a string representing the\n"
876 " filename, or a file-like object, or a file descriptor\n"
877 "@param nofollow: if given and True, and the function is passed a\n"
878 " filename that points to a symlink, the function will act on the\n"
879 " symlink itself instead of its target\n"
880 "@type nofollow: boolean\n"
881 "@param namespace: if given, the attribute must not contain the\n"
882 " namespace itself, but instead the namespace will be taken from\n"
884 "@type namespace: string\n"
885 "@return: list of strings; note that if a namespace argument was\n"
886 " passed, it (and the separator) will be stripped from the names\n"
889 "@raise EnvironmentError: caused by any system errors\n"
893 /* Wrapper for listxattr */
895 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
899 ssize_t nalloc, nret;
906 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
908 /* Parse the arguments */
909 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iet", kwlist,
910 &myarg, &nofollow, NULL, &ns))
912 if(!convertObj(myarg, &tgt, nofollow)) {
917 /* Find out the needed size of the buffer */
918 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
919 res = PyErr_SetFromErrno(PyExc_IOError);
923 /* Try to allocate the memory, using Python's allocator */
924 if((buf = PyMem_Malloc(nalloc)) == NULL) {
930 /* Now retrieve the list of attributes */
931 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
932 res = PyErr_SetFromErrno(PyExc_IOError);
936 /* Compute the number of attributes in the list */
937 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
938 if(matches_ns(ns, s)!=NULL)
941 /* Create the list which will hold the result */
942 res = PyList_New(nattrs);
944 /* Create and insert the attributes as strings in the list */
945 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
946 const char *name = matches_ns(ns, s);
948 PyList_SET_ITEM(res, nattrs, PyBytes_FromString(name));
954 /* Free the buffer, now it is no longer needed */
962 /* Return the result */
966 static PyMethodDef xattr_methods[] = {
967 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
968 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
970 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
972 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
973 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
975 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
976 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
978 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
979 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
981 {NULL, NULL, 0, NULL} /* Sentinel */
984 static char __xattr_doc__[] = \
985 "Interface to extended filesystem attributes.\n"
987 "This module gives access to the extended attributes present\n"
988 "in some operating systems/filesystems. You can list attributes,\n"
989 "get, set and remove them.\n"
991 "The module exposes two sets of functions:\n"
992 " - the 'old' L{listxattr}, L{getxattr}, L{setxattr}, L{removexattr}\n"
993 " functions which are deprecated since version 0.4\n"
994 " - the new L{list}, L{get}, L{get_all}, L{set}, L{remove} functions\n"
995 " which expose a namespace-aware API and simplify a bit the calling\n"
996 " model by using keyword arguments\n"
999 " >>> import xattr\n"
1000 " >>> xattr.listxattr(\"file.txt\")\n"
1001 " ['user.mime_type']\n"
1002 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1004 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1005 "\"Simple text file\")\n"
1006 " >>> xattr.listxattr(\"file.txt\")\n"
1007 " ['user.mime_type', 'user.comment']\n"
1008 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1010 "@note: Most or all errors reported by the system while using the xattr\n"
1011 "library will be reported by raising a L{EnvironmentError}; under Linux,\n"
1012 "the following C{errno} values are used:\n"
1013 " - C{ENOATTR} and C{ENODATA} mean that the attribute name is invalid\n"
1014 " - C{ENOTSUP} and C{EOPNOTSUPP} mean that the filesystem does not\n"
1015 " support extended attributes, or that the namespace is invalid\n"
1016 " - C{E2BIG} mean that the attribute value is too big\n"
1017 " - C{ERANGE} mean that the attribute name is too big (it might also\n"
1018 " mean an error in the xattr module itself)\n"
1019 " - C{ENOSPC} and C{EDQUOT} are documented as meaning out of disk space\n"
1020 " or out of disk space because of quota limits\n"
1022 "@group Deprecated API: *xattr\n"
1023 "@group Namespace constants: NS_*\n"
1024 "@group set function flags: XATTR_CREATE, XATTR_REPLACE\n"
1025 "@sort: list, get, get_all, set, remove, listxattr, getxattr, setxattr\n"
1031 static struct PyModuleDef xattrmodule = {
1032 PyModuleDef_HEAD_INIT,
1039 #define INITERROR return NULL
1045 #define INITERROR return
1051 PyObject *m = PyModule_Create(&xattrmodule);
1053 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1058 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1059 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1060 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1061 PyModule_AddStringConstant(m, "__license__",
1062 "GNU Lesser General Public License (LGPL)");
1063 PyModule_AddStringConstant(m, "__docformat__", "epytext en");
1065 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1066 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1068 /* namespace constants */
1069 PyModule_AddObject(m, "NS_SECURITY", PyBytes_FromString("security"));
1070 PyModule_AddObject(m, "NS_SYSTEM", PyBytes_FromString("system"));
1071 PyModule_AddObject(m, "NS_TRUSTED", PyBytes_FromString("trusted"));
1072 PyModule_AddObject(m, "NS_USER", PyBytes_FromString("user"));