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
|
#include "rpmsystem-py.h"
#include <rpm/rpmstring.h>
#include "rpmfd-py.h"
struct rpmfdObject_s {
PyObject_HEAD
PyObject *md_dict;
FD_t fd;
};
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_Call((PyObject *)&rpmfd_Type,
Py_BuildValue("(O)", obj), NULL);
}
if (fdo == NULL) return 0;
if (Ferror(fdo->fd)) {
Py_DECREF(fdo);
PyErr_SetString(PyExc_IOError, Fstrerror(fdo->fd));
return 0;
}
*fdop = fdo;
return 1;
}
static PyObject *err_closed(void)
{
PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
return NULL;
}
static PyObject *rpmfd_new(PyTypeObject *subtype,
PyObject *args, PyObject *kwds)
{
char *kwlist[] = { "obj", "mode", "flags", NULL };
char *mode = "r";
char *flags = "ufdio";
PyObject *fo = NULL;
rpmfdObject *s = NULL;
FD_t fd = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|ss", kwlist,
&fo, &mode, &flags))
return NULL;
if (PyString_Check(fo)) {
char *m = rstrscat(NULL, mode, ".", flags, NULL);
Py_BEGIN_ALLOW_THREADS
fd = Fopen(PyString_AsString(fo), m);
Py_END_ALLOW_THREADS
free(m);
} else if (PyInt_Check(fo)) {
fd = fdDup(PyInt_AsLong(fo));
} else if (PyFile_Check(fo)) {
FILE *fp = PyFile_AsFile(fo);
fd = fdDup(fileno(fp));
} else {
PyErr_SetString(PyExc_TypeError, "path or file object expected");
return NULL;
}
if (Ferror(fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(fd));
return NULL;
}
if ((s = (rpmfdObject *)subtype->tp_alloc(subtype, 0)) == NULL) {
Fclose(fd);
return NULL;
}
/* TODO: remember our filename, mode & flags */
s->fd = fd;
return (PyObject*) s;
}
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);
s->ob_type->tp_free((PyObject *)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 (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
return NULL;
}
Py_RETURN_NONE;
}
static PyObject *rpmfd_read(rpmfdObject *s, PyObject *args, PyObject *kwds)
{
char *kwlist[] = { "size", NULL };
char *buf = NULL;
ssize_t reqsize = -1;
ssize_t bufsize = 0;
ssize_t read = 0;
PyObject *res = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|l", kwlist, &reqsize))
return NULL;
if (s->fd == NULL) return err_closed();
/* XXX simple, stupid for now ... and broken for anything large */
bufsize = (reqsize < 0) ? fdSize(s->fd) : reqsize;
if ((buf = malloc(bufsize+1)) == NULL) {
return PyErr_NoMemory();
}
Py_BEGIN_ALLOW_THREADS
read = Fread(buf, 1, bufsize, s->fd);
Py_END_ALLOW_THREADS
if (Ferror(s->fd)) {
PyErr_SetString(PyExc_IOError, Fstrerror(s->fd));
goto exit;
}
res = PyString_FromStringAndSize(buf, read);
exit:
free(buf);
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[] = {
{ "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 },
{ "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 PyGetSetDef rpmfd_getseters[] = {
{ "closed", (getter)rpmfd_get_closed, NULL, NULL },
{ NULL },
};
PyTypeObject rpmfd_Type = {
PyObject_HEAD_INIT(&PyType_Type)
0, /* ob_size */
"rpm.fd", /* tp_name */
sizeof(rpmfdObject), /* tp_size */
0, /* tp_itemsize */
/* methods */
(destructor) rpmfd_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc)0, /* tp_getattr */
(setattrfunc)0, /* tp_setattr */
(cmpfunc)0, /* tp_compare */
(reprfunc)0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)0, /* tp_hash */
(ternaryfunc)0, /* tp_call */
(reprfunc)0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
PyObject_GenericSetAttr, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
rpmfd_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
rpmfd_methods, /* tp_methods */
0, /* tp_members */
rpmfd_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)0, /* tp_init */
(allocfunc)0, /* tp_alloc */
(newfunc) rpmfd_new, /* tp_new */
(freefunc)0, /* tp_free */
0, /* tp_is_gc */
};
|