summaryrefslogtreecommitdiff
path: root/python/rpmfd-py.c
blob: 0df04f509d05053561bdbef267b065adbeb04e72 (plain)
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
/** \ingroup python
 * \file python/rpmfd-py.c
 */

#include "system.h"

#include "Python.h"
#ifdef __LCLINT__
#undef  PyObject_HEAD
#define PyObject_HEAD   int _PyObjectHead;
#endif

#include <glob.h>	/* XXX rpmio.h */
#include <dirent.h>	/* XXX rpmio.h */
#include <rpmio_internal.h>

#include <rpmlib.h>	/* XXX _free */

#include "header-py.h"	/* XXX pyrpmError */
#include "rpmfd-py.h"

#include "debug.h"

/*@access FD_t @*/

/*@unchecked@*/
static int _rpmfd_debug = 1;

/** \ingroup python
 * \name Class: Rpmfd
 * \class Rpmfd
 * \brief An python rpm.fd object represents an rpm I/O handle.
 */

static PyObject *
rpmfd_Debug(/*@unused@*/ rpmfdObject * s, PyObject * args)
	/*@globals _Py_NoneStruct @*/
	/*@modifies _Py_NoneStruct @*/
{
    if (!PyArg_ParseTuple(args, "i", &_rpmfd_debug)) return NULL;
    Py_INCREF(Py_None);
    return Py_None;
}

/**
 */
typedef struct FDlist_t FDlist;

/**
 */
struct FDlist_t {
    FILE * f;
    FD_t fd;
    const char * note;
    FDlist * next;
} ;

/**
 */
static FDlist *fdhead = NULL;

/**
 */
static FDlist *fdtail = NULL;

/**
 */
static int closeCallback(FILE * f)
	/*@globals fdhead @*/
	/*@modifies fdhead @*/
{
    FDlist *node, *last;

    node = fdhead;
    last = NULL;
    while (node) {
	if (node->f == f)
	    break;
	last = node;
	node = node->next;
    }
    if (node) {
	if (last)
	    last->next = node->next;
	else
	    fdhead = node->next;
	node->note = _free (node->note);
	node->fd = fdLink(node->fd, "closeCallback");
	(void) Fclose (node->fd);
	while (node->fd)
	    node->fd = fdFree(node->fd, "closeCallback");
	node = _free (node);
    }
    return 0; 
}

/**
 */
static PyObject *
rpmfd_Fopen(/*@unused@*/ PyObject * s, PyObject * args)
	/*@globals fdhead, fdtail @*/
	/*@modifies fdhead, fdtail @*/
{
    char * path;
    char * mode = "r.ufdio";
    FDlist *node;
    
    if (!PyArg_ParseTuple(args, "s|s", &path, &mode))
	return NULL;
    
    node = xmalloc (sizeof(FDlist));
    
    node->fd = Fopen(path, mode);
    node->fd = fdLink(node->fd, "doFopen");
    node->note = xstrdup (path);

    if (!node->fd) {
	PyErr_SetFromErrno(pyrpmError);
	node = _free (node);
	return NULL;
    }
    
    if (Ferror(node->fd)) {
	const char *err = Fstrerror(node->fd);
	node = _free(node);
	if (err)
	    PyErr_SetString(pyrpmError, err);
	return NULL;
    }

    node->f = fdGetFp(node->fd);
    if (!node->f) {
	PyErr_SetString(pyrpmError, "FD_t has no FILE*");
	free(node);
	return NULL;
    }

    node->next = NULL;
    if (!fdhead) {
	fdhead = fdtail = node;
    } else if (fdtail) {
	fdtail->next = node;
    } else {
	fdhead = node;
    }
    fdtail = node;
    
    return PyFile_FromFile (node->f, path, mode, closeCallback);
}

/** \ingroup python
 */
/*@-fullinitblock@*/
/*@unchecked@*/ /*@observer@*/
static struct PyMethodDef rpmfd_methods[] = {
    {"Debug",	(PyCFunction)rpmfd_Debug,	METH_VARARGS,
	NULL},
    {"Fopen",	(PyCFunction)rpmfd_Fopen,	METH_VARARGS,
	NULL},
    {NULL,		NULL}		/* sentinel */
};
/*@=fullinitblock@*/

/* ---------- */

/** \ingroup python
 */
static void
rpmfd_dealloc(/*@only@*/ /*@null@*/ rpmfdObject * s)
	/*@modifies s @*/
{
    if (s) {
	Fclose(s->fd);
	s->fd = NULL;
	PyObject_Del(s);
    }
}

/** \ingroup python
 */
static PyObject * rpmfd_getattr(rpmfdObject * o, char * name)
	/*@*/
{
    return Py_FindMethod(rpmfd_methods, (PyObject *) o, name);
}

/** \ingroup python
 */
static int rpmfd_init(rpmfdObject * s, PyObject *args, PyObject *kwds)
	/*@*/
{
    char * path;
    char * mode = "r.ufdio";

if (_rpmfd_debug)
fprintf(stderr, "*** rpmfd_init(%p,%p,%p)\n", s, args, kwds);

    if (!PyArg_ParseTuple(args, "s|s:rpmfd_init", &path, &mode))
	return -1;

    s->fd = Fopen(path, mode);

    if (s->fd == NULL) {
	PyErr_SetFromErrno(pyrpmError);
	return -1;
    }

    if (Ferror(s->fd)) {
	const char *err = Fstrerror(s->fd);
	if (s->fd)
	    Fclose(s->fd);
	if (err)
	    PyErr_SetString(pyrpmError, err);
	return -1;
    }
    return 0;
}

/** \ingroup python
 */
static void rpmfd_free(/*@only@*/ rpmfdObject * s)
	/*@modifies s @*/
{
if (_rpmfd_debug)
fprintf(stderr, "%p -- fd %p\n", s, s->fd);
    if (s->fd)
	Fclose(s->fd);

    PyObject_Del((PyObject *)s);
}

/** \ingroup python
 */
static PyObject * rpmfd_alloc(PyTypeObject * subtype, int nitems)
	/*@*/
{
    PyObject * s = PyType_GenericAlloc(subtype, nitems);

if (_rpmfd_debug)
fprintf(stderr, "*** rpmfd_alloc(%p,%d) ret %p\n", subtype, nitems, s);
    return s;
}

/** \ingroup python
 */
static rpmfdObject * rpmfd_new(PyTypeObject * subtype, PyObject *args, PyObject *kwds)
	/*@*/
{
    rpmfdObject * s = PyObject_New(rpmfdObject, subtype);

    /* Perform additional initialization. */
    if (rpmfd_init(s, args, kwds) < 0) {
	rpmfd_free(s);
	return NULL;
    }

if (_rpmfd_debug)
fprintf(stderr, "%p ++ fd %p\n", s, s->fd);

    return s;
}

/**
 */
/*@unchecked@*/ /*@observer@*/
static char rpmfd_doc[] =
"";

/** \ingroup python
 */
/*@-fullinitblock@*/
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) rpmfd_getattr, 	/* 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 */
	0,				/* tp_getattro */
	0,				/* tp_setattro */
	0,				/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,		/* tp_flags */
	rpmfd_doc,			/* tp_doc */
#if Py_TPFLAGS_HAVE_ITER
	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 */
	0,				/* tp_getset */
	0,				/* tp_base */
	0,				/* tp_dict */
	0,				/* tp_descr_get */
	0,				/* tp_descr_set */
	0,				/* tp_dictoffset */
	(initproc) rpmfd_init,		/* tp_init */
	(allocfunc) rpmfd_alloc,	/* tp_alloc */
	(newfunc) rpmfd_new,		/* tp_new */
	(destructor) rpmfd_free,	/* tp_free */
	0,				/* tp_is_gc */
#endif
};
/*@=fullinitblock@*/

rpmfdObject * rpmfd_Wrap(FD_t fd)
{
    rpmfdObject *s = PyObject_New(rpmfdObject, &rpmfd_Type);
    if (s == NULL)
	return NULL;
    s->fd = fd;
    return s;
}