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);
100 if (buf[bufl - 1] != '\n')
102 if (bufa - bufl < 256)
105 buf = solv_realloc(buf, bufa);
113 pool_debug(pool, SOLV_ERROR, "bad line <%s>\n", buf);
117 s = pool_id2solvable(pool, repo_add_solvable(repo));
118 if (!strncmp(buf + 1, "filesize@", 9))
119 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(buf + 10, 0, 10));
120 else if (!strncmp(buf + 1, "summary@", 8))
121 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, buf + 9);
122 else if (!strncmp(buf + 1, "provides@", 9))
123 s->provides = parse_deps(s, buf + 10, 0);
124 else if (!strncmp(buf + 1, "requires@", 9))
125 s->requires = parse_deps(s, buf + 10, SOLVABLE_PREREQMARKER);
126 else if (!strncmp(buf + 1, "suggests@", 9))
127 s->suggests = parse_deps(s, buf + 10, 0);
128 else if (!strncmp(buf + 1, "obsoletes@", 10))
129 s->obsoletes = parse_deps(s, buf + 11, 0);
130 else if (!strncmp(buf + 1, "conflicts@", 10))
131 s->conflicts = parse_deps(s, buf + 11, 0);
132 else if (!strncmp(buf + 1, "info@", 5))
134 char *nvra = buf + 6;
139 if ((epochstr = strchr(nvra, '@')) != 0)
143 if ((sizestr = strchr(epochstr, '@')) != 0)
147 if ((groupstr = strchr(sizestr, '@')) != 0)
151 if ((n = strchr(groupstr, '@')) != 0)
154 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, groupstr);
156 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(sizestr, 0, 10));
159 filename = pool_tmpjoin(pool, nvra, ".rpm", 0);
160 arch = strrchr(nvra, '.');
164 s->arch = pool_str2id(pool, arch, 1);
166 /* argh, do we have a distepoch or not, check self-provides */
169 Id id, lastid, *idp = s->repo->idarraydata + s->provides;
171 for (idp = s->repo->idarraydata + s->provides; (id = *idp) != 0; idp++)
173 const char *evr, *name;
178 rd = GETRELDEP(pool, id);
179 if (rd->flags != REL_EQ)
181 name = pool_id2str(pool, rd->name);
182 namel = strlen(name);
183 if (strncmp(name, nvra, namel) != 0 || nvra[namel] != '-')
185 evr = pool_id2str(pool, rd->evr);
186 evr = strrchr(evr, '-');
187 if (evr && strchr(evr, ':') != 0)
192 /* self provides found, and it contains a distepoch */
193 /* replace with self-provides distepoch to get rid of the disttag */
194 char *nvradistepoch = strrchr(nvra, '-');
197 Reldep *rd = GETRELDEP(pool, lastid);
198 const char *evr = pool_id2str(pool, rd->evr);
199 evr = strrchr(evr, '-');
200 if (evr && (evr = strchr(evr, ':')) != 0)
202 if (strlen(evr) < strlen(nvradistepoch))
203 strcpy(nvradistepoch, evr);
208 version = strrchr(nvra, '-');
211 char *release = version;
213 version = strrchr(nvra, '-');
221 s->name = pool_str2id(pool, nvra, 1);
222 if (epochstr && *epochstr && strcmp(epochstr, "0") != 0)
224 char *evr = pool_tmpjoin(pool, epochstr, ":", version);
225 s->evr = pool_str2id(pool, evr, 1);
228 s->evr = pool_str2id(pool, version, 1);
229 repodata_set_location(data, s - pool->solvables, 0, 0, filename);
230 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
231 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
236 char *tagend = strchr(buf + 1, '@');
239 pool_debug(pool, SOLV_ERROR, "unknown tag <%s>\n", buf + 1);
245 pool_debug(pool, SOLV_ERROR, "unclosed package at EOF\n");
246 repo_free_solvable(s->repo, s - pool->solvables, 1);
249 if (!(flags & REPO_NO_INTERNALIZE))
250 repodata_internalize(data);
269 /* must be sorted by first column */
270 static struct stateswitch stateswitches[] = {
271 { STATE_START, "media_info", STATE_MEDIA_INFO, 0 },
272 { STATE_MEDIA_INFO, "info", STATE_INFO, 1 },
273 { STATE_MEDIA_INFO, "files", STATE_FILES, 1 },
288 struct stateswitch *swtab[NUMSTATES];
289 enum state sbtab[NUMSTATES];
292 Hashval joinhashmask;
295 static inline const char *
296 find_attr(const char *txt, const char **atts)
298 for (; *atts; atts += 2)
300 if (!strcmp(*atts, txt))
307 joinhash_init(Repo *repo, Hashval *hmp)
309 Hashval hm = mkmask(repo->nsolvables);
310 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
315 FOR_REPO_SOLVABLES(repo, i, s)
317 hh = HASHCHAIN_START;
320 h = HASHCHAIN_NEXT(h, hh, hm);
328 joinhash_lookup(Repo *repo, Hashtable ht, Hashval hm, const char *fn, const char *distepoch)
331 const char *p, *vrstart, *vrend;
336 if (distepoch && !*distepoch)
344 arch = pool_str2id(repo->pool, p + 1, 0);
367 name = pool_strn2id(repo->pool, fn, p - fn, 0);
370 hh = HASHCHAIN_START;
374 Solvable *s = repo->pool->solvables + ht[h];
375 if (s->name == name && s->arch == arch)
377 /* too bad we don't know the epoch... */
378 const char *evr = pool_id2str(repo->pool, s->evr);
379 for (p = evr; *p >= '0' && *p <= '9'; p++)
381 if (p > evr && *p == ':')
385 if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == ':' && !strcmp(distepoch, evr + (vrend - vrstart + 1)))
388 else if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == 0)
391 h = HASHCHAIN_NEXT(h, hh, hm);
397 startElement(void *userData, const char *name, const char **atts)
399 struct parsedata *pd = userData;
400 Pool *pool = pd->pool;
401 struct stateswitch *sw;
403 if (pd->depth != pd->statedepth)
409 if (!pd->swtab[pd->state])
411 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
412 if (!strcmp(sw->ename, name))
414 if (sw->from != pd->state)
417 pd->docontent = sw->docontent;
418 pd->statedepth = pd->depth;
425 const char *fn = find_attr("fn", atts);
426 const char *distepoch = find_attr("distepoch", atts);
428 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
431 str = find_attr("url", atts);
433 repodata_set_str(pd->data, pd->solvable - pool->solvables, SOLVABLE_URL, str);
434 str = find_attr("license", atts);
436 repodata_set_poolstr(pd->data, pd->solvable - pool->solvables, SOLVABLE_LICENSE, str);
437 str = find_attr("sourcerpm", atts);
439 repodata_set_sourcepkg(pd->data, pd->solvable - pool->solvables, str);
444 const char *fn = find_attr("fn", atts);
445 const char *distepoch = find_attr("distepoch", atts);
446 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
455 endElement(void *userData, const char *name)
457 struct parsedata *pd = userData;
458 Solvable *s = pd->solvable;
459 if (pd->depth != pd->statedepth)
469 if (s && *pd->content)
470 repodata_set_str(pd->data, s - pd->pool->solvables, SOLVABLE_DESCRIPTION, pd->content);
473 if (s && *pd->content)
476 for (p = pd->content; p && *p; p = np)
479 np = strchr(p, '\n');
484 sl = strrchr(p, '/');
488 id = repodata_str2dir(pd->data, p, 1);
496 id = repodata_str2dir(pd->data, "/", 1);
497 repodata_add_dirstr(pd->data, s - pd->pool->solvables, SOLVABLE_FILELIST, id, sl);
504 pd->state = pd->sbtab[pd->state];
509 characterData(void *userData, const XML_Char *s, int len)
511 struct parsedata *pd = userData;
516 l = pd->lcontent + len + 1;
517 if (l > pd->acontent)
519 pd->content = solv_realloc(pd->content, l + 256);
520 pd->acontent = l + 256;
522 c = pd->content + pd->lcontent;
529 #define BUFF_SIZE 8192
532 repo_add_mdk_info(Repo *repo, FILE *fp, int flags)
538 struct stateswitch *sw;
541 if (!(flags & REPO_EXTEND_SOLVABLES))
543 pool_debug(repo->pool, SOLV_ERROR, "repo_add_mdk_info: can only extend existing solvables\n");
547 data = repo_add_repodata(repo, flags);
549 memset(&pd, 0, sizeof(pd));
551 pd.pool = repo->pool;
554 pd.content = solv_malloc(256);
557 pd.joinhash = joinhash_init(repo, &pd.joinhashmask);
559 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
561 if (!pd.swtab[sw->from])
562 pd.swtab[sw->from] = sw;
563 pd.sbtab[sw->to] = sw->from;
566 parser = XML_ParserCreate(NULL);
567 XML_SetUserData(parser, &pd);
568 XML_SetElementHandler(parser, startElement, endElement);
569 XML_SetCharacterDataHandler(parser, characterData);
572 l = fread(buf, 1, sizeof(buf), fp);
573 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
575 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));
581 XML_ParserFree(parser);
582 solv_free(pd.content);
583 solv_free(pd.joinhash);
584 if (!(flags & REPO_NO_INTERNALIZE))
585 repodata_internalize(data);