2 * Copyright (c) 2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
23 static unsigned char *
24 decompress(unsigned char *in, int inl, int *outlp)
30 memset(&strm, 0, sizeof(strm));
33 out = solv_malloc(4096);
35 strm.avail_out = 4096;
37 ret = inflateInit2(&strm, -MAX_WBITS);
45 if (strm.avail_out == 0)
48 out = solv_realloc(out, outl + 4096);
49 strm.next_out = out + outl;
50 strm.avail_out = 4096;
52 ret = inflate(&strm, Z_NO_FLUSH);
53 if (ret == Z_STREAM_END)
61 outl += 4096 - strm.avail_out;
68 parseonedep(Pool *pool, char *p)
70 char *n, *ne, *e, *ee;
74 while (*p == ' ' || *p == '\t' || *p == '\n')
79 /* find end of name */
80 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '(' && *p != '|')
83 while (*p == ' ' || *p == '\t' || *p == '\n')
91 while (*p == ' ' || *p == '\t' || *p == '\n')
112 while (*p == ' ' || *p == '\t' || *p == '\n')
115 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != ')')
118 while (*p && *p != ')')
122 while (*p == ' ' || *p == '\t' || *p == '\n')
125 name = pool_strn2id(pool, n, ne - n, 1);
128 evr = pool_strn2id(pool, e, ee - e, 1);
129 name = pool_rel2id(pool, name, evr, flags, 1);
133 Id id = parseonedep(pool, p + 1);
135 name = pool_rel2id(pool, name, id, REL_OR, 1);
141 makedeps(Repo *repo, char *deps, unsigned int olddeps, Id marker)
143 Pool *pool = repo->pool;
147 while ((p = strchr(deps, ',')) != 0)
150 olddeps = makedeps(repo, deps, olddeps, marker);
154 id = parseonedep(pool, deps);
157 return repo_addid_dep(repo, olddeps, id, marker);
161 /* put data from control file into the solvable */
162 /* warning: does inplace changes */
164 control2solvable(Solvable *s, Repodata *data, char *control)
166 Repo *repo = s->repo;
167 Pool *pool = repo->pool;
168 char *p, *q, *end, *tag;
171 char checksum[32 * 2 + 1];
181 if (p[1] == ' ' || p[1] == '\t')
184 /* continuation line */
186 while (q >= control && *q == ' ' && *q == '\t')
190 memmove(p + 1 - l, control, l);
199 /* strip trailing space */
200 while (end >= control && *end == ' ' && *end == '\t')
204 q = strchr(tag, ':');
205 if (!q || q - tag < 4)
208 while (*q == ' ' || *q == '\t')
210 x = '@' + (tag[0] & 0x1f);
211 x = (x << 8) + '@' + (tag[1] & 0x1f);
215 if (!strcasecmp(tag, "architecture"))
216 s->arch = pool_str2id(pool, q, 1);
219 if (!strcasecmp(tag, "breaks"))
220 s->conflicts = makedeps(repo, q, s->conflicts, 0);
223 if (!strcasecmp(tag, "conflicts"))
224 s->conflicts = makedeps(repo, q, s->conflicts, 0);
227 if (!strcasecmp(tag, "depends"))
228 s->requires = makedeps(repo, q, s->requires, -SOLVABLE_PREREQMARKER);
229 else if (!strcasecmp(tag, "description"))
231 char *ld = strchr(q, '\n');
235 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, ld);
238 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, q);
239 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, q);
243 if (!strcasecmp(tag, "enhances"))
244 s->enhances = makedeps(repo, q, s->enhances, 0);
247 if (!strcasecmp(tag, "filename"))
248 repodata_set_location(data, s - pool->solvables, 0, 0, q);
251 if (!strcasecmp(tag, "homepage"))
252 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, q);
255 if (!strcasecmp(tag, "installed-size"))
256 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(q, 0, 10) << 10);
259 if (!strcasecmp(tag, "md5sum") && !checksumtype && strlen(q) == 16 * 2)
262 checksumtype = REPOKEY_TYPE_MD5;
266 if (!strcasecmp(tag, "package"))
267 s->name = pool_str2id(pool, q, 1);
270 if (!strcasecmp(tag, "pre-depends"))
271 s->requires = makedeps(repo, q, s->requires, SOLVABLE_PREREQMARKER);
272 else if (!strcasecmp(tag, "provides"))
273 s->provides = makedeps(repo, q, s->provides, 0);
276 if (!strcasecmp(tag, "replaces"))
277 s->obsoletes = makedeps(repo, q, s->obsoletes, 0);
278 else if (!strcasecmp(tag, "recommends"))
279 s->recommends = makedeps(repo, q, s->recommends, 0);
282 newtype = solv_chksum_str2type(tag);
283 if (!newtype || solv_chksum_len(newtype) * 2 != strlen(q))
285 if (!checksumtype || (newtype == REPOKEY_TYPE_SHA1 && checksumtype != REPOKEY_TYPE_SHA256) || newtype == REPOKEY_TYPE_SHA256)
288 checksumtype = newtype;
292 if (!strcasecmp(tag, "source"))
295 /* ignore version for now */
296 for (q2 = q; *q2; q2++)
297 if (*q2 == ' ' || *q2 == '\t')
302 if (s->name && !strcmp(q, pool_id2str(pool, s->name)))
303 repodata_set_void(data, s - pool->solvables, SOLVABLE_SOURCENAME);
305 repodata_set_id(data, s - pool->solvables, SOLVABLE_SOURCENAME, pool_str2id(pool, q, 1));
310 if (!strcasecmp(tag, "suggests"))
311 s->suggests = makedeps(repo, q, s->suggests, 0);
314 if (!strcasecmp(tag, "version"))
315 s->evr = pool_str2id(pool, q, 1);
320 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, checksumtype, checksum);
326 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
327 if (s->name && !havesource)
328 repodata_set_void(data, s - pool->solvables, SOLVABLE_SOURCENAME);
331 /* obsoletes only count when the packages also conflict */
332 /* XXX: should not transcode here */
335 for (i = j = s->obsoletes; (d = repo->idarraydata[i]) != 0; i++)
339 for (k = s->conflicts; (cid = repo->idarraydata[k]) != 0; k++)
341 if (repo->idarraydata[k] == cid)
345 Reldep *rd = GETRELDEP(pool, cid);
346 if (rd->flags < 8 && rd->name == d)
347 break; /* specialize obsoletes */
351 repo->idarraydata[j++] = cid;
353 repo->idarraydata[j] = 0;
354 if (j == s->obsoletes)
360 repo_add_debpackages(Repo *repo, FILE *fp, int flags)
362 Pool *pool = repo->pool;
368 data = repo_add_repodata(repo, flags);
369 buf = solv_malloc(4096);
376 if (!(p = strchr(p, '\n')))
379 if (l + 1024 >= bufl)
381 buf = solv_realloc(buf, bufl + 4096);
387 ll = fread(p, 1, bufl - l - 1, fp);
391 while ((l3 = strlen(p)) < ll)
401 s = pool_id2solvable(pool, repo_add_solvable(repo));
402 control2solvable(s, data, buf);
404 repo_free_solvable(repo, s - pool->solvables, 1);
406 memmove(buf, p + 1, l - ll);
413 s = pool_id2solvable(pool, repo_add_solvable(repo));
414 control2solvable(s, data, buf);
416 repo_free_solvable(repo, s - pool->solvables, 1);
419 if (!(flags & REPO_NO_INTERNALIZE))
420 repodata_internalize(data);
425 repo_add_debdb(Repo *repo, int flags)
428 const char *path = "/var/lib/dpkg/status";
429 if (flags & REPO_USE_ROOTDIR)
430 path = pool_prepend_rootdir_tmp(repo->pool, path);
431 if ((fp = fopen(path, "r")) == 0)
432 return pool_error(repo->pool, -1, "%s: %s", path, strerror(errno));
433 repo_add_debpackages(repo, fp, flags);
439 repo_add_deb(Repo *repo, const char *deb, int flags)
441 Pool *pool = repo->pool;
443 unsigned char buf[4096], *bp;
444 int l, l2, vlen, clen, ctarlen;
446 unsigned char pkgid[16];
453 data = repo_add_repodata(repo, flags);
454 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, deb) : deb, "r")) == 0)
456 pool_error(pool, -1, "%s: %s", deb, strerror(errno));
459 if (fstat(fileno(fp), &stb))
461 pool_error(pool, -1, "fstat: %s", strerror(errno));
465 l = fread(buf, 1, sizeof(buf), fp);
466 if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary ", 8 + 16) != 0)
468 pool_error(pool, -1, "%s: not a deb package", deb);
472 vlen = atoi((char *)buf + 8 + 48);
473 if (vlen < 0 || vlen > l)
475 pool_error(pool, -1, "%s: not a deb package", deb);
480 if (l < 8 + 60 + vlen + 60)
482 pool_error(pool, -1, "%s: unhandled deb package", deb);
486 if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz ", 16) != 0)
488 pool_error(pool, -1, "%s: control.tar.gz is not second entry", deb);
492 clen = atoi((char *)buf + 8 + 60 + vlen + 48);
493 if (clen <= 0 || clen >= 0x100000)
495 pool_error(pool, -1, "%s: control.tar.gz has illegal size", deb);
499 ctgz = solv_calloc(1, clen + 4);
500 bp = buf + 8 + 60 + vlen + 60;
501 l -= 8 + 60 + vlen + 60;
508 if (fread(ctgz + l, clen - l, 1, fp) != 1)
510 pool_error(pool, -1, "%s: unexpected EOF", deb);
518 if (flags & DEBS_ADD_WITH_PKGID)
520 void *handle = solv_chksum_create(REPOKEY_TYPE_MD5);
521 solv_chksum_add(handle, ctgz, clen);
522 solv_chksum_free(handle, pkgid);
525 if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
527 pool_error(pool, -1, "%s: control.tar.gz is not gzipped", deb);
531 if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
533 pool_error(pool, -1, "%s: control.tar.gz is compressed in a strange way", deb);
538 bp += 6; /* skip time, xflags and OS code */
541 /* skip extra field */
542 l = bp[0] | bp[1] << 8;
544 if (bp >= ctgz + clen)
546 pool_error(pool, -1, "%s: control.tar.gz is corrupt", deb);
551 if (ctgz[3] & 0x08) /* orig filename */
554 if (ctgz[3] & 0x10) /* file comment */
557 if (ctgz[3] & 0x02) /* header crc */
559 if (bp >= ctgz + clen)
561 pool_error(pool, -1, "%s: control.tar.gz is corrupt", deb);
565 ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
569 pool_error(pool, -1, "%s: control.tar.gz is corrupt", deb);
578 for (j = 124; j < 124 + 12; j++)
579 if (bp[j] >= '0' && bp[j] <= '7')
580 l2 = l2 * 8 + (bp[j] - '0');
581 if (!strcmp((char *)bp, "./control") || !strcmp((char *)bp, "control"))
583 l2 = 512 + ((l2 + 511) & ~511);
587 if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
589 pool_error(pool, -1, "%s: control.tar.gz contains no control file", deb);
593 memmove(ctar, bp + 512, l2);
594 ctar = solv_realloc(ctar, l2 + 1);
596 s = pool_id2solvable(pool, repo_add_solvable(repo));
597 control2solvable(s, data, (char *)ctar);
598 if (!(flags & REPO_NO_LOCATION))
599 repodata_set_location(data, s - pool->solvables, 0, 0, deb);
600 if (S_ISREG(stb.st_mode))
601 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
603 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
605 if (!(flags & REPO_NO_INTERNALIZE))
606 repodata_internalize(data);
607 return s - pool->solvables;