Don't try and stat the installation root if it's the empty string.
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.
34 /* Import a yum filelist as a razor package set. */
38 YUM_STATE_PACKAGE_NAME,
39 YUM_STATE_PACKAGE_ARCH,
41 YUM_STATE_DESCRIPTION,
53 XML_Parser primary_parser;
54 XML_Parser filelists_parser;
55 XML_Parser current_parser;
57 struct razor_importer *importer;
58 struct import_property_context *current_property_context;
59 char name[256], arch[64], summary[512], description[4096];
60 char url[256], license[64], buffer[512], *p;
62 uint32_t property_type;
69 yum_to_razor_relation (const char *flags)
71 if (flags[0] == 'L') {
73 return RAZOR_PROPERTY_LESS;
75 return RAZOR_PROPERTY_LESS | RAZOR_PROPERTY_EQUAL;
76 } else if (flags[0] == 'G') {
78 return RAZOR_PROPERTY_GREATER;
80 return RAZOR_PROPERTY_GREATER | RAZOR_PROPERTY_EQUAL;
82 return RAZOR_PROPERTY_EQUAL;
86 yum_primary_start_element(void *data, const char *name, const char **atts)
88 struct yum_context *ctx = data;
89 const char *n, *epoch, *version, *release;
91 uint32_t pre, relation, flags;
94 if (strcmp(name, "metadata") == 0) {
95 for (i = 0; atts[i]; i += 2) {
96 if (strcmp(atts[i], "packages") == 0)
97 ctx->total = atoi(atts[i + 1]);
99 } else if (strcmp(name, "name") == 0) {
100 ctx->state = YUM_STATE_PACKAGE_NAME;
102 } else if (strcmp(name, "arch") == 0) {
103 ctx->state = YUM_STATE_PACKAGE_ARCH;
105 } else if (strcmp(name, "version") == 0) {
109 for (i = 0; atts[i]; i += 2) {
110 if (strcmp(atts[i], "epoch") == 0)
112 else if (strcmp(atts[i], "ver") == 0)
113 version = atts[i + 1];
114 else if (strcmp(atts[i], "rel") == 0)
115 release = atts[i + 1];
117 if (version == NULL || release == NULL) {
118 fprintf(stderr, "invalid version tag, "
119 "missing version or release attribute\n");
123 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
124 razor_importer_begin_package(ctx->importer,
125 ctx->name, buffer, ctx->arch);
126 } else if (strcmp(name, "summary") == 0) {
127 ctx->p = ctx->summary;
128 ctx->state = YUM_STATE_SUMMARY;
129 } else if (strcmp(name, "description") == 0) {
130 ctx->p = ctx->description;
131 ctx->state = YUM_STATE_DESCRIPTION;
132 } else if (strcmp(name, "url") == 0) {
134 ctx->state = YUM_STATE_URL;
135 } else if (strcmp(name, "checksum") == 0) {
137 ctx->state = YUM_STATE_CHECKSUM;
138 } else if (strcmp(name, "rpm:license") == 0) {
139 ctx->p = ctx->license;
140 ctx->state = YUM_STATE_LICENSE;
141 } else if (strcmp(name, "rpm:requires") == 0) {
142 ctx->state = YUM_STATE_REQUIRES;
143 ctx->property_type = RAZOR_PROPERTY_REQUIRES;
144 } else if (strcmp(name, "rpm:provides") == 0) {
145 ctx->state = YUM_STATE_PROVIDES;
146 ctx->property_type = RAZOR_PROPERTY_PROVIDES;
147 } else if (strcmp(name, "rpm:obsoletes") == 0) {
148 ctx->state = YUM_STATE_OBSOLETES;
149 ctx->property_type = RAZOR_PROPERTY_OBSOLETES;
150 } else if (strcmp(name, "rpm:conflicts") == 0) {
151 ctx->state = YUM_STATE_CONFLICTS;
152 ctx->property_type = RAZOR_PROPERTY_CONFLICTS;
153 } else if (strcmp(name, "rpm:entry") == 0 &&
154 ctx->state != YUM_STATE_BEGIN) {
159 relation = RAZOR_PROPERTY_EQUAL;
161 for (i = 0; atts[i]; i += 2) {
162 if (strcmp(atts[i], "name") == 0)
164 else if (strcmp(atts[i], "epoch") == 0)
166 else if (strcmp(atts[i], "ver") == 0)
167 version = atts[i + 1];
168 else if (strcmp(atts[i], "rel") == 0)
169 release = atts[i + 1];
170 else if (strcmp(atts[i], "flags") == 0)
171 relation = yum_to_razor_relation(atts[i + 1]);
172 else if (strcmp(atts[i], "pre") == 0)
175 RAZOR_PROPERTY_POST |
176 RAZOR_PROPERTY_PREUN |
177 RAZOR_PROPERTY_POSTUN;
181 fprintf(stderr, "invalid rpm:entry, "
182 "missing name or version attributes\n");
186 razor_build_evr(buffer, sizeof buffer, epoch, version, release);
187 flags = ctx->property_type | relation | pre;
188 razor_importer_add_property(ctx->importer, n, flags, buffer);
193 yum_primary_end_element (void *data, const char *name)
195 struct yum_context *ctx = data;
197 switch (ctx->state) {
198 case YUM_STATE_PACKAGE_NAME:
199 case YUM_STATE_PACKAGE_ARCH:
200 case YUM_STATE_SUMMARY:
201 case YUM_STATE_DESCRIPTION:
203 case YUM_STATE_LICENSE:
204 case YUM_STATE_CHECKSUM:
206 ctx->state = YUM_STATE_BEGIN;
210 if (strcmp(name, "package") == 0) {
211 razor_importer_add_details(ctx->importer, ctx->summary,
212 ctx->description, ctx->url,
215 XML_StopParser(ctx->current_parser, XML_TRUE);
216 ctx->current_parser = ctx->filelists_parser;
218 printf("\rimporting %d/%d", ++ctx->current, ctx->total);
224 yum_character_data (void *data, const XML_Char *s, int len)
226 struct yum_context *ctx = data;
228 switch (ctx->state) {
229 case YUM_STATE_PACKAGE_NAME:
230 case YUM_STATE_PACKAGE_ARCH:
231 case YUM_STATE_SUMMARY:
232 case YUM_STATE_DESCRIPTION:
234 case YUM_STATE_LICENSE:
235 case YUM_STATE_CHECKSUM:
237 memcpy(ctx->p, s, len);
245 yum_filelists_start_element(void *data, const char *name, const char **atts)
247 struct yum_context *ctx = data;
248 const char *pkg, *pkgid;
251 if (strcmp(name, "package") == 0) {
254 for (i = 0; atts[i]; i += 2) {
255 if (strcmp(atts[i], "name") == 0)
257 else if (strcmp(atts[i], "pkgid") == 0)
260 if (strcmp(pkgid, ctx->pkgid) != 0)
261 fprintf(stderr, "primary.xml and filelists.xml "
262 "mismatch for %s: %s vs %s",
263 pkg, pkgid, ctx->pkgid);
264 } else if (strcmp(name, "file") == 0) {
265 ctx->state = YUM_STATE_FILE;
266 ctx->p = ctx->buffer;
272 yum_filelists_end_element (void *data, const char *name)
274 struct yum_context *ctx = data;
276 ctx->state = YUM_STATE_BEGIN;
277 if (strcmp(name, "package") == 0) {
278 XML_StopParser(ctx->current_parser, XML_TRUE);
279 ctx->current_parser = ctx->primary_parser;
280 razor_importer_finish_package(ctx->importer);
281 } else if (strcmp(name, "file") == 0)
282 razor_importer_add_file(ctx->importer, ctx->buffer);
286 #define XML_BUFFER_SIZE 4096
289 razor_set_create_from_yum(void)
291 struct yum_context ctx;
294 gzFile primary, filelists;
295 XML_ParsingStatus status;
297 ctx.importer = razor_importer_create();
298 ctx.state = YUM_STATE_BEGIN;
300 ctx.primary_parser = XML_ParserCreate(NULL);
301 XML_SetUserData(ctx.primary_parser, &ctx);
302 XML_SetElementHandler(ctx.primary_parser,
303 yum_primary_start_element,
304 yum_primary_end_element);
305 XML_SetCharacterDataHandler(ctx.primary_parser,
308 ctx.filelists_parser = XML_ParserCreate(NULL);
309 XML_SetUserData(ctx.filelists_parser, &ctx);
310 XML_SetElementHandler(ctx.filelists_parser,
311 yum_filelists_start_element,
312 yum_filelists_end_element);
313 XML_SetCharacterDataHandler(ctx.filelists_parser,
316 primary = gzopen("primary.xml.gz", "rb");
319 filelists = gzopen("filelists.xml.gz", "rb");
320 if (filelists == NULL)
323 ctx.current_parser = ctx.primary_parser;
328 XML_GetParsingStatus(ctx.current_parser, &status);
329 switch (status.parsing) {
331 ret = XML_ResumeParser(ctx.current_parser);
334 case XML_INITIALIZED:
335 buf = XML_GetBuffer(ctx.current_parser,
337 if (ctx.current_parser == ctx.primary_parser)
338 len = gzread(primary, buf, XML_BUFFER_SIZE);
340 len = gzread(filelists, buf, XML_BUFFER_SIZE);
343 "couldn't read input: %s\n",
348 XML_ParseBuffer(ctx.current_parser, len, len == 0);
353 } while (status.parsing != XML_FINISHED);
356 XML_ParserFree(ctx.primary_parser);
357 XML_ParserFree(ctx.filelists_parser);
362 printf ("\nsaving\n");
363 return razor_importer_finish(ctx.importer);