2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
18 #include "repo_rpmmd.h"
39 STATE_RECOMMENDSENTRY,
41 STATE_SUPPLEMENTSENTRY,
60 static struct stateswitch stateswitches[] = {
61 { STATE_START, "metadata", STATE_METADATA, 0 },
62 { STATE_METADATA, "package", STATE_PACKAGE, 0 },
63 { STATE_PACKAGE, "name", STATE_NAME, 1 },
64 { STATE_PACKAGE, "arch", STATE_ARCH, 1 },
65 { STATE_PACKAGE, "version", STATE_VERSION, 0 },
66 { STATE_PACKAGE, "format", STATE_FORMAT, 0 },
67 { STATE_FORMAT, "rpm:vendor", STATE_VENDOR, 1 },
68 { STATE_FORMAT, "rpm:provides", STATE_PROVIDES, 0 },
69 { STATE_FORMAT, "rpm:requires", STATE_REQUIRES, 0 },
70 { STATE_FORMAT, "rpm:obsoletes", STATE_OBSOLETES , 0 },
71 { STATE_FORMAT, "rpm:conflicts", STATE_CONFLICTS , 0 },
72 { STATE_FORMAT, "rpm:recommends" , STATE_RECOMMENDS , 0 },
73 { STATE_FORMAT, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
74 { STATE_FORMAT, "rpm:suggests", STATE_SUGGESTS, 0 },
75 { STATE_FORMAT, "rpm:enhances", STATE_ENHANCES, 0 },
76 { STATE_FORMAT, "rpm:freshens", STATE_FRESHENS, 0 },
77 { STATE_FORMAT, "file", STATE_FILE, 1 },
78 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
79 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
80 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
81 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
82 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
83 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
84 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
85 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
86 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
103 struct stateswitch *swtab[NUMSTATES];
104 enum state sbtab[NUMSTATES];
108 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
110 const char *e, *v, *r, *v2;
115 for (; *atts; atts += 2)
117 if (!strcmp(*atts, "epoch"))
119 else if (!strcmp(*atts, "ver"))
121 else if (!strcmp(*atts, "rel"))
124 if (e && !strcmp(e, "0"))
128 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
130 if (v2 > v && *v2 == ':')
140 if (l > pd->acontent)
142 pd->content = xrealloc(pd->content, l + 256);
143 pd->acontent = l + 256;
167 fprintf(stderr, "evr: %s\n", pd->content);
169 return str2id(pool, pd->content, 1);
172 static char *flagtab[] = {
182 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
185 const char *n, *f, *k;
189 for (a = atts; *a; a += 2)
191 if (!strcmp(*a, "name"))
193 else if (!strcmp(*a, "flags"))
195 else if (!strcmp(*a, "kind"))
197 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
202 if (k && !strcmp(k, "package"))
206 int l = strlen(k) + 1 + strlen(n) + 1;
207 if (l > pd->acontent)
209 pd->content = xrealloc(pd->content, l + 256);
210 pd->acontent = l + 256;
212 sprintf(pd->content, "%s:%s", k, n);
213 name = str2id(pool, pd->content, 1);
216 name = str2id(pool, (char *)n, 1);
219 Id evr = makeevr_atts(pool, pd, atts);
221 for (flags = 0; flags < 6; flags++)
222 if (!strcmp(f, flagtab[flags]))
224 flags = flags < 6 ? flags + 1 : 0;
225 id = rel2id(pool, name, evr, flags, 1);
230 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
232 return repo_addid_dep(pd->repo, olddeps, id, isreq);
237 startElement(void *userData, const char *name, const char **atts)
239 struct parsedata *pd = userData;
240 Pool *pool = pd->pool;
241 Solvable *s = pd->start ? pd->start + pd->pack : 0;
242 struct stateswitch *sw;
244 if (pd->depth != pd->statedepth)
250 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
251 if (!strcmp(sw->ename, name))
253 if (sw->from != pd->state)
256 fprintf(stderr, "into unknown: %s\n", name);
261 pd->docontent = sw->docontent;
262 pd->statedepth = pd->depth;
268 for (; *atts; atts += 2)
270 if (!strcmp(*atts, "packages"))
272 pd->numpacks = atoi(atts[1]);
274 fprintf(stderr, "numpacks: %d\n", pd->numpacks);
276 pool->solvables = xrealloc2(pool->solvables, (pool->nsolvables + pd->numpacks), sizeof(Solvable));
277 pd->start = pool->solvables + pool->nsolvables;
278 memset(pd->start, 0, pd->numpacks * sizeof(Solvable));
283 if (pd->pack >= pd->numpacks)
285 fprintf(stderr, "repomd lied about the package number\n");
289 fprintf(stderr, "package #%d\n", pd->pack);
293 s->evr = makeevr_atts(pool, pd, atts);
298 case STATE_PROVIDESENTRY:
299 s->provides = adddep(pool, pd, s->provides, atts, 0);
304 case STATE_REQUIRESENTRY:
305 s->requires = adddep(pool, pd, s->requires, atts, 1);
307 case STATE_OBSOLETES:
310 case STATE_OBSOLETESENTRY:
311 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
313 case STATE_CONFLICTS:
316 case STATE_CONFLICTSENTRY:
317 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
319 case STATE_RECOMMENDS:
322 case STATE_RECOMMENDSENTRY:
323 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
325 case STATE_SUPPLEMENTS:
328 case STATE_SUPPLEMENTSENTRY:
329 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
334 case STATE_SUGGESTSENTRY:
335 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
340 case STATE_ENHANCESENTRY:
341 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
346 case STATE_FRESHENSENTRY:
347 s->freshens = adddep(pool, pd, s->freshens, atts, 0);
355 endElement(void *userData, const char *name)
357 struct parsedata *pd = userData;
358 Pool *pool = pd->pool;
359 Solvable *s = pd->start ? pd->start + pd->pack : 0;
362 if (pd->depth != pd->statedepth)
365 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
375 s->arch = ARCH_NOARCH;
376 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
377 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
378 s->supplements = repo_fix_legacy(pd->repo, s->provides, s->supplements);
382 s->name = str2id(pool, pd->content, 1);
385 s->arch = str2id(pool, pd->content, 1);
388 s->vendor = str2id(pool, pd->content, 1);
391 id = str2id(pool, pd->content, 1);
392 s->provides = repo_addid(pd->repo, s->provides, id);
397 pd->state = pd->sbtab[pd->state];
399 // printf("back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
403 characterData(void *userData, const XML_Char *s, int len)
405 struct parsedata *pd = userData;
411 l = pd->lcontent + len + 1;
412 if (l > pd->acontent)
414 pd->content = xrealloc(pd->content, l + 256);
415 pd->acontent = l + 256;
417 c = pd->content + pd->lcontent;
425 #define BUFF_SIZE 8192
428 repo_add_rpmmd(Repo *repo, FILE *fp)
430 Pool *pool = repo->pool;
434 struct stateswitch *sw;
436 if (!repo->start || repo->start == repo->end)
437 repo->start = pool->nsolvables;
438 repo->end = pool->nsolvables;
440 memset(&pd, 0, sizeof(pd));
441 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
443 if (!pd.swtab[sw->from])
444 pd.swtab[sw->from] = sw;
445 pd.sbtab[sw->to] = sw->from;
449 pd.content = malloc(256);
452 XML_Parser parser = XML_ParserCreate(NULL);
453 XML_SetUserData(parser, &pd);
454 XML_SetElementHandler(parser, startElement, endElement);
455 XML_SetCharacterDataHandler(parser, characterData);
458 l = fread(buf, 1, sizeof(buf), fp);
459 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
461 fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
467 XML_ParserFree(parser);
469 pool->nsolvables += pd.pack;
470 repo->nsolvables += pd.pack;
471 repo->end += pd.pack;