2 #include <attr/xattr.h>
4 /** Converts from a string, file or int argument to what we need. */
5 static int convertObj(PyObject *myobj, int *ishandle, int *filehandle, char **filename) {
6 if(PyString_Check(myobj)) {
8 *filename = PyString_AS_STRING(myobj);
9 } else if((*filehandle = PyObject_AsFileDescriptor(myobj)) != -1) {
12 PyErr_SetString(PyExc_TypeError, "argument 1 must be string or int");
18 /* Wrapper for getxattr */
19 static char __pygetxattr_doc__[] = \
20 "Get the value of a given extended attribute.\n" \
23 "\t- a string representing filename, or a file-like object,\n" \
24 "\t or a file descriptor; this represents the file on \n" \
26 "\t- a string, representing the attribute whose value to retrieve;\n" \
27 "\t usually in form of system.posix_acl or user.mime_type\n" \
28 "\t- (optional) a boolean value (defaults to false), which, if\n" \
29 "\t the file name given is a symbolic link, makes the\n" \
30 "\t function operate on the symbolic link itself instead\n" \
35 pygetxattr(PyObject *self, PyObject *args)
39 int filedes, ishandle, dolink=0;
45 /* Parse the arguments */
46 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
48 if(!convertObj(myarg, &ishandle, &filedes, &file))
51 /* Find out the needed size of the buffer */
53 fgetxattr(filedes, attrname, NULL, 0) :
55 lgetxattr(file, attrname, NULL, 0) :
56 getxattr(file, attrname, NULL, 0);
58 return PyErr_SetFromErrno(PyExc_IOError);
61 /* Try to allocate the memory, using Python's allocator */
62 if((buf = PyMem_Malloc(nalloc)) == NULL) {
67 /* Now retrieve the attribute value */
69 fgetxattr(filedes, attrname, buf, nalloc) :
70 getxattr(file, attrname, buf, nalloc);
72 return PyErr_SetFromErrno(PyExc_IOError);
75 /* Create the string which will hold the result */
76 res = PyString_FromStringAndSize(buf, nret);
78 /* Free the buffer, now it is no longer needed */
81 /* Return the result */
85 static char __pysetxattr_doc__[] = \
86 "Set the value of a given extended attribute.\n" \
87 "Be carefull in case you want to set attributes on symbolic\n" \
88 "links, you have to use all the 5 parameters; use 0 for the \n" \
89 "flags value if you want the default behavior (create or " \
93 "\t- a string representing filename, or a file-like object,\n" \
94 "\t or a file descriptor; this represents the file on \n" \
96 "\t- a string, representing the attribute whose value to set;\n" \
97 "\t usually in form of system.posix_acl or user.mime_type\n" \
98 "\t- a string, possibly with embedded NULLs; note that there\n" \
99 "\t are restrictions regarding the size of the value, for\n" \
100 "\t example, for ext2/ext3, maximum size is the block size\n" \
101 "\t- (optional) flags; if 0 or ommited the attribute will be \n" \
102 "\t created or replaced; if XATTR_CREATE, the attribute \n" \
103 "\t will be created, giving an error if it already exists;\n" \
104 "\t of XATTR_REPLACE, the attribute will be replaced,\n" \
105 "\t giving an error if it doesn't exists;\n" \
106 "\t- (optional) a boolean value (defaults to false), which, if\n" \
107 "\t the file name given is a symbolic link, makes the\n" \
108 "\t function operate on the symbolic link itself instead\n" \
109 "\t of its target;" \
112 /* Wrapper for setxattr */
114 pysetxattr(PyObject *self, PyObject *args)
118 int ishandle, filedes, dolink=0;
124 /* Parse the arguments */
125 if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname, &buf, &bufsize, &flags, &dolink))
127 if(!convertObj(myarg, &ishandle, &filedes, &file))
130 /* Set the attribute's value */
132 fsetxattr(filedes, attrname, buf, bufsize, flags) :
134 lsetxattr(file, attrname, buf, bufsize, flags) :
135 setxattr(file, attrname, buf, bufsize, flags);
138 return PyErr_SetFromErrno(PyExc_IOError);
141 /* Return the result */
146 static char __pyremovexattr_doc__[] = \
147 "Remove an attribute from a file\n" \
150 "\t- a string representing filename, or a file-like object,\n" \
151 "\t or a file descriptor; this represents the file on \n" \
152 "\t which to act\n" \
153 "\t- a string, representing the attribute to be removed;\n" \
154 "\t usually in form of system.posix_acl or user.mime_type\n" \
155 "\t- (optional) a boolean value (defaults to false), which, if\n" \
156 "\t the file name given is a symbolic link, makes the\n" \
157 "\t function operate on the symbolic link itself instead\n" \
158 "\t of its target;" \
161 /* Wrapper for removexattr */
163 pyremovexattr(PyObject *self, PyObject *args)
167 int ishandle, filedes, dolink=0;
171 /* Parse the arguments */
172 if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
175 if(!convertObj(myarg, &ishandle, &filedes, &file))
178 /* Remove the attribute */
180 fremovexattr(filedes, attrname) :
182 lremovexattr(file, attrname) :
183 removexattr(file, attrname);
186 return PyErr_SetFromErrno(PyExc_IOError);
188 /* Return the result */
193 static char __pylistxattr_doc__[] = \
194 "Return the tuple of attribute names from a file\n" \
197 "\t- a string representing filename, or a file-like object,\n" \
198 "\t or a file descriptor; this represents the file to \n" \
200 "\t- (optional) a boolean value (defaults to false), which, if\n" \
201 "\t the file name given is a symbolic link, makes the\n" \
202 "\t function operate on the symbolic link itself instead\n" \
203 "\t of its target;" \
206 /* Wrapper for listxattr */
208 pylistxattr(PyObject *self, PyObject *args)
213 int ishandle, dolink=0;
220 /* Parse the arguments */
221 if (!PyArg_ParseTuple(args, "O|i", &myarg, &dolink))
223 if(!convertObj(myarg, &ishandle, &filedes, &file))
226 /* Find out the needed size of the buffer */
228 flistxattr(filedes, NULL, 0) :
229 listxattr(file, NULL, 0);
232 return PyErr_SetFromErrno(PyExc_IOError);
235 /* Try to allocate the memory, using Python's allocator */
236 if((buf = PyMem_Malloc(nalloc)) == NULL) {
241 /* Now retrieve the list of attributes */
243 flistxattr(filedes, buf, nalloc) :
245 llistxattr(file, buf, nalloc) :
246 listxattr(file, buf, nalloc);
249 return PyErr_SetFromErrno(PyExc_IOError);
252 /* Compute the number of attributes in the list */
253 for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
257 /* Create the tuple which will hold the result */
258 mytuple = PyTuple_New(nattrs);
260 /* Create and insert the attributes as strings in the tuple */
261 for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
262 PyTuple_SET_ITEM(mytuple, nattrs, PyString_FromString(s));
266 /* Free the buffer, now it is no longer needed */
269 /* Return the result */
273 static PyMethodDef xattr_methods[] = {
274 {"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
275 {"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
276 {"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
277 {"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
278 {NULL, NULL, 0, NULL} /* Sentinel */
281 static char __xattr_doc__[] = \
282 "Access extended filesystem attributes\n" \
284 "This module gives access to the extended attributes present\n" \
285 "in some operating systems/filesystems. You can list attributes,\n"\
286 "get, set and remove them.\n"\
287 "The last and optional parameter for all functions is a boolean \n"\
288 "value which enables the 'l-' version of the functions - acting\n"\
289 "on symbolic links and not their destination.\n"\
292 ">>> import xattr\n" \
293 ">>> xattr.listxattr(\"file.txt\")\n" \
294 "('user.mime_type',)\n" \
295 ">>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n" \
297 ">>> xattr.setxattr(\"file.txt\", \"user.comment\", \"Simple text file\")\n"\
298 ">>> xattr.listxattr(\"file.txt\")\n" \
299 "('user.mime_type', 'user.comment')\n" \
300 ">>> xattr.removexattr (\"file.txt\", \"user.comment\")\n" \
307 PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
309 PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
310 PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);