2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 /* Import a yum filelist as a razor package set. */
39 YUM_STATE_PACKAGE_NAME,
40 YUM_STATE_PACKAGE_ARCH,
42 YUM_STATE_DESCRIPTION,
54 XML_Parser primary_parser;
55 XML_Parser filelists_parser;
56 XML_Parser current_parser;
58 struct razor_importer *importer;
59 struct import_property_context *current_property_context;
60 char name[256], arch[64], summary[512], description[4096];
61 char url[256], license[64], buffer[512], *p;
63 uint32_t property_type;
68 yum_to_razor_relation (const char *flags)
70 if (flags[0] == 'L') {
72 return RAZOR_PROPERTY_LESS;
74 return RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL;
75 } else if (flags[0] == 'G') {
77 return RAZOR_PROPERTY_GREATER;
79 return RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL;
81 return RAZOR_PROPERTY_EQUAL;
85 yum_primary_start_element(void *data, const char *name, const char **atts)
87 struct yum_context *ctx = data;
88 const char *n, *epoch, *version, *release;
90 uint32_t pre, relation, flags;
93 if (strcmp(name, "name") == 0) {
94 ctx->state = YUM_STATE_PACKAGE_NAME;
96 } else if (strcmp(name, "arch") == 0) {
97 ctx->state = YUM_STATE_PACKAGE_ARCH;
99 } else if (strcmp(name, "version") == 0) {
103 for (i = 0; atts[i]; i += 2) {
104 if (strcmp(atts[i], "epoch") == 0)
106 else if (strcmp(atts[i], "ver") == 0)
107 version = atts[i + 1];
108 else if (strcmp(atts[i], "rel") == 0)
109 release = atts[i + 1];
111 if (version == NULL || release == NULL) {
112 fprintf(stderr, "invalid version tag, "
113 "missing version or release attribute\n");
117 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
118 razor_importer_begin_package(ctx->importer,
119 ctx->name, buffer, ctx->arch);
120 } else if (strcmp(name, "summary") == 0) {
121 ctx->p = ctx->summary;
122 ctx->state = YUM_STATE_SUMMARY;
123 } else if (strcmp(name, "description") == 0) {
124 ctx->p = ctx->description;
125 ctx->state = YUM_STATE_DESCRIPTION;
126 } else if (strcmp(name, "url") == 0) {
128 ctx->state = YUM_STATE_URL;
129 } else if (strcmp(name, "checksum") == 0) {
131 ctx->state = YUM_STATE_CHECKSUM;
132 } else if (strcmp(name, "rpm:license") == 0) {
133 ctx->p = ctx->license;
134 ctx->state = YUM_STATE_LICENSE;
135 } else if (strcmp(name, "rpm:requires") == 0) {
136 ctx->state = YUM_STATE_REQUIRES;
137 ctx->property_type = RAZOR_PROPERTY_REQUIRES;
138 } else if (strcmp(name, "rpm:provides") == 0) {
139 ctx->state = YUM_STATE_PROVIDES;
140 ctx->property_type = RAZOR_PROPERTY_PROVIDES;
141 } else if (strcmp(name, "rpm:obsoletes") == 0) {
142 ctx->state = YUM_STATE_OBSOLETES;
143 ctx->property_type = RAZOR_PROPERTY_OBSOLETES;
144 } else if (strcmp(name, "rpm:conflicts") == 0) {
145 ctx->state = YUM_STATE_CONFLICTS;
146 ctx->property_type = RAZOR_PROPERTY_CONFLICTS;
147 } else if (strcmp(name, "rpm:entry") == 0 &&
148 ctx->state != YUM_STATE_BEGIN) {
153 relation = RAZOR_PROPERTY_EQUAL;
155 for (i = 0; atts[i]; i += 2) {
156 if (strcmp(atts[i], "name") == 0)
158 else if (strcmp(atts[i], "epoch") == 0)
160 else if (strcmp(atts[i], "ver") == 0)
161 version = atts[i + 1];
162 else if (strcmp(atts[i], "rel") == 0)
163 release = atts[i + 1];
164 else if (strcmp(atts[i], "flags") == 0)
165 relation = yum_to_razor_relation(atts[i + 1]);
166 else if (strcmp(atts[i], "pre") == 0)
169 RAZOR_PROPERTY_POST |
170 RAZOR_PROPERTY_PREUN |
171 RAZOR_PROPERTY_POSTUN;
175 fprintf(stderr, "invalid rpm:entry, "
176 "missing name or version attributes\n");
180 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
181 flags = ctx->property_type | relation | pre;
182 razor_importer_add_property(ctx->importer, n, flags, buffer);
187 yum_primary_end_element (void *data, const char *name)
189 struct yum_context *ctx = data;
191 switch (ctx->state) {
192 case YUM_STATE_PACKAGE_NAME:
193 case YUM_STATE_PACKAGE_ARCH:
194 case YUM_STATE_SUMMARY:
195 case YUM_STATE_DESCRIPTION:
197 case YUM_STATE_LICENSE:
198 case YUM_STATE_CHECKSUM:
200 ctx->state = YUM_STATE_BEGIN;
204 if (strcmp(name, "package") == 0) {
205 razor_importer_add_details(ctx->importer, ctx->summary,
206 ctx->description, ctx->url,
209 XML_StopParser(ctx->current_parser, XML_TRUE);
210 ctx->current_parser = ctx->filelists_parser;
215 yum_character_data (void *data, const XML_Char *s, int len)
217 struct yum_context *ctx = data;
219 switch (ctx->state) {
220 case YUM_STATE_PACKAGE_NAME:
221 case YUM_STATE_PACKAGE_ARCH:
222 case YUM_STATE_SUMMARY:
223 case YUM_STATE_DESCRIPTION:
225 case YUM_STATE_LICENSE:
226 case YUM_STATE_CHECKSUM:
228 memcpy(ctx->p, s, len);
236 yum_filelists_start_element(void *data, const char *name, const char **atts)
238 struct yum_context *ctx = data;
239 const char *pkg, *pkgid;
242 if (strcmp(name, "package") == 0) {
245 for (i = 0; atts[i]; i += 2) {
246 if (strcmp(atts[i], "name") == 0)
248 else if (strcmp(atts[i], "pkgid") == 0)
251 if (strcmp(pkgid, ctx->pkgid) != 0)
252 fprintf(stderr, "primary.xml and filelists.xml "
253 "mismatch for %s: %s vs %s",
254 pkg, pkgid, ctx->pkgid);
255 } else if (strcmp(name, "file") == 0) {
256 ctx->state = YUM_STATE_FILE;
257 ctx->p = ctx->buffer;
263 yum_filelists_end_element (void *data, const char *name)
265 struct yum_context *ctx = data;
267 ctx->state = YUM_STATE_BEGIN;
268 if (strcmp(name, "package") == 0) {
269 XML_StopParser(ctx->current_parser, XML_TRUE);
270 ctx->current_parser = ctx->primary_parser;
271 razor_importer_finish_package(ctx->importer);
272 } else if (strcmp(name, "file") == 0)
273 razor_importer_add_file(ctx->importer, ctx->buffer);
277 #define XML_BUFFER_SIZE 4096
280 razor_set_create_from_yum(void)
282 struct yum_context ctx;
285 gzFile primary, filelists;
286 XML_ParsingStatus status;
288 ctx.importer = razor_importer_new();
289 ctx.state = YUM_STATE_BEGIN;
291 ctx.primary_parser = XML_ParserCreate(NULL);
292 XML_SetUserData(ctx.primary_parser, &ctx);
293 XML_SetElementHandler(ctx.primary_parser,
294 yum_primary_start_element,
295 yum_primary_end_element);
296 XML_SetCharacterDataHandler(ctx.primary_parser,
299 ctx.filelists_parser = XML_ParserCreate(NULL);
300 XML_SetUserData(ctx.filelists_parser, &ctx);
301 XML_SetElementHandler(ctx.filelists_parser,
302 yum_filelists_start_element,
303 yum_filelists_end_element);
304 XML_SetCharacterDataHandler(ctx.filelists_parser,
307 primary = gzopen("primary.xml.gz", "rb");
310 filelists = gzopen("filelists.xml.gz", "rb");
311 if (filelists == NULL)
314 ctx.current_parser = ctx.primary_parser;
317 XML_GetParsingStatus(ctx.current_parser, &status);
318 switch (status.parsing) {
320 ret = XML_ResumeParser(ctx.current_parser);
323 case XML_INITIALIZED:
324 buf = XML_GetBuffer(ctx.current_parser,
326 if (ctx.current_parser == ctx.primary_parser)
327 len = gzread(primary, buf, XML_BUFFER_SIZE);
329 len = gzread(filelists, buf, XML_BUFFER_SIZE);
332 "couldn't read input: %s\n",
337 XML_ParseBuffer(ctx.current_parser, len, len == 0);
342 } while (status.parsing != XML_FINISHED);
345 XML_ParserFree(ctx.primary_parser);
346 XML_ParserFree(ctx.filelists_parser);
351 return razor_importer_finish(ctx.importer);