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))
117 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(buf + 10, 0, 10));
118 else if (!strncmp(buf + 1, "summary@", 8))
119 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, buf + 9);
120 else if (!strncmp(buf + 1, "provides@", 9))
121 s->provides = parse_deps(s, buf + 10, 0);
122 else if (!strncmp(buf + 1, "requires@", 9))
123 s->requires = parse_deps(s, buf + 10, SOLVABLE_PREREQMARKER);
124 else if (!strncmp(buf + 1, "suggests@", 9))
125 s->suggests = parse_deps(s, buf + 10, 0);
126 else if (!strncmp(buf + 1, "obsoletes@", 10))
127 s->obsoletes = parse_deps(s, buf + 11, 0);
128 else if (!strncmp(buf + 1, "conflicts@", 10))
129 s->conflicts = parse_deps(s, buf + 11, 0);
130 else if (!strncmp(buf + 1, "info@", 5))
132 char *nvra = buf + 6;
137 if ((epochstr = strchr(nvra, '@')) != 0)
141 if ((sizestr = strchr(epochstr, '@')) != 0)
145 if ((groupstr = strchr(sizestr, '@')) != 0)
149 if ((n = strchr(groupstr, '@')) != 0)
152 repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, groupstr);
154 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(sizestr, 0, 10));
157 filename = pool_tmpjoin(pool, nvra, ".rpm", 0);
158 arch = strrchr(nvra, '.');
162 s->arch = pool_str2id(pool, arch, 1);
164 /* argh, do we have a distepoch or not, check self-provides */
167 Id id, lastid, *idp = s->repo->idarraydata + s->provides;
169 for (idp = s->repo->idarraydata + s->provides; (id = *idp) != 0; idp++)
171 const char *evr, *name;
176 rd = GETRELDEP(pool, id);
177 if (rd->flags != REL_EQ)
179 name = pool_id2str(pool, rd->name);
180 namel = strlen(name);
181 if (strncmp(name, nvra, namel) != 0 || nvra[namel] != '-')
183 evr = pool_id2str(pool, rd->evr);
184 evr = strrchr(evr, '-');
185 if (evr && strchr(evr, ':') != 0)
190 /* self provides found, and it contains a distepoch */
191 /* replace with self-provides distepoch to get rid of the disttag */
192 char *nvradistepoch = strrchr(nvra, '-');
195 Reldep *rd = GETRELDEP(pool, lastid);
196 const char *evr = pool_id2str(pool, rd->evr);
197 evr = strrchr(evr, '-');
198 if (evr && (evr = strchr(evr, ':')) != 0)
200 if (strlen(evr) < strlen(nvradistepoch))
201 strcpy(nvradistepoch, evr);
206 version = strrchr(nvra, '-');
209 char *release = version;
211 version = strrchr(nvra, '-');
219 s->name = pool_str2id(pool, nvra, 1);
220 if (epochstr && *epochstr && strcmp(epochstr, "0") != 0)
222 char *evr = pool_tmpjoin(pool, epochstr, ":", version);
223 s->evr = pool_str2id(pool, evr, 1);
226 s->evr = pool_str2id(pool, version, 1);
227 repodata_set_location(data, s - pool->solvables, 0, 0, filename);
228 if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
229 s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
234 char *tagend = strchr(buf + 1, '@');
237 pool_debug(pool, SOLV_ERROR, "unknown tag <%s>\n", buf + 1);
243 pool_debug(pool, SOLV_ERROR, "unclosed package at EOF\n");
244 repo_free_solvable(s->repo, s - pool->solvables, 1);
247 if (!(flags & REPO_NO_INTERNALIZE))
248 repodata_internalize(data);
267 /* must be sorted by first column */
268 static struct stateswitch stateswitches[] = {
269 { STATE_START, "media_info", STATE_MEDIA_INFO, 0 },
270 { STATE_MEDIA_INFO, "info", STATE_INFO, 1 },
271 { STATE_MEDIA_INFO, "files", STATE_FILES, 1 },
286 struct stateswitch *swtab[NUMSTATES];
287 enum state sbtab[NUMSTATES];
290 Hashval joinhashmask;
293 static inline const char *
294 find_attr(const char *txt, const char **atts)
296 for (; *atts; atts += 2)
298 if (!strcmp(*atts, txt))
305 joinhash_init(Repo *repo, Hashval *hmp)
307 Hashval hm = mkmask(repo->nsolvables);
308 Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
313 FOR_REPO_SOLVABLES(repo, i, s)
315 hh = HASHCHAIN_START;
318 h = HASHCHAIN_NEXT(h, hh, hm);
326 joinhash_lookup(Repo *repo, Hashtable ht, Hashval hm, const char *fn, const char *distepoch)
329 const char *p, *vrstart, *vrend;
334 if (distepoch && !*distepoch)
342 arch = pool_str2id(repo->pool, p + 1, 0);
365 name = pool_strn2id(repo->pool, fn, p - fn, 0);
368 hh = HASHCHAIN_START;
372 Solvable *s = repo->pool->solvables + ht[h];
373 if (s->name == name && s->arch == arch)
375 /* too bad we don't know the epoch... */
376 const char *evr = pool_id2str(repo->pool, s->evr);
377 for (p = evr; *p >= '0' && *p <= '9'; p++)
379 if (p > evr && *p == ':')
383 if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == ':' && !strcmp(distepoch, evr + (vrend - vrstart + 1)))
386 else if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == 0)
389 h = HASHCHAIN_NEXT(h, hh, hm);
395 startElement(void *userData, const char *name, const char **atts)
397 struct parsedata *pd = userData;
398 Pool *pool = pd->pool;
399 struct stateswitch *sw;
401 if (pd->depth != pd->statedepth)
407 if (!pd->swtab[pd->state])
409 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
410 if (!strcmp(sw->ename, name))
412 if (sw->from != pd->state)
415 pd->docontent = sw->docontent;
416 pd->statedepth = pd->depth;
423 const char *fn = find_attr("fn", atts);
424 const char *distepoch = find_attr("distepoch", atts);
426 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
429 str = find_attr("url", atts);
431 repodata_set_str(pd->data, pd->solvable - pool->solvables, SOLVABLE_URL, str);
432 str = find_attr("license", atts);
434 repodata_set_poolstr(pd->data, pd->solvable - pool->solvables, SOLVABLE_LICENSE, str);
435 str = find_attr("sourcerpm", atts);
437 repodata_set_sourcepkg(pd->data, pd->solvable - pool->solvables, str);
442 const char *fn = find_attr("fn", atts);
443 const char *distepoch = find_attr("distepoch", atts);
444 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
453 endElement(void *userData, const char *name)
455 struct parsedata *pd = userData;
456 Solvable *s = pd->solvable;
457 if (pd->depth != pd->statedepth)
467 if (s && *pd->content)
468 repodata_set_str(pd->data, s - pd->pool->solvables, SOLVABLE_DESCRIPTION, pd->content);
471 if (s && *pd->content)
474 for (p = pd->content; p && *p; p = np)
477 np = strchr(p, '\n');
482 sl = strrchr(p, '/');
486 id = repodata_str2dir(pd->data, p, 1);
494 id = repodata_str2dir(pd->data, "/", 1);
495 repodata_add_dirstr(pd->data, s - pd->pool->solvables, SOLVABLE_FILELIST, id, sl);
502 pd->state = pd->sbtab[pd->state];
507 characterData(void *userData, const XML_Char *s, int len)
509 struct parsedata *pd = userData;
514 l = pd->lcontent + len + 1;
515 if (l > pd->acontent)
517 pd->content = solv_realloc(pd->content, l + 256);
518 pd->acontent = l + 256;
520 c = pd->content + pd->lcontent;
527 #define BUFF_SIZE 8192
530 repo_add_mdk_info(Repo *repo, FILE *fp, int flags)
536 struct stateswitch *sw;
539 if (!(flags & REPO_EXTEND_SOLVABLES))
541 pool_debug(repo->pool, SOLV_ERROR, "repo_add_mdk_info: can only extend existing solvables\n");
545 data = repo_add_repodata(repo, flags);
547 memset(&pd, 0, sizeof(pd));
549 pd.pool = repo->pool;
552 pd.content = solv_malloc(256);
555 pd.joinhash = joinhash_init(repo, &pd.joinhashmask);
557 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
559 if (!pd.swtab[sw->from])
560 pd.swtab[sw->from] = sw;
561 pd.sbtab[sw->to] = sw->from;
564 parser = XML_ParserCreate(NULL);
565 XML_SetUserData(parser, &pd);
566 XML_SetElementHandler(parser, startElement, endElement);
567 XML_SetCharacterDataHandler(parser, characterData);
570 l = fread(buf, 1, sizeof(buf), fp);
571 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
573 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));
579 XML_ParserFree(parser);
580 solv_free(pd.content);
581 solv_free(pd.joinhash);
582 if (!(flags & REPO_NO_INTERNALIZE))
583 repodata_internalize(data);