2 * Copyright (c) 2012, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
23 parse_deps(Solvable *s, char *bp, Id marker)
25 Pool *pool = s->repo->pool;
34 nbp = strchr(bp, '@');
36 ebp = bp + strlen(bp);
45 while (sbp >= bp && *sbp != '[')
47 if (sbp >= bp && sbp[1] != '*')
50 for (fbp = sbp + 1;; fbp++)
63 evr = pool_strn2id(pool, fbp, ebp - 1 - fbp, 1);
67 if (ebp[-1] == ']' && ebp >= bp + 3 && !strncmp(ebp - 3, "[*]", 3))
72 id = pool_strn2id(pool, bp, ebp - bp, 1);
74 id = pool_rel2id(pool, id, evr, flags, 1);
75 deps = repo_addid_dep(s->repo, deps, id, ispre ? marker : 0);
82 repo_add_mdk(Repo *repo, FILE *fp, int flags)
84 Pool *pool = repo->pool;
90 data = repo_add_repodata(repo, flags);
92 buf = solv_malloc(bufa);
95 while (fgets(buf + bufl, bufa - bufl, fp) > 0)
97 bufl += strlen(buf + bufl);
98 if (bufl && buf[bufl - 1] != '\n')
100 if (bufa - bufl < 256)
103 buf = solv_realloc(buf, bufa);
111 pool_debug(pool, SOLV_ERROR, "bad line <%s>\n", buf);
115 s = pool_id2solvable(pool, repo_add_solvable(repo));
116 if (!strncmp(buf + 1, "filesize@", 9))
118 unsigned long filesize = strtoul(buf + 10, 0, 10);
119 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((filesize + 1023) / 1024));
121 else if (!strncmp(buf + 1, "summary@", 8))
122 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, buf + 9);
123 else if (!strncmp(buf + 1, "provides@", 9))
124 s->provides = parse_deps(s, buf + 10, 0);
125 else if (!strncmp(buf + 1, "requires@", 9))
126 s->requires = parse_deps(s, buf + 10, SOLVABLE_PREREQMARKER);
127 else if (!strncmp(buf + 1, "suggests@", 9))
128 s->suggests = parse_deps(s, buf + 10, 0);
129 else if (!strncmp(buf + 1, "obsoletes@", 10))
130 s->obsoletes = parse_deps(s, buf + 11, 0);
131 else if (!strncmp(buf + 1, "conflicts@", 10))
132 s->conflicts = parse_deps(s, buf + 11, 0);
133 else if (!strncmp(buf + 1, "info@", 5))
135 char *nvra = buf + 6;
140 if ((epochstr = strchr(nvra, '@')) != 0)
144 if ((sizestr = strchr(epochstr, '@')) != 0)
149 if ((groupstr = strchr(sizestr, '@')) != 0)
153 if ((n = strchr(groupstr, '@')) != 0)
156 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, groupstr);
158 size = strtoul(sizestr, 0, 10);
159 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, (unsigned int)((size + 1023) / 1024));
162 filename = pool_tmpjoin(pool, nvra, ".rpm", 0);
163 arch = strrchr(nvra, '.');
167 s->arch = pool_str2id(pool, arch, 1);
169 /* argh, do we have a distepoch or not, check self-provides */
172 Id id, lastid, *idp = s->repo->idarraydata + s->provides;
174 for (idp = s->repo->idarraydata + s->provides; (id = *idp) != 0; idp++)
176 const char *evr, *name;
181 rd = GETRELDEP(pool, id);
182 if (rd->flags != REL_EQ)
184 name = pool_id2str(pool, rd->name);
185 namel = strlen(name);
186 if (strncmp(name, nvra, namel) != 0 || nvra[namel] != '-')
188 evr = pool_id2str(pool, rd->evr);
189 evr = strrchr(evr, '-');
190 if (evr && strchr(evr, ':') != 0)
195 /* self provides found, and it contains a distepoch */
196 /* replace with self-provides distepoch to get rid of the disttag */
197 char *nvradistepoch = strrchr(nvra, '-');
200 Reldep *rd = GETRELDEP(pool, lastid);
201 const char *evr = pool_id2str(pool, rd->evr);
202 evr = strrchr(evr, '-');
203 if (evr && (evr = strchr(evr, ':')) != 0)
205 if (strlen(evr) < strlen(nvradistepoch))
206 strcpy(nvradistepoch, evr);
211 version = strrchr(nvra, '-');
214 char *release = version;
216 version = strrchr(nvra, '-');
224 s->name = pool_str2id(pool, nvra, 1);
225 if (epochstr && *epochstr && strcmp(epochstr, "0") != 0)
227 char *evr = pool_tmpjoin(pool, epochstr, ":", version);
228 s->evr = pool_str2id(pool, evr, 1);
231 s->evr = pool_str2id(pool, version, 1);
232 repodata_set_location(data, s - pool->solvables, 0, 0, filename);
233 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
234 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
239 char *tagend = strchr(buf + 1, '@');
242 pool_debug(pool, SOLV_ERROR, "unknown tag <%s>\n", buf + 1);
248 pool_debug(pool, SOLV_ERROR, "unclosed package at EOF\n");
249 repo_free_solvable_block(s->repo, s - pool->solvables, 1, 1);
252 if (!(flags & REPO_NO_INTERNALIZE))
253 repodata_internalize(data);
271 /* must be sorted by first column */
272 static struct stateswitch stateswitches[] = {
273 { STATE_START, "media_info", STATE_MEDIA_INFO, 0 },
274 { STATE_MEDIA_INFO, "info", STATE_INFO, 1 },
289 struct stateswitch *swtab[NUMSTATES];
290 enum state sbtab[NUMSTATES];
293 Hashmask joinhashmask;
296 static inline const char *
297 find_attr(const char *txt, const char **atts)
299 for (; *atts; atts += 2)
301 if (!strcmp(*atts, txt))
308 joinhash_init(Repo *repo, Hashmask *hmp)
310 Hashmask hm = mkmask(repo->nsolvables);
311 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
316 FOR_REPO_SOLVABLES(repo, i, s)
318 hh = HASHCHAIN_START;
321 h = HASHCHAIN_NEXT(h, hh, hm);
329 joinhash_lookup(Repo *repo, Hashtable ht, Hashmask hm, const char *fn, const char *distepoch)
332 const char *p, *vrstart, *vrend;
337 if (distepoch && !*distepoch)
345 arch = pool_str2id(repo->pool, p + 1, 0);
368 name = pool_strn2id(repo->pool, fn, p - fn, 0);
371 hh = HASHCHAIN_START;
375 Solvable *s = repo->pool->solvables + ht[h];
376 if (s->name == name && s->arch == arch)
378 /* too bad we don't know the epoch... */
379 const char *evr = pool_id2str(repo->pool, s->evr);
380 for (p = evr; *p >= '0' && *p <= '9'; p++)
382 if (p > evr && *p == ':')
386 if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == ':' && !strcmp(distepoch, evr + (vrend - vrstart + 1)))
389 else if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == 0)
392 h = HASHCHAIN_NEXT(h, hh, hm);
398 set_sourcerpm(Repodata *data, Solvable *s, Id handle, const char *sourcerpm)
400 const char *p, *sevr, *sarch, *name, *evr;
403 p = strrchr(sourcerpm, '.');
404 if (!p || strcmp(p, ".rpm") != 0)
407 while (p > sourcerpm && *p != '.')
409 if (*p != '.' || p == sourcerpm)
412 while (p > sourcerpm && *p != '-')
414 if (*p != '-' || p == sourcerpm)
417 while (p > sourcerpm && *p != '-')
419 if (*p != '-' || p == sourcerpm)
422 pool = s->repo->pool;
423 if (!strcmp(sarch, "src.rpm"))
424 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
425 else if (!strcmp(sarch, "nosrc.rpm"))
426 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
428 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
429 evr = pool_id2str(pool, s->evr);
430 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
431 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
433 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
434 name = pool_id2str(pool, s->name);
435 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
436 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
438 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
443 startElement(void *userData, const char *name, const char **atts)
445 struct parsedata *pd = userData;
446 Pool *pool = pd->pool;
447 struct stateswitch *sw;
449 if (pd->depth != pd->statedepth)
455 if (!pd->swtab[pd->state])
457 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
458 if (!strcmp(sw->ename, name))
460 if (sw->from != pd->state)
463 pd->docontent = sw->docontent;
464 pd->statedepth = pd->depth;
471 const char *fn = find_attr("fn", atts);
472 const char *distepoch = find_attr("distepoch", atts);
474 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
477 str = find_attr("url", atts);
479 repodata_set_str(pd->data, pd->solvable - pool->solvables, SOLVABLE_URL, str);
480 str = find_attr("license", atts);
482 repodata_set_poolstr(pd->data, pd->solvable - pool->solvables, SOLVABLE_LICENSE, str);
483 str = find_attr("sourcerpm", atts);
485 set_sourcerpm(pd->data, pd->solvable, pd->solvable - pool->solvables, str);
494 endElement(void *userData, const char *name)
496 struct parsedata *pd = userData;
497 Solvable *s = pd->solvable;
498 if (pd->depth != pd->statedepth)
508 if (s && *pd->content)
509 repodata_set_str(pd->data, s - pd->pool->solvables, SOLVABLE_DESCRIPTION, pd->content);
514 pd->state = pd->sbtab[pd->state];
519 characterData(void *userData, const XML_Char *s, int len)
521 struct parsedata *pd = userData;
526 l = pd->lcontent + len + 1;
527 if (l > pd->acontent)
529 pd->content = solv_realloc(pd->content, l + 256);
530 pd->acontent = l + 256;
532 c = pd->content + pd->lcontent;
539 #define BUFF_SIZE 8192
542 repo_add_mdk_info(Repo *repo, FILE *fp, int flags)
548 struct stateswitch *sw;
551 if (!(flags & REPO_EXTEND_SOLVABLES))
553 pool_debug(repo->pool, SOLV_ERROR, "repo_add_mdk_info: can only extend existing solvables\n");
557 data = repo_add_repodata(repo, flags);
559 memset(&pd, 0, sizeof(pd));
561 pd.pool = repo->pool;
564 pd.content = solv_malloc(256);
567 pd.joinhash = joinhash_init(repo, &pd.joinhashmask);
569 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
571 if (!pd.swtab[sw->from])
572 pd.swtab[sw->from] = sw;
573 pd.sbtab[sw->to] = sw->from;
576 parser = XML_ParserCreate(NULL);
577 XML_SetUserData(parser, &pd);
578 XML_SetElementHandler(parser, startElement, endElement);
579 XML_SetCharacterDataHandler(parser, characterData);
582 l = fread(buf, 1, sizeof(buf), fp);
583 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
585 pool_debug(pd.pool, SOLV_ERROR, "%s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
591 XML_ParserFree(parser);
592 solv_free(pd.content);
593 solv_free(pd.joinhash);
594 if (!(flags & REPO_NO_INTERNALIZE))
595 repodata_internalize(data);