1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include <Python.h>
#include <attr/xattr.h>
/** Converts from a string, file or int argument to what we need. */
static int convertObj(PyObject *myobj, int *ishandle, int *filehandle, char **filename) {
if(PyString_Check(myobj)) {
*ishandle = 0;
*filename = PyString_AS_STRING(myobj);
} else if((*filehandle = PyObject_AsFileDescriptor(myobj)) != -1) {
*ishandle = 1;
} else {
PyErr_SetString(PyExc_TypeError, "argument 1 must be string or int");
return 0;
}
return 1;
}
/* Wrapper for getxattr */
static char __pygetxattr_doc__[] = \
"Get the value of a given extended attribute.\n" \
"\n" \
"Parameters:\n" \
"\t- a string representing filename, or a file-like object,\n" \
"\t or a file descriptor; this represents the file on \n" \
"\t which to act\n" \
"\t- a string, representing the attribute whose value to retrieve;\n" \
"\t usually in form of system.posix_acl or user.mime_type\n" \
"\t- (optional) a boolean value (defaults to false), which, if\n" \
"\t the file name given is a symbolic link, makes the\n" \
"\t function operate on the symbolic link itself instead\n" \
"\t of its target;" \
;
static PyObject *
pygetxattr(PyObject *self, PyObject *args)
{
PyObject *myarg;
char *file = NULL;
int filedes = -1, ishandle, dolink=0;
char *attrname;
char *buf;
int nalloc, nret;
PyObject *res;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
return NULL;
if(!convertObj(myarg, &ishandle, &filedes, &file))
return NULL;
/* Find out the needed size of the buffer */
nalloc = ishandle ?
fgetxattr(filedes, attrname, NULL, 0) :
dolink ?
lgetxattr(file, attrname, NULL, 0) :
getxattr(file, attrname, NULL, 0);
if(nalloc == -1) {
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
PyErr_NoMemory();
return NULL;
}
/* Now retrieve the attribute value */
nret = ishandle ?
fgetxattr(filedes, attrname, buf, nalloc) :
dolink ?
lgetxattr(file, attrname, buf, nalloc) :
getxattr(file, attrname, buf, nalloc);
if(nret == -1) {
PyMem_Free(buf);
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Create the string which will hold the result */
res = PyString_FromStringAndSize(buf, nret);
/* Free the buffer, now it is no longer needed */
PyMem_Free(buf);
/* Return the result */
return res;
}
static char __pysetxattr_doc__[] = \
"Set the value of a given extended attribute.\n" \
"Be carefull in case you want to set attributes on symbolic\n" \
"links, you have to use all the 5 parameters; use 0 for the \n" \
"flags value if you want the default behavior (create or " \
"replace)\n" \
"\n" \
"Parameters:\n" \
"\t- a string representing filename, or a file-like object,\n" \
"\t or a file descriptor; this represents the file on \n" \
"\t which to act\n" \
"\t- a string, representing the attribute whose value to set;\n" \
"\t usually in form of system.posix_acl or user.mime_type\n" \
"\t- a string, possibly with embedded NULLs; note that there\n" \
"\t are restrictions regarding the size of the value, for\n" \
"\t example, for ext2/ext3, maximum size is the block size\n" \
"\t- (optional) flags; if 0 or ommited the attribute will be \n" \
"\t created or replaced; if XATTR_CREATE, the attribute \n" \
"\t will be created, giving an error if it already exists;\n" \
"\t of XATTR_REPLACE, the attribute will be replaced,\n" \
"\t giving an error if it doesn't exists;\n" \
"\t- (optional) a boolean value (defaults to false), which, if\n" \
"\t the file name given is a symbolic link, makes the\n" \
"\t function operate on the symbolic link itself instead\n" \
"\t of its target;" \
;
/* Wrapper for setxattr */
static PyObject *
pysetxattr(PyObject *self, PyObject *args)
{
PyObject *myarg;
char *file;
int ishandle, filedes, dolink=0;
char *attrname;
char *buf;
int bufsize, nret;
int flags = 0;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Oss#|bi", &myarg, &attrname, &buf, &bufsize, &flags, &dolink))
return NULL;
if(!convertObj(myarg, &ishandle, &filedes, &file))
return NULL;
/* Set the attribute's value */
nret = ishandle ?
fsetxattr(filedes, attrname, buf, bufsize, flags) :
dolink ?
lsetxattr(file, attrname, buf, bufsize, flags) :
setxattr(file, attrname, buf, bufsize, flags);
if(nret == -1) {
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
static char __pyremovexattr_doc__[] = \
"Remove an attribute from a file\n" \
"\n" \
"Parameters:\n" \
"\t- a string representing filename, or a file-like object,\n" \
"\t or a file descriptor; this represents the file on \n" \
"\t which to act\n" \
"\t- a string, representing the attribute to be removed;\n" \
"\t usually in form of system.posix_acl or user.mime_type\n" \
"\t- (optional) a boolean value (defaults to false), which, if\n" \
"\t the file name given is a symbolic link, makes the\n" \
"\t function operate on the symbolic link itself instead\n" \
"\t of its target;" \
;
/* Wrapper for removexattr */
static PyObject *
pyremovexattr(PyObject *self, PyObject *args)
{
PyObject *myarg;
char *file;
int ishandle, filedes, dolink=0;
char *attrname;
int nret;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "Os|i", &myarg, &attrname, &dolink))
return NULL;
if(!convertObj(myarg, &ishandle, &filedes, &file))
return NULL;
/* Remove the attribute */
nret = ishandle ?
fremovexattr(filedes, attrname) :
dolink ?
lremovexattr(file, attrname) :
removexattr(file, attrname);
if(nret == -1)
return PyErr_SetFromErrno(PyExc_IOError);
/* Return the result */
Py_INCREF(Py_None);
return Py_None;
}
static char __pylistxattr_doc__[] = \
"Return the tuple of attribute names from a file\n" \
"\n" \
"Parameters:\n" \
"\t- a string representing filename, or a file-like object,\n" \
"\t or a file descriptor; this represents the file to \n" \
"\t be queried\n" \
"\t- (optional) a boolean value (defaults to false), which, if\n" \
"\t the file name given is a symbolic link, makes the\n" \
"\t function operate on the symbolic link itself instead\n" \
"\t of its target;" \
;
/* Wrapper for listxattr */
static PyObject *
pylistxattr(PyObject *self, PyObject *args)
{
char *file = NULL;
int filedes = -1;
char *buf;
int ishandle, dolink=0;
int nalloc, nret;
PyObject *myarg;
PyObject *mytuple;
int nattrs;
char *s;
/* Parse the arguments */
if (!PyArg_ParseTuple(args, "O|i", &myarg, &dolink))
return NULL;
if(!convertObj(myarg, &ishandle, &filedes, &file))
return NULL;
/* Find out the needed size of the buffer */
nalloc = ishandle ?
flistxattr(filedes, NULL, 0) :
listxattr(file, NULL, 0);
if(nalloc == -1) {
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Try to allocate the memory, using Python's allocator */
if((buf = PyMem_Malloc(nalloc)) == NULL) {
PyErr_NoMemory();
return NULL;
}
/* Now retrieve the list of attributes */
nret = ishandle ?
flistxattr(filedes, buf, nalloc) :
dolink ?
llistxattr(file, buf, nalloc) :
listxattr(file, buf, nalloc);
if(nret == -1) {
return PyErr_SetFromErrno(PyExc_IOError);
}
/* Compute the number of attributes in the list */
for(s = buf, nattrs = 0; (s - buf) < nret; s += strlen(s) + 1) {
nattrs++;
}
/* Create the tuple which will hold the result */
mytuple = PyTuple_New(nattrs);
/* Create and insert the attributes as strings in the tuple */
for(s = buf, nattrs = 0; s - buf < nret; s += strlen(s) + 1) {
PyTuple_SET_ITEM(mytuple, nattrs, PyString_FromString(s));
nattrs++;
}
/* Free the buffer, now it is no longer needed */
PyMem_Free(buf);
/* Return the result */
return mytuple;
}
static PyMethodDef xattr_methods[] = {
{"getxattr", pygetxattr, METH_VARARGS, __pygetxattr_doc__ },
{"setxattr", pysetxattr, METH_VARARGS, __pysetxattr_doc__ },
{"removexattr", pyremovexattr, METH_VARARGS, __pyremovexattr_doc__ },
{"listxattr", pylistxattr, METH_VARARGS, __pylistxattr_doc__ },
{NULL, NULL, 0, NULL} /* Sentinel */
};
static char __xattr_doc__[] = \
"Access extended filesystem attributes\n" \
"\n" \
"This module gives access to the extended attributes present\n" \
"in some operating systems/filesystems. You can list attributes,\n"\
"get, set and remove them.\n"\
"The last and optional parameter for all functions is a boolean \n"\
"value which enables the 'l-' version of the functions - acting\n"\
"on symbolic links and not their destination.\n"\
"\n" \
"Example: \n" \
">>> import xattr\n" \
">>> xattr.listxattr(\"file.txt\")\n" \
"('user.mime_type',)\n" \
">>> xattr.getxattr(\"file.txt\", \"user.mime_type\")\n" \
"'text/plain'\n" \
">>> xattr.setxattr(\"file.txt\", \"user.comment\", \"Simple text file\")\n"\
">>> xattr.listxattr(\"file.txt\")\n" \
"('user.mime_type', 'user.comment')\n" \
">>> xattr.removexattr (\"file.txt\", \"user.comment\")\n" \
"" \
;
void
initxattr(void)
{
PyObject *m = Py_InitModule3("xattr", xattr_methods, __xattr_doc__);
PyModule_AddIntConstant(m, "XATTR_CREATE", XATTR_CREATE);
PyModule_AddIntConstant(m, "XATTR_REPLACE", XATTR_REPLACE);
}
|