-
Notifications
You must be signed in to change notification settings - Fork 93
/
xml_dump_updateinfo.c
243 lines (197 loc) · 7.94 KB
/
xml_dump_updateinfo.c
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
/* createrepo_c - Library of routines for manipulation with repodata
* Copyright (C) 2014 Tomas Mlcoch
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
#include <glib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
#include <libxml/xmlsave.h>
#include "error.h"
#include "updateinfo.h"
#include "xml_dump.h"
#include "xml_dump_internal.h"
#define ERR_DOMAIN CREATEREPO_C_ERROR
#define INDENT 2
void
cr_xml_dump_updatecollectionpackages(xmlNodePtr collection, GSList *packages)
{
for (GSList *elem = packages; elem; elem = g_slist_next(elem)) {
cr_UpdateCollectionPackage *pkg = elem->data;
xmlNodePtr package;
package = xmlNewChild(collection, NULL, BAD_CAST "package", NULL);
cr_xmlNewProp_c(package, BAD_CAST "name", BAD_CAST pkg->name);
cr_xmlNewProp_c(package, BAD_CAST "version", BAD_CAST pkg->version);
cr_xmlNewProp_c(package, BAD_CAST "release", BAD_CAST pkg->release);
cr_xmlNewProp_c(package, BAD_CAST "epoch", BAD_CAST pkg->epoch);
cr_xmlNewProp_c(package, BAD_CAST "arch", BAD_CAST pkg->arch);
cr_xmlNewProp_c(package, BAD_CAST "src", BAD_CAST pkg->src);
cr_xmlNewTextChild_c(package, NULL, BAD_CAST "filename",
BAD_CAST pkg->filename);
if (pkg->sum) {
xmlNodePtr sum;
sum = cr_xmlNewTextChild_c(package,
NULL,
BAD_CAST "sum",
BAD_CAST pkg->sum);
cr_xmlNewProp_c(sum,
BAD_CAST "type",
BAD_CAST cr_checksum_name_str(pkg->sum_type));
}
if (pkg->reboot_suggested)
xmlNewChild(package, NULL, BAD_CAST "reboot_suggested", NULL);
}
}
void
cr_xml_dump_updateinforecord_pkglist(xmlNodePtr update, GSList *collections)
{
xmlNodePtr pkglist;
pkglist = xmlNewChild(update, NULL, BAD_CAST "pkglist", NULL);
for (GSList *elem = collections; elem; elem = g_slist_next(elem)) {
cr_UpdateCollection *col = elem->data;
xmlNodePtr collection;
collection = xmlNewChild(pkglist, NULL, BAD_CAST "collection", NULL);
cr_xmlNewProp_c(collection, BAD_CAST "short", BAD_CAST col->shortname);
cr_xmlNewTextChild_c(collection,
NULL,
BAD_CAST "name",
BAD_CAST col->name);
cr_xml_dump_updatecollectionpackages(collection, col->packages);
}
}
void
cr_xml_dump_updateinforecord_references(xmlNodePtr update, GSList *refs)
{
xmlNodePtr references;
references = xmlNewChild(update, NULL, BAD_CAST "references", NULL);
for (GSList *elem = refs; elem; elem = g_slist_next(elem)) {
cr_UpdateReference *ref = elem->data;
xmlNodePtr reference;
reference = xmlNewChild(references, NULL, BAD_CAST "reference", NULL);
cr_xmlNewProp_c(reference, BAD_CAST "href", BAD_CAST ref->href);
cr_xmlNewProp_c(reference, BAD_CAST "id", BAD_CAST ref->id);
cr_xmlNewProp_c(reference, BAD_CAST "type", BAD_CAST ref->type);
cr_xmlNewProp_c(reference, BAD_CAST "title", BAD_CAST ref->title);
}
}
xmlNodePtr
cr_xml_dump_updateinforecord_internal(xmlNodePtr root, cr_UpdateRecord *rec)
{
xmlNodePtr update, node;
if (!rec)
return NULL;
// Update element
if (!root)
update = xmlNewNode(NULL, BAD_CAST "update");
else
update = xmlNewChild(root, NULL, BAD_CAST "update", NULL);
cr_xmlNewProp_c(update, BAD_CAST "from", BAD_CAST rec->from);
cr_xmlNewProp_c(update, BAD_CAST "status", BAD_CAST rec->status);
cr_xmlNewProp_c(update, BAD_CAST "type", BAD_CAST rec->type);
cr_xmlNewProp_c(update, BAD_CAST "version", BAD_CAST rec->version);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "id", BAD_CAST rec->id);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "title", BAD_CAST rec->title);
if (rec->issued_date) {
node = xmlNewChild(update, NULL, BAD_CAST "issued", NULL);
cr_xmlNewProp(node, BAD_CAST "date", BAD_CAST rec->issued_date);
}
if (rec->updated_date) {
node = xmlNewChild(update, NULL, BAD_CAST "updated", NULL);
cr_xmlNewProp(node, BAD_CAST "date", BAD_CAST rec->updated_date);
}
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "rights", BAD_CAST rec->rights);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "release", BAD_CAST rec->release);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "pushcount", BAD_CAST rec->pushcount);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "severity", BAD_CAST rec->severity);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "summary", BAD_CAST rec->summary);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "description", BAD_CAST rec->description);
cr_xmlNewTextChild_c(update, NULL, BAD_CAST "solution", BAD_CAST rec->solution);
// References
cr_xml_dump_updateinforecord_references(update, rec->references);
// Pkglist
cr_xml_dump_updateinforecord_pkglist(update, rec->collections);
return update;
}
void
cr_xml_dump_updateinfo_body(xmlNodePtr root, cr_UpdateInfo *ui)
{
GSList *element;
// Dump updates
for (element = ui->updates; element; element = g_slist_next(element)) {
cr_UpdateRecord *rec = element->data;
cr_xml_dump_updateinforecord_internal(root, rec);
}
}
char *
cr_xml_dump_updateinfo(cr_UpdateInfo *updateinfo, GError **err)
{
xmlDocPtr doc;
xmlNodePtr root;
char *result;
assert(!err || *err == NULL);
if (!updateinfo)
return NULL;
// Dump IT!
doc = xmlNewDoc(BAD_CAST XML_DOC_VERSION);
root = xmlNewNode(NULL, BAD_CAST "updates");
cr_xml_dump_updateinfo_body(root, updateinfo);
xmlDocSetRootElement(doc, root);
xmlDocDumpFormatMemoryEnc(doc,
(xmlChar **) &result,
NULL,
XML_ENCODING,
FORMAT_XML);
// Clean up
xmlFreeDoc(doc);
return result;
}
char *
cr_xml_dump_updaterecord(cr_UpdateRecord *rec, GError **err)
{
xmlNodePtr root;
char *result;
assert(!err || *err == NULL);
if (!rec) {
g_set_error(err, CREATEREPO_C_ERROR, CRE_BADARG,
"No updateinfo object to dump specified");
return NULL;
}
// Dump IT!
xmlBufferPtr buf = xmlBufferCreate();
if (buf == NULL) {
g_critical("%s: Error creating the xml buffer", __func__);
g_set_error(err, ERR_DOMAIN, CRE_MEMORY,
"Cannot create an xml buffer");
return NULL;
}
root = cr_xml_dump_updateinforecord_internal(NULL, rec);
// xmlNodeDump seems to be a little bit faster than xmlDocDumpFormatMemory
xmlNodeDump(buf, NULL, root, 1, FORMAT_XML);
assert(buf->content);
// First line in the buf is not indented, we must indent it by ourself
result = g_malloc(sizeof(char *) * buf->use + INDENT + 1);
for (int x = 0; x < INDENT; x++) result[x] = ' ';
memcpy((void *) result+INDENT, buf->content, buf->use);
result[buf->use + INDENT] = '\n';
result[buf->use + INDENT + 1] = '\0';
// Cleanup
xmlBufferFree(buf);
xmlFreeNode(root);
return result;
}