2 xattr - a python module for manipulating filesystem extended attributes
4 Copyright (C) 2002, 2003, 2006, 2008, 2012, 2013 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
37 #define BYTES_CHAR "y"
39 #define BYTES_CHAR "s"
40 #define PyBytes_Check PyString_Check
41 #define PyBytes_AS_STRING PyString_AS_STRING
42 #define PyBytes_FromStringAndSize PyString_FromStringAndSize
43 #define PyBytes_FromString PyString_FromString
47 ":param item: a string representing a file-name, or a file-like\n" \
48 " object, or a file descriptor; this represents the file on \n" \
51 #define NOFOLLOW_DOC \
52 ":param nofollow: if true and if\n" \
53 " the file name given is a symbolic link, the\n" \
54 " function will operate on the symbolic link itself instead\n" \
55 " of its target; defaults to false\n" \
56 ":type nofollow: boolean, optional\n" \
59 ":param namespace: if given, the attribute must not contain the\n" \
60 " namespace, but instead it will be taken from this parameter\n" \
61 ":type namespace: bytes\n"
63 #define NAME_GET_DOC \
64 ":param string name: the attribute whose value to retrieve;\n" \
65 " usually in the form of ``system.posix_acl`` or ``user.mime_type``\n"
67 #define NAME_SET_DOC \
68 ":param string name: the attribute whose value to set;\n" \
69 " usually in the form of ``system.posix_acl`` or ``user.mime_type``\n"
71 #define NAME_REMOVE_DOC \
72 ":param string name: the attribute to remove;\n" \
73 " usually in the form of ``system.posix_acl`` or \n" \
74 " ``user.mime_type``\n"
77 ":param string value: possibly with embedded NULLs; note that there\n" \
78 " are restrictions regarding the size of the value, for\n" \
79 " example, for ext2/ext3, maximum size is the block size\n" \
82 ":param flags: if 0 or omitted the attribute will be\n" \
83 " created or replaced; if :const:`XATTR_CREATE`, the attribute\n" \
84 " will be created, giving an error if it already exists;\n" \
85 " if :const:`XATTR_REPLACE`, the attribute will be replaced,\n" \
86 " giving an error if it doesn't exist;\n" \
87 ":type flags: integer\n"
89 #define NS_CHANGED_DOC \
90 ".. versionchanged:: 0.5.1\n" \
91 " The namespace argument, if passed, cannot be None anymore; to\n" \
92 " explicitly specify an empty namespace, pass an empty\n" \
93 " string (byte string under Python 3)."
96 /* the estimated (startup) attribute buffer size in
98 #define ESTIMATE_ATTR_SIZE 256
100 typedef enum {T_FD, T_PATH, T_LINK} target_e;
111 /* Cleans up a tgt structure */
112 static void free_tgt(target_t *tgt) {
113 if (tgt->tmp != NULL) {
118 /* Used for cpychecker: */
119 /* The checker automatically defines this preprocessor name when creating
120 the custom attribute: */
121 #if defined(WITH_CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION_ATTRIBUTE)
122 #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION \
123 __attribute__((cpychecker_negative_result_sets_exception))
125 #define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
128 static int convert_obj(PyObject *myobj, target_t *tgt, int nofollow)
129 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
131 static int merge_ns(const char *ns, const char *name,
132 const char **result, char **buf)
133 CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
136 /** Converts from a string, file or int argument to what we need.
138 * Returns -1 on failure, 0 on success.
140 static int convert_obj(PyObject *myobj, target_t *tgt, int nofollow) {
143 if(PyBytes_Check(myobj)) {
144 tgt->type = nofollow ? T_LINK : T_PATH;
145 tgt->name = PyBytes_AS_STRING(myobj);
146 } else if(PyUnicode_Check(myobj)) {
147 tgt->type = nofollow ? T_LINK : T_PATH;
149 PyUnicode_AsEncodedString(myobj,
150 Py_FileSystemDefaultEncoding, "strict");
153 tgt->name = PyBytes_AS_STRING(tgt->tmp);
154 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
158 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
164 /* Combine a namespace string and an attribute name into a
165 fully-qualified name */
166 static int merge_ns(const char *ns, const char *name,
167 const char **result, char **buf) {
168 if(ns != NULL && *ns != '\0') {
170 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
171 if((*buf = PyMem_Malloc(new_size)) == NULL) {
175 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
176 if(cnt > new_size || cnt < 0) {
177 PyErr_SetString(PyExc_ValueError,
178 "can't format the attribute name");
190 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
191 if(tgt->type == T_FD)
192 return flistxattr(tgt->fd, list, size);
193 else if (tgt->type == T_LINK)
194 return llistxattr(tgt->name, list, size);
196 return listxattr(tgt->name, list, size);
199 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
201 if(tgt->type == T_FD)
202 return fgetxattr(tgt->fd, name, value, size);
203 else if (tgt->type == T_LINK)
204 return lgetxattr(tgt->name, name, value, size);
206 return getxattr(tgt->name, name, value, size);
209 static int _set_obj(target_t *tgt, const char *name,
210 const void *value, size_t size, int flags) {
211 if(tgt->type == T_FD)
212 return fsetxattr(tgt->fd, name, value, size, flags);
213 else if (tgt->type == T_LINK)
214 return lsetxattr(tgt->name, name, value, size, flags);
216 return setxattr(tgt->name, name, value, size, flags);
219 static int _remove_obj(target_t *tgt, const char *name) {
220 if(tgt->type == T_FD)
221 return fremovexattr(tgt->fd, name);
222 else if (tgt->type == T_LINK)
223 return lremovexattr(tgt->name, name);
225 return removexattr(tgt->name, name);
229 Checks if an attribute name matches an optional namespace.
231 If the namespace is NULL or an empty string, it will return the
232 name itself. If the namespace is non-NULL and the name matches, it
233 will return a pointer to the offset in the name after the namespace
234 and the separator. If however the name doesn't match the namespace,
238 const char *matches_ns(const char *ns, const char *name) {
240 if (ns == NULL || *ns == '\0')
242 ns_size = strlen(ns);
244 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
245 name[ns_size] == '.')
246 return name + ns_size + 1;
250 /* Wrapper for getxattr */
251 static char __pygetxattr_doc__[] =
252 "getxattr(item, attribute[, nofollow=False])\n"
253 "Get the value of a given extended attribute (deprecated).\n"
259 ".. deprecated:: 0.4\n"
260 " this function has been deprecated\n"
261 " by the :func:`get` function.\n"
265 pygetxattr(PyObject *self, PyObject *args)
270 char *attrname = NULL;
272 ssize_t nalloc, nret;
275 /* Parse the arguments */
276 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
278 if(convert_obj(myarg, &tgt, nofollow) < 0) {
283 /* Find out the needed size of the buffer */
284 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
285 res = PyErr_SetFromErrno(PyExc_IOError);
289 /* Try to allocate the memory, using Python's allocator */
290 if((buf = PyMem_Malloc(nalloc)) == NULL) {
291 res = PyErr_NoMemory();
295 /* Now retrieve the attribute value */
296 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
297 res = PyErr_SetFromErrno(PyExc_IOError);
301 /* Create the string which will hold the result */
302 res = PyBytes_FromStringAndSize(buf, nret);
305 /* Free the buffer, now it is no longer needed */
310 PyMem_Free(attrname);
312 /* Return the result */
316 /* Wrapper for getxattr */
317 static char __get_doc__[] =
318 "get(item, name[, nofollow=False, namespace=None])\n"
319 "Get the value of a given extended attribute.\n"
322 " >>> xattr.get('/path/to/file', 'user.comment')\n"
324 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
331 ":return: the value of the extended attribute (can contain NULLs)\n"
333 ":raises EnvironmentError: caused by any system errors\n"
335 ".. versionadded:: 0.4\n"
340 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
345 char *attrname = NULL, *namebuf;
346 const char *fullname;
348 const char *ns = NULL;
349 ssize_t nalloc, nret;
351 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
353 /* Parse the arguments */
354 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
355 &myarg, NULL, &attrname, &nofollow, &ns))
357 if(convert_obj(myarg, &tgt, nofollow) < 0) {
362 if(merge_ns(ns, attrname, &fullname, &namebuf) < 0) {
367 /* Find out the needed size of the buffer */
368 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
369 res = PyErr_SetFromErrno(PyExc_IOError);
373 /* Try to allocate the memory, using Python's allocator */
374 if((buf = PyMem_Malloc(nalloc)) == NULL) {
375 res = PyErr_NoMemory();
379 /* Now retrieve the attribute value */
380 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
381 res = PyErr_SetFromErrno(PyExc_IOError);
385 /* Create the string which will hold the result */
386 res = PyBytes_FromStringAndSize(buf, nret);
388 /* Free the buffers, they are no longer needed */
396 PyMem_Free(attrname);
398 /* Return the result */
402 /* Wrapper for getxattr */
403 static char __get_all_doc__[] =
404 "get_all(item[, nofollow=False, namespace=None])\n"
405 "Get all the extended attributes of an item.\n"
407 "This function performs a bulk-get of all extended attribute names\n"
408 "and the corresponding value.\n"
411 " >>> xattr.get_all('/path/to/file')\n"
412 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
413 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
414 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
415 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
418 ":keyword namespace: an optional namespace for filtering the\n"
419 " attributes; for example, querying all user attributes can be\n"
420 " accomplished by passing namespace=:const:`NS_USER`\n"
421 ":type namespace: string\n"
423 ":return: list of tuples (name, value); note that if a namespace\n"
424 " argument was passed, it (and the separator) will be stripped from\n"
425 " the names returned\n"
427 ":raises EnvironmentError: caused by any system errors\n"
429 ".. note:: Since reading the whole attribute list is not an atomic\n"
430 " operation, it might be possible that attributes are added\n"
431 " or removed between the initial query and the actual reading\n"
432 " of the attributes; the returned list will contain only the\n"
433 " attributes that were present at the initial listing of the\n"
434 " attribute names and that were still present when the read\n"
435 " attempt for the value is made.\n"
436 ".. versionadded:: 0.4\n"
441 get_all(PyObject *self, PyObject *args, PyObject *keywds)
443 PyObject *myarg, *res;
445 const char *ns = NULL;
446 char *buf_list, *buf_val;
448 ssize_t nalloc, nlist, nval;
451 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
453 /* Parse the arguments */
454 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
455 &myarg, &nofollow, &ns))
457 if(convert_obj(myarg, &tgt, nofollow) < 0)
460 /* Compute first the list of attributes */
462 /* Find out the needed size of the buffer for the attribute list */
463 nalloc = _list_obj(&tgt, NULL, 0);
466 res = PyErr_SetFromErrno(PyExc_IOError);
475 /* Try to allocate the memory, using Python's allocator */
476 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
477 res = PyErr_NoMemory();
481 /* Now retrieve the list of attributes */
482 nlist = _list_obj(&tgt, buf_list, nalloc);
485 res = PyErr_SetFromErrno(PyExc_IOError);
489 /* Create the list which will hold the result */
490 mylist = PyList_New(0);
496 nalloc = ESTIMATE_ATTR_SIZE;
497 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
499 res = PyErr_NoMemory();
503 /* Create and insert the attributes as strings in the list */
504 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
509 if((name=matches_ns(ns, s))==NULL)
511 /* Now retrieve the attribute value */
514 nval = _get_obj(&tgt, s, buf_val, nalloc);
517 if(errno == ERANGE) {
518 nval = _get_obj(&tgt, s, NULL, 0);
519 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
520 res = PyErr_NoMemory();
526 } else if(errno == ENODATA || errno == ENOATTR) {
527 /* this attribute has gone away since we queried
528 the attribute list */
532 /* else we're dealing with a different error, which we
533 don't know how to handle nicely, so we abort */
535 res = PyErr_SetFromErrno(PyExc_IOError);
543 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
545 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
547 if (my_tuple == NULL) {
552 PyList_Append(mylist, my_tuple);
556 /* Successful exit */
563 PyMem_Free(buf_list);
568 /* Return the result */
573 static char __pysetxattr_doc__[] =
574 "setxattr(item, name, value[, flags=0, nofollow=False])\n"
575 "Set the value of a given extended attribute (deprecated).\n"
577 "Be careful in case you want to set attributes on symbolic\n"
578 "links, you have to use all the 5 parameters; use 0 for the \n"
579 "flags value if you want the default behaviour (create or "
588 ".. deprecated:: 0.4\n"
589 " this function has been deprecated\n"
590 " by the :func:`set` function.\n"
593 /* Wrapper for setxattr */
595 pysetxattr(PyObject *self, PyObject *args)
597 PyObject *myarg, *res;
599 char *attrname = NULL;
606 /* Parse the arguments */
607 if (!PyArg_ParseTuple(args, "Oetet#|ii", &myarg, NULL, &attrname,
608 NULL, &buf, &bufsize, &flags, &nofollow))
610 if(convert_obj(myarg, &tgt, nofollow) < 0) {
615 /* Set the attribute's value */
616 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
621 res = PyErr_SetFromErrno(PyExc_IOError);
629 PyMem_Free(attrname);
632 /* Return the result */
636 static char __set_doc__[] =
637 "set(item, name, value[, flags=0, namespace=None])\n"
638 "Set the value of a given extended attribute.\n"
642 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
643 " >>> xattr.set('/path/to/file', 'comment', 'test',"
644 " namespace=xattr.NS_USER)\n"
653 ":raises EnvironmentError: caused by any system errors\n"
655 ".. versionadded:: 0.4\n"
659 /* Wrapper for setxattr */
661 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
663 PyObject *myarg, *res;
665 char *attrname = NULL;
671 const char *ns = NULL;
673 const char *full_name;
674 static char *kwlist[] = {"item", "name", "value", "flags",
675 "nofollow", "namespace", NULL};
677 /* Parse the arguments */
678 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|ii" BYTES_CHAR,
679 kwlist, &myarg, NULL, &attrname, NULL,
680 &buf, &bufsize, &flags, &nofollow, &ns))
682 if(convert_obj(myarg, &tgt, nofollow) < 0) {
687 if(merge_ns(ns, attrname, &full_name, &newname) < 0) {
692 /* Set the attribute's value */
693 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
700 res = PyErr_SetFromErrno(PyExc_IOError);
708 PyMem_Free(attrname);
711 /* Return the result */
716 static char __pyremovexattr_doc__[] =
717 "removexattr(item, name[, nofollow])\n"
718 "Remove an attribute from a file (deprecated).\n"
724 ".. deprecated:: 0.4\n"
725 " this function has been deprecated by the :func:`remove` function.\n"
728 /* Wrapper for removexattr */
730 pyremovexattr(PyObject *self, PyObject *args)
732 PyObject *myarg, *res;
734 char *attrname = NULL;
738 /* Parse the arguments */
739 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
742 if(convert_obj(myarg, &tgt, nofollow) < 0) {
747 /* Remove the attribute */
748 nret = _remove_obj(&tgt, attrname);
753 res = PyErr_SetFromErrno(PyExc_IOError);
761 PyMem_Free(attrname);
763 /* Return the result */
767 static char __remove_doc__[] =
768 "remove(item, name[, nofollow=False, namespace=None])\n"
769 "Remove an attribute from a file.\n"
773 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
780 ":raises EnvironmentError: caused by any system errors\n"
782 ".. versionadded:: 0.4\n"
786 /* Wrapper for removexattr */
788 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
790 PyObject *myarg, *res;
792 char *attrname = NULL, *name_buf;
793 const char *ns = NULL;
794 const char *full_name;
797 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
799 /* Parse the arguments */
800 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
801 &myarg, NULL, &attrname, &nofollow, &ns))
804 if(convert_obj(myarg, &tgt, nofollow) < 0) {
809 if(merge_ns(ns, attrname, &full_name, &name_buf) < 0) {
814 /* Remove the attribute */
815 nret = _remove_obj(&tgt, full_name);
817 PyMem_Free(name_buf);
822 res = PyErr_SetFromErrno(PyExc_IOError);
830 PyMem_Free(attrname);
832 /* Return the result */
836 static char __pylistxattr_doc__[] =
837 "listxattr(item[, nofollow=False])\n"
838 "Return the list of attribute names for a file (deprecated).\n"
843 ".. deprecated:: 0.4\n"
844 " this function has been deprecated by the :func:`list` function.\n"
847 /* Wrapper for listxattr */
849 pylistxattr(PyObject *self, PyObject *args)
853 ssize_t nalloc, nret;
860 /* Parse the arguments */
861 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
863 if(convert_obj(myarg, &tgt, nofollow) < 0)
866 /* Find out the needed size of the buffer */
867 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
868 mylist = PyErr_SetFromErrno(PyExc_IOError);
873 mylist = PyList_New(0);
877 /* Try to allocate the memory, using Python's allocator */
878 if((buf = PyMem_Malloc(nalloc)) == NULL) {
879 mylist = PyErr_NoMemory();
883 /* Now retrieve the list of attributes */
884 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
885 mylist = PyErr_SetFromErrno(PyExc_IOError);
889 /* Compute the number of attributes in the list */
890 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
894 /* Create the list which will hold the result */
895 mylist = PyList_New(nattrs);
899 /* Create and insert the attributes as strings in the list */
900 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
901 PyObject *item = PyBytes_FromString(s);
907 PyList_SET_ITEM(mylist, nattrs, item);
912 /* Free the buffer, now it is no longer needed */
918 /* Return the result */
922 static char __list_doc__[] =
923 "list(item[, nofollow=False, namespace=None])\n"
924 "Return the list of attribute names for a file.\n"
928 " >>> xattr.list('/path/to/file')\n"
929 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
930 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
931 " ['test', 'comment']\n"
936 ":returns: the list of attributes; note that if a namespace \n"
937 " argument was passed, it (and the separator) will be stripped\n"
941 ":raises EnvironmentError: caused by any system errors\n"
943 ".. versionadded:: 0.4\n"
947 /* Wrapper for listxattr */
949 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
953 ssize_t nalloc, nret;
956 const char *ns = NULL;
960 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
962 /* Parse the arguments */
963 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
964 &myarg, &nofollow, &ns))
966 if(convert_obj(myarg, &tgt, nofollow) < 0) {
971 /* Find out the needed size of the buffer */
972 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
973 res = PyErr_SetFromErrno(PyExc_IOError);
982 /* Try to allocate the memory, using Python's allocator */
983 if((buf = PyMem_Malloc(nalloc)) == NULL) {
984 res = PyErr_NoMemory();
988 /* Now retrieve the list of attributes */
989 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
990 res = PyErr_SetFromErrno(PyExc_IOError);
994 /* Compute the number of attributes in the list */
995 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
996 if(matches_ns(ns, s) != NULL)
999 /* Create the list which will hold the result */
1000 res = PyList_New(nattrs);
1004 /* Create and insert the attributes as strings in the list */
1005 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
1006 const char *name = matches_ns(ns, s);
1008 PyObject *item = PyBytes_FromString(name);
1014 PyList_SET_ITEM(res, nattrs, item);
1020 /* Free the buffer, now it is no longer needed */
1027 /* Return the result */
1031 static PyMethodDef xattr_methods[] = {
1032 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
1033 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
1035 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
1037 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
1038 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
1040 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
1041 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
1043 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
1044 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
1046 {NULL, NULL, 0, NULL} /* Sentinel */
1049 static char __xattr_doc__[] = \
1050 "This module gives access to the extended attributes present\n"
1051 "in some operating systems/filesystems. You can list attributes,\n"
1052 "get, set and remove them.\n"
1054 "The module exposes two sets of functions:\n"
1055 " - the 'old' :func:`listxattr`, :func:`getxattr`, :func:`setxattr`,\n"
1056 " :func:`removexattr`\n"
1057 " functions which are deprecated since version 0.4\n"
1058 " - the new :func:`list`, :func:`get`, :func:`get_all`, :func:`set`,\n"
1059 " :func:`remove` functions\n"
1060 " which expose a namespace-aware API and simplify a bit the calling\n"
1061 " model by using keyword arguments\n"
1064 " >>> import xattr\n"
1065 " >>> xattr.listxattr(\"file.txt\")\n"
1066 " ['user.mime_type']\n"
1067 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1069 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1070 "\"Simple text file\")\n"
1071 " >>> xattr.listxattr(\"file.txt\")\n"
1072 " ['user.mime_type', 'user.comment']\n"
1073 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1075 ".. note:: Most or all errors reported by the system while using\n"
1076 " the ``xattr`` library will be reported by raising\n"
1077 " a :exc:`EnvironmentError`; under\n"
1078 " Linux, the following ``errno`` values are used:\n"
1080 " - ``ENOATTR`` and ``ENODATA`` mean that the attribute name is\n"
1082 " - ``ENOTSUP`` and ``EOPNOTSUPP`` mean that the filesystem does not\n"
1083 " support extended attributes, or that the namespace is invalid\n"
1084 " - ``E2BIG`` mean that the attribute value is too big\n"
1085 " - ``ERANGE`` mean that the attribute name is too big (it might also\n"
1086 " mean an error in the xattr module itself)\n"
1087 " - ``ENOSPC`` and ``EDQUOT`` are documented as meaning out of disk\n"
1088 " space or out of disk space because of quota limits\n"
1089 ".. note:: Under Python 3, the namespace argument is a byte string,\n"
1090 " not a unicode string.\n"
1096 static struct PyModuleDef xattrmodule = {
1097 PyModuleDef_HEAD_INIT,
1104 #define INITERROR return NULL
1110 #define INITERROR return
1115 PyObject *ns_security = NULL;
1116 PyObject *ns_system = NULL;
1117 PyObject *ns_trusted = NULL;
1118 PyObject *ns_user = NULL;
1120 PyObject *m = PyModule_Create(&xattrmodule);
1122 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1127 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1128 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1129 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1130 PyModule_AddStringConstant(m, "__license__",
1131 "GNU Lesser General Public License (LGPL)");
1132 PyModule_AddStringConstant(m, "__docformat__", "restructuredtext en");
1134 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1135 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1137 /* namespace constants */
1138 if((ns_security = PyBytes_FromString("security")) == NULL)
1140 if((ns_system = PyBytes_FromString("system")) == NULL)
1142 if((ns_trusted = PyBytes_FromString("trusted")) == NULL)
1144 if((ns_user = PyBytes_FromString("user")) == NULL)
1146 if(PyModule_AddObject(m, "NS_SECURITY", ns_security) < 0)
1149 if(PyModule_AddObject(m, "NS_SYSTEM", ns_system) < 0)
1152 if(PyModule_AddObject(m, "NS_TRUSTED", ns_trusted) < 0)
1155 if(PyModule_AddObject(m, "NS_USER", ns_user) < 0)
1166 Py_XDECREF(ns_user);
1167 Py_XDECREF(ns_trusted);
1168 Py_XDECREF(ns_system);
1169 Py_XDECREF(ns_security);