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 },
183 struct parsedata_common common;
194 struct stateswitch *swtab[NUMSTATES];
195 enum state sbtab[NUMSTATES];
198 // used to store tmp attributes
199 // while the tag ends
204 Id (*dirs)[3]; // dirid, size, nfiles
206 Id langcache[ID_NUM_INTERNAL];
211 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
213 const char *e, *v, *r, *v2;
218 for (; *atts; atts += 2)
220 if (!strcmp(*atts, "epoch"))
222 else if (!strcmp(*atts, "ver"))
224 else if (!strcmp(*atts, "rel"))
227 if (e && !strcmp(e, "0"))
231 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
233 if (v2 > v && *v2 == ':')
243 if (l > pd->acontent)
245 pd->content = sat_realloc(pd->content, l + 256);
246 pd->acontent = l + 256;
270 fprintf(stderr, "evr: %s\n", pd->content);
272 return str2id(pool, pd->content, 1);
275 static inline const char *
276 find_attr(const char *txt, const char **atts)
278 for (; *atts; atts += 2)
280 if (!strcmp(*atts, txt))
286 static char *flagtab[] = {
296 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
299 const char *n, *f, *k;
303 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
304 for (a = atts; *a; a += 2)
306 if (!strcmp(*a, "name"))
308 else if (!strcmp(*a, "flags"))
310 else if (!strcmp(*a, "kind"))
312 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
313 marker = SOLVABLE_PREREQMARKER;
317 if (k && !strcmp(k, "package"))
321 int l = strlen(k) + 1 + strlen(n) + 1;
322 if (l > pd->acontent)
324 pd->content = sat_realloc(pd->content, l + 256);
325 pd->acontent = l + 256;
327 sprintf(pd->content, "%s:%s", k, n);
328 name = str2id(pool, pd->content, 1);
331 name = str2id(pool, (char *)n, 1);
334 Id evr = makeevr_atts(pool, pd, atts);
336 for (flags = 0; flags < 6; flags++)
337 if (!strcmp(f, flagtab[flags]))
339 flags = flags < 6 ? flags + 1 : 0;
340 id = rel2id(pool, name, evr, flags, 1);
345 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
347 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
351 set_desciption_author(Repodata *data, Id handle, char *str)
357 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
358 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
362 /* oh my, found SUSE special author section */
365 while (l > 0 && str[l - 1] == '\n')
368 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
370 aut = str; /* copy over */
371 while (*p == ' ' || *p == '\n')
384 while (aut != str && aut[-1] == '\n')
388 repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
391 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
395 set_sourcerpm(Repodata *data, Solvable *s, Id handle, char *sourcerpm)
397 const char *p, *sevr, *sarch, *name, *evr;
400 p = strrchr(sourcerpm, '.');
401 if (!p || strcmp(p, ".rpm") != 0)
404 while (p > sourcerpm && *p != '.')
406 if (*p != '.' || p == sourcerpm)
409 while (p > sourcerpm && *p != '-')
411 if (*p != '-' || p == sourcerpm)
414 while (p > sourcerpm && *p != '-')
416 if (*p != '-' || p == sourcerpm)
419 pool = s->repo->pool;
420 name = id2str(pool, s->name);
421 evr = id2str(pool, s->evr);
422 if (!strcmp(sarch, "src.rpm"))
423 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
424 else if (!strcmp(sarch, "nosrc.rpm"))
425 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
427 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
428 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
429 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
431 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
432 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm -
434 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
436 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
440 startElement(void *userData, const char *name, const char **atts)
442 //fprintf(stderr,"+tag: %s\n", name);
443 struct parsedata *pd = userData;
444 Pool *pool = pd->common.pool;
445 Solvable *s = pd->solvable;
446 struct stateswitch *sw;
448 Id handle = pd->handle;
450 // fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth);
452 if (pd->depth != pd->statedepth)
458 if (pd->state == STATE_START && !strcmp(name, "patterns"))
460 if (pd->state == STATE_START && !strcmp(name, "metadata"))
462 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
466 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
467 if (!strcmp(sw->ename, name))
469 if (sw->from != pd->state)
472 fprintf(stderr, "into unknown: %s\n", name);
477 pd->docontent = sw->docontent;
478 pd->statedepth = pd->depth;
485 if (name[2] == 't' && name[3] == 't')
486 pd->kind = "pattern";
487 else if (name[1] == 'r')
488 pd->kind = "product";
489 else if (name[2] == 't' && name[3] == 'c')
493 * now we have two cases, one is that we are in the package tag of a new package
494 * and the other is that we are in the tag of an extension file like other.xml or
496 * We identify it by looking the attributes
507 * <package pkgid="b78f8664cd90efe42e09a345e272997ef1b53c18"
508 * name="zaptel-kmp-default"
511 * ver="1.2.10_2.6.22_rc4_git6_2"
515 /* first check if there is a pkgid attribute */
516 const char *pkgid = find_attr( "pkgid", atts);
520 /* this is a new package */
521 /*fprintf(stderr, "new package\n");*/
522 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
524 repodata_extend(pd->data, pd->solvable - pool->solvables);
528 /* we need to look for the package based on the pkgid */
530 dataiterator_init(&di, pd->common.repo, -1, SOLVABLE_CHECKSUM, pkgid, SEARCH_STRING);
531 if (dataiterator_step(&di))
534 /*fprintf(stderr, "package found '%i'\n", di.solvid);*/
535 pd->solvable = pool_id2solvable(pool, di.solvid);
539 /* this is bad, here we should ignore the whole solvable */
540 fprintf(stderr, "can't find package with checksum '%s'\n", pkgid);
545 pd->handle = repodata_get_handle(pd->data, (pd->solvable - pool->solvables) - pd->data->start);
547 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
551 s->evr = makeevr_atts(pool, pd, atts);
556 case STATE_PROVIDESENTRY:
557 s->provides = adddep(pool, pd, s->provides, atts, 0);
562 case STATE_REQUIRESENTRY:
563 s->requires = adddep(pool, pd, s->requires, atts, 1);
565 case STATE_OBSOLETES:
568 case STATE_OBSOLETESENTRY:
569 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
571 case STATE_CONFLICTS:
574 case STATE_CONFLICTSENTRY:
575 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
577 case STATE_RECOMMENDS:
580 case STATE_RECOMMENDSENTRY:
581 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
583 case STATE_SUPPLEMENTS:
586 case STATE_SUPPLEMENTSENTRY:
587 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
592 case STATE_SUGGESTSENTRY:
593 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
598 case STATE_ENHANCESENTRY:
599 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
604 case STATE_FRESHENSENTRY:
605 pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
608 case STATE_DESCRIPTION:
609 pd->lang = find_attr("lang", atts);
612 str = find_attr("href", atts);
615 const char *str2 = strrchr(str, '/');
618 char *str3 = strdup(str);
619 str3[str2 - str] = 0;
620 repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIADIR, str3);
622 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str2 + 1);
625 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str);
629 pd->tmpattr = find_attr("type", atts);
634 str = find_attr("build", atts);
635 if (str && (t = atoi(str)) != 0)
636 repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
642 str = find_attr("installed", atts);
643 if (str && (k = atoi(str)) != 0)
644 repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, (k + 1023) / 1024);
645 /* XXX the "package" attribute gives the size of the rpm file,
646 i.e. the download size. Except on packman, there it seems to be
647 something else entirely, it has a value near to the other two
648 values, as if the rpm is uncompressed. */
649 str = find_attr("package", atts);
650 if (str && (k = atoi(str)) != 0)
651 repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, (k + 1023) / 1024);
654 case STATE_HEADERRANGE:
657 str = find_attr("end", atts);
658 if (str && (end = atoi(str)) != 0)
659 repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
667 endElement(void *userData, const char *name)
669 //fprintf(stderr,"-tag: %s\n", name);
670 struct parsedata *pd = userData;
671 Pool *pool = pd->common.pool;
672 Solvable *s = pd->solvable;
673 Repo *repo = pd->common.repo;
674 Id handle = pd->handle;
678 if (pd->depth != pd->statedepth)
681 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
685 /* ignore patterns & metadata */
686 if (pd->state == STATE_START && !strcmp(name, "patterns"))
688 if (pd->state == STATE_START && !strcmp(name, "metadata"))
690 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
701 s->arch = ARCH_NOARCH;
703 s->evr = ID_EMPTY; /* some patterns have this */
704 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
705 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
706 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements, pd->freshens);
712 s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
714 s->name = str2id(pool, pd->content, 1);
717 s->arch = str2id(pool, pd->content, 1);
720 s->vendor = str2id(pool, pd->content, 1);
722 case STATE_RPM_GROUP:
723 repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
725 case STATE_RPM_LICENSE:
726 repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
732 if (!strcasecmp (pd->tmpattr, "sha") || !strcasecmp (pd->tmpattr, "sha1"))
733 l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1;
734 else if (!strcasecmp (pd->tmpattr, "md5"))
735 l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5;
738 fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
741 if (strlen(pd->content) != l)
743 fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
746 repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, type, pd->content);
751 id = str2id(pool, pd->content, 1);
752 s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
754 if ((p = strrchr(pd->content, '/')) != 0)
757 id = repodata_str2dir(pd->data, pd->content, 1);
765 id = repodata_str2dir(pd->data, "/", 1);
766 repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
770 repodata_set_str(pd->data, handle, SOLVABLE_SUMMARY, pd->content);
772 case STATE_DESCRIPTION:
774 set_desciption_author(pd->data, handle, pd->content);
778 repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
782 repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
784 case STATE_SOURCERPM:
785 set_sourcerpm(pd->data, s, handle, pd->content);
790 pd->state = pd->sbtab[pd->state];
792 // fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
796 characterData(void *userData, const XML_Char *s, int len)
798 struct parsedata *pd = userData;
804 l = pd->lcontent + len + 1;
805 if (l > pd->acontent)
807 pd->content = sat_realloc(pd->content, l + 256);
808 pd->acontent = l + 256;
810 c = pd->content + pd->lcontent;
818 #define BUFF_SIZE 8192
821 repo_add_rpmmd(Repo *repo, FILE *fp, int flags)
823 Pool *pool = repo->pool;
827 struct stateswitch *sw;
829 memset(&pd, 0, sizeof(pd));
830 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
832 if (!pd.swtab[sw->from])
833 pd.swtab[sw->from] = sw;
834 pd.sbtab[sw->to] = sw->from;
836 pd.common.pool = pool;
837 pd.common.repo = repo;
839 pd.data = repo_add_repodata(repo, 0);
841 pd.content = sat_malloc(256);
847 XML_Parser parser = XML_ParserCreate(NULL);
848 XML_SetUserData(parser, &pd);
850 XML_SetElementHandler(parser, startElement, endElement);
851 XML_SetCharacterDataHandler(parser, characterData);
854 l = fread(buf, 1, sizeof(buf), fp);
855 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
857 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));
863 XML_ParserFree(parser);
866 repodata_internalize(pd.data);
867 sat_free(pd.content);