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;
230 struct stateswitch *swtab[NUMSTATES];
231 enum state sbtab[NUMSTATES];
234 // used to store tmp attributes
235 // while the tag ends
240 static char *flagtabnum[] = {
250 * adds plain dependencies, that is strings like "foo > 2.0"
251 * which are used in libzypp xml store, not in rpm-md.
254 adddepplain(Pool *pool, struct parsedata_common *pd, unsigned int olddeps, char *line, Id marker, const char *kind)
260 i = split(line, sp, 4);
261 if (i != 1 && i != 3)
263 fprintf(stderr, "Bad dependency line: %s\n", line);
267 id = str2id(pool, join2(kind, ":", sp[0]), 1);
269 id = str2id(pool, sp[0], 1);
272 evrid = makeevr(pool, sp[2]);
273 for (flags = 0; flags < 6; flags++)
274 if (!strcmp(sp[1], flagtabnum[flags]))
278 if ( !strcmp(sp[1], "=="))
284 fprintf(stderr, "Unknown relation '%s'\n", sp[1]);
288 id = rel2id(pool, id, evrid, flags + 1, 1);
290 return repo_addid_dep(pd->repo, olddeps, id, marker);
294 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
296 const char *e, *v, *r, *v2;
301 for (; *atts; atts += 2)
303 if (!strcmp(*atts, "epoch"))
305 else if (!strcmp(*atts, "ver"))
307 else if (!strcmp(*atts, "rel"))
310 if (e && !strcmp(e, "0"))
314 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
316 if (v2 > v && *v2 == ':')
326 if (l > pd->acontent)
328 pd->content = sat_realloc(pd->content, l + 256);
329 pd->acontent = l + 256;
353 fprintf(stderr, "evr: %s\n", pd->content);
355 return str2id(pool, pd->content, 1);
359 find_attr(const char *txt, const char **atts)
361 for (; *atts; atts += 2)
363 if (!strcmp(*atts, txt))
369 static char *flagtab[] = {
379 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
382 const char *n, *f, *k;
386 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
387 for (a = atts; *a; a += 2)
389 if (!strcmp(*a, "name"))
391 else if (!strcmp(*a, "flags"))
393 else if (!strcmp(*a, "kind"))
395 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
396 marker = SOLVABLE_PREREQMARKER;
400 if (k && !strcmp(k, "package"))
404 int l = strlen(k) + 1 + strlen(n) + 1;
405 if (l > pd->acontent)
407 pd->content = sat_realloc(pd->content, l + 256);
408 pd->acontent = l + 256;
410 sprintf(pd->content, "%s:%s", k, n);
411 name = str2id(pool, pd->content, 1);
414 name = str2id(pool, (char *)n, 1);
417 Id evr = makeevr_atts(pool, pd, atts);
419 for (flags = 0; flags < 6; flags++)
420 if (!strcmp(f, flagtab[flags]))
422 flags = flags < 6 ? flags + 1 : 0;
423 id = rel2id(pool, name, evr, flags, 1);
428 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
430 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
434 set_desciption_author(Repodata *data, Id entry, char *str)
440 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
441 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
445 /* oh my, found SUSE special author section */
448 while (l > 0 && str[l - 1] == '\n')
451 repodata_set_str(data, entry, id_description, str);
453 aut = str; /* copy over */
454 while (*p == ' ' || *p == '\n')
467 while (aut != str && aut[-1] == '\n')
471 repodata_set_str(data, entry, id_authors, str);
474 repodata_set_str(data, entry, id_description, str);
478 startElement(void *userData, const char *name, const char **atts)
480 //fprintf(stderr,"+tag: %s\n", name);
481 struct parsedata *pd = userData;
482 Pool *pool = pd->common.pool;
483 Solvable *s = pd->solvable;
484 struct stateswitch *sw;
486 Id entry = s ? (s - pool->solvables) - pd->data->start : 0;
488 if (pd->depth != pd->statedepth)
494 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
495 if (!strcmp(sw->ename, name))
497 if (sw->from != pd->state)
500 fprintf(stderr, "into unknown: %s\n", name);
505 pd->docontent = sw->docontent;
506 pd->statedepth = pd->depth;
513 if (name[2] == 't' && name[3] == 't')
514 pd->kind = "pattern";
515 else if (name[1] == 'r')
516 pd->kind = "product";
517 else if (name[2] == 't' && name[3] == 'c')
520 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
521 repodata_extend(pd->data, pd->solvable - pool->solvables);
523 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
527 s->evr = makeevr_atts(pool, pd, atts);
529 case STATE_CAPS_PROVIDES:
533 case STATE_PROVIDESENTRY:
534 s->provides = adddep(pool, pd, s->provides, atts, 0);
536 case STATE_CAPS_REQUIRES:
540 case STATE_REQUIRESENTRY:
541 s->requires = adddep(pool, pd, s->requires, atts, 1);
543 case STATE_CAPS_OBSOLETES:
544 case STATE_OBSOLETES:
547 case STATE_OBSOLETESENTRY:
548 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
550 case STATE_CAPS_CONFLICTS:
551 case STATE_CONFLICTS:
554 case STATE_CONFLICTSENTRY:
555 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
557 case STATE_CAPS_RECOMMENDS:
558 case STATE_RECOMMENDS:
561 case STATE_RECOMMENDSENTRY:
562 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
564 case STATE_CAPS_SUPPLEMENTS:
565 case STATE_SUPPLEMENTS:
568 case STATE_SUPPLEMENTSENTRY:
569 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
571 case STATE_CAPS_SUGGESTS:
575 case STATE_SUGGESTSENTRY:
576 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
578 case STATE_CAPS_ENHANCES:
582 case STATE_ENHANCESENTRY:
583 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
585 case STATE_CAPS_FRESHENS:
589 case STATE_FRESHENSENTRY:
590 s->freshens = adddep(pool, pd, s->freshens, atts, 0);
592 case STATE_CAP_PROVIDES:
593 case STATE_CAP_REQUIRES:
594 case STATE_CAP_OBSOLETES:
595 case STATE_CAP_CONFLICTS:
596 case STATE_CAP_RECOMMENDS:
597 case STATE_CAP_SUPPLEMENTS:
598 case STATE_CAP_SUGGESTS:
599 case STATE_CAP_ENHANCES:
600 case STATE_CAP_FRESHENS:
601 pd->capkind = find_attr("kind", atts);
602 //fprintf(stderr,"capkind es: %s\n", pd->capkind);
605 case STATE_DESCRIPTION:
606 pd->lang = find_attr("lang", atts);
609 str = find_attr("href", atts);
611 repodata_set_str(pd->data, entry, id_mediafile, str);
614 pd->tmpattr = find_attr("type", atts);
619 str = find_attr("build", atts);
620 if (str && (t = atoi(str)) != 0)
621 repodata_set_num(pd->data, entry, id_time, t);
627 str = find_attr("installed", atts);
628 if (str && (k = atoi(str)) != 0)
629 repodata_set_num(pd->data, entry, id_installsize, (k + 1023) / 1024);
630 /* XXX the "package" attribute gives the size of the rpm file,
631 i.e. the download size. Except on packman, there it seems to be
632 something else entirely, it has a value near to the other two
633 values, as if the rpm is uncompressed. */
634 str = find_attr("package", atts);
635 if (str && (k = atoi(str)) != 0)
636 repodata_set_num(pd->data, entry, id_downloadsize, (k + 1023) / 1024);
645 endElement(void *userData, const char *name)
647 //fprintf(stderr,"-tag: %s\n", name);
648 struct parsedata *pd = userData;
649 Pool *pool = pd->common.pool;
650 Solvable *s = pd->solvable;
651 Repo *repo = pd->common.repo;
652 Id entry = s ? (s - pool->solvables) - pd->data->start : 0;
656 if (pd->depth != pd->statedepth)
659 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
670 s->arch = ARCH_NOARCH;
671 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
672 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
673 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements);
678 s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
680 s->name = str2id(pool, pd->content, 1);
683 s->arch = str2id(pool, pd->content, 1);
686 s->vendor = str2id(pool, pd->content, 1);
688 case STATE_RPM_GROUP:
689 repodata_set_poolstr(pd->data, entry, id_group, pd->content);
691 case STATE_RPM_LICENSE:
692 repodata_set_poolstr(pd->data, entry, id_license, pd->content);
696 id = str2id(pool, pd->content, 1);
697 s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
699 if ((p = strrchr(pd->content, '/')) != 0) {
701 id = repodata_str2dir(pd->data, pd->content, 1);
706 repodata_add_dirstr(pd->data, entry, id_filelist, id, p);
708 // xml store capabilities
709 case STATE_CAP_PROVIDES:
710 s->provides = adddepplain(pool, &pd->common, s->provides, pd->content, 0, pd->capkind);
712 case STATE_CAP_REQUIRES:
713 s->requires = adddepplain(pool, &pd->common, s->requires, pd->content, 0, pd->capkind);
715 case STATE_CAP_OBSOLETES:
716 s->obsoletes = adddepplain(pool, &pd->common, s->obsoletes, pd->content, 0, pd->capkind);
718 case STATE_CAP_CONFLICTS:
719 s->conflicts = adddepplain(pool, &pd->common, s->conflicts, pd->content, 0, pd->capkind);
721 case STATE_CAP_RECOMMENDS:
722 s->recommends = adddepplain(pool, &pd->common, s->recommends, pd->content, 0, pd->capkind);
724 case STATE_CAP_SUPPLEMENTS:
725 s->supplements = adddepplain(pool, &pd->common, s->supplements, pd->content, 0, pd->capkind);
727 case STATE_CAP_SUGGESTS:
728 s->suggests = adddepplain(pool, &pd->common, s->suggests, pd->content, 0, pd->capkind);
730 case STATE_CAP_ENHANCES:
731 s->enhances = adddepplain(pool, &pd->common, s->enhances, pd->content, 0, pd->capkind);
733 case STATE_CAP_FRESHENS:
734 s->freshens = adddepplain(pool, &pd->common, s->freshens, pd->content, 0, pd->capkind);
738 repodata_set_str(pd->data, entry, id_summary, pd->content);
740 case STATE_DESCRIPTION:
742 set_desciption_author(pd->data, entry, pd->content);
747 pd->state = pd->sbtab[pd->state];
749 //fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
753 characterData(void *userData, const XML_Char *s, int len)
755 struct parsedata *pd = userData;
761 l = pd->lcontent + len + 1;
762 if (l > pd->acontent)
764 pd->content = sat_realloc(pd->content, l + 256);
765 pd->acontent = l + 256;
767 c = pd->content + pd->lcontent;
775 #define BUFF_SIZE 8192
778 repo_add_rpmmd(Repo *repo, FILE *fp, int flags)
780 Pool *pool = repo->pool;
784 struct stateswitch *sw;
786 memset(&pd, 0, sizeof(pd));
787 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
789 if (!pd.swtab[sw->from])
790 pd.swtab[sw->from] = sw;
791 pd.sbtab[sw->to] = sw->from;
793 pd.common.pool = pool;
794 pd.common.repo = repo;
796 pd.data = repo_add_repodata(repo);
799 pd.content = sat_malloc(256);
805 XML_Parser parser = XML_ParserCreate(NULL);
806 XML_SetUserData(parser, &pd);
807 XML_SetElementHandler(parser, startElement, endElement);
808 XML_SetCharacterDataHandler(parser, characterData);
811 l = fread(buf, 1, sizeof(buf), fp);
812 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
814 fprintf(stderr, "repo_rpmmd: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
820 XML_ParserFree(parser);
823 repodata_internalize(pd.data);
824 sat_free(pd.content);