]> git.k1024.org Git - pyxattr.git/blob - xattr.c
Docstring updates
[pyxattr.git] / xattr.c
1 #define PY_SSIZE_T_CLEAN
2 #include <Python.h>
3 #include <attr/xattr.h>
4 #include <stdio.h>
5
6 /* Compatibility with python 2.4 regarding python size type (PEP 353) */
7 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
8 typedef int Py_ssize_t;
9 #define PY_SSIZE_T_MAX INT_MAX
10 #define PY_SSIZE_T_MIN INT_MIN
11 #endif
12
13 /* the estimated (startup) attribute buffer size in
14    multi-operations */
15 #define ESTIMATE_ATTR_SIZE 256
16
17 typedef enum {T_FD, T_PATH, T_LINK} target_e;
18
19 typedef struct {
20     target_e type;
21     union {
22         const char *name;
23         int fd;
24     };
25 } target_t;
26
27 /** Converts from a string, file or int argument to what we need. */
28 static int convertObj(PyObject *myobj, target_t *tgt, int nofollow) {
29     int fd;
30     if(PyString_Check(myobj)) {
31         tgt->type = nofollow ? T_LINK : T_PATH;
32         tgt->name = PyString_AS_STRING(myobj);
33     } else if((fd = PyObject_AsFileDescriptor(myobj)) != -1) {
34         tgt->type = T_FD;
35         tgt->fd = fd;
36     } else {
37         PyErr_SetString(PyExc_TypeError, "argument must be string or int");
38         return 0;
39     }
40     return 1;
41 }
42
43 /* Combine a namespace string and an attribute name into a
44    fully-qualified name */
45 static const char* merge_ns(const char *ns, const char *name, char **buf) {
46     if(ns != NULL) {
47         int cnt;
48         size_t new_size = strlen(ns) + 1 + strlen(name) + 1;
49         if((*buf = PyMem_Malloc(new_size)) == NULL) {
50             PyErr_NoMemory();
51             return NULL;
52         }
53         cnt = snprintf(*buf, new_size, "%s.%s", ns, name);
54         if(cnt > new_size || cnt < 0) {
55             PyErr_SetString(PyExc_ValueError,
56                             "can't format the attribute name");
57             PyMem_Free(*buf);
58             return NULL;
59         }
60         return *buf;
61     } else {
62         *buf = NULL;
63         return name;
64     }
65 }
66
67 static ssize_t _list_obj(target_t *tgt, char *list, size_t size) {
68     if(tgt->type == T_FD)
69         return flistxattr(tgt->fd, list, size);
70     else if (tgt->type == T_LINK)
71         return llistxattr(tgt->name, list, size);
72     else
73         return listxattr(tgt->name, list, size);
74 }
75
76 static ssize_t _get_obj(target_t *tgt, const char *name, void *value,
77                         size_t size) {
78     if(tgt->type == T_FD)
79         return fgetxattr(tgt->fd, name, value, size);
80     else if (tgt->type == T_LINK)
81         return lgetxattr(tgt->name, name, value, size);
82     else
83         return getxattr(tgt->name, name, value, size);
84 }
85
86 static int _set_obj(target_t *tgt, const char *name,
87                     const void *value, size_t size, int flags) {
88     if(tgt->type == T_FD)
89         return fsetxattr(tgt->fd, name, value, size, flags);
90     else if (tgt->type == T_LINK)
91         return lsetxattr(tgt->name, name, value, size, flags);
92     else
93         return setxattr(tgt->name, name, value, size, flags);
94 }
95
96 static int _remove_obj(target_t *tgt, const char *name) {
97     if(tgt->type == T_FD)
98         return fremovexattr(tgt->fd, name);
99     else if (tgt->type == T_LINK)
100         return lremovexattr(tgt->name, name);
101     else
102         return removexattr(tgt->name, name);
103 }
104
105 /*
106    Checks if an attribute name matches an optional namespace.
107
108    If the namespace is NULL, it will return the name itself.  If the
109    namespace is non-NULL and the name matches, it will return a
110    pointer to the offset in the name after the namespace and the
111    separator. If however the name doesn't match the namespace, it will
112    return NULL.
113 */
114 const char *matches_ns(const char *ns, const char *name) {
115     size_t ns_size;
116     if (ns == NULL)
117         return name;
118     ns_size = strlen(ns);
119
120     if (strlen(name) > (ns_size+1) && !strncmp(name, ns, ns_size) &&
121         name[ns_size] == '.')
122         return name + ns_size + 1;
123     return NULL;
124 }
125
126 /* Wrapper for getxattr */
127 static char __pygetxattr_doc__[] =
128     "Get the value of a given extended attribute (deprecated).\n"
129     "\n"
130     "Parameters:\n"
131     "  - a string representing filename, or a file-like object,\n"
132     "    or a file descriptor; this represents the file on \n"
133     "    which to act\n"
134     "  - a string, representing the attribute whose value to retrieve;\n"
135     "    usually in form of system.posix_acl or user.mime_type\n"
136     "  - (optional) a boolean value (defaults to false), which, if\n"
137     "    the file name given is a symbolic link, makes the\n"
138     "    function operate on the symbolic link itself instead\n"
139     "    of its target;\n"
140     "@deprecated: since version 0.4, this function has been deprecated\n"
141     "    by the L{get} function\n"
142     ;
143
144 static PyObject *
145 pygetxattr(PyObject *self, PyObject *args)
146 {
147     PyObject *myarg;
148     target_t tgt;
149     int nofollow=0;
150     char *attrname;
151     char *buf;
152     ssize_t nalloc, nret;
153     PyObject *res;
154
155     /* Parse the arguments */
156     if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
157         return NULL;
158     if(!convertObj(myarg, &tgt, nofollow))
159         return NULL;
160
161     /* Find out the needed size of the buffer */
162     if((nalloc = _get_obj(&tgt, attrname, NULL, 0)) == -1) {
163         return PyErr_SetFromErrno(PyExc_IOError);
164     }
165
166     /* Try to allocate the memory, using Python's allocator */
167     if((buf = PyMem_Malloc(nalloc)) == NULL) {
168         PyErr_NoMemory();
169         return NULL;
170     }
171
172     /* Now retrieve the attribute value */
173     if((nret = _get_obj(&tgt, attrname, buf, nalloc)) == -1) {
174         PyMem_Free(buf);
175         return PyErr_SetFromErrno(PyExc_IOError);
176     }
177
178     /* Create the string which will hold the result */
179     res = PyString_FromStringAndSize(buf, nret);
180
181     /* Free the buffer, now it is no longer needed */
182     PyMem_Free(buf);
183
184     /* Return the result */
185     return res;
186 }
187
188 /* Wrapper for getxattr */
189 static char __get_doc__[] =
190     "Get the value of a given extended attribute.\n"
191     "\n"
192     "@param item: the item to query; either a string representing the\n"
193     "    filename, or a file-like object, or a file descriptor\n"
194     "@param name: the attribute whose value to set; usually in form of\n"
195     "    system.posix_acl or user.mime_type\n"
196     "@type name: string\n"
197     "@param nofollow: if given and True, and the function is passed a\n"
198     "    filename that points to a symlink, the function will act on the\n"
199     "    symlink itself instead of its target\n"
200     "@type nofollow: boolean\n"
201     "@param namespace: if given, the attribute must not contain the\n"
202     "    namespace itself, but instead the namespace will be taken from\n"
203     "    this parameter\n"
204     "@type namespace: string\n"
205     "@return: the value of the extended attribute (can contain NULLs)\n"
206     "@rtype: string\n"
207     "@raise EnvironmentError: caused by any system errors\n"
208     "@since: 0.4\n"
209     ;
210
211 static PyObject *
212 xattr_get(PyObject *self, PyObject *args, PyObject *keywds)
213 {
214     PyObject *myarg;
215     target_t tgt;
216     int nofollow=0;
217     char *attrname, *namebuf;
218     const char *fullname;
219     char *buf;
220     char *ns = NULL;
221     ssize_t nalloc, nret;
222     PyObject *res;
223     static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
224
225     /* Parse the arguments */
226     if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|iz", kwlist,
227                                      &myarg, &attrname, &nofollow, &ns))
228         return NULL;
229     if(!convertObj(myarg, &tgt, nofollow))
230         return NULL;
231
232     fullname = merge_ns(ns, attrname, &namebuf);
233
234     /* Find out the needed size of the buffer */
235     if((nalloc = _get_obj(&tgt, fullname, NULL, 0)) == -1) {
236         return PyErr_SetFromErrno(PyExc_IOError);
237     }
238
239     /* Try to allocate the memory, using Python's allocator */
240     if((buf = PyMem_Malloc(nalloc)) == NULL) {
241         PyMem_Free(namebuf);
242         PyErr_NoMemory();
243         return NULL;
244     }
245
246     /* Now retrieve the attribute value */
247     if((nret = _get_obj(&tgt, fullname, buf, nalloc)) == -1) {
248         PyMem_Free(buf);
249         PyMem_Free(namebuf);
250         return PyErr_SetFromErrno(PyExc_IOError);
251     }
252
253     /* Create the string which will hold the result */
254     res = PyString_FromStringAndSize(buf, nret);
255
256     /* Free the buffers, they are no longer needed */
257     PyMem_Free(namebuf);
258     PyMem_Free(buf);
259
260     /* Return the result */
261     return res;
262 }
263
264 /* Wrapper for getxattr */
265 static char __get_all_doc__[] =
266     "Get all the extended attributes of an item.\n"
267     "\n"
268     "This function performs a bulk-get of all extended attribute names\n"
269     "and the corresponding value.\n"
270     "Example:\n"
271     "    >>> xattr.get_all('/path/to/file')\n"
272     "    [('user.mime-type', 'plain/text'), ('user.comment', 'test'),\n"
273     "     ('system.posix_acl_access', '\\x02\\x00...')]\n"
274     "    >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
275     "    [('mime-type', 'plain/text'), ('comment', 'test')]\n"
276     "\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     "@keyword namespace: an optional namespace for filtering the\n"
280     "    attributes; for example, querying all user attributes can be\n"
281     "    accomplished by passing namespace=L{NS_USER}\n"
282     "@type namespace: string\n"
283     "@keyword nofollow: if passed and true, if the target file is a\n"
284     "    symbolic link, the attributes for the link itself will be\n"
285     "    returned, instead of the attributes of the target\n"
286     "@type nofollow: boolean\n"
287     "@return: list of tuples (name, value); note that if a namespace\n"
288     "    argument was passed, it (and the separator) will be stripped from\n"
289     "    the names returned\n"
290     "@rtype: list\n"
291     "@raise EnvironmentError: caused by any system errors\n"
292     "@note: Since reading the whole attribute list is not an atomic\n"
293     "    operation, it might be possible that attributes are added\n"
294     "    or removed between the initial query and the actual reading\n"
295     "    of the attributes; the returned list will contain only the\n"
296     "    attributes that were present at the initial listing of the\n"
297     "    attribute names and that were still present when the read\n"
298     "    attempt for the value is made.\n"
299     "@since: 0.4\n"
300     ;
301
302 static PyObject *
303 get_all(PyObject *self, PyObject *args, PyObject *keywds)
304 {
305     PyObject *myarg;
306     int dolink=0;
307     char *ns = NULL;
308     char *buf_list, *buf_val;
309     char *s;
310     size_t nalloc, nlist, nval;
311     PyObject *mylist;
312     target_t tgt;
313     static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
314
315     /* Parse the arguments */
316     if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
317                                      &myarg, &dolink, &ns))
318         return NULL;
319     if(!convertObj(myarg, &tgt, dolink))
320         return NULL;
321
322     /* Compute first the list of attributes */
323
324     /* Find out the needed size of the buffer for the attribute list */
325     nalloc = _list_obj(&tgt, NULL, 0);
326
327     if(nalloc == -1) {
328         return PyErr_SetFromErrno(PyExc_IOError);
329     }
330
331     /* Try to allocate the memory, using Python's allocator */
332     if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
333         PyErr_NoMemory();
334         return NULL;
335     }
336
337     /* Now retrieve the list of attributes */
338     nlist = _list_obj(&tgt, buf_list, nalloc);
339
340     if(nlist == -1) {
341         PyErr_SetFromErrno(PyExc_IOError);
342         goto free_buf_list;
343     }
344
345     /* Create the list which will hold the result */
346     mylist = PyList_New(0);
347     nalloc = ESTIMATE_ATTR_SIZE;
348     if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
349         PyErr_NoMemory();
350         goto free_list;
351     }
352
353     /* Create and insert the attributes as strings in the list */
354     for(s = buf_list; s - buf_list < nlist; s += strlen(s) + 1) {
355         PyObject *my_tuple;
356         int missing;
357         const char *name;
358
359         if((name=matches_ns(ns, s))==NULL)
360             continue;
361         /* Now retrieve the attribute value */
362         missing = 0;
363         while(1) {
364             nval = _get_obj(&tgt, s, buf_val, nalloc);
365
366             if(nval == -1) {
367                 if(errno == ERANGE) {
368                     nval = _get_obj(&tgt, s, NULL, 0);
369                     if((buf_val = PyMem_Realloc(buf_val, nval)) == NULL)
370                         goto free_list;
371                     nalloc = nval;
372                     continue;
373                 } else if(errno == ENODATA || errno == ENOATTR) {
374                     /* this attribute has gone away since we queried
375                        the attribute list */
376                     missing = 1;
377                     break;
378                 }
379                 goto exit_errno;
380             }
381             break;
382         }
383         if(missing)
384             continue;
385         my_tuple = Py_BuildValue("ss#", name, buf_val, nval);
386
387         PyList_Append(mylist, my_tuple);
388         Py_DECREF(my_tuple);
389     }
390
391     /* Free the buffers, now they are no longer needed */
392     PyMem_Free(buf_val);
393     PyMem_Free(buf_list);
394
395     /* Return the result */
396     return mylist;
397  exit_errno:
398     PyErr_SetFromErrno(PyExc_IOError);
399     PyMem_Free(buf_val);
400  free_list:
401     Py_DECREF(mylist);
402  free_buf_list:
403     PyMem_Free(buf_list);
404     return NULL;
405 }
406
407
408 static char __pysetxattr_doc__[] =
409     "Set the value of a given extended attribute (deprecated).\n"
410     "Be carefull in case you want to set attributes on symbolic\n"
411     "links, you have to use all the 5 parameters; use 0 for the \n"
412     "flags value if you want the default behavior (create or "
413     "replace)\n"
414     "\n"
415     "Parameters:\n"
416     "  - a string representing filename, or a file-like object,\n"
417     "    or a file descriptor; this represents the file on \n"
418     "    which to act\n"
419     "  - a string, representing the attribute whose value to set;\n"
420     "    usually in form of system.posix_acl or user.mime_type\n"
421     "  - a string, possibly with embedded NULLs; note that there\n"
422     "    are restrictions regarding the size of the value, for\n"
423     "    example, for ext2/ext3, maximum size is the block size\n"
424     "  - (optional) flags; if 0 or ommited the attribute will be \n"
425     "    created or replaced; if XATTR_CREATE, the attribute \n"
426     "    will be created, giving an error if it already exists;\n"
427     "    of XATTR_REPLACE, the attribute will be replaced,\n"
428     "    giving an error if it doesn't exists;\n"
429     "  - (optional) a boolean value (defaults to false), which, if\n"
430     "    the file name given is a symbolic link, makes the\n"
431     "    function operate on the symbolic link itself instead\n"
432     "    of its target;\n"
433     "@deprecated: since version 0.4, this function has been deprecated\n"
434     "    by the L{set} function\n"
435     ;
436
437 /* Wrapper for setxattr */
438 static PyObject *
439 pysetxattr(PyObject *self, PyObject *args)
440 {
441     PyObject *myarg;
442     int nofollow=0;
443     char *attrname;
444     char *buf;
445     Py_ssize_t bufsize;
446     int nret;
447     int flags = 0;
448     target_t tgt;
449
450     /* Parse the arguments */
451     if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname,
452                           &buf, &bufsize, &flags, &nofollow))
453         return NULL;
454     if(!convertObj(myarg, &tgt, nofollow))
455         return NULL;
456
457     /* Set the attribute's value */
458     if((nret = _set_obj(&tgt, attrname, buf, bufsize, flags)) == -1) {
459         return PyErr_SetFromErrno(PyExc_IOError);
460     }
461
462     /* Return the result */
463     Py_RETURN_NONE;
464 }
465
466 static char __set_doc__[] =
467     "Set the value of a given extended attribute.\n"
468     "\n"
469     "@param item: the item to query; either a string representing the\n"
470     "    filename, or a file-like object, or a file descriptor\n"
471     "@param name: the attribute whose value to set; usually in form of\n"
472     "    system.posix_acl or user.mime_type\n"
473     "@type name: string\n"
474     "@param value: a string, possibly with embedded NULLs; note that there\n"
475     "    are restrictions regarding the size of the value, for\n"
476     "    example, for ext2/ext3, maximum size is the block size\n"
477     "@type value: string\n"
478     "@param flags: if 0 or ommited the attribute will be\n"
479     "    created or replaced; if L{XATTR_CREATE}, the attribute\n"
480     "    will be created, giving an error if it already exists;\n"
481     "    if L{XATTR_REPLACE}, the attribute will be replaced,\n"
482     "    giving an error if it doesn't exists;\n"
483     "@type flags: integer\n"
484     "@param nofollow: if given and True, and the function is passed a\n"
485     "    filename that points to a symlink, the function will act on the\n"
486     "    symlink itself instead of its target\n"
487     "@type nofollow: boolean\n"
488     "@param namespace: if given, the attribute must not contain the\n"
489     "    namespace itself, but instead the namespace will be taken from\n"
490     "    this parameter\n"
491     "@type namespace: string\n"
492     "@rtype: None\n"
493     "@raise EnvironmentError: caused by any system errors\n"
494     "@since: 0.4\n"
495     ;
496
497 /* Wrapper for setxattr */
498 static PyObject *
499 xattr_set(PyObject *self, PyObject *args, PyObject *keywds)
500 {
501     PyObject *myarg;
502     int nofollow=0;
503     char *attrname;
504     char *buf;
505     Py_ssize_t bufsize;
506     int nret;
507     int flags = 0;
508     target_t tgt;
509     char *ns = NULL;
510     char *newname;
511     const char *full_name;
512     static char *kwlist[] = {"item", "name", "value", "flags",
513                              "nofollow", "namespace", NULL};
514
515     /* Parse the arguments */
516     if (!PyArg_ParseTupleAndKeywords(args, keywds, "Oss#|iiz", kwlist,
517                                      &myarg, &attrname,
518                                      &buf, &bufsize, &flags, &nofollow, &ns))
519         return NULL;
520     if(!convertObj(myarg, &tgt, nofollow))
521         return NULL;
522
523     full_name = merge_ns(ns, attrname, &newname);
524     /* Set the attribute's value */
525     nret = _set_obj(&tgt, full_name, buf, bufsize, flags);
526     if(newname != NULL)
527         PyMem_Free(newname);
528     if(nret == -1) {
529         return PyErr_SetFromErrno(PyExc_IOError);
530     }
531
532     /* Return the result */
533     Py_RETURN_NONE;
534 }
535
536
537 static char __pyremovexattr_doc__[] =
538     "Remove an attribute from a file (deprecated)\n"
539     "\n"
540     "Parameters:\n"
541     "  - a string representing filename, or a file-like object,\n"
542     "    or a file descriptor; this represents the file on \n"
543     "    which to act\n"
544     "  - a string, representing the attribute to be removed;\n"
545     "    usually in form of system.posix_acl or user.mime_type\n"
546     "  - (optional) a boolean value (defaults to false), which, if\n"
547     "    the file name given is a symbolic link, makes the\n"
548     "    function operate on the symbolic link itself instead\n"
549     "    of its target;\n"
550     "@deprecated: since version 0.4, this function has been deprecated\n"
551     "    by the L{remove}"
552     " function\n"
553     ;
554
555 /* Wrapper for removexattr */
556 static PyObject *
557 pyremovexattr(PyObject *self, PyObject *args)
558 {
559     PyObject *myarg;
560     int nofollow=0;
561     char *attrname;
562     int nret;
563     target_t tgt;
564
565     /* Parse the arguments */
566     if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &nofollow))
567         return NULL;
568
569     if(!convertObj(myarg, &tgt, nofollow))
570         return NULL;
571
572     /* Remove the attribute */
573     if((nret = _remove_obj(&tgt, attrname)) == -1) {
574         return PyErr_SetFromErrno(PyExc_IOError);
575     }
576
577     /* Return the result */
578     Py_RETURN_NONE;
579 }
580
581 static char __remove_doc__[] =
582     "Remove an attribute from a file\n"
583     "\n"
584     "@param item: the item to query; either a string representing the\n"
585     "    filename, or a file-like object, or a file descriptor\n"
586     "@param name: the attribute whose value to set; usually in form of\n"
587     "    system.posix_acl or user.mime_type\n"
588     "@type name: string\n"
589     "@param nofollow: if given and True, and the function is passed a\n"
590     "    filename that points to a symlink, the function will act on the\n"
591     "    symlink itself instead of its target\n"
592     "@type nofollow: boolean\n"
593     "@param namespace: if given, the attribute must not contain the\n"
594     "    namespace itself, but instead the namespace will be taken from\n"
595     "    this parameter\n"
596     "@type namespace: string\n"
597     "@since: 0.4\n"
598     "@rtype: None\n"
599     "@raise EnvironmentError: caused by any system errors\n"
600     ;
601
602 /* Wrapper for removexattr */
603 static PyObject *
604 xattr_remove(PyObject *self, PyObject *args, PyObject *keywds)
605 {
606     PyObject *myarg;
607     int nofollow=0;
608     char *attrname, *name_buf;
609     char *ns = NULL;
610     const char *full_name;
611     int nret;
612     target_t tgt;
613     static char *kwlist[] = {"item", "name", "nofollow", "namespace", NULL};
614
615     /* Parse the arguments */
616     if (!PyArg_ParseTupleAndKeywords(args, keywds, "Os|iz", kwlist,
617                                      &myarg, &attrname, &nofollow, &ns))
618         return NULL;
619
620     if(!convertObj(myarg, &tgt, nofollow))
621         return NULL;
622     full_name = merge_ns(ns, attrname, &name_buf);
623     if(full_name == NULL)
624         return NULL;
625
626     /* Remove the attribute */
627     nret = _remove_obj(&tgt, full_name);
628     PyMem_Free(name_buf);
629     if(nret == -1) {
630         return PyErr_SetFromErrno(PyExc_IOError);
631     }
632
633     /* Return the result */
634     Py_RETURN_NONE;
635 }
636
637 static char __pylistxattr_doc__[] =
638     "Return the list of attribute names for a file (deprecated)\n"
639     "\n"
640     "Parameters:\n"
641     "  - a string representing filename, or a file-like object,\n"
642     "    or a file descriptor; this represents the file to \n"
643     "    be queried\n"
644     "  - (optional) a boolean value (defaults to false), which, if\n"
645     "    the file name given is a symbolic link, makes the\n"
646     "    function operate on the symbolic link itself instead\n"
647     "    of its target;\n"
648     "@deprecated: since version 0.4, this function has been deprecated\n"
649     "    by the L{list}"
650     " function\n"
651     ;
652
653 /* Wrapper for listxattr */
654 static PyObject *
655 pylistxattr(PyObject *self, PyObject *args)
656 {
657     char *buf;
658     int nofollow=0;
659     ssize_t nalloc, nret;
660     PyObject *myarg;
661     PyObject *mylist;
662     Py_ssize_t nattrs;
663     char *s;
664     target_t tgt;
665
666     /* Parse the arguments */
667     if (!PyArg_ParseTuple(args, "O|i", &myarg, &nofollow))
668         return NULL;
669     if(!convertObj(myarg, &tgt, nofollow))
670         return NULL;
671
672     /* Find out the needed size of the buffer */
673     if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
674         return PyErr_SetFromErrno(PyExc_IOError);
675     }
676
677     /* Try to allocate the memory, using Python's allocator */
678     if((buf = PyMem_Malloc(nalloc)) == NULL) {
679         PyErr_NoMemory();
680         return NULL;
681     }
682
683     /* Now retrieve the list of attributes */
684     if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
685         PyMem_Free(buf);
686         return PyErr_SetFromErrno(PyExc_IOError);
687     }
688
689     /* Compute the number of attributes in the list */
690     for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
691         nattrs++;
692     }
693
694     /* Create the list which will hold the result */
695     mylist = PyList_New(nattrs);
696
697     /* Create and insert the attributes as strings in the list */
698     for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
699         PyList_SET_ITEM(mylist, nattrs, PyString_FromString(s));
700         nattrs++;
701     }
702
703     /* Free the buffer, now it is no longer needed */
704     PyMem_Free(buf);
705
706     /* Return the result */
707     return mylist;
708 }
709
710 static char __list_doc__[] =
711     "Return the list of attribute names for a file\n"
712     "\n"
713     "Example:\n"
714     "    >>> xattr.list('/path/to/file')\n"
715     "    ['user.test', 'user.comment', 'system.posix_acl_access']\n"
716     "    >>> xattr.list('/path/to/file', namespace=xattr.NS_USER)\n"
717     "    ['test', 'comment']\n"
718     "\n"
719     "@param item: the item to query; either a string representing the\n"
720     "    filename, or a file-like object, or a file descriptor\n"
721     "@param nofollow: if given and True, and the function is passed a\n"
722     "    filename that points to a symlink, the function will act on the\n"
723     "    symlink itself instead of its target\n"
724     "@type nofollow: boolean\n"
725     "@param namespace: if given, the attribute must not contain the\n"
726     "    namespace itself, but instead the namespace will be taken from\n"
727     "    this parameter\n"
728     "@type namespace: string\n"
729     "@return: list of strings; note that if a namespace argument was\n"
730     "    passed, it (and the separator) will be stripped from the names\n"
731     "    returned\n"
732     "@rtype: list\n"
733     "@raise EnvironmentError: caused by any system errors\n"
734     "@since: 0.4\n"
735     ;
736
737 /* Wrapper for listxattr */
738 static PyObject *
739 xattr_list(PyObject *self, PyObject *args, PyObject *keywds)
740 {
741     char *buf;
742     int nofollow=0;
743     ssize_t nalloc, nret;
744     PyObject *myarg;
745     PyObject *mylist;
746     char *ns = NULL;
747     Py_ssize_t nattrs;
748     char *s;
749     target_t tgt;
750     static char *kwlist[] = {"item", "nofollow", "namespace", NULL};
751
752     /* Parse the arguments */
753     if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
754                           &myarg, &nofollow, &ns))
755         return NULL;
756     if(!convertObj(myarg, &tgt, nofollow))
757         return NULL;
758
759     /* Find out the needed size of the buffer */
760     if((nalloc = _list_obj(&tgt, NULL, 0)) == -1) {
761         return PyErr_SetFromErrno(PyExc_IOError);
762     }
763
764     /* Try to allocate the memory, using Python's allocator */
765     if((buf = PyMem_Malloc(nalloc)) == NULL) {
766         PyErr_NoMemory();
767         return NULL;
768     }
769
770     /* Now retrieve the list of attributes */
771     if((nret = _list_obj(&tgt, buf, nalloc)) == -1) {
772         PyMem_Free(buf);
773         return PyErr_SetFromErrno(PyExc_IOError);
774     }
775
776     /* Compute the number of attributes in the list */
777     for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
778         if(matches_ns(ns, s)!=NULL)
779             nattrs++;
780     }
781     /* Create the list which will hold the result */
782     mylist = PyList_New(nattrs);
783
784     /* Create and insert the attributes as strings in the list */
785     for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
786         const char *name = matches_ns(ns, s);
787         if(name!=NULL) {
788             PyList_SET_ITEM(mylist, nattrs, PyString_FromString(name));
789             nattrs++;
790         }
791     }
792
793     /* Free the buffer, now it is no longer needed */
794     PyMem_Free(buf);
795
796     /* Return the result */
797     return mylist;
798 }
799
800 static PyMethodDef xattr_methods[] = {
801     {"getxattr",  pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
802     {"get",  (PyCFunction) xattr_get, METH_VARARGS | METH_KEYWORDS,
803      __get_doc__ },
804     {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
805      __get_all_doc__ },
806     {"setxattr",  pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
807     {"set",  (PyCFunction) xattr_set, METH_VARARGS | METH_KEYWORDS,
808      __set_doc__ },
809     {"removexattr",  pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
810     {"remove",  (PyCFunction) xattr_remove, METH_VARARGS | METH_KEYWORDS,
811      __remove_doc__ },
812     {"listxattr",  pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
813     {"list",  (PyCFunction) xattr_list, METH_VARARGS | METH_KEYWORDS,
814      __list_doc__ },
815     {NULL, NULL, 0, NULL}        /* Sentinel */
816 };
817
818 static char __xattr_doc__[] = \
819     "Access extended filesystem attributes\n"
820     "\n"
821     "This module gives access to the extended attributes present\n"
822     "in some operating systems/filesystems. You can list attributes,\n"
823     "get, set and remove them.\n"
824     "\n"
825     "The module exposes two sets of functions:\n"
826     "  - the 'old' L{listxattr}, L{getxattr}, L{setxattr}, L{removexattr}\n"
827     "    functions which are deprecated since version 0.4\n"
828     "  - the new L{list}, L{get}, L{get_all}, L{set}, L{remove} functions\n"
829     "    which expose a namespace-aware API and simplify a bit the calling\n"
830     "    model by using keyword arguments\n"
831     "\n"
832     "Example: \n\n"
833     "  >>> import xattr\n"
834     "  >>> xattr.listxattr(\"file.txt\")\n"
835     "  ['user.mime_type']\n"
836     "  >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
837     "  'text/plain'\n"
838     "  >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
839     "\"Simple text file\")\n"
840     "  >>> xattr.listxattr(\"file.txt\")\n"
841     "  ['user.mime_type', 'user.comment']\n"
842     "  >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
843     "\n"
844     "@note: Most or all errors reported by the system while using the xattr\n"
845     "library will be reported by raising a L{EnvironmentError}; under Linux,\n"
846     "the following C{errno} values are used:\n"
847     "  - C{ENOATTR} and C{ENODATA} mean that the attribute name is invalid\n"
848     "  - C{ENOTSUP} and C{EOPNOTSUPP} mean that the filesystem does not\n"
849     "    support extended attributes, or that the namespace is invalid\n"
850     "  - C{E2BIG} mean that the attribute value is too big\n"
851     "  - C{ERANGE} mean that the attribute name is too big (it might also\n"
852     "    mean an error in the xattr module itself)\n"
853     "  - C{ENOSPC} and C{EDQUOT} are documented as meaning out of disk space\n"
854     "    or out of disk space because of quota limits\n"
855     "\n"
856     "@group Deprecated API: *xattr\n"
857     "@group Namespace constants: NS_*\n"
858     "@group set function flags: XATTR_CREATE, XATTR_REPLACE\n"
859     "@sort: list, get, get_all, set, remove, listxattr, getxattr, setxattr\n"
860     "    removexattr\n"
861     ;
862
863 void
864 initxattr(void)
865 {
866     PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
867
868     PyModule_AddStringConstant(m, "__author__", _XATTR_AUTHOR);
869     PyModule_AddStringConstant(m, "__version__", _XATTR_VERSION);
870     PyModule_AddStringConstant(m, "__docformat__", "epytext en");
871
872     PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
873     PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
874
875     /* namespace constants */
876     PyModule_AddStringConstant(m, "NS_SECURITY", "security");
877     PyModule_AddStringConstant(m, "NS_SYSTEM", "system");
878     PyModule_AddStringConstant(m, "NS_TRUSTED", "trusted");
879     PyModule_AddStringConstant(m, "NS_USER", "user");
880
881 }