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 */
62 STATE_CATEGORY, /* pattern and patches */
71 STATE_DISTNAME, // obsolete
72 STATE_DISTEDITION, // obsolete
82 /* rpm-md dependencies inside the
100 STATE_RECOMMENDSENTRY,
101 STATE_SUPPLEMENTSENTRY,
119 static struct stateswitch stateswitches[] = {
121 { STATE_START, "product", STATE_SOLVABLE, 0 },
122 { STATE_START, "pattern", STATE_SOLVABLE, 0 },
123 { STATE_START, "patch", STATE_SOLVABLE, 0 },
124 { STATE_START, "package", STATE_SOLVABLE, 0 },
126 { STATE_SOLVABLE, "name", STATE_NAME, 1 },
127 { STATE_SOLVABLE, "arch", STATE_ARCH, 1 },
128 { STATE_SOLVABLE, "version", STATE_VERSION, 0 },
130 // package attributes rpm-md
131 { STATE_SOLVABLE, "location", STATE_LOCATION, 0 },
132 { STATE_SOLVABLE, "checksum", STATE_CHECKSUM, 1 },
134 /* resobject attributes */
136 { STATE_SOLVABLE, "summary", STATE_SUMMARY, 1 },
137 { STATE_SOLVABLE, "description", STATE_DESCRIPTION, 1 },
138 { STATE_SOLVABLE, "distribution", STATE_DISTRIBUTION, 1 },
139 { STATE_SOLVABLE, "url", STATE_URL, 1 },
140 { STATE_SOLVABLE, "packager", STATE_PACKAGER, 1 },
141 //{ STATE_SOLVABLE, "???", STATE_INSNOTIFY, 1 },
142 //{ STATE_SOLVABLE, "??", STATE_DELNOTIFY, 1 },
143 { STATE_SOLVABLE, "vendor", STATE_VENDOR, 1 },
144 { STATE_SOLVABLE, "size", STATE_SIZE, 0 },
145 { STATE_SOLVABLE, "archive-size", STATE_DOWNLOADSIZE, 1 },
146 { STATE_SOLVABLE, "install-time", STATE_INSTALLTIME, 1 },
147 { STATE_SOLVABLE, "install-only", STATE_INSTALLONLY, 1 },
148 { STATE_SOLVABLE, "time", STATE_TIME, 0 },
151 { STATE_SOLVABLE, "script", STATE_SCRIPT, 1 },
152 { STATE_SOLVABLE, "icon", STATE_ICON, 1 },
153 { STATE_SOLVABLE, "uservisible", STATE_USERVISIBLE, 1 },
154 { STATE_SOLVABLE, "category", STATE_CATEGORY, 1 },
155 { STATE_SOLVABLE, "default", STATE_DEFAULT, 1 },
156 { STATE_SOLVABLE, "install-time", STATE_INSTALL_TIME, 1 },
158 /* product attributes */
159 /* note the product type is an attribute */
160 { STATE_SOLVABLE, "release-notes-url", STATE_RELNOTESURL, 1 },
161 { STATE_SOLVABLE, "update-url", STATE_UPDATEURL, 1 },
162 { STATE_SOLVABLE, "optional-url", STATE_OPTIONALURL, 1 },
163 { STATE_SOLVABLE, "flag", STATE_FLAG, 1 },
164 { STATE_SOLVABLE, "flavor", STATE_FLAVOR, 1 },
165 { STATE_SOLVABLE, "references", STATE_REFERENCES, 1 },
167 { STATE_SOLVABLE, "rpm:vendor", STATE_VENDOR, 1 },
168 { STATE_SOLVABLE, "rpm:group", STATE_RPM_GROUP, 1 },
169 { STATE_SOLVABLE, "rpm:license", STATE_RPM_LICENSE, 1 },
171 /* rpm-md dependencies */
172 { STATE_SOLVABLE, "rpm:provides", STATE_PROVIDES, 0 },
173 { STATE_SOLVABLE, "rpm:requires", STATE_REQUIRES, 0 },
174 { STATE_SOLVABLE, "rpm:obsoletes", STATE_OBSOLETES, 0 },
175 { STATE_SOLVABLE, "rpm:conflicts", STATE_CONFLICTS, 0 },
176 { STATE_SOLVABLE, "rpm:recommends", STATE_RECOMMENDS , 0 },
177 { STATE_SOLVABLE, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
178 { STATE_SOLVABLE, "rpm:suggests", STATE_SUGGESTS, 0 },
179 { STATE_SOLVABLE, "rpm:enhances", STATE_ENHANCES, 0 },
180 { STATE_SOLVABLE, "rpm:freshens", STATE_FRESHENS, 0 },
181 { STATE_SOLVABLE, "rpm:sourcerpm", STATE_SOURCERPM, 1 },
182 { STATE_SOLVABLE, "rpm:header-range", STATE_HEADERRANGE, 0 },
183 { STATE_SOLVABLE, "file", STATE_FILE, 1 },
185 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
186 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
187 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
188 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
189 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
190 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
191 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
192 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
193 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
200 struct parsedata_common common;
211 struct stateswitch *swtab[NUMSTATES];
212 enum state sbtab[NUMSTATES];
215 // used to store tmp attributes
216 // while the tag ends
221 Id (*dirs)[3]; // dirid, size, nfiles
223 Id langcache[ID_NUM_INTERNAL];
228 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
230 const char *e, *v, *r, *v2;
235 for (; *atts; atts += 2)
237 if (!strcmp(*atts, "epoch"))
239 else if (!strcmp(*atts, "ver"))
241 else if (!strcmp(*atts, "rel"))
244 if (e && !strcmp(e, "0"))
248 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
250 if (v2 > v && *v2 == ':')
260 if (l > pd->acontent)
262 pd->content = sat_realloc(pd->content, l + 256);
263 pd->acontent = l + 256;
287 fprintf(stderr, "evr: %s\n", pd->content);
289 return str2id(pool, pd->content, 1);
292 static inline const char *
293 find_attr(const char *txt, const char **atts)
295 for (; *atts; atts += 2)
297 if (!strcmp(*atts, txt))
303 static char *flagtab[] = {
313 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
316 const char *n, *f, *k;
320 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
321 for (a = atts; *a; a += 2)
323 if (!strcmp(*a, "name"))
325 else if (!strcmp(*a, "flags"))
327 else if (!strcmp(*a, "kind"))
329 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
330 marker = SOLVABLE_PREREQMARKER;
334 if (k && !strcmp(k, "package"))
338 int l = strlen(k) + 1 + strlen(n) + 1;
339 if (l > pd->acontent)
341 pd->content = sat_realloc(pd->content, l + 256);
342 pd->acontent = l + 256;
344 sprintf(pd->content, "%s:%s", k, n);
345 name = str2id(pool, pd->content, 1);
348 name = str2id(pool, (char *)n, 1);
351 Id evr = makeevr_atts(pool, pd, atts);
353 for (flags = 0; flags < 6; flags++)
354 if (!strcmp(f, flagtab[flags]))
356 flags = flags < 6 ? flags + 1 : 0;
357 id = rel2id(pool, name, evr, flags, 1);
362 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
364 return repo_addid_dep(pd->common.repo, olddeps, id, marker);
368 set_desciption_author(Repodata *data, Id handle, char *str)
374 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
375 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
379 /* oh my, found SUSE special author section */
382 while (l > 0 && str[l - 1] == '\n')
385 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
387 aut = str; /* copy over */
388 while (*p == ' ' || *p == '\n')
401 while (aut != str && aut[-1] == '\n')
405 repodata_set_str(data, handle, SOLVABLE_AUTHORS, str);
408 repodata_set_str(data, handle, SOLVABLE_DESCRIPTION, str);
412 set_sourcerpm(Repodata *data, Solvable *s, Id handle, char *sourcerpm)
414 const char *p, *sevr, *sarch, *name, *evr;
417 p = strrchr(sourcerpm, '.');
418 if (!p || strcmp(p, ".rpm") != 0)
421 while (p > sourcerpm && *p != '.')
423 if (*p != '.' || p == sourcerpm)
426 while (p > sourcerpm && *p != '-')
428 if (*p != '-' || p == sourcerpm)
431 while (p > sourcerpm && *p != '-')
433 if (*p != '-' || p == sourcerpm)
436 pool = s->repo->pool;
437 name = id2str(pool, s->name);
438 evr = id2str(pool, s->evr);
439 if (!strcmp(sarch, "src.rpm"))
440 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
441 else if (!strcmp(sarch, "nosrc.rpm"))
442 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
444 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, strn2id(pool, sarch, strlen(sarch) - 4, 1));
445 if (!strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
446 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
448 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, strn2id(pool, sevr, sarch - sevr - 1, 1));
449 if (!strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm -
451 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
453 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
457 startElement(void *userData, const char *name, const char **atts)
459 //fprintf(stderr,"+tag: %s\n", name);
460 struct parsedata *pd = userData;
461 Pool *pool = pd->common.pool;
462 Solvable *s = pd->solvable;
463 struct stateswitch *sw;
465 Id handle = pd->handle;
467 // fprintf(stderr, "into %s, from %d, depth %d, statedepth %d\n", name, pd->state, pd->depth, pd->statedepth);
469 if (pd->depth != pd->statedepth)
475 if (pd->state == STATE_START && !strcmp(name, "patterns"))
477 if (pd->state == STATE_START && !strcmp(name, "metadata"))
479 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
483 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
484 if (!strcmp(sw->ename, name))
486 if (sw->from != pd->state)
489 fprintf(stderr, "into unknown: %s\n", name);
494 pd->docontent = sw->docontent;
495 pd->statedepth = pd->depth;
502 if (name[2] == 't' && name[3] == 't')
503 pd->kind = "pattern";
504 else if (name[1] == 'r')
505 pd->kind = "product";
506 else if (name[2] == 't' && name[3] == 'c')
509 /* this is a new package */
510 /*fprintf(stderr, "new package\n");*/
511 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->common.repo));
513 repodata_extend(pd->data, pd->solvable - pool->solvables);
514 pd->handle = repodata_get_handle(pd->data, (pd->solvable - pool->solvables) - pd->data->start);
516 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
520 s->evr = makeevr_atts(pool, pd, atts);
525 case STATE_PROVIDESENTRY:
526 s->provides = adddep(pool, pd, s->provides, atts, 0);
531 case STATE_REQUIRESENTRY:
532 s->requires = adddep(pool, pd, s->requires, atts, 1);
534 case STATE_OBSOLETES:
537 case STATE_OBSOLETESENTRY:
538 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
540 case STATE_CONFLICTS:
543 case STATE_CONFLICTSENTRY:
544 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
546 case STATE_RECOMMENDS:
549 case STATE_RECOMMENDSENTRY:
550 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
552 case STATE_SUPPLEMENTS:
555 case STATE_SUPPLEMENTSENTRY:
556 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
561 case STATE_SUGGESTSENTRY:
562 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
567 case STATE_ENHANCESENTRY:
568 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
573 case STATE_FRESHENSENTRY:
574 pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
577 case STATE_DESCRIPTION:
578 pd->lang = find_attr("lang", atts);
581 str = find_attr("href", atts);
584 const char *str2 = strrchr(str, '/');
587 char *str3 = strdup(str);
588 str3[str2 - str] = 0;
589 repodata_set_poolstr(pd->data, handle, SOLVABLE_MEDIADIR, str3);
591 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str2 + 1);
594 repodata_set_str(pd->data, handle, SOLVABLE_MEDIAFILE, str);
598 pd->tmpattr = find_attr("type", atts);
603 str = find_attr("build", atts);
604 if (str && (t = atoi(str)) != 0)
605 repodata_set_num(pd->data, handle, SOLVABLE_BUILDTIME, t);
611 str = find_attr("installed", atts);
612 if (str && (k = atoi(str)) != 0)
613 repodata_set_num(pd->data, handle, SOLVABLE_INSTALLSIZE, (k + 1023) / 1024);
614 /* XXX the "package" attribute gives the size of the rpm file,
615 i.e. the download size. Except on packman, there it seems to be
616 something else entirely, it has a value near to the other two
617 values, as if the rpm is uncompressed. */
618 str = find_attr("package", atts);
619 if (str && (k = atoi(str)) != 0)
620 repodata_set_num(pd->data, handle, SOLVABLE_DOWNLOADSIZE, (k + 1023) / 1024);
623 case STATE_HEADERRANGE:
626 str = find_attr("end", atts);
627 if (str && (end = atoi(str)) != 0)
628 repodata_set_num(pd->data, handle, SOLVABLE_HEADEREND, end);
636 endElement(void *userData, const char *name)
638 //fprintf(stderr,"-tag: %s\n", name);
639 struct parsedata *pd = userData;
640 Pool *pool = pd->common.pool;
641 Solvable *s = pd->solvable;
642 Repo *repo = pd->common.repo;
643 Id handle = pd->handle;
647 if (pd->depth != pd->statedepth)
650 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
654 /* ignore patterns & metadata */
655 if (pd->state == STATE_START && !strcmp(name, "patterns"))
657 if (pd->state == STATE_START && !strcmp(name, "metadata"))
659 if (pd->state == STATE_SOLVABLE && !strcmp(name, "format"))
670 s->arch = ARCH_NOARCH;
672 s->evr = ID_EMPTY; /* some patterns have this */
673 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
674 s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
675 s->supplements = repo_fix_legacy(repo, s->provides, s->supplements, pd->freshens);
681 s->name = str2id(pool, join2( pd->kind, ":", pd->content), 1);
683 s->name = str2id(pool, pd->content, 1);
686 s->arch = str2id(pool, pd->content, 1);
689 s->vendor = str2id(pool, pd->content, 1);
691 case STATE_RPM_GROUP:
692 repodata_set_poolstr(pd->data, handle, SOLVABLE_GROUP, pd->content);
694 case STATE_RPM_LICENSE:
695 repodata_set_poolstr(pd->data, handle, SOLVABLE_LICENSE, pd->content);
701 if (!strcasecmp (pd->tmpattr, "sha") || !strcasecmp (pd->tmpattr, "sha1"))
702 l = SIZEOF_SHA1 * 2, type = REPOKEY_TYPE_SHA1;
703 else if (!strcasecmp (pd->tmpattr, "md5"))
704 l = SIZEOF_MD5 * 2, type = REPOKEY_TYPE_MD5;
707 fprintf(stderr, "Unknown checksum type: %d: %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
710 if (strlen(pd->content) != l)
712 fprintf(stderr, "Invalid checksum length: %d: for %s\n", (unsigned int)XML_GetCurrentLineNumber(*pd->parser), pd->tmpattr);
715 repodata_set_checksum(pd->data, handle, SOLVABLE_CHECKSUM, type, pd->content);
720 id = str2id(pool, pd->content, 1);
721 s->provides = repo_addid_dep(repo, s->provides, id, SOLVABLE_FILEMARKER);
723 if ((p = strrchr(pd->content, '/')) != 0)
726 id = repodata_str2dir(pd->data, pd->content, 1);
734 id = repodata_str2dir(pd->data, "/", 1);
735 repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, id, p);
739 repodata_set_str(pd->data, handle, SOLVABLE_SUMMARY, pd->content);
741 case STATE_DESCRIPTION:
743 set_desciption_author(pd->data, handle, pd->content);
745 case STATE_DISTRIBUTION:
746 repodata_set_poolstr(pd->data, handle, SOLVABLE_DISTRIBUTION, pd->content);
750 repodata_set_str(pd->data, handle, SOLVABLE_URL, pd->content);
754 repodata_set_poolstr(pd->data, handle, SOLVABLE_PACKAGER, pd->content);
756 case STATE_SOURCERPM:
757 set_sourcerpm(pd->data, s, handle, pd->content);
759 case STATE_RELNOTESURL:
761 repodata_set_poolstr(pd->data, handle, PRODUCT_RELNOTESURL, pd->content);
763 case STATE_UPDATEURL:
765 repodata_set_poolstr(pd->data, handle, PRODUCT_EXTRAURLS, pd->content);
767 case STATE_OPTIONALURL:
769 repodata_set_poolstr(pd->data, handle, PRODUCT_OPTIONALURLS, pd->content);
773 repodata_set_poolstr(pd->data, handle, PRODUCT_FLAGS, pd->content);
777 repodata_set_str(pd->data, handle, PRODUCT_FLAVOR, pd->content);
779 case STATE_REFERENCES:
781 repodata_set_str(pd->data, handle, PRODUCT_REFERENCES, pd->content);
786 pd->state = pd->sbtab[pd->state];
788 // fprintf(stderr, "back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
792 characterData(void *userData, const XML_Char *s, int len)
794 struct parsedata *pd = userData;
800 l = pd->lcontent + len + 1;
801 if (l > pd->acontent)
803 pd->content = sat_realloc(pd->content, l + 256);
804 pd->acontent = l + 256;
806 c = pd->content + pd->lcontent;
814 #define BUFF_SIZE 8192
817 repo_add_rpmmd(Repo *repo, FILE *fp, int flags)
819 Pool *pool = repo->pool;
823 struct stateswitch *sw;
825 memset(&pd, 0, sizeof(pd));
826 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
828 if (!pd.swtab[sw->from])
829 pd.swtab[sw->from] = sw;
830 pd.sbtab[sw->to] = sw->from;
832 pd.common.pool = pool;
833 pd.common.repo = repo;
835 pd.data = repo_add_repodata(repo, 0);
837 pd.content = sat_malloc(256);
843 XML_Parser parser = XML_ParserCreate(NULL);
844 XML_SetUserData(parser, &pd);
846 XML_SetElementHandler(parser, startElement, endElement);
847 XML_SetCharacterDataHandler(parser, characterData);
850 l = fread(buf, 1, sizeof(buf), fp);
851 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
853 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));
859 XML_ParserFree(parser);
862 repodata_internalize(pd.data);
863 sat_free(pd.content);