2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
19 #include "tools_util.h"
20 #include "repo_rpmmd.h"
40 /* resobject attributes */
60 // xml store pattern attributes
61 STATE_CATEGORY, /* pattern and patches */
75 /* rpm-md dependencies inside the
93 STATE_RECOMMENDSENTRY,
94 STATE_SUPPLEMENTSENTRY,
112 static struct stateswitch stateswitches[] = {
114 { STATE_START, "product", STATE_SOLVABLE, 0 },
115 { STATE_START, "pattern", STATE_SOLVABLE, 0 },
116 { STATE_START, "patch", STATE_SOLVABLE, 0 },
117 { STATE_START, "package", STATE_SOLVABLE, 0 },
119 { STATE_SOLVABLE, "name", STATE_NAME, 1 },
120 { STATE_SOLVABLE, "arch", STATE_ARCH, 1 },
121 { STATE_SOLVABLE, "version", STATE_VERSION, 0 },
123 // package attributes rpm-md
124 { STATE_SOLVABLE, "location", STATE_LOCATION, 0 },
125 { STATE_SOLVABLE, "checksum", STATE_CHECKSUM, 1 },
127 /* resobject attributes */
129 { STATE_SOLVABLE, "summary", STATE_SUMMARY, 1 },
130 { STATE_SOLVABLE, "description", STATE_DESCRIPTION, 1 },
131 { STATE_SOLVABLE, "url", STATE_URL, 1 },
132 { STATE_SOLVABLE, "packager", STATE_PACKAGER, 1 },
133 //{ STATE_SOLVABLE, "???", STATE_INSNOTIFY, 1 },
134 //{ STATE_SOLVABLE, "??", STATE_DELNOTIFY, 1 },
135 { STATE_SOLVABLE, "vendor", STATE_VENDOR, 1 },
136 { STATE_SOLVABLE, "size", STATE_SIZE, 0 },
137 { STATE_SOLVABLE, "archive-size", STATE_DOWNLOADSIZE, 1 },
138 { STATE_SOLVABLE, "install-time", STATE_INSTALLTIME, 1 },
139 { STATE_SOLVABLE, "install-only", STATE_INSTALLONLY, 1 },
140 { STATE_SOLVABLE, "time", STATE_TIME, 0 },
142 // xml store pattern attributes
143 { STATE_SOLVABLE, "script", STATE_SCRIPT, 1 },
144 { STATE_SOLVABLE, "icon", STATE_ICON, 1 },
145 { STATE_SOLVABLE, "uservisible", STATE_USERVISIBLE, 1 },
146 { STATE_SOLVABLE, "category", STATE_CATEGORY, 1 },
147 { STATE_SOLVABLE, "default", STATE_DEFAULT, 1 },
148 { STATE_SOLVABLE, "install-time", STATE_INSTALL_TIME, 1 },
150 { STATE_SOLVABLE, "rpm:vendor", STATE_VENDOR, 1 },
151 { STATE_SOLVABLE, "rpm:group", STATE_RPM_GROUP, 1 },
152 { STATE_SOLVABLE, "rpm:license", STATE_RPM_LICENSE, 1 },
154 /* rpm-md dependencies */
155 { STATE_SOLVABLE, "rpm:provides", STATE_PROVIDES, 0 },
156 { STATE_SOLVABLE, "rpm:requires", STATE_REQUIRES, 0 },
157 { STATE_SOLVABLE, "rpm:obsoletes", STATE_OBSOLETES , 0 },
158 { STATE_SOLVABLE, "rpm:conflicts", STATE_CONFLICTS , 0 },
159 { STATE_SOLVABLE, "rpm:recommends", STATE_RECOMMENDS , 0 },
160 { STATE_SOLVABLE, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
161 { STATE_SOLVABLE, "rpm:suggests", STATE_SUGGESTS, 0 },
162 { STATE_SOLVABLE, "rpm:enhances", STATE_ENHANCES, 0 },
163 { STATE_SOLVABLE, "rpm:freshens", STATE_FRESHENS, 0 },
164 { STATE_SOLVABLE, "rpm:sourcerpm", STATE_SOURCERPM, 1 },
165 { STATE_SOLVABLE, "rpm:header-range", STATE_HEADERRANGE, 0 },
166 { STATE_SOLVABLE, "file", STATE_FILE, 1 },
168 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
169 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
170 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
171 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
172 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
173 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
174 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
175 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
176 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
182 struct parsedata_common common;
193 struct stateswitch *swtab[NUMSTATES];
194 enum state sbtab[NUMSTATES];
197 // used to store tmp attributes
198 // while the tag ends
205 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
207 const char *e, *v, *r, *v2;
212 for (; *atts; atts += 2)
214 if (!strcmp(*atts, "epoch"))
216 else if (!strcmp(*atts, "ver"))
218 else if (!strcmp(*atts, "rel"))
221 if (e && !strcmp(e, "0"))
225 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
227 if (v2 > v && *v2 == ':')
237 if (l > pd->acontent)
239 pd->content = sat_realloc(pd->content, l + 256);
240 pd->acontent = l + 256;
264 fprintf(stderr, "evr: %s\n", pd->content);
266 return str2id(pool, pd->content, 1);
269 static inline const char *
270 find_attr(const char *txt, const char **atts)
272 for (; *atts; atts += 2)
274 if (!strcmp(*atts, txt))
280 static char *flagtab[] = {
290 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
293 const char *n, *f, *k;
297 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
298 for (a = atts; *a; a += 2)
300 if (!strcmp(*a, "name"))
302 else if (!strcmp(*a, "flags"))
304 else if (!strcmp(*a, "kind"))
306 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
307 marker = SOLVABLE_PREREQMARKER;
311 if (k && !strcmp(k, "package"))
315 int l = strlen(k) + 1 + strlen(n) + 1;
316 if (l > pd->acontent)
318 pd->content = sat_realloc(pd->content, l + 256);
319 pd->acontent = l + 256;
321 sprintf(pd->content, "%s:%s", k, n);
322 name = str2id(pool, pd->content, 1);
325 name = str2id(pool, (char *)n, 1);
328 Id evr = makeevr_atts(pool, pd, atts);
330 for (flags = 0; flags < 6; flags++)
331 if (!strcmp(f, flagtab[flags]))
333 flags = flags < 6 ? flags + 1 : 0;
334 id = rel2id(pool, name, evr, flags, 1);
339 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
341 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
345 set_desciption_author(Repodata *data, Id handle, char *str)
351 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
352 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
356 /* oh my, found SUSE special author section */
359 while (l > 0 && str[l - 1] == '\n')
362 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
364 aut = str; /* copy over */
365 while (*p == ' ' || *p == '\n')
378 while (aut != str && aut[-1] == '\n')
382 repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
385 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
389 set_sourcerpm(Repodata *data, Solvable *s, Id handle, char *sourcerpm)
391 const char *p, *sevr, *sarch, *name, *evr;
394 p = strrchr(sourcerpm, '.');
395 if (!p || strcmp(p, ".rpm") != 0)
398 while (p > sourcerpm && *p != '.')
400 if (*p != '.' || p == sourcerpm)
403 while (p > sourcerpm && *p != '-')
405 if (*p != '-' || p == sourcerpm)
408 while (p > sourcerpm && *p != '-')
410 if (*p != '-' || p == sourcerpm)
413 pool = s->repo->pool;
414 name = id2str(pool, s->name);
415 evr = id2str(pool, s->evr);
416 if (!strcmp(sarch, "src.rpm"))
417 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
418 else if (!strcmp(sarch, "nosrc.rpm"))
419 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
421 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
422 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
423 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
425 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
426 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm -
428 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
430 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
434 startElement(void *userData, const char *name, const char **atts)
436 //fprintf(stderr,"+tag: %s\n", name);
437 struct parsedata *pd = userData;
438 Pool *pool = pd->common.pool;
439 Solvable *s = pd->solvable;
440 struct stateswitch *sw;
442 Id handle = pd->handle;
444 // fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth);
446 if (pd->depth != pd->statedepth)
452 if (pd->state == STATE_START && !strcmp(name, "patterns"))
454 if (pd->state == STATE_START && !strcmp(name, "metadata"))
456 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
460 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
461 if (!strcmp(sw->ename, name))
463 if (sw->from != pd->state)
466 fprintf(stderr, "into unknown: %s\n", name);
471 pd->docontent = sw->docontent;
472 pd->statedepth = pd->depth;
479 if (name[2] == 't' && name[3] == 't')
480 pd->kind = "pattern";
481 else if (name[1] == 'r')
482 pd->kind = "product";
483 else if (name[2] == 't' && name[3] == 'c')
486 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
488 repodata_extend(pd->data, pd->solvable - pool->solvables);
489 pd->handle = repodata_get_handle(pd->data, (pd->solvable - pool->solvables) - pd->data->start);
491 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
495 s->evr = makeevr_atts(pool, pd, atts);
500 case STATE_PROVIDESENTRY:
501 s->provides = adddep(pool, pd, s->provides, atts, 0);
506 case STATE_REQUIRESENTRY:
507 s->requires = adddep(pool, pd, s->requires, atts, 1);
509 case STATE_OBSOLETES:
512 case STATE_OBSOLETESENTRY:
513 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
515 case STATE_CONFLICTS:
518 case STATE_CONFLICTSENTRY:
519 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
521 case STATE_RECOMMENDS:
524 case STATE_RECOMMENDSENTRY:
525 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
527 case STATE_SUPPLEMENTS:
530 case STATE_SUPPLEMENTSENTRY:
531 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
536 case STATE_SUGGESTSENTRY:
537 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
542 case STATE_ENHANCESENTRY:
543 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
548 case STATE_FRESHENSENTRY:
549 pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
552 case STATE_DESCRIPTION:
553 pd->lang = find_attr("lang", atts);
556 str = find_attr("href", atts);
559 const char *str2 = strrchr(str, '/');
562 char *str3 = strdup(str);
563 str3[str2 - str] = 0;
564 repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIADIR, str3);
566 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str2 + 1);
569 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str);
573 pd->tmpattr = find_attr("type", atts);
578 str = find_attr("build", atts);
579 if (str && (t = atoi(str)) != 0)
580 repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
586 str = find_attr("installed", atts);
587 if (str && (k = atoi(str)) != 0)
588 repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, (k + 1023) / 1024);
589 /* XXX the "package" attribute gives the size of the rpm file,
590 i.e. the download size. Except on packman, there it seems to be
591 something else entirely, it has a value near to the other two
592 values, as if the rpm is uncompressed. */
593 str = find_attr("package", atts);
594 if (str && (k = atoi(str)) != 0)
595 repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, (k + 1023) / 1024);
598 case STATE_HEADERRANGE:
601 str = find_attr("end", atts);
602 if (str && (end = atoi(str)) != 0)
603 repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
611 endElement(void *userData, const char *name)
613 //fprintf(stderr,"-tag: %s\n", name);
614 struct parsedata *pd = userData;
615 Pool *pool = pd->common.pool;
616 Solvable *s = pd->solvable;
617 Repo *repo = pd->common.repo;
618 Id handle = pd->handle;
622 if (pd->depth != pd->statedepth)
625 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
629 /* ignore patterns & metadata */
630 if (pd->state == STATE_START && !strcmp(name, "patterns"))
632 if (pd->state == STATE_START && !strcmp(name, "metadata"))
634 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
645 s->arch = ARCH_NOARCH;
647 s->evr = ID_EMPTY; /* some patterns have this */
648 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
649 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
650 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements, pd->freshens);
656 s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
658 s->name = str2id(pool, pd->content, 1);
661 s->arch = str2id(pool, pd->content, 1);
664 s->vendor = str2id(pool, pd->content, 1);
666 case STATE_RPM_GROUP:
667 repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
669 case STATE_RPM_LICENSE:
670 repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
674 id = str2id(pool, pd->content, 1);
675 s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
677 if ((p = strrchr(pd->content, '/')) != 0)
680 id = repodata_str2dir(pd->data, pd->content, 1);
688 id = repodata_str2dir(pd->data, "/", 1);
689 repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
693 repodata_set_str(pd->data, handle, SOLVABLE_SUMMARY, pd->content);
695 case STATE_DESCRIPTION:
697 set_desciption_author(pd->data, handle, pd->content);
701 repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
705 repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
707 case STATE_SOURCERPM:
708 set_sourcerpm(pd->data, s, handle, pd->content);
713 pd->state = pd->sbtab[pd->state];
715 // fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
719 characterData(void *userData, const XML_Char *s, int len)
721 struct parsedata *pd = userData;
727 l = pd->lcontent + len + 1;
728 if (l > pd->acontent)
730 pd->content = sat_realloc(pd->content, l + 256);
731 pd->acontent = l + 256;
733 c = pd->content + pd->lcontent;
741 #define BUFF_SIZE 8192
744 repo_add_rpmmd(Repo *repo, FILE *fp, int flags)
746 Pool *pool = repo->pool;
750 struct stateswitch *sw;
752 memset(&pd, 0, sizeof(pd));
753 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
755 if (!pd.swtab[sw->from])
756 pd.swtab[sw->from] = sw;
757 pd.sbtab[sw->to] = sw->from;
759 pd.common.pool = pool;
760 pd.common.repo = repo;
762 pd.data = repo_add_repodata(repo, 0);
764 pd.content = sat_malloc(256);
770 XML_Parser parser = XML_ParserCreate(NULL);
771 XML_SetUserData(parser, &pd);
772 XML_SetElementHandler(parser, startElement, endElement);
773 XML_SetCharacterDataHandler(parser, characterData);
776 l = fread(buf, 1, sizeof(buf), fp);
777 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
779 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));
785 XML_ParserFree(parser);
788 repodata_internalize(pd.data);
789 sat_free(pd.content);