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