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
|
/* Copyright 2009 Red Hat, Inc.
*
* This program is licensed under the BSD license, read LICENSE.BSD
* for further information
*/
#include "Python.h"
#include "marshal.h"
#include "cfile.h"
#include "deltarpm.h"
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
PyObject *createDict(struct deltarpm d)
{
PyObject *dict;
PyObject *o;
dict = PyDict_New();
/* Old NEVR */
if (d.nevr) {
o = PyString_FromString(d.nevr);
PyDict_SetItemString(dict, "old_nevr", o);
Py_DECREF(o);
} else {
PyDict_SetItemString(dict, "old_nevr", Py_None);
}
/* New NEVR */
if (d.targetnevr) {
o = PyString_FromString(d.targetnevr);
PyDict_SetItemString(dict, "nevr", o);
Py_DECREF(o);
} else {
PyDict_SetItemString(dict, "nevr", Py_None);
}
/* Sequence */
if (d.seq) {
char *tmp = calloc(d.seql * 2 + 1, sizeof(char));
int i;
for (i = 0; i < d.seql; i++) {
char buf[3];
snprintf(buf, 3, "%02x", d.seq[i]);
strcat(tmp, buf);
}
o = PyString_FromString(tmp);
free(tmp);
PyDict_SetItemString(dict, "seq", o);
Py_DECREF(o);
} else {
PyDict_SetItemString(dict, "seq", Py_None);
}
return dict;
}
static PyObject *doRead(PyObject *s, PyObject *args)
{
char *filename;
struct deltarpm d;
PyObject *ret;
int pid;
int ipcpipe[2];
if (!PyArg_ParseTuple(args, "s", &filename)) {
PyErr_SetFromErrno(PyExc_SystemError);
return NULL;
}
/* The delta rpm code does not expect to be used in its way. Its error handling
* conststs of 'printf' and 'exit'. So, dirty hacks abound. */
if (pipe(ipcpipe) == -1) {
PyErr_SetFromErrno(PyExc_SystemError);
return NULL;
}
if ((pid = fork())) {
FILE *readend = fdopen(ipcpipe[0], "r");
int rc, status;
rc = waitpid(pid, &status, 0);
if (rc == -1 || (WIFEXITED(status) && WEXITSTATUS(status) != 0)) {
PyErr_SetFromErrno(PyExc_SystemError);
return NULL;
}
ret = PyMarshal_ReadObjectFromFile(readend);
fclose(readend);
} else {
FILE *writend = fdopen(ipcpipe[1], "w");
readdeltarpm(filename, &d, NULL);
PyMarshal_WriteObjectToFile(createDict(d), writend, Py_MARSHAL_VERSION);
fclose(writend);
_exit(0);
}
close(ipcpipe[1]);
return ret;
}
static PyMethodDef deltarpmMethods[] = {
{ "read", (PyCFunction) doRead, METH_VARARGS, NULL },
{ NULL }
};
void init_deltarpm(void)
{
PyObject *m;
m = Py_InitModule("_deltarpm", deltarpmMethods);
}
|