2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
18 #include "repo_patchxml.h"
19 #include "repo_rpmmd.h"
49 STATE_RECOMMENDSENTRY,
51 STATE_SUPPLEMENTSENTRY,
71 static struct stateswitch stateswitches[] = {
72 { STATE_START, "patch", STATE_PATCH, 0 },
73 { STATE_START, "package", STATE_ATOM, 0 },
74 { STATE_START, "patches", STATE_START, 0},
75 { STATE_PATCH, "yum:name", STATE_NAME, 1 },
76 { STATE_PATCH, "yum:arch", STATE_ARCH, 1 },
77 { STATE_PATCH, "yum:version", STATE_VERSION, 0 },
78 { STATE_PATCH, "name", STATE_NAME, 1 },
79 { STATE_PATCH, "arch", STATE_ARCH, 1 },
80 { STATE_PATCH, "version", STATE_VERSION, 0 },
81 { STATE_PATCH, "summary", STATE_SUMMARY, 1 },
82 { STATE_PATCH, "description", STATE_DESCRIPTION, 1 },
83 { STATE_PATCH, "category", STATE_CATEGORY, 1 },
84 { STATE_PATCH, "reboot-needed", STATE_REBOOT, 0 },
85 { STATE_PATCH, "package-manager", STATE_RESTART, 0 },
86 { STATE_PATCH, "rpm:requires", STATE_REQUIRES, 0 },
87 { STATE_PATCH, "rpm:provides", STATE_PROVIDES, 0 },
88 { STATE_PATCH, "rpm:requires", STATE_REQUIRES, 0 },
89 { STATE_PATCH, "rpm:obsoletes", STATE_OBSOLETES , 0 },
90 { STATE_PATCH, "rpm:conflicts", STATE_CONFLICTS , 0 },
91 { STATE_PATCH, "rpm:recommends" , STATE_RECOMMENDS , 0 },
92 { STATE_PATCH, "rpm:supplements", STATE_SUPPLEMENTS, 0 },
93 { STATE_PATCH, "rpm:suggests", STATE_SUGGESTS, 0 },
94 { STATE_PATCH, "rpm:enhances", STATE_ENHANCES, 0 },
95 { STATE_PATCH, "rpm:freshens", STATE_FRESHENS, 0 },
96 { STATE_PATCH, "suse:freshens", STATE_FRESHENS, 0 },
97 { STATE_PATCH, "atoms", STATE_START, 0 },
98 { STATE_PATCH, "pkgfiles", STATE_PKGFILES, 0 },
99 { STATE_PKGFILES, "deltarpm", STATE_DELTARPM, 0 },
100 { STATE_PKGFILES, "patchrpm", STATE_DELTARPM, 0 },
101 { STATE_DELTARPM, "location", STATE_DLOCATION, 0 },
102 { STATE_DELTARPM, "checksum", STATE_DCHECKSUM, 1 },
103 { STATE_DELTARPM, "time", STATE_DTIME, 0 },
104 { STATE_DELTARPM, "size", STATE_DSIZE, 0 },
105 { STATE_DELTARPM, "base-version", STATE_DBASEVERSION, 0 },
106 { STATE_PROVIDES, "rpm:entry", STATE_PROVIDESENTRY, 0 },
107 { STATE_REQUIRES, "rpm:entry", STATE_REQUIRESENTRY, 0 },
108 { STATE_OBSOLETES, "rpm:entry", STATE_OBSOLETESENTRY, 0 },
109 { STATE_CONFLICTS, "rpm:entry", STATE_CONFLICTSENTRY, 0 },
110 { STATE_RECOMMENDS, "rpm:entry", STATE_RECOMMENDSENTRY, 0 },
111 { STATE_SUPPLEMENTS, "rpm:entry", STATE_SUPPLEMENTSENTRY, 0 },
112 { STATE_SUGGESTS, "rpm:entry", STATE_SUGGESTSENTRY, 0 },
113 { STATE_ENHANCES, "rpm:entry", STATE_ENHANCESENTRY, 0 },
114 { STATE_FRESHENS, "rpm:entry", STATE_FRESHENSENTRY, 0 },
115 { STATE_FRESHENS, "suse:entry", STATE_FRESHENSENTRY, 0 },
119 /* Cumulated info about the current deltarpm or patchrpm */
126 unsigned downloadsize, archivesize;
128 /* Baseversions. deltarpm only has one, patchrpm may have more. */
131 /* If deltarpm, then this is filled. */
148 unsigned int datanum;
152 unsigned int timestamp;
154 struct stateswitch *swtab[NUMSTATES];
155 enum state sbtab[NUMSTATES];
159 struct deltarpm delta;
164 append_str(struct parsedata *pd, const char *s)
168 int l = pd->ltemp + strlen(s) + 1;
171 pd->tempstr = realloc(pd->tempstr, l + 256);
174 strcpy(pd->tempstr + pd->ltemp, s);
175 pd->ltemp += strlen(s);
180 * create evr (as Id) from 'epoch', 'ver' and 'rel' attributes
184 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
186 const char *e, *v, *r, *v2;
191 for (; *atts; atts += 2)
193 if (!strcmp(*atts, "epoch"))
195 else if (!strcmp(*atts, "ver"))
197 else if (!strcmp(*atts, "rel"))
200 if (e && !strcmp(e, "0"))
204 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
206 if (v2 > v && *v2 == ':')
216 if (l > pd->acontent)
218 pd->content = realloc(pd->content, l + 256);
219 pd->acontent = l + 256;
243 fprintf(stderr, "evr: %s\n", pd->content);
245 return str2id(pool, pd->content, 1);
254 find_attr(const char *txt, const char **atts)
256 for (; *atts; atts += 2)
258 if (!strcmp(*atts, txt))
266 * relation comparision operators
269 static char *flagtab[] = {
284 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
287 const char *n, *f, *k;
291 marker = isreq ? -SOLVABLE_PREREQMARKER : 0;
292 for (a = atts; *a; a += 2)
294 if (!strcmp(*a, "name"))
296 else if (!strcmp(*a, "flags"))
298 else if (!strcmp(*a, "kind"))
300 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
301 marker = SOLVABLE_PREREQMARKER;
305 if (k && !strcmp(k, "package")) /* kind 'package' -> ignore */
309 int l = strlen(k) + 1 + strlen(n) + 1;
310 if (l > pd->acontent)
312 pd->content = realloc(pd->content, l + 256);
313 pd->acontent = l + 256;
315 sprintf(pd->content, "%s:%s", k, n); /* prepend kind to name */
316 name = str2id(pool, pd->content, 1);
319 name = str2id(pool, (char *)n, 1);
320 if (f) /* flags means name,operator,relation */
322 Id evr = makeevr_atts(pool, pd, atts);
324 for (flags = 0; flags < 6; flags++)
325 if (!strcmp(f, flagtab[flags]))
327 flags = flags < 6 ? flags + 1 : 0;
328 id = rel2id(pool, name, evr, flags, 1);
333 fprintf(stderr, "new dep %s%s%s\n", id2str(pool, d), id2rel(pool, d), id2evr(pool, d));
335 return repo_addid_dep(pd->repo, olddeps, id, marker);
340 startElement(void *userData, const char *name, const char **atts)
342 struct parsedata *pd = userData;
343 Pool *pool = pd->pool;
344 Solvable *s = pd->solvable;
345 struct stateswitch *sw;
348 if (pd->depth != pd->statedepth)
354 if (pd->state == STATE_PATCH && !strcmp(name, "format"))
358 if (!pd->swtab[pd->state])
360 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++) /* find name in statetable */
361 if (!strcmp(sw->ename, name))
364 if (sw->from != pd->state)
367 fprintf(stderr, "into unknown: %s\n", name);
372 pd->docontent = sw->docontent;
373 pd->statedepth = pd->depth;
381 strcpy(pd->content, pd->kind);
382 pd->lcontent = strlen(pd->content);
383 pd->content[pd->lcontent++] = ':';
384 pd->content[pd->lcontent] = 0;
388 if (sw->from == STATE_START)
390 if ((str = find_attr("timestamp", atts)))
392 pd->timestamp = strtoul(str, NULL, 10);
397 if (pd->state == STATE_ATOM)
399 /* HACK: close patch */
400 if (pd->kind && !strcmp(pd->kind, "patch"))
403 s->arch = ARCH_NOARCH;
404 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
407 pd->state = STATE_PATCH;
412 pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
415 if (!strcmp(pd->kind, "patch"))
417 pd->datanum = (pd->solvable - pool->solvables) - pd->repo->start;
418 repodata_extend(pd->data, pd->solvable - pool->solvables);
419 pd->datanum = repodata_get_handle(pd->data, pd->datanum);
420 repodata_set_num(pd->data, pd->datanum, SOLVABLE_BUILDTIME, pd->timestamp);
423 fprintf(stderr, "package #%d\n", pd->solvable - pool->solvables);
427 memset(&pd->delta, 0, sizeof (pd->delta));
431 case STATE_DLOCATION:
432 if ((str = find_attr("href", atts)))
434 /* Separate the filename into its different parts.
435 rpm/x86_64/alsa-1.0.14-31_31.2.x86_64.delta.rpm
439 suffix = x86_64.delta.rpm. */
440 char *real_str = strdup(str);
443 s1 = strrchr (s, '/');
446 pd->delta.locdir = strn2id(pool, s, s1 - s, 1);
450 s1 = strrchr (s, '.');
453 for (s2 = s1 - 1; s2 > s; s2--)
456 if (!strcmp (s2, ".delta.rpm") || !strcmp (s2, ".patch.rpm"))
459 /* We accept one more item as suffix. */
460 for (s2 = s1 - 1; s2 > s; s2--)
467 pd->delta.locsuffix = str2id(pool, s1, 1);
470 s1 = strrchr (s, '-');
473 /* Second to last '-'. */
474 for (s2 = s1 - 1; s2 > s; s2--)
480 if (s2 > s && *s2 == '-')
483 pd->delta.locevr = str2id(pool, s2, 1);
485 pd->delta.locname = str2id(pool, s, 1);
490 str = find_attr("build", atts);
492 pd->delta.buildtime = atoi(str);
495 if ((str = find_attr("package", atts)))
496 pd->delta.downloadsize = atoi(str);
497 if ((str = find_attr("archive", atts)))
498 pd->delta.archivesize = atoi(str);
500 case STATE_DBASEVERSION:
501 if ((str = find_attr("sequence_info", atts)))
504 s1 = strrchr(str, '-');
507 for (s2 = s1 - 1; s2 > str; s2--)
512 for (s2 = s2 - 1; s2 > str; s2--)
517 pd->delta.seqevr = strn2id(pool, s2 + 1, s1 - s2 - 1, 1);
518 pd->delta.seqname = strn2id(pool, str, s2 - str, 1);
523 pd->delta.seqnum = strdup(str);
526 pd->delta.bevr = sat_realloc (pd->delta.bevr, pd->delta.nbevr * sizeof(Id));
527 pd->delta.bevr[pd->delta.nbevr - 1] = makeevr_atts(pool, pd, atts);
530 s->evr = makeevr_atts(pool, pd, atts);
535 case STATE_PROVIDESENTRY:
536 s->provides = adddep(pool, pd, s->provides, atts, 0);
541 case STATE_REQUIRESENTRY:
542 s->requires = adddep(pool, pd, s->requires, atts, 1);
544 case STATE_OBSOLETES:
547 case STATE_OBSOLETESENTRY:
548 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
550 case STATE_CONFLICTS:
553 case STATE_CONFLICTSENTRY:
554 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
556 case STATE_RECOMMENDS:
559 case STATE_RECOMMENDSENTRY:
560 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
562 case STATE_SUPPLEMENTS:
565 case STATE_SUPPLEMENTSENTRY:
566 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
571 case STATE_SUGGESTSENTRY:
572 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
577 case STATE_ENHANCESENTRY:
578 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
583 case STATE_FRESHENSENTRY:
584 pd->freshens = adddep(pool, pd, pd->freshens, atts, 0);
587 repodata_set_void(pd->data, pd->datanum, UPDATE_REBOOT);
590 repodata_set_void(pd->data, pd->datanum, UPDATE_RESTART);
598 endElement(void *userData, const char *name)
600 struct parsedata *pd = userData;
601 Pool *pool = pd->pool;
602 Solvable *s = pd->solvable;
604 if (pd->depth != pd->statedepth)
607 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
611 if (pd->state == STATE_PATCH && !strcmp(name, "format"))
619 if (!strcmp(name, "patch") && strcmp(pd->kind, "patch"))
620 break; /* already closed */
622 s->arch = ARCH_NOARCH;
623 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
624 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
625 s->supplements = repo_fix_supplements(pd->repo, s->provides, s->supplements, pd->freshens);
626 s->conflicts = repo_fix_conflicts(pd->repo, s->conflicts);
630 s->name = str2id(pool, pd->content, 1);
633 s->arch = str2id(pool, pd->content, 1);
636 repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
638 case STATE_DESCRIPTION:
639 repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
642 repodata_set_str(pd->data, pd->datanum, SOLVABLE_PATCHCATEGORY, pd->content);
648 struct deltarpm *d = &pd->delta;
649 fprintf (stderr, "found deltarpm for %s:\n", id2str(pool, s->name));
650 fprintf (stderr, " loc: %s %s %s %s\n", id2str(pool, d->locdir),
651 id2str(pool, d->locname), id2str(pool, d->locevr),
652 id2str(pool, d->locsuffix));
653 fprintf (stderr, " time: %u\n", d->buildtime);
654 fprintf (stderr, " size: %d down, %d archive\n", d->downloadsize,
656 fprintf (stderr, " chek: %s\n", d->filechecksum);
659 fprintf (stderr, " base: %s, seq: %s %s %s\n",
660 id2str(pool, d->bevr[0]), id2str(pool, d->seqname),
661 id2str(pool, d->seqevr), d->seqnum);
662 if (d->seqevr != d->bevr[0])
663 fprintf (stderr, "XXXXX evr\n");
664 /* Name of package ("atom:xxxx") should match the sequence info
666 if (strcmp(id2str(pool, d->seqname), id2str(pool, s->name) + 5))
667 fprintf (stderr, "XXXXX name\n");
671 fprintf (stderr, " base:");
672 for (i = 0; i < d->nbevr; i++)
673 fprintf (stderr, " %s", id2str(pool, d->bevr[i]));
674 fprintf (stderr, "\n");
678 free(pd->delta.filechecksum);
679 free(pd->delta.bevr);
680 free(pd->delta.seqnum);
682 case STATE_DCHECKSUM:
683 pd->delta.filechecksum = strdup(pd->content);
688 pd->state = pd->sbtab[pd->state];
690 // printf("back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
694 characterData(void *userData, const XML_Char *s, int len)
696 struct parsedata *pd = userData;
702 l = pd->lcontent + len + 1;
703 if (l > pd->acontent)
705 pd->content = realloc(pd->content, l + 256);
706 pd->acontent = l + 256;
708 c = pd->content + pd->lcontent;
716 #define BUFF_SIZE 8192
719 repo_add_patchxml(Repo *repo, FILE *fp, int flags)
721 Pool *pool = repo->pool;
725 struct stateswitch *sw;
727 memset(&pd, 0, sizeof(pd));
728 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
730 if (!pd.swtab[sw->from])
731 pd.swtab[sw->from] = sw;
732 pd.sbtab[sw->to] = sw->from;
736 pd.data = repo_add_repodata(pd.repo, 0);
738 pd.content = malloc(256);
741 pd.tempstr = malloc(256);
744 XML_Parser parser = XML_ParserCreate(NULL);
745 XML_SetUserData(parser, &pd);
746 XML_SetElementHandler(parser, startElement, endElement);
747 XML_SetCharacterDataHandler(parser, characterData);
750 l = fread(buf, 1, sizeof(buf), fp);
751 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
753 fprintf(stderr, "repo_patchxml: %s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
759 XML_ParserFree(parser);
762 repodata_internalize(pd.data);