Fix swapping of map entries in __qsort_with_data.
12 #include <rpm/rpmlib.h>
13 #include <rpm/rpmdb.h>
18 parse_package(struct import_context *ctx, 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 import_context_add_package(ctx, name, version);
40 parse_property(struct import_context *ctx, const char **atts, void *data)
42 const char *name = NULL, *version = NULL;
45 for (i = 0; atts[i]; i += 2) {
46 if (strcmp(atts[i], "name") == 0)
48 if (strcmp(atts[i], "version") == 0)
49 version = atts[i + 1];
53 fprintf(stderr, "invalid tag, missing name attribute\n");
57 import_context_add_property(ctx, data, name, version);
61 start_element(void *data, const char *name, const char **atts)
63 struct import_context *ctx = data;
65 if (strcmp(name, "package") == 0)
66 parse_package(ctx, atts, NULL);
67 else if (strcmp(name, "requires") == 0)
68 parse_property(ctx, atts, &ctx->requires);
69 else if (strcmp(name, "provides") == 0)
70 parse_property(ctx, atts, &ctx->provides);
74 end_element (void *data, const char *name)
76 struct import_context *ctx = data;
78 if (strcmp(name, "package") == 0)
79 import_context_finish_package(ctx);
83 import_rzr_file(struct import_context *ctx, const char *filename)
90 unsigned char hash[20];
92 fd = open(filename, O_RDONLY);
93 if (fstat(fd, &stat) < 0)
95 p = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
99 parser = XML_ParserCreate(NULL);
100 XML_SetUserData(parser, ctx);
101 XML_SetElementHandler(parser, start_element, end_element);
102 if (XML_Parse(parser, p, stat.st_size, 1) == XML_STATUS_ERROR) {
104 "%s at line %d, %s\n",
105 XML_ErrorString(XML_GetErrorCode(parser)),
106 XML_GetCurrentLineNumber(parser),
111 XML_ParserFree(parser);
114 SHA1_Update(&sha1, p, stat.st_size);
115 SHA1_Final(hash, &sha1);
119 munmap(p, stat.st_size);
125 razor_import_rzr_files(int count, const char *files[])
127 struct import_context ctx;
130 razor_prepare_import(&ctx);
132 for (i = 0; i < count; i++) {
133 if (import_rzr_file(&ctx, files[i]) < 0) {
134 fprintf(stderr, "failed to import %s\n", files[i]);
139 return razor_finish_import(&ctx);
142 /* Import a yum filelist as a razor package set. */
146 YUM_STATE_PACKAGE_NAME
150 struct import_context ctx;
151 struct import_property_context *current_property_context;
157 yum_start_element(void *data, const char *name, const char **atts)
159 struct yum_context *ctx = data;
160 const char *n, *version;
163 if (strcmp(name, "name") == 0) {
164 ctx->state = YUM_STATE_PACKAGE_NAME;
165 } else if (strcmp(name, "version") == 0) {
167 for (i = 0; atts[i]; i += 2) {
168 if (strcmp(atts[i], "ver") == 0)
169 version = atts[i + 1];
171 import_context_add_package(&ctx->ctx, ctx->name, version);
172 } else if (strcmp(name, "rpm:requires") == 0) {
173 ctx->current_property_context = &ctx->ctx.requires;
174 } else if (strcmp(name, "rpm:provides") == 0) {
175 ctx->current_property_context = &ctx->ctx.provides;
176 } else if (strcmp(name, "rpm:entry") == 0 &&
177 ctx->current_property_context != NULL) {
180 for (i = 0; atts[i]; i += 2) {
181 if (strcmp(atts[i], "name") == 0)
183 else if (strcmp(atts[i], "ver") == 0)
184 version = atts[i + 1];
188 fprintf(stderr, "invalid rpm:entry, "
189 "missing name or version attributes\n");
193 import_context_add_property(&ctx->ctx,
194 ctx->current_property_context,
200 yum_end_element (void *data, const char *name)
202 struct yum_context *ctx = data;
204 if (strcmp(name, "package") == 0) {
206 import_context_finish_package(&ctx->ctx);
207 } else if (strcmp(name, "name") == 0) {
209 } else if (strcmp(name, "rpm:requires") == 0) {
210 ctx->current_property_context = NULL;
211 } else if (strcmp(name, "rpm:provides") == 0) {
212 ctx->current_property_context = NULL;
217 yum_character_data (void *data, const XML_Char *s, int len)
219 struct yum_context *ctx = data;
221 if (ctx->state == YUM_STATE_PACKAGE_NAME)
222 ctx->name = strndup(s, len);
226 razor_set_create_from_yum_filelist(int fd)
228 struct yum_context ctx;
233 razor_prepare_import(&ctx.ctx);
235 parser = XML_ParserCreate(NULL);
236 XML_SetUserData(parser, &ctx);
237 XML_SetElementHandler(parser, yum_start_element, yum_end_element);
238 XML_SetCharacterDataHandler(parser, yum_character_data);
241 len = read(fd, buf, sizeof buf);
244 "couldn't read input: %s\n", strerror(errno));
249 if (XML_Parse(parser, buf, len, 0) == XML_STATUS_ERROR) {
252 XML_ErrorString(XML_GetErrorCode(parser)),
253 XML_GetCurrentLineNumber(parser));
258 XML_ParserFree(parser);
260 return razor_finish_import(&ctx.ctx);
264 razor_set_create_from_rpmdb(void)
266 struct import_context ctx;
267 rpmdbMatchIterator iter;
269 int_32 type, count, i;
270 char *name, *version, *release;
271 char **properties, **property_versions;
274 rpmReadConfigFiles(NULL, NULL);
276 if (rpmdbOpen("", &db, O_RDONLY, 0644) != 0) {
277 fprintf(stderr, "cannot open rpm database\n");
281 razor_prepare_import(&ctx);
283 iter = rpmdbInitIterator(db, 0, NULL, 0);
284 while (h = rpmdbNextIterator(iter), h != NULL) {
285 headerGetEntry(h, RPMTAG_NAME, &type,
286 (void **) &name, &count);
287 headerGetEntry(h, RPMTAG_VERSION, &type,
288 (void **) &version, &count);
289 headerGetEntry(h, RPMTAG_RELEASE, &type,
290 (void **) &release, &count);
291 import_context_add_package(&ctx, name, version);
294 headerGetEntry(h, RPMTAG_REQUIRES, &type,
295 (void **) &properties, &count);
296 headerGetEntry(h, RPMTAG_REQUIREVERSION, &type,
297 (void **) &property_versions, &count);
298 for (i = 0; i < count; i++)
299 import_context_add_property(&ctx,
302 property_versions[i]);
304 headerGetEntry(h, RPMTAG_PROVIDES, &type,
305 (void **) &properties, &count);
306 headerGetEntry(h, RPMTAG_PROVIDEVERSION, &type,
307 (void **) &property_versions, &count);
308 for (i = 0; i < count; i++)
309 import_context_add_property(&ctx,
312 property_versions[i]);
314 import_context_finish_package(&ctx);
319 return razor_finish_import(&ctx);