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,
159 tgt->name = PyBytes_AS_STRING(tgt->tmp);
160 } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
164 PyErr_SetString(PyExc_TypeError, "argument must be string or int");
170 /* Combine a namespace string and an attribute name into a
171 fully-qualified name */
172 static int merge_ns(const char *ns, const char *name,
173 const char **result, char **buf) {
174 if(ns != NULL && *ns != '\0') {
176 size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
177 if((*buf = PyMem_Malloc(new_size)) == NULL) {
181 cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
182 if(cnt > new_size || cnt < 0) {
183 PyErr_SetString(PyExc_ValueError,
184 "can't format the attribute name");
196 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
197 if(tgt->type == T_FD)
198 return flistxattr(tgt->fd, list, size);
199 else if (tgt->type == T_LINK)
200 return llistxattr(tgt->name, list, size);
202 return listxattr(tgt->name, list, size);
205 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
207 if(tgt->type == T_FD)
208 return fgetxattr(tgt->fd, name, value, size);
209 else if (tgt->type == T_LINK)
210 return lgetxattr(tgt->name, name, value, size);
212 return getxattr(tgt->name, name, value, size);
215 static int _set_obj(target_t *tgt, const char *name,
216 const void *value, size_t size, int flags) {
217 if(tgt->type == T_FD)
218 return fsetxattr(tgt->fd, name, value, size, flags);
219 else if (tgt->type == T_LINK)
220 return lsetxattr(tgt->name, name, value, size, flags);
222 return setxattr(tgt->name, name, value, size, flags);
225 static int _remove_obj(target_t *tgt, const char *name) {
226 if(tgt->type == T_FD)
227 return fremovexattr(tgt->fd, name);
228 else if (tgt->type == T_LINK)
229 return lremovexattr(tgt->name, name);
231 return removexattr(tgt->name, name);
235 Checks if an attribute name matches an optional namespace.
237 If the namespace is NULL or an empty string, it will return the
238 name itself. If the namespace is non-NULL and the name matches, it
239 will return a pointer to the offset in the name after the namespace
240 and the separator. If however the name doesn't match the namespace,
244 const char *matches_ns(const char *ns, const char *name) {
246 if (ns == NULL || *ns == '\0')
248 ns_size = strlen(ns);
250 if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
251 name[ns_size] == '.')
252 return name + ns_size + 1;
256 /* Wrapper for getxattr */
257 static char __pygetxattr_doc__[] =
258 "getxattr(item, attribute[, nofollow=False])\n"
259 "Get the value of a given extended attribute (deprecated).\n"
265 ".. deprecated:: 0.4\n"
266 " this function has been deprecated\n"
267 " by the :func:`get` function.\n"
271 pygetxattr(PyObject *self, PyObject *args)
276 char *attrname = NULL;
278 ssize_t nalloc, nret;
281 /* Parse the arguments */
282 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
284 if(convert_obj(myarg, &tgt, nofollow) < 0) {
289 /* Find out the needed size of the buffer */
290 if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
291 res = PyErr_SetFromErrno(PyExc_IOError);
295 /* Try to allocate the memory, using Python's allocator */
296 if((buf = PyMem_Malloc(nalloc)) == NULL) {
297 res = PyErr_NoMemory();
301 /* Now retrieve the attribute value */
302 if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
303 res = PyErr_SetFromErrno(PyExc_IOError);
307 /* Create the string which will hold the result */
308 res = PyBytes_FromStringAndSize(buf, nret);
311 /* Free the buffer, now it is no longer needed */
316 PyMem_Free(attrname);
318 /* Return the result */
322 /* Wrapper for getxattr */
323 static char __get_doc__[] =
324 "get(item, name[, nofollow=False, namespace=None])\n"
325 "Get the value of a given extended attribute.\n"
328 " >>> xattr.get('/path/to/file', 'user.comment')\n"
330 " >>> xattr.get('/path/to/file', 'comment', namespace=xattr.NS_USER)\n"
337 ":return: the value of the extended attribute (can contain NULLs)\n"
339 ":raises EnvironmentError: caused by any system errors\n"
341 ".. versionadded:: 0.4\n"
346 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
351 char *attrname = NULL, *namebuf;
352 const char *fullname;
354 const char *ns = NULL;
355 ssize_t nalloc, nret;
357 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
359 /* Parse the arguments */
360 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
361 &myarg, NULL, &attrname, &nofollow, &ns))
363 if(convert_obj(myarg, &tgt, nofollow) < 0) {
368 if(merge_ns(ns, attrname, &fullname, &namebuf) < 0) {
373 /* Find out the needed size of the buffer */
374 if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
375 res = PyErr_SetFromErrno(PyExc_IOError);
379 /* Try to allocate the memory, using Python's allocator */
380 if((buf = PyMem_Malloc(nalloc)) == NULL) {
381 res = PyErr_NoMemory();
385 /* Now retrieve the attribute value */
386 if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
387 res = PyErr_SetFromErrno(PyExc_IOError);
391 /* Create the string which will hold the result */
392 res = PyBytes_FromStringAndSize(buf, nret);
394 /* Free the buffers, they are no longer needed */
402 PyMem_Free(attrname);
404 /* Return the result */
408 /* Wrapper for getxattr */
409 static char __get_all_doc__[] =
410 "get_all(item[, nofollow=False, namespace=None])\n"
411 "Get all the extended attributes of an item.\n"
413 "This function performs a bulk-get of all extended attribute names\n"
414 "and the corresponding value.\n"
417 " >>> xattr.get_all('/path/to/file')\n"
418 " [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
419 " ('system.posix_acl_access', '\\x02\\x00...')]\n"
420 " >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
421 " [('mime-type', 'plain/text'), ('comment', 'test')]\n"
424 ":keyword namespace: an optional namespace for filtering the\n"
425 " attributes; for example, querying all user attributes can be\n"
426 " accomplished by passing namespace=:const:`NS_USER`\n"
427 ":type namespace: string\n"
429 ":return: list of tuples (name, value); note that if a namespace\n"
430 " argument was passed, it (and the separator) will be stripped from\n"
431 " the names returned\n"
433 ":raises EnvironmentError: caused by any system errors\n"
435 ".. note:: Since reading the whole attribute list is not an atomic\n"
436 " operation, it might be possible that attributes are added\n"
437 " or removed between the initial query and the actual reading\n"
438 " of the attributes; the returned list will contain only the\n"
439 " attributes that were present at the initial listing of the\n"
440 " attribute names and that were still present when the read\n"
441 " attempt for the value is made.\n"
442 ".. versionadded:: 0.4\n"
447 get_all(PyObject *self, PyObject *args, PyObject *keywds)
449 PyObject *myarg, *res;
451 const char *ns = NULL;
452 char *buf_list, *buf_val;
454 ssize_t nalloc, nlist, nval;
457 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
459 /* Parse the arguments */
460 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
461 &myarg, &nofollow, &ns))
463 if(convert_obj(myarg, &tgt, nofollow) < 0)
466 /* Compute first the list of attributes */
468 /* Find out the needed size of the buffer for the attribute list */
469 nalloc = _list_obj(&tgt, NULL, 0);
472 res = PyErr_SetFromErrno(PyExc_IOError);
476 /* Try to allocate the memory, using Python's allocator */
477 if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
478 res = PyErr_NoMemory();
482 /* Now retrieve the list of attributes */
483 nlist = _list_obj(&tgt, buf_list, nalloc);
486 res = PyErr_SetFromErrno(PyExc_IOError);
490 /* Create the list which will hold the result */
491 mylist = PyList_New(0);
497 nalloc = ESTIMATE_ATTR_SIZE;
498 if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
500 res = PyErr_NoMemory();
504 /* Create and insert the attributes as strings in the list */
505 for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
510 if((name=matches_ns(ns, s))==NULL)
512 /* Now retrieve the attribute value */
515 nval = _get_obj(&tgt, s, buf_val, nalloc);
518 if(errno == ERANGE) {
519 nval = _get_obj(&tgt, s, NULL, 0);
520 if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL) {
521 res = PyErr_NoMemory();
527 } else if(errno == ENODATA || errno == ENOATTR) {
528 /* this attribute has gone away since we queried
529 the attribute list */
533 /* else we're dealing with a different error, which we
534 don't know how to handle nicely, so we abort */
536 res = PyErr_SetFromErrno(PyExc_IOError);
544 my_tuple = Py_BuildValue("yy#", name, buf_val, nval);
546 my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
548 if (my_tuple == NULL) {
553 PyList_Append(mylist, my_tuple);
557 /* Successful exit */
564 PyMem_Free(buf_list);
569 /* Return the result */
574 static char __pysetxattr_doc__[] =
575 "setxattr(item, name, value[, flags=0, nofollow=False])\n"
576 "Set the value of a given extended attribute (deprecated).\n"
578 "Be careful in case you want to set attributes on symbolic\n"
579 "links, you have to use all the 5 parameters; use 0 for the \n"
580 "flags value if you want the default behaviour (create or "
589 ".. deprecated:: 0.4\n"
590 " this function has been deprecated\n"
591 " by the :func:`set` function.\n"
594 /* Wrapper for setxattr */
596 pysetxattr(PyObject *self, PyObject *args)
598 PyObject *myarg, *res;
600 char *attrname = NULL;
607 /* Parse the arguments */
608 if (!PyArg_ParseTuple(args, "Oetet#|ii", &myarg, NULL, &attrname,
609 NULL, &buf, &bufsize, &flags, &nofollow))
611 if(convert_obj(myarg, &tgt, nofollow) < 0) {
616 /* Set the attribute's value */
617 nret = _set_obj(&tgt, attrname, buf, bufsize, flags);
622 res = PyErr_SetFromErrno(PyExc_IOError);
630 PyMem_Free(attrname);
633 /* Return the result */
637 static char __set_doc__[] =
638 "set(item, name, value[, flags=0, namespace=None])\n"
639 "Set the value of a given extended attribute.\n"
643 " >>> xattr.set('/path/to/file', 'user.comment', 'test')\n"
644 " >>> xattr.set('/path/to/file', 'comment', 'test',"
645 " namespace=xattr.NS_USER)\n"
654 ":raises EnvironmentError: caused by any system errors\n"
656 ".. versionadded:: 0.4\n"
660 /* Wrapper for setxattr */
662 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
664 PyObject *myarg, *res;
666 char *attrname = NULL;
672 const char *ns = NULL;
674 const char *full_name;
675 static char *kwlist[] = {"item", "name", "value", "flags",
676 "nofollow", "namespace", NULL};
678 /* Parse the arguments */
679 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oetet#|ii" BYTES_CHAR,
680 kwlist, &myarg, NULL, &attrname, NULL,
681 &buf, &bufsize, &flags, &nofollow, &ns))
683 if(convert_obj(myarg, &tgt, nofollow) < 0) {
688 if(merge_ns(ns, attrname, &full_name, &newname) < 0) {
693 /* Set the attribute's value */
694 nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
701 res = PyErr_SetFromErrno(PyExc_IOError);
709 PyMem_Free(attrname);
712 /* Return the result */
717 static char __pyremovexattr_doc__[] =
718 "removexattr(item, name[, nofollow])\n"
719 "Remove an attribute from a file (deprecated).\n"
725 ".. deprecated:: 0.4\n"
726 " this function has been deprecated by the :func:`remove` function.\n"
729 /* Wrapper for removexattr */
731 pyremovexattr(PyObject *self, PyObject *args)
733 PyObject *myarg, *res;
735 char *attrname = NULL;
739 /* Parse the arguments */
740 if (!PyArg_ParseTuple(args, "Oet|i", &myarg, NULL, &attrname, &nofollow))
743 if(convert_obj(myarg, &tgt, nofollow) < 0) {
748 /* Remove the attribute */
749 nret = _remove_obj(&tgt, attrname);
754 res = PyErr_SetFromErrno(PyExc_IOError);
762 PyMem_Free(attrname);
764 /* Return the result */
768 static char __remove_doc__[] =
769 "remove(item, name[, nofollow=False, namespace=None])\n"
770 "Remove an attribute from a file.\n"
774 " >>> xattr.remove('/path/to/file', 'user.comment')\n"
781 ":raises EnvironmentError: caused by any system errors\n"
783 ".. versionadded:: 0.4\n"
787 /* Wrapper for removexattr */
789 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
791 PyObject *myarg, *res;
793 char *attrname = NULL, *name_buf;
794 const char *ns = NULL;
795 const char *full_name;
798 static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
800 /* Parse the arguments */
801 if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oet|i" BYTES_CHAR, kwlist,
802 &myarg, NULL, &attrname, &nofollow, &ns))
805 if(convert_obj(myarg, &tgt, nofollow) < 0) {
810 if(merge_ns(ns, attrname, &full_name, &name_buf) < 0) {
815 /* Remove the attribute */
816 nret = _remove_obj(&tgt, full_name);
818 PyMem_Free(name_buf);
823 res = PyErr_SetFromErrno(PyExc_IOError);
831 PyMem_Free(attrname);
833 /* Return the result */
837 static char __pylistxattr_doc__[] =
838 "listxattr(item[, nofollow=False])\n"
839 "Return the list of attribute names for a file (deprecated).\n"
844 ".. deprecated:: 0.4\n"
845 " this function has been deprecated by the :func:`list` function.\n"
848 /* Wrapper for listxattr */
850 pylistxattr(PyObject *self, PyObject *args)
854 ssize_t nalloc, nret;
861 /* Parse the arguments */
862 if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
864 if(convert_obj(myarg, &tgt, nofollow) < 0)
867 /* Find out the needed size of the buffer */
868 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
869 mylist = PyErr_SetFromErrno(PyExc_IOError);
873 /* Try to allocate the memory, using Python's allocator */
874 if((buf = PyMem_Malloc(nalloc)) == NULL) {
875 mylist = PyErr_NoMemory();
879 /* Now retrieve the list of attributes */
880 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
881 mylist = PyErr_SetFromErrno(PyExc_IOError);
885 /* Compute the number of attributes in the list */
886 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
890 /* Create the list which will hold the result */
891 mylist = PyList_New(nattrs);
895 /* Create and insert the attributes as strings in the list */
896 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
897 PyObject *item = PyBytes_FromString(s);
903 PyList_SET_ITEM(mylist, nattrs, item);
908 /* Free the buffer, now it is no longer needed */
914 /* Return the result */
918 static char __list_doc__[] =
919 "list(item[, nofollow=False, namespace=None])\n"
920 "Return the list of attribute names for a file.\n"
924 " >>> xattr.list('/path/to/file')\n"
925 " ['user.test', 'user.comment', 'system.posix_acl_access']\n"
926 " >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
927 " ['test', 'comment']\n"
932 ":returns: the list of attributes; note that if a namespace \n"
933 " argument was passed, it (and the separator) will be stripped\n"
937 ":raises EnvironmentError: caused by any system errors\n"
939 ".. versionadded:: 0.4\n"
943 /* Wrapper for listxattr */
945 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
949 ssize_t nalloc, nret;
952 const char *ns = NULL;
956 static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
958 /* Parse the arguments */
959 if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|i" BYTES_CHAR, kwlist,
960 &myarg, &nofollow, &ns))
962 if(convert_obj(myarg, &tgt, nofollow) < 0) {
967 /* Find out the needed size of the buffer */
968 if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
969 res = PyErr_SetFromErrno(PyExc_IOError);
973 /* Try to allocate the memory, using Python's allocator */
974 if((buf = PyMem_Malloc(nalloc)) == NULL) {
975 res = PyErr_NoMemory();
979 /* Now retrieve the list of attributes */
980 if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
981 res = PyErr_SetFromErrno(PyExc_IOError);
985 /* Compute the number of attributes in the list */
986 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
987 if(matches_ns(ns, s) != NULL)
990 /* Create the list which will hold the result */
991 res = PyList_New(nattrs);
995 /* Create and insert the attributes as strings in the list */
996 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
997 const char *name = matches_ns(ns, s);
999 PyObject *item = PyBytes_FromString(name);
1005 PyList_SET_ITEM(res, nattrs, item);
1011 /* Free the buffer, now it is no longer needed */
1018 /* Return the result */
1022 static PyMethodDef xattr_methods[] = {
1023 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
1024 {"get", (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
1026 {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
1028 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
1029 {"set", (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
1031 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
1032 {"remove", (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
1034 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
1035 {"list", (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
1037 {NULL, NULL, 0, NULL} /* Sentinel */
1040 static char __xattr_doc__[] = \
1041 "This module gives access to the extended attributes present\n"
1042 "in some operating systems/filesystems. You can list attributes,\n"
1043 "get, set and remove them.\n"
1045 "The module exposes two sets of functions:\n"
1046 " - the 'old' :func:`listxattr`, :func:`getxattr`, :func:`setxattr`,\n"
1047 " :func:`removexattr`\n"
1048 " functions which are deprecated since version 0.4\n"
1049 " - the new :func:`list`, :func:`get`, :func:`get_all`, :func:`set`,\n"
1050 " :func:`remove` functions\n"
1051 " which expose a namespace-aware API and simplify a bit the calling\n"
1052 " model by using keyword arguments\n"
1055 " >>> import xattr\n"
1056 " >>> xattr.listxattr(\"file.txt\")\n"
1057 " ['user.mime_type']\n"
1058 " >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
1060 " >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
1061 "\"Simple text file\")\n"
1062 " >>> xattr.listxattr(\"file.txt\")\n"
1063 " ['user.mime_type', 'user.comment']\n"
1064 " >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
1066 ".. note:: Most or all errors reported by the system while using\n"
1067 " the ``xattr`` library will be reported by raising\n"
1068 " a :exc:`EnvironmentError`; under\n"
1069 " Linux, the following ``errno`` values are used:\n"
1071 " - ``ENOATTR`` and ``ENODATA`` mean that the attribute name is\n"
1073 " - ``ENOTSUP`` and ``EOPNOTSUPP`` mean that the filesystem does not\n"
1074 " support extended attributes, or that the namespace is invalid\n"
1075 " - ``E2BIG`` mean that the attribute value is too big\n"
1076 " - ``ERANGE`` mean that the attribute name is too big (it might also\n"
1077 " mean an error in the xattr module itself)\n"
1078 " - ``ENOSPC`` and ``EDQUOT`` are documented as meaning out of disk\n"
1079 " space or out of disk space because of quota limits\n"
1080 ".. note:: Under Python 3, the namespace argument is a byte string,\n"
1081 " not a unicode string.\n"
1087 static struct PyModuleDef xattrmodule = {
1088 PyModuleDef_HEAD_INIT,
1095 #define INITERROR return NULL
1101 #define INITERROR return
1106 PyObject *ns_security = NULL;
1107 PyObject *ns_system = NULL;
1108 PyObject *ns_trusted = NULL;
1109 PyObject *ns_user = NULL;
1111 PyObject *m = PyModule_Create(&xattrmodule);
1113 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
1118 PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
1119 PyModule_AddStringConstant(m, "__contact__", _XATTR_EMAIL);
1120 PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
1121 PyModule_AddStringConstant(m, "__license__",
1122 "GNU Lesser General Public License (LGPL)");
1123 PyModule_AddStringConstant(m, "__docformat__", "restructuredtext en");
1125 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
1126 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
1128 /* namespace constants */
1129 if((ns_security = PyBytes_FromString("security")) == NULL)
1131 if((ns_system = PyBytes_FromString("system")) == NULL)
1133 if((ns_trusted = PyBytes_FromString("trusted")) == NULL)
1135 if((ns_user = PyBytes_FromString("user")) == NULL)
1137 if(PyModule_AddObject(m, "NS_SECURITY", ns_security) < 0)
1140 if(PyModule_AddObject(m, "NS_SYSTEM", ns_system) < 0)
1143 if(PyModule_AddObject(m, "NS_TRUSTED", ns_trusted) < 0)
1146 if(PyModule_AddObject(m, "NS_USER", ns_user) < 0)
1157 Py_XDECREF(ns_user);
1158 Py_XDECREF(ns_trusted);
1159 Py_XDECREF(ns_system);
1160 Py_XDECREF(ns_security);