]> git.k1024.org Git - pyxattr.git/blob - xattr.c
Epydoc conversion of get_all docstring
[pyxattr.git] / xattr.c
1 #include <Python.h>
2 #include <attr/xattr.h>
3
4 /** Converts from a string, file or int argument to what we need. */
5 static int convertObj(PyObject *myobj, int *ishandle, int *filehandle,
6                       char **filename) {
7     if(PyString_Check(myobj)) {
8         *ishandle = 0;
9         *filename = PyString_AS_STRING(myobj);
10     } else if((*filehandle = PyObject_AsFileDescriptor(myobj)) != -1) {
11         *ishandle = 1;
12     } else {
13         PyErr_SetString(PyExc_TypeError, "argument 1 must be string or int");
14         return 0;
15     }
16     return 1;
17 }
18
19 /* Checks if an attribute name matches an optional namespace */
20 static int matches_ns(const char *name, const char *ns) {
21     size_t ns_size;
22     if (ns == NULL)
23         return 1;
24     ns_size = strlen(ns);
25
26     if (strlen(name) > ns_size && !strncmp(name, ns, ns_size) &&
27         name[ns_size] == '.')
28         return 1;
29     return 0;
30 }
31
32 /* Wrapper for getxattr */
33 static char __pygetxattr_doc__[] =
34     "Get the value of a given extended attribute.\n"
35     "\n"
36     "Parameters:\n"
37     "  - a string representing filename, or a file-like object,\n"
38     "    or a file descriptor; this represents the file on \n"
39     "    which to act\n"
40     "  - a string, representing the attribute whose value to retrieve;\n"
41     "    usually in form of system.posix_acl or user.mime_type\n"
42     "  - (optional) a boolean value (defaults to false), which, if\n"
43     "    the file name given is a symbolic link, makes the\n"
44     "    function operate on the symbolic link itself instead\n"
45     "    of its target;\n"
46     "@deprecated: this function has been replace with the L{get_all} function"
47     " which replaces the positional parameters with keyword ones\n"
48     ;
49
50 static PyObject *
51 pygetxattr(PyObject *self, PyObject *args)
52 {
53     PyObject *myarg;
54     char *file = NULL;
55     int filedes = -1, ishandle, dolink=0;
56     char *attrname;
57     char *buf;
58     int nalloc, nret;
59     PyObject *res;
60
61     /* Parse the arguments */
62     if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
63         return NULL;
64     if(!convertObj(myarg, &ishandle, &filedes, &file))
65         return NULL;
66
67     /* Find out the needed size of the buffer */
68     nalloc = ishandle ?
69         fgetxattr(filedes, attrname, NULL, 0) :
70         dolink ?
71         lgetxattr(file, attrname, NULL, 0) :
72         getxattr(file, attrname, NULL, 0);
73     if(nalloc == -1) {
74         return PyErr_SetFromErrno(PyExc_IOError);
75     }
76
77     /* Try to allocate the memory, using Python's allocator */
78     if((buf = PyMem_Malloc(nalloc)) == NULL) {
79         PyErr_NoMemory();
80         return NULL;
81     }
82
83     /* Now retrieve the attribute value */
84     nret = ishandle ?
85         fgetxattr(filedes, attrname, buf, nalloc) :
86         dolink ?
87         lgetxattr(file, attrname, buf, nalloc) :
88         getxattr(file, attrname, buf, nalloc);
89     if(nret == -1) {
90         PyMem_Free(buf);
91         return PyErr_SetFromErrno(PyExc_IOError);
92     }
93
94     /* Create the string which will hold the result */
95     res = PyString_FromStringAndSize(buf, nret);
96
97     /* Free the buffer, now it is no longer needed */
98     PyMem_Free(buf);
99
100     /* Return the result */
101     return res;
102 }
103
104 /* Wrapper for getxattr */
105 static char __get_all_doc__[] =
106     "Get all the extended attributes of an item.\n"
107     "\n"
108     "This function performs a bulk-get of all extended attribute names\n"
109     "and the corresponding value.\n"
110     "Example:\n"
111     "  >>> xattr.get_all('/path/to/file')\n"
112     "  [('user.mime-type', 'plain/text'), ('user.comment', 'test'),"
113     " ('system.posix_acl_access', '\\x02\\x00...')]\n"
114     "  >>> xattr.get_all('/path/to/file', namespace=xattr.NS_USER)\n"
115     "  [('user.mime-type', 'plain/text'), ('user.comment', 'test')]\n"
116     "\n"
117     "@param item: the item to query; either a string representing the"
118     " filename, or a file-like object, or a file descriptor\n"
119     "@keyword namespace: an optional namespace for filtering the"
120     " attributes; for example, querying all user attributes can be"
121     " accomplished by passing namespace=L{NS_USER}\n"
122     "@type namespace: string\n"
123     "@keyword noderef: if passed and true, if the target file is a symbolic"
124     " link,"
125     " the attributes for the link itself will be returned, instead of the"
126     " attributes of the target\n"
127     "@type noderef: boolean\n"
128     "@return: list of tuples (name, value)\n"
129     ;
130
131 static PyObject *
132 get_all(PyObject *self, PyObject *args, PyObject *keywds)
133 {
134     PyObject *myarg;
135     char *file = NULL;
136     int filedes = -1, ishandle, dolink=0;
137     char *ns = NULL;
138     char *buf_list, *buf_val;
139     char *s;
140     int nalloc, nlist, nval, nattrs;
141     PyObject *mylist;
142     static char *kwlist[] = {"item", "noderef", "namespace", NULL};
143
144     /* Parse the arguments */
145     if (!PyArg_ParseTupleAndKeywords(args, keywds, "O|iz", kwlist,
146                                      &myarg, &dolink, &ns))
147         return NULL;
148     if(!convertObj(myarg, &ishandle, &filedes, &file))
149         return NULL;
150
151     /* Compute first the list of attributes */
152
153     /* Find out the needed size of the buffer for the attribute list */
154     nalloc = ishandle ?
155         flistxattr(filedes, NULL, 0) :
156         dolink ?
157         llistxattr(file, NULL, 0) :
158         listxattr(file, NULL, 0);
159
160     if(nalloc == -1) {
161         return PyErr_SetFromErrno(PyExc_IOError);
162     }
163
164     /* Try to allocate the memory, using Python's allocator */
165     if((buf_list = PyMem_Malloc(nalloc)) == NULL) {
166         PyErr_NoMemory();
167         return NULL;
168     }
169
170     /* Now retrieve the list of attributes */
171     nlist = ishandle ?
172         flistxattr(filedes, buf_list, nalloc) :
173         dolink ?
174         llistxattr(file, buf_list, nalloc) :
175         listxattr(file, buf_list, nalloc);
176
177     if(nlist == -1) {
178         return PyErr_SetFromErrno(PyExc_IOError);
179     }
180
181     /* Compute the number of attributes in the list */
182     for(s = buf_list, nattrs = 0; (s - buf_list) < nlist; s += strlen(s) + 1) {
183         if(matches_ns(s, ns))
184             nattrs++;
185     }
186
187     /* Create the list which will hold the result */
188     mylist = PyList_New(nattrs);
189
190     /* Create and insert the attributes as strings in the list */
191     for(s = buf_list, nattrs = 0; s - buf_list < nlist; s += strlen(s) + 1) {
192         PyObject *my_tuple;
193
194         if(!matches_ns(s, ns))
195             continue;
196
197         /* Find out the needed size of the value buffer */
198         nalloc = ishandle ?
199             fgetxattr(filedes, s, NULL, 0) :
200             dolink ?
201             lgetxattr(file, s, NULL, 0) :
202             getxattr(file, s, NULL, 0);
203         if(nalloc == -1) {
204             PyMem_Free(buf_list);
205             return PyErr_SetFromErrno(PyExc_IOError);
206         }
207
208         /* Try to allocate the memory, using Python's allocator */
209         if((buf_val = PyMem_Malloc(nalloc)) == NULL) {
210             PyMem_Free(buf_list);
211             PyErr_NoMemory();
212             return NULL;
213         }
214
215         /* Now retrieve the attribute value */
216         nval = ishandle ?
217             fgetxattr(filedes, s, buf_val, nalloc) :
218             dolink ?
219             lgetxattr(file, s, buf_val, nalloc) :
220             getxattr(file, s, buf_val, nalloc);
221
222         if(nval == -1) {
223             PyMem_Free(buf_list);
224             PyMem_Free(buf_val);
225             return PyErr_SetFromErrno(PyExc_IOError);
226         }
227         my_tuple = Py_BuildValue("ss#", s, buf_val, nval);
228
229         /* Free the buffer, now it is no longer needed */
230         PyMem_Free(buf_val);
231
232         PyList_SET_ITEM(mylist, nattrs, my_tuple);
233         nattrs++;
234     }
235
236     /* Free the buffer, now it is no longer needed */
237     PyMem_Free(buf_list);
238
239     /* Return the result */
240     return mylist;
241
242 }
243
244
245 static char __pysetxattr_doc__[] =
246     "Set the value of a given extended attribute.\n"
247     "Be carefull in case you want to set attributes on symbolic\n"
248     "links, you have to use all the 5 parameters; use 0 for the \n"
249     "flags value if you want the default behavior (create or "
250     "replace)\n"
251     "\n"
252     "Parameters:\n"
253     "  - a string representing filename, or a file-like object,\n"
254     "    or a file descriptor; this represents the file on \n"
255     "    which to act\n"
256     "  - a string, representing the attribute whose value to set;\n"
257     "    usually in form of system.posix_acl or user.mime_type\n"
258     "  - a string, possibly with embedded NULLs; note that there\n"
259     "    are restrictions regarding the size of the value, for\n"
260     "    example, for ext2/ext3, maximum size is the block size\n"
261     "  - (optional) flags; if 0 or ommited the attribute will be \n"
262     "    created or replaced; if XATTR_CREATE, the attribute \n"
263     "    will be created, giving an error if it already exists;\n"
264     "    of XATTR_REPLACE, the attribute will be replaced,\n"
265     "    giving an error if it doesn't exists;\n"
266     "  - (optional) a boolean value (defaults to false), which, if\n"
267     "    the file name given is a symbolic link, makes the\n"
268     "    function operate on the symbolic link itself instead\n"
269     "    of its target;"
270     ;
271
272 /* Wrapper for setxattr */
273 static PyObject *
274 pysetxattr(PyObject *self, PyObject *args)
275 {
276     PyObject *myarg;
277     char *file;
278     int ishandle, filedes, dolink=0;
279     char *attrname;
280     char *buf;
281     int bufsize, nret;
282     int flags = 0;
283
284     /* Parse the arguments */
285     if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname,
286                           &buf, &bufsize, &flags, &dolink))
287         return NULL;
288     if(!convertObj(myarg, &ishandle, &filedes, &file))
289         return NULL;
290
291     /* Set the attribute's value */
292     nret = ishandle ?
293         fsetxattr(filedes, attrname, buf, bufsize, flags) :
294         dolink ?
295         lsetxattr(file, attrname, buf, bufsize, flags) :
296         setxattr(file, attrname, buf, bufsize, flags);
297
298     if(nret == -1) {
299         return PyErr_SetFromErrno(PyExc_IOError);
300     }
301
302     /* Return the result */
303     Py_INCREF(Py_None);
304     return Py_None;
305 }
306
307 static char __pyremovexattr_doc__[] =
308     "Remove an attribute from a file\n"
309     "\n"
310     "Parameters:\n"
311     "  - a string representing filename, or a file-like object,\n"
312     "    or a file descriptor; this represents the file on \n"
313     "    which to act\n"
314     "  - a string, representing the attribute to be removed;\n"
315     "    usually in form of system.posix_acl or user.mime_type\n"
316     "  - (optional) a boolean value (defaults to false), which, if\n"
317     "    the file name given is a symbolic link, makes the\n"
318     "    function operate on the symbolic link itself instead\n"
319     "    of its target;\n"
320     ;
321
322 /* Wrapper for removexattr */
323 static PyObject *
324 pyremovexattr(PyObject *self, PyObject *args)
325 {
326     PyObject *myarg;
327     char *file;
328     int ishandle, filedes, dolink=0;
329     char *attrname;
330     int nret;
331
332     /* Parse the arguments */
333     if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
334         return NULL;
335
336     if(!convertObj(myarg, &ishandle, &filedes, &file))
337         return NULL;
338
339     /* Remove the attribute */
340     nret = ishandle ?
341         fremovexattr(filedes, attrname) :
342         dolink ?
343         lremovexattr(file, attrname) :
344         removexattr(file, attrname);
345
346     if(nret == -1)
347         return PyErr_SetFromErrno(PyExc_IOError);
348
349     /* Return the result */
350     Py_INCREF(Py_None);
351     return Py_None;
352 }
353
354 static char __pylistxattr_doc__[] =
355     "Return the list of attribute names for a file\n"
356     "\n"
357     "Parameters:\n"
358     "  - a string representing filename, or a file-like object,\n"
359     "    or a file descriptor; this represents the file to \n"
360     "    be queried\n"
361     "  - (optional) a boolean value (defaults to false), which, if\n"
362     "    the file name given is a symbolic link, makes the\n"
363     "    function operate on the symbolic link itself instead\n"
364     "    of its target;\n"
365     ;
366
367 /* Wrapper for listxattr */
368 static PyObject *
369 pylistxattr(PyObject *self, PyObject *args)
370 {
371     char *file = NULL;
372     int filedes = -1;
373     char *buf;
374     int ishandle, dolink=0;
375     int nalloc, nret;
376     PyObject *myarg;
377     PyObject *mylist;
378     int nattrs;
379     char *s;
380
381     /* Parse the arguments */
382     if (!PyArg_ParseTuple(args, "O|i", &myarg, &dolink))
383         return NULL;
384     if(!convertObj(myarg, &ishandle, &filedes, &file))
385         return NULL;
386
387     /* Find out the needed size of the buffer */
388     nalloc = ishandle ?
389         flistxattr(filedes, NULL, 0) :
390         dolink ?
391         llistxattr(file, NULL, 0) :
392         listxattr(file, NULL, 0);
393
394     if(nalloc == -1) {
395         return PyErr_SetFromErrno(PyExc_IOError);
396     }
397
398     /* Try to allocate the memory, using Python's allocator */
399     if((buf = PyMem_Malloc(nalloc)) == NULL) {
400         PyErr_NoMemory();
401         return NULL;
402     }
403
404     /* Now retrieve the list of attributes */
405     nret = ishandle ?
406         flistxattr(filedes, buf, nalloc) :
407         dolink ?
408         llistxattr(file, buf, nalloc) :
409         listxattr(file, buf, nalloc);
410
411     if(nret == -1) {
412         return PyErr_SetFromErrno(PyExc_IOError);
413     }
414
415     /* Compute the number of attributes in the list */
416     for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
417         nattrs++;
418     }
419
420     /* Create the list which will hold the result */
421     mylist = PyList_New(nattrs);
422
423     /* Create and insert the attributes as strings in the list */
424     for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
425         PyList_SET_ITEM(mylist, nattrs, PyString_FromString(s));
426         nattrs++;
427     }
428
429     /* Free the buffer, now it is no longer needed */
430     PyMem_Free(buf);
431
432     /* Return the result */
433     return mylist;
434 }
435
436 static PyMethodDef xattr_methods[] = {
437     {"getxattr",  pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
438     {"get_all", (PyCFunction) get_all, METH_VARARGS | METH_KEYWORDS,
439      __get_all_doc__ },
440     {"setxattr",  pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
441     {"removexattr",  pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
442     {"listxattr",  pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
443     {NULL, NULL, 0, NULL}        /* Sentinel */
444 };
445
446 static char __xattr_doc__[] = \
447     "Access extended filesystem attributes\n"
448     "\n"
449     "This module gives access to the extended attributes present\n"
450     "in some operating systems/filesystems. You can list attributes,\n"
451     "get, set and remove them.\n"
452     "The last and optional parameter for all functions is a boolean \n"
453     "value which enables the 'l-' version of the functions - acting\n"
454     "on symbolic links and not their destination.\n"
455     "\n"
456     "Example: \n\n"
457     "  >>> import xattr\n"
458     "  >>> xattr.listxattr(\"file.txt\")\n"
459     "  ['user.mime_type']\n"
460     "  >>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n"
461     "  'text/plain'\n"
462     "  >>> xattr.setxattr(\"file.txt\", \"user.comment\", "
463     "\"Simple text file\")\n"
464     "  >>> xattr.listxattr(\"file.txt\")\n"
465     "  ['user.mime_type', 'user.comment']\n"
466     "  >>> xattr.removexattr (\"file.txt\", \"user.comment\")\n"
467     ""
468     ;
469
470 void
471 initxattr(void)
472 {
473     PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
474
475     PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
476     PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
477
478     /* namespace constants */
479     PyModule_AddStringConstant(m, "NS_SECURITY", "security");
480     PyModule_AddStringConstant(m, "NS_SYSTEM", "system");
481     PyModule_AddStringConstant(m, "NS_TRUSTED", "trusted");
482     PyModule_AddStringConstant(m, "NS_USER", "user");
483
484 }