Import obsoletes and conflicts; add commands to list them.
12 #include <rpm/rpmlib.h>
13 #include <rpm/rpmdb.h>
18 parse_package(struct razor_importer *importer, const char **atts, void *data)
20 const char *name = NULL, *version = NULL;
23 for (i = 0; atts[i]; i += 2) {
24 if (strcmp(atts[i], "name") == 0)
26 else if (strcmp(atts[i], "version") == 0)
27 version = atts[i + 1];
30 if (name == NULL || version == NULL) {
31 fprintf(stderr, "invalid package tag, "
32 "missing name or version attributes\n");
36 razor_importer_begin_package(importer, name, version);
40 RZR_REQUIRES, RZR_PROVIDES
44 parse_property(struct razor_importer *importer, const char **atts, void *data)
46 const char *name = NULL, *version = NULL;
49 for (i = 0; atts[i]; i += 2) {
50 if (strcmp(atts[i], "name") == 0)
52 if (strcmp(atts[i], "version") == 0)
53 version = atts[i + 1];
57 fprintf(stderr, "invalid tag, missing name attribute\n");
63 razor_importer_add_property(importer, name, version,
64 RAZOR_PROPERTY_REQUIRES);
67 razor_importer_add_property(importer, name, version,
68 RAZOR_PROPERTY_PROVIDES);
74 start_element(void *data, const char *name, const char **atts)
76 struct razor_importer *importer = data;
78 if (strcmp(name, "package") == 0)
79 parse_package(importer, atts, NULL);
80 else if (strcmp(name, "requires") == 0)
81 parse_property(importer, atts, (void *) RZR_REQUIRES);
82 else if (strcmp(name, "provides") == 0)
83 parse_property(importer, atts, (void*) RZR_PROVIDES);
87 end_element (void *data, const char *name)
89 struct razor_importer *importer = data;
91 if (strcmp(name, "package") == 0)
92 razor_importer_finish_package(importer);
96 import_rzr_file(struct razor_importer *importer, const char *filename)
103 unsigned char hash[20];
105 fd = open(filename, O_RDONLY);
106 if (fstat(fd, &stat) < 0)
108 p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
112 parser = XML_ParserCreate(NULL);
113 XML_SetUserData(parser, importer);
114 XML_SetElementHandler(parser, start_element, end_element);
115 if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
117 "%s at line %ld, %s\n",
118 XML_ErrorString(XML_GetErrorCode(parser)),
119 XML_GetCurrentLineNumber(parser),
124 XML_ParserFree(parser);
127 SHA1_Update(&sha1, p, stat.st_size);
128 SHA1_Final(hash, &sha1);
132 munmap(p, stat.st_size);
138 razor_import_rzr_files(int count, const char *files[])
140 struct razor_importer *importer;
143 importer = razor_importer_new();
145 for (i = 0; i < count; i++) {
146 if (import_rzr_file(importer, files[i]) < 0) {
147 fprintf(stderr, "failed to import %s\n", files[i]);
152 return razor_importer_finish(importer);
155 /* Import a yum filelist as a razor package set. */
159 YUM_STATE_PACKAGE_NAME,
168 struct razor_importer *importer;
169 struct import_property_context *current_property_context;
170 char name[256], buffer[512], *p;
175 yum_start_element(void *data, const char *name, const char **atts)
177 struct yum_context *ctx = data;
178 const char *n, *version, *release;
182 if (strcmp(name, "name") == 0) {
183 ctx->state = YUM_STATE_PACKAGE_NAME;
185 } else if (strcmp(name, "version") == 0) {
188 for (i = 0; atts[i]; i += 2) {
189 if (strcmp(atts[i], "ver") == 0)
190 version = atts[i + 1];
191 else if (strcmp(atts[i], "rel") == 0)
192 release = atts[i + 1];
194 if (version == NULL || release == NULL) {
195 fprintf(stderr, "invalid version tag, "
196 "missing version or release attribute\n");
200 snprintf(buffer, sizeof buffer, "%s-%s", version, release);
201 razor_importer_begin_package(ctx->importer, ctx->name, buffer);
202 } else if (strcmp(name, "rpm:requires") == 0) {
203 ctx->state = YUM_STATE_REQUIRES;
204 } else if (strcmp(name, "rpm:provides") == 0) {
205 ctx->state = YUM_STATE_PROVIDES;
206 } else if (strcmp(name, "rpm:obsoletes") == 0) {
207 ctx->state = YUM_STATE_OBSOLETES;
208 } else if (strcmp(name, "rpm:conflicts") == 0) {
209 ctx->state = YUM_STATE_CONFLICTS;
210 } else if (strcmp(name, "rpm:entry") == 0 &&
211 ctx->state != YUM_STATE_BEGIN) {
215 for (i = 0; atts[i]; i += 2) {
216 if (strcmp(atts[i], "name") == 0)
218 else if (strcmp(atts[i], "ver") == 0)
219 version = atts[i + 1];
220 else if (strcmp(atts[i], "rel") == 0)
221 release = atts[i + 1];
225 fprintf(stderr, "invalid rpm:entry, "
226 "missing name or version attributes\n");
230 if (version && release)
231 snprintf(buffer, sizeof buffer,
232 "%s-%s", version, release);
234 strcpy(buffer, version);
238 switch (ctx->state) {
239 case YUM_STATE_REQUIRES:
240 razor_importer_add_property(ctx->importer, n, buffer,
241 RAZOR_PROPERTY_REQUIRES);
243 case YUM_STATE_PROVIDES:
244 razor_importer_add_property(ctx->importer, n, buffer,
245 RAZOR_PROPERTY_PROVIDES);
247 case YUM_STATE_OBSOLETES:
248 razor_importer_add_property(ctx->importer, n, buffer,
249 RAZOR_PROPERTY_OBSOLETES);
251 case YUM_STATE_CONFLICTS:
252 razor_importer_add_property(ctx->importer, n, buffer,
253 RAZOR_PROPERTY_CONFLICTS);
256 } else if (strcmp(name, "file") == 0) {
257 ctx->state = YUM_STATE_FILE;
258 ctx->p = ctx->buffer;
263 yum_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 razor_importer_finish_package(ctx->importer);
270 else if (strcmp(name, "file") == 0)
271 razor_importer_add_file(ctx->importer, ctx->buffer);
275 yum_character_data (void *data, const XML_Char *s, int len)
277 struct yum_context *ctx = data;
279 switch (ctx->state) {
280 case YUM_STATE_PACKAGE_NAME:
282 memcpy(ctx->p, s, len);
290 razor_set_create_from_yum_filelist(int fd)
292 struct yum_context ctx;
297 ctx.importer = razor_importer_new();
298 ctx.state = YUM_STATE_BEGIN;
300 parser = XML_ParserCreate(NULL);
301 XML_SetUserData(parser, &ctx);
302 XML_SetElementHandler(parser, yum_start_element, yum_end_element);
303 XML_SetCharacterDataHandler(parser, yum_character_data);
306 len = read(fd, buf, sizeof buf);
309 "couldn't read input: %s\n", strerror(errno));
314 if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
317 XML_ErrorString(XML_GetErrorCode(parser)),
318 XML_GetCurrentLineNumber(parser));
323 XML_ParserFree(parser);
325 return razor_importer_finish(ctx.importer);
336 razor_set_create_from_rpmdb(void)
338 struct razor_importer *importer;
339 rpmdbMatchIterator iter;
341 int_32 type, count, i;
342 union rpm_entry name, version, release;
343 union rpm_entry property_names, property_versions, property_flags;
344 union rpm_entry basenames, dirnames, dirindexes;
345 char filename[PATH_MAX];
348 rpmReadConfigFiles(NULL, NULL);
350 if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
351 fprintf(stderr, "cannot open rpm database\n");
355 importer = razor_importer_new();
357 iter = rpmdbInitIterator(db, 0, NULL, 0);
358 while (h = rpmdbNextIterator(iter), h != NULL) {
359 headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
360 headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
361 headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
362 snprintf(filename, sizeof filename, "%s-%s",
363 version.string, release.string);
364 razor_importer_begin_package(importer, name.string, filename);
366 headerGetEntry(h, RPMTAG_REQUIRENAME, &type,
367 &property_names.p, &count);
368 headerGetEntry(h, RPMTAG_REQUIREVERSION, &type,
369 &property_versions.p, &count);
370 headerGetEntry(h, RPMTAG_REQUIREFLAGS, &type,
371 &property_flags.p, &count);
372 for (i = 0; i < count; i++)
373 razor_importer_add_property(importer,
374 property_names.list[i],
375 property_versions.list[i],
376 RAZOR_PROPERTY_REQUIRES);
378 headerGetEntry(h, RPMTAG_PROVIDENAME, &type,
379 &property_names.p, &count);
380 headerGetEntry(h, RPMTAG_PROVIDEVERSION, &type,
381 &property_versions.p, &count);
382 headerGetEntry(h, RPMTAG_PROVIDEFLAGS, &type,
383 &property_flags.p, &count);
384 for (i = 0; i < count; i++)
385 razor_importer_add_property(importer,
386 property_names.list[i],
387 property_versions.list[i],
388 RAZOR_PROPERTY_PROVIDES);
390 headerGetEntry(h, RPMTAG_OBSOLETENAME, &type,
391 &property_names.p, &count);
392 headerGetEntry(h, RPMTAG_OBSOLETEVERSION, &type,
393 &property_versions.p, &count);
394 headerGetEntry(h, RPMTAG_OBSOLETEFLAGS, &type,
395 &property_flags.p, &count);
396 for (i = 0; i < count; i++)
397 razor_importer_add_property(importer,
398 property_names.list[i],
399 property_versions.list[i],
400 RAZOR_PROPERTY_OBSOLETES);
402 headerGetEntry(h, RPMTAG_CONFLICTNAME, &type,
403 &property_names.p, &count);
404 headerGetEntry(h, RPMTAG_CONFLICTVERSION, &type,
405 &property_versions.p, &count);
406 headerGetEntry(h, RPMTAG_CONFLICTFLAGS, &type,
407 &property_flags.p, &count);
408 for (i = 0; i < count; i++)
409 razor_importer_add_property(importer,
410 property_names.list[i],
411 property_versions.list[i],
412 RAZOR_PROPERTY_CONFLICTS);
414 headerGetEntry(h, RPMTAG_BASENAMES, &type,
415 &basenames.p, &count);
416 headerGetEntry(h, RPMTAG_DIRNAMES, &type,
417 &dirnames.p, &count);
418 headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
419 &dirindexes.p, &count);
420 for (i = 0; i < count; i++) {
421 snprintf(filename, sizeof filename, "%s%s",
422 dirnames.list[dirindexes.flags[i]],
424 razor_importer_add_file(importer, filename);
427 razor_importer_finish_package(importer);
432 return razor_importer_finish(importer);