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 Hashmask 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, Hashmask *hmp)
307 Hashmask 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, Hashmask 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 set_sourcerpm(Repodata *data, Solvable *s, Id handle, const char *sourcerpm)
397 const char *p, *sevr, *sarch, *name, *evr;
400 p = strrchr(sourcerpm, '.');
401 if (!p || strcmp(p, ".rpm") != 0)
404 while (p > sourcerpm && *p != '.')
406 if (*p != '.' || p == sourcerpm)
409 while (p > sourcerpm && *p != '-')
411 if (*p != '-' || p == sourcerpm)
414 while (p > sourcerpm && *p != '-')
416 if (*p != '-' || p == sourcerpm)
419 pool = s->repo->pool;
420 if (!strcmp(sarch, "src.rpm"))
421 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_SRC);
422 else if (!strcmp(sarch, "nosrc.rpm"))
423 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
425 repodata_set_constantid(data, handle, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
426 evr = pool_id2str(pool, s->evr);
427 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
428 repodata_set_void(data, handle, SOLVABLE_SOURCEEVR);
430 repodata_set_id(data, handle, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
431 name = pool_id2str(pool, s->name);
432 if (name && !strncmp(sourcerpm, name, sevr - sourcerpm - 1) && name[sevr - sourcerpm - 1] == 0)
433 repodata_set_void(data, handle, SOLVABLE_SOURCENAME);
435 repodata_set_id(data, handle, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcerpm, sevr - sourcerpm - 1, 1));
440 startElement(void *userData, const char *name, const char **atts)
442 struct parsedata *pd = userData;
443 Pool *pool = pd->pool;
444 struct stateswitch *sw;
446 if (pd->depth != pd->statedepth)
452 if (!pd->swtab[pd->state])
454 for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
455 if (!strcmp(sw->ename, name))
457 if (sw->from != pd->state)
460 pd->docontent = sw->docontent;
461 pd->statedepth = pd->depth;
468 const char *fn = find_attr("fn", atts);
469 const char *distepoch = find_attr("distepoch", atts);
471 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
474 str = find_attr("url", atts);
476 repodata_set_str(pd->data, pd->solvable - pool->solvables, SOLVABLE_URL, str);
477 str = find_attr("license", atts);
479 repodata_set_poolstr(pd->data, pd->solvable - pool->solvables, SOLVABLE_LICENSE, str);
480 str = find_attr("sourcerpm", atts);
482 set_sourcerpm(pd->data, pd->solvable, pd->solvable - pool->solvables, str);
487 const char *fn = find_attr("fn", atts);
488 const char *distepoch = find_attr("distepoch", atts);
489 pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
498 endElement(void *userData, const char *name)
500 struct parsedata *pd = userData;
501 Solvable *s = pd->solvable;
502 if (pd->depth != pd->statedepth)
512 if (s && *pd->content)
513 repodata_set_str(pd->data, s - pd->pool->solvables, SOLVABLE_DESCRIPTION, pd->content);
516 if (s && *pd->content)
519 for (p = pd->content; p && *p; p = np)
522 np = strchr(p, '\n');
527 sl = strrchr(p, '/');
531 id = repodata_str2dir(pd->data, p, 1);
539 id = repodata_str2dir(pd->data, "/", 1);
540 repodata_add_dirstr(pd->data, s - pd->pool->solvables, SOLVABLE_FILELIST, id, sl);
547 pd->state = pd->sbtab[pd->state];
552 characterData(void *userData, const XML_Char *s, int len)
554 struct parsedata *pd = userData;
559 l = pd->lcontent + len + 1;
560 if (l > pd->acontent)
562 pd->content = solv_realloc(pd->content, l + 256);
563 pd->acontent = l + 256;
565 c = pd->content + pd->lcontent;
572 #define BUFF_SIZE 8192
575 repo_add_mdk_info(Repo *repo, FILE *fp, int flags)
581 struct stateswitch *sw;
584 if (!(flags & REPO_EXTEND_SOLVABLES))
586 pool_debug(repo->pool, SOLV_ERROR, "repo_add_mdk_info: can only extend existing solvables\n");
590 data = repo_add_repodata(repo, flags);
592 memset(&pd, 0, sizeof(pd));
594 pd.pool = repo->pool;
597 pd.content = solv_malloc(256);
600 pd.joinhash = joinhash_init(repo, &pd.joinhashmask);
602 for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
604 if (!pd.swtab[sw->from])
605 pd.swtab[sw->from] = sw;
606 pd.sbtab[sw->to] = sw->from;
609 parser = XML_ParserCreate(NULL);
610 XML_SetUserData(parser, &pd);
611 XML_SetElementHandler(parser, startElement, endElement);
612 XML_SetCharacterDataHandler(parser, characterData);
615 l = fread(buf, 1, sizeof(buf), fp);
616 if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
618 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));
624 XML_ParserFree(parser);
625 solv_free(pd.content);
626 solv_free(pd.joinhash);
627 if (!(flags & REPO_NO_INTERNALIZE))
628 repodata_internalize(data);