15 /* Import a yum filelist as a razor package set. */
19 YUM_STATE_PACKAGE_NAME,
29 XML_Parser primary_parser;
30 XML_Parser filelists_parser;
31 XML_Parser current_parser;
33 struct razor_importer *importer;
34 struct import_property_context *current_property_context;
35 char name[256], buffer[512], *p;
40 static enum razor_version_relation
41 yum_to_razor_flags (const char *flags)
45 return RAZOR_VERSION_EQUAL;
47 if (flags[0] == 'L') {
49 return RAZOR_VERSION_LESS;
51 return RAZOR_VERSION_LESS_OR_EQUAL;
52 } else if (flags[0] == 'G') {
54 return RAZOR_VERSION_GREATER;
56 return RAZOR_VERSION_GREATER_OR_EQUAL;
58 return RAZOR_VERSION_EQUAL;
62 yum_primary_start_element(void *data, const char *name, const char **atts)
64 struct yum_context *ctx = data;
65 const char *n, *version, *release, *flags;
69 if (strcmp(name, "name") == 0) {
70 ctx->state = YUM_STATE_PACKAGE_NAME;
72 } else if (strcmp(name, "version") == 0) {
75 for (i = 0; atts[i]; i += 2) {
76 if (strcmp(atts[i], "ver") == 0)
77 version = atts[i + 1];
78 else if (strcmp(atts[i], "rel") == 0)
79 release = atts[i + 1];
81 if (version == NULL || release == NULL) {
82 fprintf(stderr, "invalid version tag, "
83 "missing version or release attribute\n");
87 snprintf(buffer, sizeof buffer, "%s-%s", version, release);
88 razor_importer_begin_package(ctx->importer, ctx->name, buffer);
89 } else if (strcmp(name, "checksum") == 0) {
91 ctx->state = YUM_STATE_CHECKSUM;
92 } else if (strcmp(name, "rpm:requires") == 0) {
93 ctx->state = YUM_STATE_REQUIRES;
94 } else if (strcmp(name, "rpm:provides") == 0) {
95 ctx->state = YUM_STATE_PROVIDES;
96 } else if (strcmp(name, "rpm:obsoletes") == 0) {
97 ctx->state = YUM_STATE_OBSOLETES;
98 } else if (strcmp(name, "rpm:conflicts") == 0) {
99 ctx->state = YUM_STATE_CONFLICTS;
100 } else if (strcmp(name, "rpm:entry") == 0 &&
101 ctx->state != YUM_STATE_BEGIN) {
106 for (i = 0; atts[i]; i += 2) {
107 if (strcmp(atts[i], "name") == 0)
109 else if (strcmp(atts[i], "ver") == 0)
110 version = atts[i + 1];
111 else if (strcmp(atts[i], "rel") == 0)
112 release = atts[i + 1];
113 else if (strcmp(atts[i], "flags") == 0)
118 fprintf(stderr, "invalid rpm:entry, "
119 "missing name or version attributes\n");
123 if (version && release)
124 snprintf(buffer, sizeof buffer,
125 "%s-%s", version, release);
127 strcpy(buffer, version);
131 switch (ctx->state) {
132 case YUM_STATE_REQUIRES:
133 razor_importer_add_property(ctx->importer, n,
134 yum_to_razor_flags (flags),
136 RAZOR_PROPERTY_REQUIRES);
138 case YUM_STATE_PROVIDES:
139 razor_importer_add_property(ctx->importer, n,
140 yum_to_razor_flags (flags),
142 RAZOR_PROPERTY_PROVIDES);
144 case YUM_STATE_OBSOLETES:
145 razor_importer_add_property(ctx->importer, n,
146 yum_to_razor_flags (flags),
148 RAZOR_PROPERTY_OBSOLETES);
150 case YUM_STATE_CONFLICTS:
151 razor_importer_add_property(ctx->importer, n,
152 yum_to_razor_flags (flags),
154 RAZOR_PROPERTY_CONFLICTS);
161 yum_primary_end_element (void *data, const char *name)
163 struct yum_context *ctx = data;
165 switch (ctx->state) {
166 case YUM_STATE_PACKAGE_NAME:
167 case YUM_STATE_CHECKSUM:
169 ctx->state = YUM_STATE_BEGIN;
173 if (strcmp(name, "package") == 0) {
174 XML_StopParser(ctx->current_parser, XML_TRUE);
175 ctx->current_parser = ctx->filelists_parser;
180 yum_character_data (void *data, const XML_Char *s, int len)
182 struct yum_context *ctx = data;
184 switch (ctx->state) {
185 case YUM_STATE_PACKAGE_NAME:
186 case YUM_STATE_CHECKSUM:
188 memcpy(ctx->p, s, len);
196 yum_filelists_start_element(void *data, const char *name, const char **atts)
198 struct yum_context *ctx = data;
199 const char *pkg, *pkgid;
202 if (strcmp(name, "package") == 0) {
205 for (i = 0; atts[i]; i += 2) {
206 if (strcmp(atts[i], "name") == 0)
208 else if (strcmp(atts[i], "pkgid") == 0)
211 if (strcmp(pkgid, ctx->pkgid) != 0)
212 fprintf(stderr, "primary.xml and filelists.xml "
213 "mismatch for %s: %s vs %s",
214 pkg, pkgid, ctx->pkgid);
215 } else if (strcmp(name, "file") == 0) {
216 ctx->state = YUM_STATE_FILE;
217 ctx->p = ctx->buffer;
223 yum_filelists_end_element (void *data, const char *name)
225 struct yum_context *ctx = data;
227 ctx->state = YUM_STATE_BEGIN;
228 if (strcmp(name, "package") == 0) {
229 XML_StopParser(ctx->current_parser, XML_TRUE);
230 ctx->current_parser = ctx->primary_parser;
231 razor_importer_finish_package(ctx->importer);
232 } else if (strcmp(name, "file") == 0)
233 razor_importer_add_file(ctx->importer, ctx->buffer);
237 #define XML_BUFFER_SIZE 4096
240 razor_set_create_from_yum(void)
242 struct yum_context ctx;
245 gzFile primary, filelists;
246 XML_ParsingStatus status;
248 ctx.importer = razor_importer_new();
249 ctx.state = YUM_STATE_BEGIN;
251 ctx.primary_parser = XML_ParserCreate(NULL);
252 XML_SetUserData(ctx.primary_parser, &ctx);
253 XML_SetElementHandler(ctx.primary_parser,
254 yum_primary_start_element,
255 yum_primary_end_element);
256 XML_SetCharacterDataHandler(ctx.primary_parser,
259 ctx.filelists_parser = XML_ParserCreate(NULL);
260 XML_SetUserData(ctx.filelists_parser, &ctx);
261 XML_SetElementHandler(ctx.filelists_parser,
262 yum_filelists_start_element,
263 yum_filelists_end_element);
264 XML_SetCharacterDataHandler(ctx.filelists_parser,
267 primary = gzopen("primary.xml.gz", "rb");
270 filelists = gzopen("filelists.xml.gz", "rb");
271 if (filelists == NULL)
274 ctx.current_parser = ctx.primary_parser;
277 XML_GetParsingStatus(ctx.current_parser, &status);
278 switch (status.parsing) {
280 ret = XML_ResumeParser(ctx.current_parser);
283 case XML_INITIALIZED:
284 buf = XML_GetBuffer(ctx.current_parser,
286 if (ctx.current_parser == ctx.primary_parser)
287 len = gzread(primary, buf, XML_BUFFER_SIZE);
289 len = gzread(filelists, buf, XML_BUFFER_SIZE);
292 "couldn't read input: %s\n",
297 XML_ParseBuffer(ctx.current_parser, len, len == 0);
302 } while (status.parsing != XML_FINISHED);
305 XML_ParserFree(ctx.primary_parser);
306 XML_ParserFree(ctx.filelists_parser);
311 return razor_importer_finish(ctx.importer);