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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
#include "rpmsystem-py.h"
#include <rpm/rpmstring.h>
#include "header-py.h" /* XXX for utf8FromPyObject() only */
#include "rpmfd-py.h"
struct rpmfdObject_s {
PyObject_HEAD
PyObject *md_dict;
FD_t fd;
char *mode;
char *flags;
};
FD_t rpmfdGetFd(rpmfdObject *fdo)
{
return fdo->fd;
}
int rpmfdFromPyObject(PyObject *obj, rpmfdObject **fdop)
{
rpmfdObject *fdo = NULL;
if (rpmfdObject_Check(obj)) {
Py_INCREF(obj);
fdo = (rpmfdObject *) obj;
} else {
fdo = (rpmfdObject *) PyObject_CallFunctionObjArgs((PyObject *)rpmfd_Type,
obj, NULL);
}
if (fdo == NULL) return 0;
if (Ferror(fdo->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(fdo->fd));
Py_DECREF(fdo);
return 0;
}
*fdop = fdo;
return 1;
}
static PyObject *err_closed(void)
{
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
return NULL;
}
static FD_t openPath(const char *path, const char *mode)
{
FD_t fd;
Py_BEGIN_ALLOW_THREADS
fd = Fopen(path, mode);
Py_END_ALLOW_THREADS;
return fd;
}
static FD_t openFd(FD_t ofd, const char *mode)
{
FD_t fd;
Py_BEGIN_ALLOW_THREADS
fd = Fdopen(ofd, mode);
Py_END_ALLOW_THREADS;
return fd;
}
static int rpmfd_init(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
char *kwlist[] = { "obj", "mode", "flags", NULL };
const char *mode = "r";
const char *flags = "ufdio";
char *rpmio_mode = NULL;
PyObject *fo = NULL;
FD_t fd = NULL;
int fdno;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist,
&fo, &mode, &flags))
return -1;
rpmio_mode = rstrscat(NULL, mode, ".", flags, NULL);
if (PyBytes_Check(fo)) {
fd = openPath(PyBytes_AsString(fo), rpmio_mode);
} else if (PyUnicode_Check(fo)) {
PyObject *enc = NULL;
int rc = PyUnicode_FSConverter(fo, &enc);
if (rc) {
fd = openPath(PyBytes_AsString(enc), rpmio_mode);
Py_DECREF(enc);
}
} else if (rpmfdObject_Check(fo)) {
rpmfdObject *fdo = (rpmfdObject *)fo;
fd = openFd(fdDup(Fileno(fdo->fd)), rpmio_mode);
} else if ((fdno = PyObject_AsFileDescriptor(fo)) >= 0) {
fd = openFd(fdDup(fdno), rpmio_mode);
} else {
PyErr_SetString(PyExc_TypeError, "path or file object expected");
}
if (fd != NULL) {
Fclose(s->fd); /* in case __init__ was called again */
free(s->mode);
free(s->flags);
s->fd = fd;
s->mode = rstrdup(mode);
s->flags = rstrdup(flags);
} else {
PyErr_SetString(PyExc_IOError, Fstrerror(fd));
}
free(rpmio_mode);
return (fd == NULL) ? -1 : 0;
}
static PyObject *rpmfd_open(PyObject *cls, PyObject *args, PyObject *kwds)
{
return PyObject_Call(cls, args, kwds);
}
static PyObject *do_close(rpmfdObject *s)
{
/* mimic python fileobject: close on closed file is not an error */
if (s->fd) {
Py_BEGIN_ALLOW_THREADS
Fclose(s->fd);
Py_END_ALLOW_THREADS
s->fd = NULL;
}
Py_RETURN_NONE;
}
static PyObject *rpmfd_close(rpmfdObject *s)
{
return do_close(s);
}
static void rpmfd_dealloc(rpmfdObject *s)
{
PyObject *res = do_close(s);
Py_XDECREF(res);
free(s->mode);
free(s->flags);
freefunc free = PyType_GetSlot(Py_TYPE(s), Py_tp_free);
free(s);
}
static PyObject *rpmfd_fileno(rpmfdObject *s)
{
int fno;
if (s->fd == NULL) return err_closed();
Py_BEGIN_ALLOW_THREADS
fno = Fileno(s->fd);
Py_END_ALLOW_THREADS
if (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
return Py_BuildValue("i", fno);
}
static PyObject *rpmfd_flush(rpmfdObject *s)
{
int rc;
if (s->fd == NULL) return err_closed();
Py_BEGIN_ALLOW_THREADS
rc = Fflush(s->fd);
Py_END_ALLOW_THREADS
if (rc || Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *rpmfd_isatty(rpmfdObject *s)
{
int fileno;
if (s->fd == NULL) return err_closed();
Py_BEGIN_ALLOW_THREADS
fileno = Fileno(s->fd);
Py_END_ALLOW_THREADS
if (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
return PyBool_FromLong(isatty(fileno));
}
static PyObject *rpmfd_seek(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
char *kwlist[] = { "offset", "whence", NULL };
off_t offset;
int whence = SEEK_SET;
int rc = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "L|i", kwlist,
&offset, &whence))
return NULL;
if (s->fd == NULL) return err_closed();
Py_BEGIN_ALLOW_THREADS
rc = Fseek(s->fd, offset, whence);
Py_END_ALLOW_THREADS
if (rc < 0 || Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *rpmfd_tell(rpmfdObject *s)
{
off_t offset;
Py_BEGIN_ALLOW_THREADS
offset = Ftell(s->fd);
Py_END_ALLOW_THREADS
return Py_BuildValue("L", offset);
}
static PyObject *rpmfd_read(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
char *kwlist[] = { "size", NULL };
char buf[BUFSIZ];
ssize_t chunksize = sizeof(buf);
ssize_t left = -1;
ssize_t nb = 0;
PyObject *res = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|l", kwlist, &left))
return NULL;
if (s->fd == NULL) return err_closed();
/* ConcatAndDel() doesn't work on NULL string, meh */
res = PyBytes_FromStringAndSize(NULL, 0);
do {
if (left >= 0 && left < chunksize)
chunksize = left;
Py_BEGIN_ALLOW_THREADS
nb = Fread(buf, 1, chunksize, s->fd);
Py_END_ALLOW_THREADS
if (nb > 0) {
PyObject *tmp = PyBytes_FromStringAndSize(buf, nb);
PyBytes_ConcatAndDel(&res, tmp);
left -= nb;
}
} while (nb > 0);
if (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
Py_XDECREF(res);
return NULL;
} else {
return res;
}
}
static PyObject *rpmfd_write(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
const char *buf = NULL;
ssize_t size = 0;
char *kwlist[] = { "buffer", NULL };
ssize_t rc = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s#", kwlist, &buf, &size)) {
return NULL;
}
if (s->fd == NULL) return err_closed();
Py_BEGIN_ALLOW_THREADS
rc = Fwrite(buf, 1, size, s->fd);
Py_END_ALLOW_THREADS
if (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
return Py_BuildValue("n", rc);
}
static char rpmfd_doc[] = "";
static struct PyMethodDef rpmfd_methods[] = {
{ "open", (PyCFunction) rpmfd_open, METH_VARARGS|METH_KEYWORDS|METH_CLASS,
NULL },
{ "close", (PyCFunction) rpmfd_close, METH_NOARGS,
NULL },
{ "fileno", (PyCFunction) rpmfd_fileno, METH_NOARGS,
NULL },
{ "flush", (PyCFunction) rpmfd_flush, METH_NOARGS,
NULL },
{ "isatty", (PyCFunction) rpmfd_isatty, METH_NOARGS,
NULL },
{ "read", (PyCFunction) rpmfd_read, METH_VARARGS|METH_KEYWORDS,
NULL },
{ "seek", (PyCFunction) rpmfd_seek, METH_VARARGS|METH_KEYWORDS,
NULL },
{ "tell", (PyCFunction) rpmfd_tell, METH_NOARGS,
NULL },
{ "write", (PyCFunction) rpmfd_write, METH_VARARGS|METH_KEYWORDS,
NULL },
{ NULL, NULL }
};
static PyObject *rpmfd_get_closed(rpmfdObject *s)
{
return PyBool_FromLong((s->fd == NULL));
}
static PyObject *rpmfd_get_name(rpmfdObject *s)
{
/* XXX: rpm returns non-paths with [mumble], python files use <mumble> */
return utf8FromString(Fdescr(s->fd));
}
static PyObject *rpmfd_get_mode(rpmfdObject *s)
{
return utf8FromString(s->mode);
}
static PyObject *rpmfd_get_flags(rpmfdObject *s)
{
return utf8FromString(s->flags);
}
static PyGetSetDef rpmfd_getseters[] = {
{ "closed", (getter)rpmfd_get_closed, NULL, NULL },
{ "name", (getter)rpmfd_get_name, NULL, NULL },
{ "mode", (getter)rpmfd_get_mode, NULL, NULL },
{ "flags", (getter)rpmfd_get_flags, NULL, NULL },
{ NULL },
};
static PyType_Slot rpmfd_Type_Slots[] = {
{Py_tp_dealloc, rpmfd_dealloc},
{Py_tp_getattro, PyObject_GenericGetAttr},
{Py_tp_setattro, PyObject_GenericSetAttr},
{Py_tp_doc, rpmfd_doc},
{Py_tp_methods, rpmfd_methods},
{Py_tp_getset, rpmfd_getseters},
{Py_tp_init, rpmfd_init},
{Py_tp_new, PyType_GenericNew},
{0, NULL},
};
PyTypeObject* rpmfd_Type;
PyType_Spec rpmfd_Type_Spec = {
.name = "rpm.fd",
.basicsize = sizeof(rpmfdObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_IMMUTABLETYPE,
.slots = rpmfd_Type_Slots,
};
|