Drop unused sha1.c from repository.
13 #include <rpm/rpmlib.h>
14 #include <rpm/rpmdb.h>
18 /* Import a yum filelist as a razor package set. */
22 YUM_STATE_PACKAGE_NAME,
32 XML_Parser primary_parser;
33 XML_Parser filelists_parser;
34 XML_Parser current_parser;
36 struct razor_importer *importer;
37 struct import_property_context *current_property_context;
38 char name[256], buffer[512], *p;
44 yum_primary_start_element(void *data, const char *name, const char **atts)
46 struct yum_context *ctx = data;
47 const char *n, *version, *release;
51 if (strcmp(name, "name") == 0) {
52 ctx->state = YUM_STATE_PACKAGE_NAME;
54 } else if (strcmp(name, "version") == 0) {
57 for (i = 0; atts[i]; i += 2) {
58 if (strcmp(atts[i], "ver") == 0)
59 version = atts[i + 1];
60 else if (strcmp(atts[i], "rel") == 0)
61 release = atts[i + 1];
63 if (version == NULL || release == NULL) {
64 fprintf(stderr, "invalid version tag, "
65 "missing version or release attribute\n");
69 snprintf(buffer, sizeof buffer, "%s-%s", version, release);
70 razor_importer_begin_package(ctx->importer, ctx->name, buffer);
71 } else if (strcmp(name, "checksum") == 0) {
73 ctx->state = YUM_STATE_CHECKSUM;
74 } else if (strcmp(name, "rpm:requires") == 0) {
75 ctx->state = YUM_STATE_REQUIRES;
76 } else if (strcmp(name, "rpm:provides") == 0) {
77 ctx->state = YUM_STATE_PROVIDES;
78 } else if (strcmp(name, "rpm:obsoletes") == 0) {
79 ctx->state = YUM_STATE_OBSOLETES;
80 } else if (strcmp(name, "rpm:conflicts") == 0) {
81 ctx->state = YUM_STATE_CONFLICTS;
82 } else if (strcmp(name, "rpm:entry") == 0 &&
83 ctx->state != YUM_STATE_BEGIN) {
87 for (i = 0; atts[i]; i += 2) {
88 if (strcmp(atts[i], "name") == 0)
90 else if (strcmp(atts[i], "ver") == 0)
91 version = atts[i + 1];
92 else if (strcmp(atts[i], "rel") == 0)
93 release = atts[i + 1];
97 fprintf(stderr, "invalid rpm:entry, "
98 "missing name or version attributes\n");
102 if (version && release)
103 snprintf(buffer, sizeof buffer,
104 "%s-%s", version, release);
106 strcpy(buffer, version);
110 switch (ctx->state) {
111 case YUM_STATE_REQUIRES:
112 razor_importer_add_property(ctx->importer, n, buffer,
113 RAZOR_PROPERTY_REQUIRES);
115 case YUM_STATE_PROVIDES:
116 razor_importer_add_property(ctx->importer, n, buffer,
117 RAZOR_PROPERTY_PROVIDES);
119 case YUM_STATE_OBSOLETES:
120 razor_importer_add_property(ctx->importer, n, buffer,
121 RAZOR_PROPERTY_OBSOLETES);
123 case YUM_STATE_CONFLICTS:
124 razor_importer_add_property(ctx->importer, n, buffer,
125 RAZOR_PROPERTY_CONFLICTS);
132 yum_primary_end_element (void *data, const char *name)
134 struct yum_context *ctx = data;
136 switch (ctx->state) {
137 case YUM_STATE_PACKAGE_NAME:
138 case YUM_STATE_CHECKSUM:
140 ctx->state = YUM_STATE_BEGIN;
144 if (strcmp(name, "package") == 0) {
145 XML_StopParser(ctx->current_parser, XML_TRUE);
146 ctx->current_parser = ctx->filelists_parser;
151 yum_character_data (void *data, const XML_Char *s, int len)
153 struct yum_context *ctx = data;
155 switch (ctx->state) {
156 case YUM_STATE_PACKAGE_NAME:
157 case YUM_STATE_CHECKSUM:
159 memcpy(ctx->p, s, len);
167 yum_filelists_start_element(void *data, const char *name, const char **atts)
169 struct yum_context *ctx = data;
170 const char *pkg, *pkgid;
173 if (strcmp(name, "package") == 0) {
176 for (i = 0; atts[i]; i += 2) {
177 if (strcmp(atts[i], "name") == 0)
179 else if (strcmp(atts[i], "pkgid") == 0)
182 if (strcmp(pkgid, ctx->pkgid) != 0)
183 fprintf(stderr, "primary.xml and filelists.xml "
184 "mismatch for %s: %s vs %s",
185 pkg, pkgid, ctx->pkgid);
186 } else if (strcmp(name, "file") == 0) {
187 ctx->state = YUM_STATE_FILE;
188 ctx->p = ctx->buffer;
194 yum_filelists_end_element (void *data, const char *name)
196 struct yum_context *ctx = data;
198 ctx->state = YUM_STATE_BEGIN;
199 if (strcmp(name, "package") == 0) {
200 XML_StopParser(ctx->current_parser, XML_TRUE);
201 ctx->current_parser = ctx->primary_parser;
202 razor_importer_finish_package(ctx->importer);
203 } else if (strcmp(name, "file") == 0)
204 razor_importer_add_file(ctx->importer, ctx->buffer);
208 #define XML_BUFFER_SIZE 4096
211 razor_set_create_from_yum(void)
213 struct yum_context ctx;
216 gzFile primary, filelists;
217 XML_ParsingStatus status;
219 ctx.importer = razor_importer_new();
220 ctx.state = YUM_STATE_BEGIN;
222 ctx.primary_parser = XML_ParserCreate(NULL);
223 XML_SetUserData(ctx.primary_parser, &ctx);
224 XML_SetElementHandler(ctx.primary_parser,
225 yum_primary_start_element,
226 yum_primary_end_element);
227 XML_SetCharacterDataHandler(ctx.primary_parser,
230 ctx.filelists_parser = XML_ParserCreate(NULL);
231 XML_SetUserData(ctx.filelists_parser, &ctx);
232 XML_SetElementHandler(ctx.filelists_parser,
233 yum_filelists_start_element,
234 yum_filelists_end_element);
235 XML_SetCharacterDataHandler(ctx.filelists_parser,
238 primary = gzopen("primary.xml.gz", "rb");
241 filelists = gzopen("filelists.xml.gz", "rb");
242 if (filelists == NULL)
245 ctx.current_parser = ctx.primary_parser;
248 XML_GetParsingStatus(ctx.current_parser, &status);
249 switch (status.parsing) {
251 ret = XML_ResumeParser(ctx.current_parser);
254 case XML_INITIALIZED:
255 buf = XML_GetBuffer(ctx.current_parser,
257 if (ctx.current_parser == ctx.primary_parser)
258 len = gzread(primary, buf, XML_BUFFER_SIZE);
260 len = gzread(filelists, buf, XML_BUFFER_SIZE);
263 "couldn't read input: %s\n",
268 XML_ParseBuffer(ctx.current_parser, len, len == 0);
273 } while (status.parsing != XML_FINISHED);
276 XML_ParserFree(ctx.primary_parser);
277 XML_ParserFree(ctx.filelists_parser);
282 return razor_importer_finish(ctx.importer);
293 razor_set_create_from_rpmdb(void)
295 struct razor_importer *importer;
296 rpmdbMatchIterator iter;
298 int_32 type, count, i;
299 union rpm_entry name, version, release;
300 union rpm_entry property_names, property_versions, property_flags;
301 union rpm_entry basenames, dirnames, dirindexes;
302 char filename[PATH_MAX];
305 rpmReadConfigFiles(NULL, NULL);
307 if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
308 fprintf(stderr, "cannot open rpm database\n");
312 importer = razor_importer_new();
314 iter = rpmdbInitIterator(db, 0, NULL, 0);
315 while (h = rpmdbNextIterator(iter), h != NULL) {
316 headerGetEntry(h, RPMTAG_NAME, &type, &name.p, &count);
317 headerGetEntry(h, RPMTAG_VERSION, &type, &version.p, &count);
318 headerGetEntry(h, RPMTAG_RELEASE, &type, &release.p, &count);
319 snprintf(filename, sizeof filename, "%s-%s",
320 version.string, release.string);
321 razor_importer_begin_package(importer, name.string, filename);
323 headerGetEntry(h, RPMTAG_REQUIRENAME, &type,
324 &property_names.p, &count);
325 headerGetEntry(h, RPMTAG_REQUIREVERSION, &type,
326 &property_versions.p, &count);
327 headerGetEntry(h, RPMTAG_REQUIREFLAGS, &type,
328 &property_flags.p, &count);
329 for (i = 0; i < count; i++)
330 razor_importer_add_property(importer,
331 property_names.list[i],
332 property_versions.list[i],
333 RAZOR_PROPERTY_REQUIRES);
335 headerGetEntry(h, RPMTAG_PROVIDENAME, &type,
336 &property_names.p, &count);
337 headerGetEntry(h, RPMTAG_PROVIDEVERSION, &type,
338 &property_versions.p, &count);
339 headerGetEntry(h, RPMTAG_PROVIDEFLAGS, &type,
340 &property_flags.p, &count);
341 for (i = 0; i < count; i++)
342 razor_importer_add_property(importer,
343 property_names.list[i],
344 property_versions.list[i],
345 RAZOR_PROPERTY_PROVIDES);
347 headerGetEntry(h, RPMTAG_OBSOLETENAME, &type,
348 &property_names.p, &count);
349 headerGetEntry(h, RPMTAG_OBSOLETEVERSION, &type,
350 &property_versions.p, &count);
351 headerGetEntry(h, RPMTAG_OBSOLETEFLAGS, &type,
352 &property_flags.p, &count);
353 for (i = 0; i < count; i++)
354 razor_importer_add_property(importer,
355 property_names.list[i],
356 property_versions.list[i],
357 RAZOR_PROPERTY_OBSOLETES);
359 headerGetEntry(h, RPMTAG_CONFLICTNAME, &type,
360 &property_names.p, &count);
361 headerGetEntry(h, RPMTAG_CONFLICTVERSION, &type,
362 &property_versions.p, &count);
363 headerGetEntry(h, RPMTAG_CONFLICTFLAGS, &type,
364 &property_flags.p, &count);
365 for (i = 0; i < count; i++)
366 razor_importer_add_property(importer,
367 property_names.list[i],
368 property_versions.list[i],
369 RAZOR_PROPERTY_CONFLICTS);
371 headerGetEntry(h, RPMTAG_BASENAMES, &type,
372 &basenames.p, &count);
373 headerGetEntry(h, RPMTAG_DIRNAMES, &type,
374 &dirnames.p, &count);
375 headerGetEntry(h, RPMTAG_DIRINDEXES, &type,
376 &dirindexes.p, &count);
377 for (i = 0; i < count; i++) {
378 snprintf(filename, sizeof filename, "%s%s",
379 dirnames.list[dirindexes.flags[i]],
381 razor_importer_add_file(importer, filename);
384 razor_importer_finish_package(importer);
389 return razor_importer_finish(importer);