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_utils.h"
19 #include "repo_rpmmd.h"
47 STATE_RECOMMENDSENTRY,
50 STATE_SUPPLEMENTSENTRY,
75 static struct stateswitch stateswitches[] = {
77 { STATE_START, "product", STATE_SOLVABLE, 0 },
78 { STATE_START, "pattern", STATE_SOLVABLE, 0 },
79 { STATE_START, "patch", STATE_SOLVABLE, 0 },
81 { STATE_START, "metadata", STATE_METADATA, 0 },
82 { STATE_METADATA, "package", STATE_SOLVABLE, 0 },
84 { STATE_SOLVABLE, "name", STATE_NAME, 1 },
85 { STATE_SOLVABLE, "arch", STATE_ARCH, 1 },
86 { STATE_SOLVABLE, "version", STATE_VERSION, 0 },
87 { STATE_SOLVABLE, "vendor", STATE_VENDOR, 1 },
89 { STATE_SOLVABLE, "format", STATE_FORMAT, 0 },
91 /* those are used in libzypp xml store */
92 { STATE_SOLVABLE, "provides", STATE_PROVIDES, 0 },
93 { STATE_SOLVABLE, "requires", STATE_REQUIRES, 0 },
94 { STATE_SOLVABLE, "obsoletes", STATE_OBSOLETES , 0 },
95 { STATE_SOLVABLE, "conflicts", STATE_CONFLICTS , 0 },
96 { STATE_SOLVABLE, "recommends", STATE_RECOMMENDS , 0 },
97 { STATE_SOLVABLE, "supplements", STATE_SUPPLEMENTS, 0 },
98 { STATE_SOLVABLE, "suggests", STATE_SUGGESTS, 0 },
99 { STATE_SOLVABLE, "enhances", STATE_ENHANCES, 0 },
100 { STATE_SOLVABLE, "freshens", STATE_FRESHENS, 0 },
102 { STATE_PROVIDES, "capability", STATE_PROVIDESCAP, 1 },
103 { STATE_REQUIRES, "capability", STATE_REQUIRESCAP, 1 },
104 { STATE_OBSOLETES, "capability", STATE_OBSOLETESCAP, 1 },
105 { STATE_CONFLICTS, "capability", STATE_CONFLICTSCAP, 1 },
106 { STATE_RECOMMENDS, "capability", STATE_RECOMMENDSCAP, 1 },
107 { STATE_SUPPLEMENTS, "capability", STATE_SUPPLEMENTSCAP, 1 },
108 { STATE_SUGGESTS, "capability", STATE_SUGGESTSCAP, 1 },
109 { STATE_ENHANCES, "capability", STATE_ENHANCESCAP, 1 },
110 { STATE_FRESHENS, "capability", STATE_FRESHENSCAP, 1 },
112 { STATE_SOLVABLE, "summary", STATE_SUMMARY, 1 },
113 { STATE_SOLVABLE, "description", STATE_DESCRIPTION, 1 },
115 { STATE_FORMAT, "rpm:vendor", STATE_VENDOR, 1 },
117 /* rpm-md dependencies */
118 { STATE_FORMAT, "rpm:provides", STATE_PROVIDES, 0 },
119 { STATE_FORMAT, "rpm:requires", STATE_REQUIRES, 0 },
120 { STATE_FORMAT, "rpm:obsoletes", STATE_OBSOLETES , 0 },
121 { STATE_FORMAT, "rpm:conflicts", STATE_CONFLICTS , 0 },
122 { STATE_FORMAT, "rpm:recommends", STATE_RECOMMENDS , 0 },
123 { STATE_FORMAT, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
124 { STATE_FORMAT, "rpm:suggests", STATE_SUGGESTS, 0 },
125 { STATE_FORMAT, "rpm:enhances", STATE_ENHANCES, 0 },
126 { STATE_FORMAT, "rpm:freshens", STATE_FRESHENS, 0 },
127 { STATE_FORMAT, "file", STATE_FILE, 1 },
128 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
129 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
130 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
131 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
132 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
133 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
134 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
135 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
136 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
141 struct parsedata_common common;
152 struct stateswitch *swtab[NUMSTATES];
153 enum state sbtab[NUMSTATES];
158 static char *flagtabnum[] = {
169 * adds plain dependencies, that is strings like "foo > 2.0"
170 * which are used in libzypp xml store, not in rpm-md.
173 adddepplain(Pool *pool, struct parsedata_common *pd, unsigned int olddeps, char *line, Id marker, char *kind)
179 i = split(line + 5, sp, 4);
180 if (i != 1 && i != 3)
182 fprintf(stderr, "Bad dependency line: %s\n", line);
186 id = str2id(pool, join(pd, kind, ":", sp[0]), 1);
188 id = str2id(pool, sp[0], 1);
191 evrid = makeevr(pool, sp[2]);
192 for (flags = 0; flags < 6; flags++)
193 if (!strcmp(sp[1], flagtabnum[flags]))
197 fprintf(stderr, "Unknown relation '%s'\n", sp[1]);
200 id = rel2id(pool, id, evrid, flags + 1, 1);
202 return repo_addid_dep(pd->repo, olddeps, id, marker);
206 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
208 const char *e, *v, *r, *v2;
213 for (; *atts; atts += 2)
215 if (!strcmp(*atts, "epoch"))
217 else if (!strcmp(*atts, "ver"))
219 else if (!strcmp(*atts, "rel"))
222 if (e && !strcmp(e, "0"))
226 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
228 if (v2 > v && *v2 == ':')
238 if (l > pd->acontent)
240 pd->content = sat_realloc(pd->content, l + 256);
241 pd->acontent = l + 256;
265 fprintf(stderr, "evr: %s\n", pd->content);
267 return str2id(pool, pd->content, 1);
271 find_attr(const char *txt, const char **atts)
275 for (; *atts; atts += 2)
277 if (!strcmp(*atts, txt))
283 static char *flagtab[] = {
293 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
296 const char *n, *f, *k;
300 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
301 for (a = atts; *a; a += 2)
303 if (!strcmp(*a, "name"))
305 else if (!strcmp(*a, "flags"))
307 else if (!strcmp(*a, "kind"))
309 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
310 marker = SOLVABLE_PREREQMARKER;
314 if (k && !strcmp(k, "package"))
318 int l = strlen(k) + 1 + strlen(n) + 1;
319 if (l > pd->acontent)
321 pd->content = sat_realloc(pd->content, l + 256);
322 pd->acontent = l + 256;
324 sprintf(pd->content, "%s:%s", k, n);
325 name = str2id(pool, pd->content, 1);
328 name = str2id(pool, (char *)n, 1);
331 Id evr = makeevr_atts(pool, pd, atts);
333 for (flags = 0; flags < 6; flags++)
334 if (!strcmp(f, flagtab[flags]))
336 flags = flags < 6 ? flags + 1 : 0;
337 id = rel2id(pool, name, evr, flags, 1);
342 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
344 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
349 startElement(void *userData, const char *name, const char **atts)
351 fprintf(stderr,"+tag: %s\n", name);
352 struct parsedata *pd = userData;
353 Pool *pool = pd->common.pool;
354 Solvable *s = pd->solvable;
355 struct stateswitch *sw;
357 if (pd->depth != pd->statedepth)
363 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
364 if (!strcmp(sw->ename, name))
366 if (sw->from != pd->state)
369 fprintf(stderr, "into unknown: %s\n", name);
374 pd->docontent = sw->docontent;
375 pd->statedepth = pd->depth;
381 for (; *atts; atts += 2)
383 if (!strcmp(*atts, "packages"))
385 pd->numpacks = atoi(atts[1]);
386 if (pd->numpacks < 0)
389 fprintf(stderr, "numpacks: %d\n", pd->numpacks);
391 pd->solvable = pool_id2solvable(pool, repo_add_solvable_block(pd->common.repo, pd->numpacks));
397 if ( name[2] == 't' && name[3] == 't' )
398 pd->kind = "pattern";
399 else if ( name[1] == 'r' )
400 pd->kind = "product";
401 else if ( name[2] == 't' && name[3] == 'c' )
404 if (pd->numpacks == 0)
405 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
407 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
411 s->evr = makeevr_atts(pool, pd, atts);
416 case STATE_PROVIDESENTRY:
417 s->provides = adddep(pool, pd, s->provides, atts, 0);
422 case STATE_REQUIRESENTRY:
423 s->requires = adddep(pool, pd, s->requires, atts, 1);
425 case STATE_OBSOLETES:
428 case STATE_OBSOLETESENTRY:
429 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
431 case STATE_CONFLICTS:
434 case STATE_CONFLICTSENTRY:
435 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
437 case STATE_RECOMMENDS:
440 case STATE_RECOMMENDSENTRY:
441 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
443 case STATE_SUPPLEMENTS:
446 case STATE_SUPPLEMENTSENTRY:
447 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
452 case STATE_SUGGESTSENTRY:
453 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
458 case STATE_ENHANCESENTRY:
459 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
464 case STATE_FRESHENSENTRY:
465 s->freshens = adddep(pool, pd, s->freshens, atts, 0);
467 case STATE_PROVIDESCAP:
468 case STATE_REQUIRESCAP:
469 case STATE_OBSOLETESCAP:
470 case STATE_CONFLICTSCAP:
471 case STATE_RECOMMENDSCAP:
472 case STATE_SUPPLEMENTSCAP:
473 case STATE_SUGGESTSCAP:
474 case STATE_ENHANCESCAP:
475 case STATE_FRESHENSCAP:
476 pd->capkind = find_attr("kind", atts);
477 //fprintf(stderr,"capkind es: %s\n", pd->capkind);
480 pd->lang = find_attr("lang", atts);
482 case STATE_DESCRIPTION:
483 pd->lang = find_attr("lang", atts);
491 endElement(void *userData, const char *name)
493 fprintf(stderr,"-tag: %s\n", name);
494 struct parsedata *pd = userData;
495 Pool *pool = pd->common.pool;
496 Solvable *s = pd->solvable;
497 Repo *repo = pd->common.repo;
500 if (pd->depth != pd->statedepth)
503 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
514 s->arch = ARCH_NOARCH;
515 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
516 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
517 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
518 if (pd->numpacks > 0)
527 s->name = str2id(pool, join(&pd->common, pd->kind, ":", pd->content), 1);
529 s->name = str2id(pool, pd->content, 1);
532 s->arch = str2id(pool, pd->content, 1);
535 s->vendor = str2id(pool, pd->content, 1);
538 id = str2id(pool, pd->content, 1);
539 s->provides = repo_addid(repo, s->provides, id);
541 case STATE_PROVIDESCAP:
542 s->provides = adddepplain(pool, &pd->common, s->provides, pd->content, 0, 0);
544 case STATE_REQUIRESCAP:
545 s->requires = adddepplain(pool, &pd->common, s->requires, pd->content, 0, 0);
547 case STATE_OBSOLETESCAP:
548 s->obsoletes = adddepplain(pool, &pd->common, s->obsoletes, pd->content, 0, 0);
550 case STATE_CONFLICTSCAP:
551 s->conflicts = adddepplain(pool, &pd->common, s->conflicts, pd->content, 0, 0);
553 case STATE_RECOMMENDSCAP:
554 s->recommends = adddepplain(pool, &pd->common, s->recommends, pd->content, 0, 0);
556 case STATE_SUPPLEMENTSCAP:
557 s->supplements = adddepplain(pool, &pd->common, s->supplements, pd->content, 0, 0);
559 case STATE_SUGGESTSCAP:
560 s->suggests = adddepplain(pool, &pd->common, s->suggests, pd->content, 0, 0);
562 case STATE_ENHANCESCAP:
563 s->enhances = adddepplain(pool, &pd->common, s->enhances, pd->content, 0, 0);
565 case STATE_FRESHENSCAP:
566 s->freshens = adddepplain(pool, &pd->common, s->freshens, pd->content, 0, 0);
571 case STATE_DESCRIPTION:
577 pd->state = pd->sbtab[pd->state];
579 fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
583 characterData(void *userData, const XML_Char *s, int len)
585 struct parsedata *pd = userData;
591 l = pd->lcontent + len + 1;
592 if (l > pd->acontent)
594 pd->content = sat_realloc(pd->content, l + 256);
595 pd->acontent = l + 256;
597 c = pd->content + pd->lcontent;
605 #define BUFF_SIZE 8192
608 repo_add_rpmmd(Repo *repo, FILE *fp)
610 Pool *pool = repo->pool;
614 struct stateswitch *sw;
616 memset(&pd, 0, sizeof(pd));
617 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
619 if (!pd.swtab[sw->from])
620 pd.swtab[sw->from] = sw;
621 pd.sbtab[sw->to] = sw->from;
623 pd.common.pool = pool;
624 pd.common.repo = repo;
625 pd.content = sat_malloc(256);
631 XML_Parser parser = XML_ParserCreate(NULL);
632 XML_SetUserData(parser, &pd);
633 XML_SetElementHandler(parser, startElement, endElement);
634 XML_SetCharacterDataHandler(parser, characterData);
637 l = fread(buf, 1, sizeof(buf), fp);
638 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
640 fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
646 XML_ParserFree(parser);
648 repo_free_solvable_block(repo, pd.solvable - pool->solvables, pd.numpacks, 1);
649 sat_free(pd.content);