2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
18 #include "tools_util.h"
19 #include "repo_rpmmd.h"
39 /* resobject attributes */
57 // xml store pattern attributes
58 STATE_CATEGORY, /* pattern and patches */
74 /* rpm-md dependencies inside the
92 STATE_CAPS_RECOMMENDS,
93 STATE_CAPS_SUPPLEMENTS,
100 STATE_OBSOLETESENTRY,
101 STATE_CONFLICTSENTRY,
102 STATE_RECOMMENDSENTRY,
103 STATE_SUPPLEMENTSENTRY,
114 STATE_CAP_RECOMMENDS,
115 STATE_CAP_SUPPLEMENTS,
131 static struct stateswitch stateswitches[] = {
133 { STATE_START, "product", STATE_SOLVABLE, 0 },
134 { STATE_START, "pattern", STATE_SOLVABLE, 0 },
135 { STATE_START, "patch", STATE_SOLVABLE, 0 },
137 { STATE_START, "metadata", STATE_METADATA, 0 },
138 { STATE_METADATA, "package", STATE_SOLVABLE, 0 },
140 { STATE_SOLVABLE, "name", STATE_NAME, 1 },
141 { STATE_SOLVABLE, "arch", STATE_ARCH, 1 },
142 { STATE_SOLVABLE, "version", STATE_VERSION, 0 },
144 // package attributes rpm-md
145 { STATE_SOLVABLE, "location", STATE_LOCATION, 0 },
146 { STATE_SOLVABLE, "checksum", STATE_CHECKSUM, 1 },
148 /* resobject attributes */
150 { STATE_SOLVABLE, "summary", STATE_SUMMARY, 1 },
151 { STATE_SOLVABLE, "description", STATE_DESCRIPTION, 1 },
152 //{ STATE_SOLVABLE, "???", STATE_INSNOTIFY, 1 },
153 //{ STATE_SOLVABLE, "??", STATE_DELNOTIFY, 1 },
154 { STATE_SOLVABLE, "vendor", STATE_VENDOR, 1 },
155 { STATE_SOLVABLE, "size", STATE_SIZE, 0 },
156 { STATE_SOLVABLE, "archive-size", STATE_DOWNLOADSIZE, 1 },
157 { STATE_SOLVABLE, "install-time", STATE_INSTALLTIME, 1 },
158 { STATE_SOLVABLE, "install-only", STATE_INSTALLONLY, 1 },
159 { STATE_SOLVABLE, "time", STATE_TIME, 0 },
161 // xml store pattern attributes
162 { STATE_SOLVABLE, "script", STATE_SCRIPT, 1 },
163 { STATE_SOLVABLE, "icon", STATE_ICON, 1 },
164 { STATE_SOLVABLE, "uservisible", STATE_USERVISIBLE, 1 },
165 { STATE_SOLVABLE, "category", STATE_CATEGORY, 1 },
166 { STATE_SOLVABLE, "default", STATE_DEFAULT, 1 },
167 { STATE_SOLVABLE, "install-time", STATE_INSTALL_TIME, 1 },
169 { STATE_SOLVABLE, "format", STATE_FORMAT, 0 },
171 /* those are used in libzypp xml store */
172 { STATE_SOLVABLE, "obsoletes", STATE_CAPS_OBSOLETES , 0 },
173 { STATE_SOLVABLE, "conflicts", STATE_CAPS_CONFLICTS , 0 },
174 { STATE_SOLVABLE, "recommends", STATE_CAPS_RECOMMENDS , 0 },
175 { STATE_SOLVABLE, "supplements", STATE_CAPS_SUPPLEMENTS, 0 },
176 { STATE_SOLVABLE, "suggests", STATE_CAPS_SUGGESTS, 0 },
177 { STATE_SOLVABLE, "enhances", STATE_CAPS_ENHANCES, 0 },
178 { STATE_SOLVABLE, "freshens", STATE_CAPS_FRESHENS, 0 },
179 { STATE_SOLVABLE, "provides", STATE_CAPS_PROVIDES, 0 },
180 { STATE_SOLVABLE, "requires", STATE_CAPS_REQUIRES, 0 },
182 { STATE_CAPS_PROVIDES, "capability", STATE_CAP_PROVIDES, 1 },
183 { STATE_CAPS_REQUIRES, "capability", STATE_CAP_REQUIRES, 1 },
184 { STATE_CAPS_OBSOLETES, "capability", STATE_CAP_OBSOLETES, 1 },
185 { STATE_CAPS_CONFLICTS, "capability", STATE_CAP_CONFLICTS, 1 },
186 { STATE_CAPS_RECOMMENDS, "capability", STATE_CAP_RECOMMENDS, 1 },
187 { STATE_CAPS_SUPPLEMENTS, "capability", STATE_CAP_SUPPLEMENTS, 1 },
188 { STATE_CAPS_SUGGESTS, "capability", STATE_CAP_SUGGESTS, 1 },
189 { STATE_CAPS_ENHANCES, "capability", STATE_CAP_ENHANCES, 1 },
190 { STATE_CAPS_FRESHENS, "capability", STATE_CAP_FRESHENS, 1 },
192 { STATE_FORMAT, "rpm:vendor", STATE_VENDOR, 1 },
193 { STATE_FORMAT, "rpm:group", STATE_RPM_GROUP, 1 },
194 { STATE_FORMAT, "rpm:license", STATE_RPM_LICENSE, 1 },
196 /* rpm-md dependencies */
197 { STATE_FORMAT, "rpm:provides", STATE_PROVIDES, 0 },
198 { STATE_FORMAT, "rpm:requires", STATE_REQUIRES, 0 },
199 { STATE_FORMAT, "rpm:obsoletes", STATE_OBSOLETES , 0 },
200 { STATE_FORMAT, "rpm:conflicts", STATE_CONFLICTS , 0 },
201 { STATE_FORMAT, "rpm:recommends", STATE_RECOMMENDS , 0 },
202 { STATE_FORMAT, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
203 { STATE_FORMAT, "rpm:suggests", STATE_SUGGESTS, 0 },
204 { STATE_FORMAT, "rpm:enhances", STATE_ENHANCES, 0 },
205 { STATE_FORMAT, "rpm:freshens", STATE_FRESHENS, 0 },
206 { STATE_FORMAT, "file", STATE_FILE, 1 },
207 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
208 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
209 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
210 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
211 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
212 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
213 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
214 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
215 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
220 struct parsedata_common common;
231 struct stateswitch *swtab[NUMSTATES];
232 enum state sbtab[NUMSTATES];
235 // used to store tmp attributes
236 // while the tag ends
241 static char *flagtabnum[] = {
251 * adds plain dependencies, that is strings like "foo > 2.0"
252 * which are used in libzypp xml store, not in rpm-md.
255 adddepplain(Pool *pool, struct parsedata_common *pd, unsigned int olddeps, char *line, Id marker, const char *kind)
261 i = split(line, sp, 4);
262 if (i != 1 && i != 3)
264 fprintf(stderr, "Bad dependency line: %s\n", line);
268 id = str2id(pool, join2(kind, ":", sp[0]), 1);
270 id = str2id(pool, sp[0], 1);
273 evrid = makeevr(pool, sp[2]);
274 for (flags = 0; flags < 6; flags++)
275 if (!strcmp(sp[1], flagtabnum[flags]))
279 if ( !strcmp(sp[1], "=="))
285 fprintf(stderr, "Unknown relation '%s'\n", sp[1]);
289 id = rel2id(pool, id, evrid, flags + 1, 1);
291 return repo_addid_dep(pd->repo, olddeps, id, marker);
295 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
297 const char *e, *v, *r, *v2;
302 for (; *atts; atts += 2)
304 if (!strcmp(*atts, "epoch"))
306 else if (!strcmp(*atts, "ver"))
308 else if (!strcmp(*atts, "rel"))
311 if (e && !strcmp(e, "0"))
315 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
317 if (v2 > v && *v2 == ':')
327 if (l > pd->acontent)
329 pd->content = sat_realloc(pd->content, l + 256);
330 pd->acontent = l + 256;
354 fprintf(stderr, "evr: %s\n", pd->content);
356 return str2id(pool, pd->content, 1);
360 find_attr(const char *txt, const char **atts)
362 for (; *atts; atts += 2)
364 if (!strcmp(*atts, txt))
370 static char *flagtab[] = {
380 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
383 const char *n, *f, *k;
387 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
388 for (a = atts; *a; a += 2)
390 if (!strcmp(*a, "name"))
392 else if (!strcmp(*a, "flags"))
394 else if (!strcmp(*a, "kind"))
396 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
397 marker = SOLVABLE_PREREQMARKER;
401 if (k && !strcmp(k, "package"))
405 int l = strlen(k) + 1 + strlen(n) + 1;
406 if (l > pd->acontent)
408 pd->content = sat_realloc(pd->content, l + 256);
409 pd->acontent = l + 256;
411 sprintf(pd->content, "%s:%s", k, n);
412 name = str2id(pool, pd->content, 1);
415 name = str2id(pool, (char *)n, 1);
418 Id evr = makeevr_atts(pool, pd, atts);
420 for (flags = 0; flags < 6; flags++)
421 if (!strcmp(f, flagtab[flags]))
423 flags = flags < 6 ? flags + 1 : 0;
424 id = rel2id(pool, name, evr, flags, 1);
429 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
431 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
436 startElement(void *userData, const char *name, const char **atts)
438 //fprintf(stderr,"+tag: %s\n", name);
439 struct parsedata *pd = userData;
440 Pool *pool = pd->common.pool;
441 Repo *repo = pd->common.repo;
442 Solvable *s = pd->solvable;
443 struct stateswitch *sw;
445 Id solvid = s - pool->solvables;
447 if (pd->depth != pd->statedepth)
453 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
454 if (!strcmp(sw->ename, name))
456 if (sw->from != pd->state)
459 fprintf(stderr, "into unknown: %s\n", name);
464 pd->docontent = sw->docontent;
465 pd->statedepth = pd->depth;
471 for (; *atts; atts += 2)
473 if (!strcmp(*atts, "packages"))
475 pd->numpacks = atoi(atts[1]);
476 if (pd->numpacks < 0)
479 fprintf(stderr, "numpacks: %d\n", pd->numpacks);
481 pd->solvable = pool_id2solvable(pool, repo_add_solvable_block(pd->common.repo, pd->numpacks));
487 if (name[2] == 't' && name[3] == 't')
488 pd->kind = "pattern";
489 else if (name[1] == 'r')
490 pd->kind = "product";
491 else if (name[2] == 't' && name[3] == 'c')
494 if (pd->numpacks == 0)
495 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
497 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
501 s->evr = makeevr_atts(pool, pd, atts);
503 case STATE_CAPS_PROVIDES:
507 case STATE_PROVIDESENTRY:
508 s->provides = adddep(pool, pd, s->provides, atts, 0);
510 case STATE_CAPS_REQUIRES:
514 case STATE_REQUIRESENTRY:
515 s->requires = adddep(pool, pd, s->requires, atts, 1);
517 case STATE_CAPS_OBSOLETES:
518 case STATE_OBSOLETES:
521 case STATE_OBSOLETESENTRY:
522 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
524 case STATE_CAPS_CONFLICTS:
525 case STATE_CONFLICTS:
528 case STATE_CONFLICTSENTRY:
529 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
531 case STATE_CAPS_RECOMMENDS:
532 case STATE_RECOMMENDS:
535 case STATE_RECOMMENDSENTRY:
536 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
538 case STATE_CAPS_SUPPLEMENTS:
539 case STATE_SUPPLEMENTS:
542 case STATE_SUPPLEMENTSENTRY:
543 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
545 case STATE_CAPS_SUGGESTS:
549 case STATE_SUGGESTSENTRY:
550 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
552 case STATE_CAPS_ENHANCES:
556 case STATE_ENHANCESENTRY:
557 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
559 case STATE_CAPS_FRESHENS:
563 case STATE_FRESHENSENTRY:
564 s->freshens = adddep(pool, pd, s->freshens, atts, 0);
566 case STATE_CAP_PROVIDES:
567 case STATE_CAP_REQUIRES:
568 case STATE_CAP_OBSOLETES:
569 case STATE_CAP_CONFLICTS:
570 case STATE_CAP_RECOMMENDS:
571 case STATE_CAP_SUPPLEMENTS:
572 case STATE_CAP_SUGGESTS:
573 case STATE_CAP_ENHANCES:
574 case STATE_CAP_FRESHENS:
575 pd->capkind = find_attr("kind", atts);
576 //fprintf(stderr,"capkind es: %s\n", pd->capkind);
579 case STATE_DESCRIPTION:
580 pd->lang = find_attr("lang", atts);
583 str = find_attr("href", atts);
585 repo_set_str(repo, solvid, id_mediafile, str);
588 pd->tmpattr = find_attr("type", atts);
593 str = find_attr("build", atts);
594 if (str && (t = atoi(str)) != 0)
595 repo_set_num(repo, solvid, id_time, t);
601 str = find_attr("installed", atts);
602 if (str && (k = atoi(str)) != 0)
603 repo_set_num(repo, solvid, id_installsize, (k + 1023) / 1024);
604 /* XXX the "package" attribute gives the size of the rpm file,
605 i.e. the download size. Except on packman, there it seems to be
606 something else entirely, it has a value near to the other two
607 values, as if the rpm is uncompressed. */
608 str = find_attr("package", atts);
609 if (str && (k = atoi(str)) != 0)
610 repo_set_num(repo, solvid, id_downloadsize, (k + 1023) / 1024);
619 endElement(void *userData, const char *name)
621 //fprintf(stderr,"-tag: %s\n", name);
622 struct parsedata *pd = userData;
623 Pool *pool = pd->common.pool;
624 Solvable *s = pd->solvable;
625 Repo *repo = pd->common.repo;
628 if (pd->depth != pd->statedepth)
631 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
642 s->arch = ARCH_NOARCH;
643 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
644 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
645 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
646 if (pd->numpacks > 0)
655 s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
657 s->name = str2id(pool, pd->content, 1);
660 s->arch = str2id(pool, pd->content, 1);
663 s->vendor = str2id(pool, pd->content, 1);
665 case STATE_RPM_GROUP:
666 repo_set_poolstr(repo, s - pool->solvables, id_group, pd->content);
668 case STATE_RPM_LICENSE:
669 repo_set_poolstr(repo, s - pool->solvables, id_license, pd->content);
672 id = str2id(pool, pd->content, 1);
673 s->provides = repo_addid(repo, s->provides, id);
675 // xml store capabilities
676 case STATE_CAP_PROVIDES:
677 s->provides = adddepplain(pool, &pd->common, s->provides, pd->content, 0, pd->capkind);
679 case STATE_CAP_REQUIRES:
680 s->requires = adddepplain(pool, &pd->common, s->requires, pd->content, 0, pd->capkind);
682 case STATE_CAP_OBSOLETES:
683 s->obsoletes = adddepplain(pool, &pd->common, s->obsoletes, pd->content, 0, pd->capkind);
685 case STATE_CAP_CONFLICTS:
686 s->conflicts = adddepplain(pool, &pd->common, s->conflicts, pd->content, 0, pd->capkind);
688 case STATE_CAP_RECOMMENDS:
689 s->recommends = adddepplain(pool, &pd->common, s->recommends, pd->content, 0, pd->capkind);
691 case STATE_CAP_SUPPLEMENTS:
692 s->supplements = adddepplain(pool, &pd->common, s->supplements, pd->content, 0, pd->capkind);
694 case STATE_CAP_SUGGESTS:
695 s->suggests = adddepplain(pool, &pd->common, s->suggests, pd->content, 0, pd->capkind);
697 case STATE_CAP_ENHANCES:
698 s->enhances = adddepplain(pool, &pd->common, s->enhances, pd->content, 0, pd->capkind);
700 case STATE_CAP_FRESHENS:
701 s->freshens = adddepplain(pool, &pd->common, s->freshens, pd->content, 0, pd->capkind);
705 repo_set_str(repo, s - pool->solvables, id_summary, pd->content);
707 case STATE_DESCRIPTION:
709 repo_set_str(repo, s - pool->solvables, id_description, pd->content);
714 pd->state = pd->sbtab[pd->state];
716 //fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
720 characterData(void *userData, const XML_Char *s, int len)
722 struct parsedata *pd = userData;
728 l = pd->lcontent + len + 1;
729 if (l > pd->acontent)
731 pd->content = sat_realloc(pd->content, l + 256);
732 pd->acontent = l + 256;
734 c = pd->content + pd->lcontent;
742 #define BUFF_SIZE 8192
745 repo_add_rpmmd(Repo *repo, FILE *fp)
747 Pool *pool = repo->pool;
751 struct stateswitch *sw;
753 memset(&pd, 0, sizeof(pd));
754 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
756 if (!pd.swtab[sw->from])
757 pd.swtab[sw->from] = sw;
758 pd.sbtab[sw->to] = sw->from;
760 pd.common.pool = pool;
761 pd.common.repo = repo;
763 pd.data = repo_add_repodata(repo);
766 pd.content = sat_malloc(256);
772 XML_Parser parser = XML_ParserCreate(NULL);
773 XML_SetUserData(parser, &pd);
774 XML_SetElementHandler(parser, startElement, endElement);
775 XML_SetCharacterDataHandler(parser, characterData);
778 l = fread(buf, 1, sizeof(buf), fp);
779 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
781 fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
787 XML_ParserFree(parser);
790 repodata_internalize(pd.data);
793 repo_free_solvable_block(repo, pd.solvable - pool->solvables, pd.numpacks, 1);
794 sat_free(pd.content);