2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Parse 'helix' XML representation
16 #include <sys/types.h>
24 #include "repo_helix.h"
28 /* XML parser states */
52 STATE_RECOMMENDSENTRY,
54 STATE_SUPPLEMENTSENTRY,
76 #define PACK_BLOCK 255
85 static struct stateswitch stateswitches[] = {
86 { STATE_START, "channel", STATE_CHANNEL, 0 },
87 { STATE_CHANNEL, "subchannel", STATE_SUBCHANNEL, 0 },
88 { STATE_SUBCHANNEL, "package", STATE_PACKAGE, 0 },
89 { STATE_SUBCHANNEL, "selection", STATE_PACKAGE, 0 },
90 { STATE_SUBCHANNEL, "pattern", STATE_PACKAGE, 0 },
91 { STATE_SUBCHANNEL, "atom", STATE_PACKAGE, 0 },
92 { STATE_SUBCHANNEL, "patch", STATE_PACKAGE, 0 },
93 { STATE_SUBCHANNEL, "product", STATE_PACKAGE, 0 },
94 { STATE_PACKAGE, "name", STATE_NAME, 1 },
95 { STATE_PACKAGE, "vendor", STATE_VENDOR, 1 },
96 { STATE_PACKAGE, "epoch", STATE_PEPOCH, 1 },
97 { STATE_PACKAGE, "version", STATE_PVERSION, 1 },
98 { STATE_PACKAGE, "release", STATE_PRELEASE, 1 },
99 { STATE_PACKAGE, "arch", STATE_PARCH, 1 },
100 { STATE_PACKAGE, "history", STATE_HISTORY, 0 },
101 { STATE_PACKAGE, "provides", STATE_PROVIDES, 0 },
102 { STATE_PACKAGE, "requires", STATE_REQUIRES, 0 },
103 { STATE_PACKAGE, "obsoletes", STATE_OBSOLETES , 0 },
104 { STATE_PACKAGE, "conflicts", STATE_CONFLICTS , 0 },
105 { STATE_PACKAGE, "recommends" , STATE_RECOMMENDS , 0 },
106 { STATE_PACKAGE, "supplements", STATE_SUPPLEMENTS, 0 },
107 { STATE_PACKAGE, "suggests", STATE_SUGGESTS, 0 },
108 { STATE_PACKAGE, "enhances", STATE_ENHANCES, 0 },
109 { STATE_PACKAGE, "freshens", STATE_FRESHENS, 0 },
111 { STATE_HISTORY, "update", STATE_UPDATE, 0 },
112 { STATE_UPDATE, "epoch", STATE_EPOCH, 1 },
113 { STATE_UPDATE, "version", STATE_VERSION, 1 },
114 { STATE_UPDATE, "release", STATE_RELEASE, 1 },
115 { STATE_UPDATE, "arch", STATE_ARCH, 1 },
117 { STATE_PROVIDES, "dep", STATE_PROVIDESENTRY, 0 },
118 { STATE_REQUIRES, "dep", STATE_REQUIRESENTRY, 0 },
119 { STATE_OBSOLETES, "dep", STATE_OBSOLETESENTRY, 0 },
120 { STATE_CONFLICTS, "dep", STATE_CONFLICTSENTRY, 0 },
121 { STATE_RECOMMENDS, "dep", STATE_RECOMMENDSENTRY, 0 },
122 { STATE_SUPPLEMENTS, "dep", STATE_SUPPLEMENTSENTRY, 0 },
123 { STATE_SUGGESTS, "dep", STATE_SUGGESTSENTRY, 0 },
124 { STATE_ENHANCES, "dep", STATE_ENHANCESENTRY, 0 },
125 { STATE_FRESHENS, "dep", STATE_FRESHENSENTRY, 0 },
134 typedef struct _parsedata {
137 enum state state; // current state
139 char *content; // buffer for content of node
140 int lcontent; // actual length of current content
141 int acontent; // actual buffer size
142 int docontent; // handle content
145 int pack; // number of solvables
147 Pool *pool; // current pool
148 Repo *repo; // current repo
149 Solvable *start; // collected solvables
152 int epoch; // epoch (as offset into evrspace)
153 int version; // version (as offset into evrspace)
154 int release; // release (as offset into evrspace)
155 char *evrspace; // buffer for evr
156 int aevrspace; // actual buffer space
157 int levrspace; // actual evr length
160 struct stateswitch *swtab[NUMSTATES];
161 enum state sbtab[NUMSTATES];
165 /*------------------------------------------------------------------*/
168 // create Id from epoch:version-release
171 evr2id(Pool *pool, Parsedata *pd, const char *e, const char *v, const char *r)
176 // treat explitcit 0 as NULL
177 if (e && !strcmp(e, "0"))
183 // scan version for ":"
184 for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++) // skip leading digits
186 // if version contains ":", set epoch to "0"
187 if (v2 > v && *v2 == ':')
191 // compute length of Id string
194 l += strlen(e) + 1; // e:
198 l += strlen(r) + 1; // -r
200 // extend content if not sufficient
201 if (l > pd->acontent)
203 pd->content = (char *)realloc(pd->content, l + 256);
204 pd->acontent = l + 256;
207 // copy e-v-r to content
227 // if nothing inserted, return Id 0
231 fprintf(stderr, "evr: %s\n", pd->content);
234 return str2id(pool, pd->content, 1);
238 // create e:v-r from attributes
239 // atts is array of name,value pairs, NULL at end
240 // even index into atts is name
241 // odd index is value
244 evr_atts2id(Pool *pool, Parsedata *pd, const char **atts)
246 const char *e, *v, *r;
248 for (; *atts; atts += 2)
250 if (!strcmp(*atts, "epoch"))
252 else if (!strcmp(*atts, "version"))
254 else if (!strcmp(*atts, "release"))
257 return evr2id(pool, pd, e, v, r);
260 /*------------------------------------------------------------------*/
261 /* rel operator handling */
268 static struct flagtab flagtab[] = {
271 { ">=", REL_GT|REL_EQ },
273 { "!=", REL_GT|REL_LT },
274 { "<=", REL_LT|REL_EQ },
275 { "(any)", REL_LT|REL_EQ|REL_GT },
279 { "ge", REL_GT|REL_EQ },
281 { "ne", REL_GT|REL_LT },
282 { "le", REL_LT|REL_EQ },
283 { "gte", REL_GT|REL_EQ },
284 { "lte", REL_LT|REL_EQ },
287 { "GE", REL_GT|REL_EQ },
289 { "NE", REL_GT|REL_LT },
290 { "LE", REL_LT|REL_EQ }
294 * process new dependency from parser
295 * olddeps = already collected deps, this defines the 'kind' of dep
296 * atts = array of name,value attributes of dep
297 * isreq == 1 if its a requires
301 adddep(Pool *pool, Parsedata *pd, unsigned int olddeps, const char **atts, int isreq)
304 const char *n, *f, *k;
309 /* loop over name,value pairs */
310 for (a = atts; *a; a += 2)
312 if (!strcmp(*a, "name"))
314 if (!strcmp(*a, "kind"))
316 else if (!strcmp(*a, "op"))
318 else if (isreq && !strcmp(*a, "pre") && a[1][0] == '1')
321 if (!n) /* quit if no name found */
325 if (k && !strcmp(k, "package"))
326 k = NULL; /* package is default */
328 if (k) /* if kind!=package, intern <kind>:<name> */
330 int l = strlen(k) + 1 + strlen(n) + 1;
331 if (l > pd->acontent) /* extend buffer if needed */
333 pd->content = (char *)realloc(pd->content, l + 256);
334 pd->acontent = l + 256;
336 sprintf(pd->content, "%s:%s", k, n);
337 name = str2id(pool, pd->content, 1);
340 name = str2id(pool, n, 1); /* package: just intern <name> */
343 if (f) /* operator ? */
346 Id evr = evr_atts2id(pool, pd, atts);
347 /* parser operator to flags */
349 for (flags = 0; flags < sizeof(flagtab)/sizeof(*flagtab); flags++)
350 if (!strcmp(f, flagtab[flags].from))
352 flags = flagtab[flags].to;
358 id = rel2id(pool, name, evr, flags, 1);
361 id = name; /* no operator */
363 /* add new dependency to repo */
364 return repo_addid_dep(pd->repo, olddeps, id, isreq);
368 /*----------------------------------------------------------------*/
377 startElement(void *userData, const char *name, const char **atts)
379 Parsedata *pd = (Parsedata *)userData;
380 struct stateswitch *sw;
381 Pool *pool = pd->pool;
382 Solvable *s = pd->start ? pd->start + pd->pack : 0;
384 if (pd->depth != pd->statedepth)
390 /* ignore deps element */
391 if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
396 /* find node name in stateswitch */
397 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
399 if (!strcmp(sw->ename, name))
403 /* check if we're at the right level */
404 if (sw->from != pd->state)
407 fprintf(stderr, "into unknown: %s\n", name);
415 pd->docontent = sw->docontent;
416 pd->statedepth = pd->depth;
418 // start with empty content
419 // (will collect data until end element
427 if (pd->kind) /* if kind is set (non package) */
429 strcpy(pd->content, pd->kind);
430 pd->lcontent = strlen(pd->content);
431 pd->content[pd->lcontent++] = ':'; /* prefix name with '<kind>:' */
432 pd->content[pd->lcontent] = 0;
436 case STATE_SUBCHANNEL:
441 case STATE_PACKAGE: /* solvable name */
443 if ((pd->pack & PACK_BLOCK) == 0) /* alloc new block ? */
445 pool->solvables = (Solvable *)realloc(pool->solvables, (pool->nsolvables + pd->pack + PACK_BLOCK + 1) * sizeof(Solvable));
446 pd->start = pool->solvables + pool->nsolvables;
447 memset(pd->start + pd->pack, 0, (PACK_BLOCK + 1) * sizeof(Solvable));
450 if (!strcmp(name, "selection"))
451 pd->kind = "selection";
452 else if (!strcmp(name, "pattern"))
453 pd->kind = "pattern";
454 else if (!strcmp(name, "atom"))
456 else if (!strcmp(name, "product"))
457 pd->kind = "product";
458 else if (!strcmp(name, "patch"))
461 pd->kind = NULL; /* default is package */
467 fprintf(stderr, "package #%d\n", pd->pack);
478 case STATE_PROVIDES: /* start of provides */
481 case STATE_PROVIDESENTRY: /* entry within provides */
482 s->provides = adddep(pool, pd, s->provides, atts, 0);
487 case STATE_REQUIRESENTRY:
488 s->requires = adddep(pool, pd, s->requires, atts, 1);
490 case STATE_OBSOLETES:
493 case STATE_OBSOLETESENTRY:
494 s->obsoletes = adddep(pool, pd, s->obsoletes, atts, 0);
496 case STATE_CONFLICTS:
499 case STATE_CONFLICTSENTRY:
500 s->conflicts = adddep(pool, pd, s->conflicts, atts, 0);
502 case STATE_RECOMMENDS:
505 case STATE_RECOMMENDSENTRY:
506 s->recommends = adddep(pool, pd, s->recommends, atts, 0);
508 case STATE_SUPPLEMENTS:
511 case STATE_SUPPLEMENTSENTRY:
512 s->supplements = adddep(pool, pd, s->supplements, atts, 0);
517 case STATE_SUGGESTSENTRY:
518 s->suggests = adddep(pool, pd, s->suggests, atts, 0);
523 case STATE_ENHANCESENTRY:
524 s->enhances = adddep(pool, pd, s->enhances, atts, 0);
529 case STATE_FRESHENSENTRY:
530 s->freshens = adddep(pool, pd, s->freshens, atts, 0);
537 static const char* findKernelFlavor(Parsedata *pd, Solvable *s)
539 Pool *pool = pd->pool;
545 for (pidp = pd->repo->idarraydata + s->provides; pidp && (pid = *pidp++) != 0; )
551 continue; /* wrong provides name */
552 prd = GETRELDEP(pool, pid);
553 depname = id2str(pool, prd->name);
554 if (!strncmp(depname, "kernel-", strlen("kernel-")))
556 return depname + strlen("kernel-");
561 //fprintf(stderr, "pack %d\n", pd->pack);
562 //fprintf(stderr, "repo %d\n", s->requires);
567 for (pidp = pd->repo->idarraydata + s->requires ; pidp && (pid = *pidp++) != 0; )
569 const char *depname = 0;
573 depname = id2str(pool, pid);
577 Reldep *prd = GETRELDEP(pool, pid);
578 depname = id2str(pool, prd->name);
580 if (!strncmp(depname, "kernel-", strlen("kernel-")))
582 return depname + strlen("kernel-");
594 * create Solvable from collected data
598 endElement(void *userData, const char *name)
600 Parsedata *pd = (Parsedata *)userData;
601 Pool *pool = pd->pool;
602 Solvable *s = pd->start ? pd->start + pd->pack : NULL;
605 if (pd->depth != pd->statedepth)
608 // printf("back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
612 /* ignore deps element */
613 if (pd->state == STATE_PACKAGE && !strcmp(name, "deps"))
621 case STATE_PACKAGE: /* package complete */
624 if (!s->arch) /* default to "noarch" */
625 s->arch = ARCH_NOARCH;
627 if (!s->evr && pd->version) /* set solvable evr */
628 s->evr = evr2id(pool, pd,
629 pd->epoch ? pd->evrspace + pd->epoch : 0,
630 pd->version ? pd->evrspace + pd->version : 0,
631 pd->release ? pd->evrspace + pd->release : 0);
632 /* ensure self-provides */
633 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
634 s->provides = repo_addid_dep(pd->repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
635 s->supplements = repo_fix_legacy(pd->repo, s->provides, s->supplements);
637 const char *flavor = findKernelFlavor(pd, s);
640 char *cflavor = strdup(flavor); /* make pointer safe */
645 /* this is either a kernel package or a kmp */
648 Offset prov = s->provides;
650 while ((pid = pd->repo->idarraydata[prov++]) != 0)
652 const char *depname = 0;
657 prd = GETRELDEP(pool, pid);
658 depname = id2str(pool, prd->name);
662 depname = id2str(pool, pid);
666 if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
669 strcpy(newdep, "kernel(");
670 strncat(newdep, cflavor, sizeof(newdep));
671 strncat(newdep, ":", sizeof(newdep));
672 strncat(newdep, depname + strlen("kernel("), 100);
673 pid = str2id(pool, newdep, 1);
675 pid = rel2id(pool, pid, prd->evr, prd->flags, 1);
678 npr = repo_addid_dep(pd->repo, npr, pid, 0);
686 Offset reqs = s->requires;
688 while ((pid = pd->repo->idarraydata[reqs++]) != 0)
690 const char *depname = 0;
695 prd = GETRELDEP(pool, pid);
696 depname = id2str(pool, prd->name);
700 depname = id2str(pool, pid);
703 if (!strncmp(depname, "kernel(", strlen("kernel(")) && !strchr(depname, ':'))
706 strcpy(newdep, "kernel(");
707 strncat(newdep, cflavor, sizeof(newdep));
708 strncat(newdep, ":", sizeof(newdep));
709 strncat(newdep, depname + strlen("kernel("), 100);
710 pid = str2id(pool, newdep, 1);
712 pid = rel2id(pool, pid, prd->evr, prd->flags, 1);
714 npr = repo_addid_dep(pd->repo, npr, pid, 0);
722 pd->pack++; /* inc pack count */
726 s->name = str2id(pool, pd->content, 1);
729 s->vendor = str2id(pool, pd->content, 1);
731 case STATE_UPDATE: /* new version, keeping all other metadata */
732 evr = evr2id(pool, pd,
733 pd->epoch ? pd->evrspace + pd->epoch : 0,
734 pd->version ? pd->evrspace + pd->version : 0,
735 pd->release ? pd->evrspace + pd->release : 0);
740 /* use highest evr */
741 if (!s->evr || evrcmp(pool, s->evr, evr) <= 0)
750 /* ensure buffer space */
751 if (pd->lcontent + 1 + pd->levrspace > pd->aevrspace)
753 pd->evrspace = (char *)realloc(pd->evrspace, pd->lcontent + 1 + pd->levrspace + 256);
754 pd->aevrspace = pd->lcontent + 1 + pd->levrspace + 256;
756 memcpy(pd->evrspace + pd->levrspace, pd->content, pd->lcontent + 1);
757 if (pd->state == STATE_EPOCH || pd->state == STATE_PEPOCH)
758 pd->epoch = pd->levrspace;
759 else if (pd->state == STATE_VERSION || pd->state == STATE_PVERSION)
760 pd->version = pd->levrspace;
762 pd->release = pd->levrspace;
763 pd->levrspace += pd->lcontent + 1;
767 s->arch = str2id(pool, pd->content, 1);
772 pd->state = pd->sbtab[pd->state];
774 // printf("back from known %d %d %d\n", pd->state, pd->depth, pd->statedepth);
785 characterData(void *userData, const XML_Char *s, int len)
787 Parsedata *pd = (Parsedata *)userData;
791 // check if current nodes content is interesting
795 // adapt content buffer
796 l = pd->lcontent + len + 1;
797 if (l > pd->acontent)
799 pd->content = (char *)realloc(pd->content, l + 256);
800 pd->acontent = l + 256;
802 // append new content to buffer
803 c = pd->content + pd->lcontent;
810 /*-------------------------------------------------------------------*/
812 #define BUFF_SIZE 8192
815 * read 'helix' type xml from fp
816 * add packages to pool/repo
821 repo_add_helix(Repo *repo, FILE *fp)
823 Pool *pool = repo->pool;
827 struct stateswitch *sw;
829 if (repo->start && repo->start + repo->nsolvables != pool->nsolvables)
832 repo->start = pool->nsolvables;
834 /* prepare parsedata */
835 memset(&pd, 0, sizeof(pd));
836 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
838 if (!pd.swtab[sw->from])
839 pd.swtab[sw->from] = sw;
840 pd.sbtab[sw->to] = sw->from;
846 pd.content = (char *)malloc(256); /* must hold all solvable kinds! */
850 pd.evrspace = (char *)malloc(256);
856 XML_Parser parser = XML_ParserCreate(NULL);
857 XML_SetUserData(parser, &pd); /* make parserdata available to XML callbacks */
858 XML_SetElementHandler(parser, startElement, endElement);
859 XML_SetCharacterDataHandler(parser, characterData);
861 // read/parse XML file
864 l = fread(buf, 1, sizeof(buf), fp);
865 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
867 fprintf(stderr, "%s at line %u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser));
873 XML_ParserFree(parser);
875 // adapt package count
876 pool->nsolvables += pd.pack;
877 repo->nsolvables += pd.pack;