2 * Copyright (c) 2009, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
22 static unsigned char *
23 decompress(unsigned char *in, int inl, int *outlp)
29 memset(&strm, 0, sizeof(strm));
32 out = solv_malloc(4096);
34 strm.avail_out = 4096;
36 ret = inflateInit2(&strm, -MAX_WBITS);
44 if (strm.avail_out == 0)
47 out = solv_realloc(out, outl + 4096);
48 strm.next_out = out + outl;
49 strm.avail_out = 4096;
51 ret = inflate(&strm, Z_NO_FLUSH);
52 if (ret == Z_STREAM_END)
60 outl += 4096 - strm.avail_out;
67 parseonedep(Pool *pool, char *p)
69 char *n, *ne, *e, *ee;
73 while (*p == ' ' || *p == '\t' || *p == '\n')
78 /* find end of name */
79 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '(' && *p != '|')
82 while (*p == ' ' || *p == '\t' || *p == '\n')
90 while (*p == ' ' || *p == '\t' || *p == '\n')
111 while (*p == ' ' || *p == '\t' || *p == '\n')
114 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != ')')
117 while (*p && *p != ')')
121 while (*p == ' ' || *p == '\t' || *p == '\n')
124 name = pool_strn2id(pool, n, ne - n, 1);
127 evr = pool_strn2id(pool, e, ee - e, 1);
128 name = pool_rel2id(pool, name, evr, flags, 1);
132 Id id = parseonedep(pool, p + 1);
134 name = pool_rel2id(pool, name, id, REL_OR, 1);
140 makedeps(Repo *repo, char *deps, unsigned int olddeps, Id marker)
142 Pool *pool = repo->pool;
146 while ((p = strchr(deps, ',')) != 0)
149 olddeps = makedeps(repo, deps, olddeps, marker);
153 id = parseonedep(pool, deps);
156 return repo_addid_dep(repo, olddeps, id, marker);
160 /* put data from control file into the solvable */
161 /* warning: does inplace changes */
163 control2solvable(Solvable *s, Repodata *data, char *control)
165 Repo *repo = s->repo;
166 Pool *pool = repo->pool;
167 char *p, *q, *end, *tag;
170 char checksum[32 * 2 + 1];
180 if (p[1] == ' ' || p[1] == '\t')
183 /* continuation line */
185 while (q >= control && *q == ' ' && *q == '\t')
189 memmove(p + 1 - l, control, l);
198 /* strip trailing space */
199 while (end >= control && *end == ' ' && *end == '\t')
203 q = strchr(tag, ':');
204 if (!q || q - tag < 4)
207 while (*q == ' ' || *q == '\t')
209 x = '@' + (tag[0] & 0x1f);
210 x = (x << 8) + '@' + (tag[1] & 0x1f);
214 if (!strcasecmp(tag, "architecture"))
215 s->arch = pool_str2id(pool, q, 1);
218 if (!strcasecmp(tag, "breaks"))
219 s->conflicts = makedeps(repo, q, s->conflicts, 0);
222 if (!strcasecmp(tag, "conflicts"))
223 s->conflicts = makedeps(repo, q, s->conflicts, 0);
226 if (!strcasecmp(tag, "depends"))
227 s->requires = makedeps(repo, q, s->requires, -SOLVABLE_PREREQMARKER);
228 else if (!strcasecmp(tag, "description"))
230 char *ld = strchr(q, '\n');
234 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, ld);
237 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, q);
238 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, q);
242 if (!strcasecmp(tag, "enhances"))
243 s->enhances = makedeps(repo, q, s->enhances, 0);
246 if (!strcasecmp(tag, "filename"))
247 repodata_set_location(data, s - pool->solvables, 0, 0, q);
250 if (!strcasecmp(tag, "homepage"))
251 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, q);
254 if (!strcasecmp(tag, "installed-size"))
255 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
258 if (!strcasecmp(tag, "md5sum") && !checksumtype && strlen(q) == 16 * 2)
261 checksumtype = REPOKEY_TYPE_MD5;
265 if (!strcasecmp(tag, "package"))
266 s->name = pool_str2id(pool, q, 1);
269 if (!strcasecmp(tag, "pre-depends"))
270 s->requires = makedeps(repo, q, s->requires, SOLVABLE_PREREQMARKER);
271 else if (!strcasecmp(tag, "provides"))
272 s->provides = makedeps(repo, q, s->provides, 0);
275 if (!strcasecmp(tag, "replaces"))
276 s->obsoletes = makedeps(repo, q, s->conflicts, 0);
277 else if (!strcasecmp(tag, "recommends"))
278 s->recommends = makedeps(repo, q, s->recommends, 0);
281 newtype = solv_chksum_str2type(tag);
282 if (!newtype || solv_chksum_len(newtype) * 2 != strlen(q))
284 if (!checksumtype || (newtype == REPOKEY_TYPE_SHA1 && checksumtype != REPOKEY_TYPE_SHA256) || newtype == REPOKEY_TYPE_SHA256)
287 checksumtype = newtype;
291 if (!strcasecmp(tag, "source"))
294 /* ignore version for now */
295 for (q2 = q; *q2; q2++)
296 if (*q2 == ' ' || *q2 == '\t')
301 if (s->name && !strcmp(q, pool_id2str(pool, s->name)))
302 repodata_set_void(data, s - pool->solvables, SOLVABLE_SOURCENAME);
304 repodata_set_id(data, s - pool->solvables, SOLVABLE_SOURCENAME, pool_str2id(pool, q, 1));
309 if (!strcasecmp(tag, "suggests"))
310 s->suggests = makedeps(repo, q, s->suggests, 0);
313 if (!strcasecmp(tag, "version"))
314 s->evr = pool_str2id(pool, q, 1);
319 repodata_set_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, checksumtype, checksum);
325 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
326 if (s->name && !havesource)
327 repodata_set_void(data, s - pool->solvables, SOLVABLE_SOURCENAME);
330 /* obsoletes only count when the packages also conflict */
333 for (i = j = s->obsoletes; (d = repo->idarraydata[i]) != 0; i++)
337 for (k = s->conflicts; repo->idarraydata[k] != 0; k++)
338 if (repo->idarraydata[k] == d)
340 if (repo->idarraydata[k])
342 repo->idarraydata[j++] = d;
346 repo->idarraydata[j] = 0;
351 repo_add_debpackages(Repo *repo, FILE *fp, int flags)
353 Pool *pool = repo->pool;
359 data = repo_add_repodata(repo, flags);
360 buf = solv_malloc(4096);
367 if (!(p = strchr(p, '\n')))
370 if (l + 1024 >= bufl)
372 buf = solv_realloc(buf, bufl + 4096);
378 ll = fread(p, 1, bufl - l - 1, fp);
382 while ((l3 = strlen(p)) < ll)
392 s = pool_id2solvable(pool, repo_add_solvable(repo));
393 control2solvable(s, data, buf);
395 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
397 memmove(buf, p + 1, l - ll);
404 s = pool_id2solvable(pool, repo_add_solvable(repo));
405 control2solvable(s, data, buf);
407 repo_free_solvable_block(repo, s - pool->solvables, 1, 1);
410 if (!(flags & REPO_NO_INTERNALIZE))
411 repodata_internalize(data);
416 repo_add_debdb(Repo *repo, const char *rootdir, int flags)
419 const char *path = "/var/lib/dpkg/status";
421 path = pool_tmpjoin(repo->pool, rootdir, path, 0);
422 if ((fp = fopen(path, "r")) == 0)
427 repo_add_debpackages(repo, fp, flags);
433 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
435 Pool *pool = repo->pool;
437 unsigned char buf[4096], *bp;
438 int i, l, l2, vlen, clen, ctarlen;
440 unsigned char pkgid[16];
447 data = repo_add_repodata(repo, flags);
448 for (i = 0; i < ndebs; i++)
450 if ((fp = fopen(debs[i], "r")) == 0)
455 if (fstat(fileno(fp), &stb))
460 l = fread(buf, 1, sizeof(buf), fp);
461 if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary ", 8 + 16) != 0)
463 fprintf(stderr, "%s: not a deb package\n", debs[i]);
467 vlen = atoi((char *)buf + 8 + 48);
468 if (vlen < 0 || vlen > l)
470 fprintf(stderr, "%s: not a deb package\n", debs[i]);
475 if (l < 8 + 60 + vlen + 60)
477 fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
481 if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz ", 16) != 0)
483 fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
487 clen = atoi((char *)buf + 8 + 60 + vlen + 48);
490 fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
494 ctgz = solv_calloc(1, clen + 4);
495 bp = buf + 8 + 60 + vlen + 60;
496 l -= 8 + 60 + vlen + 60;
503 if (fread(ctgz + l, clen - l, 1, fp) != 1)
505 fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
513 if (flags & DEBS_ADD_WITH_PKGID)
515 void *handle = solv_chksum_create(REPOKEY_TYPE_MD5);
516 solv_chksum_add(handle, ctgz, clen);
517 solv_chksum_free(handle, pkgid);
520 if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
522 fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
526 if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
528 fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
533 bp += 6; /* skip time, xflags and OS code */
536 /* skip extra field */
537 l = bp[0] | bp[1] << 8;
539 if (bp >= ctgz + clen)
541 fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
546 if (ctgz[3] & 0x08) /* orig filename */
549 if (ctgz[3] & 0x10) /* file comment */
552 if (ctgz[3] & 0x02) /* header crc */
554 if (bp >= ctgz + clen)
556 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
560 ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
564 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
573 for (j = 124; j < 124 + 12; j++)
574 if (bp[j] >= '0' && bp[j] <= '7')
575 l2 = l2 * 8 + (bp[j] - '0');
576 if (!strcmp((char *)bp, "./control") || !strcmp((char *)bp, "control"))
578 l2 = 512 + ((l2 + 511) & ~511);
582 if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
584 fprintf(stderr, "%s: control.tar.gz contains no control file\n", debs[i]);
588 memmove(ctar, bp + 512, l2);
589 ctar = solv_realloc(ctar, l2 + 1);
591 s = pool_id2solvable(pool, repo_add_solvable(repo));
592 control2solvable(s, data, (char *)ctar);
593 repodata_set_location(data, s - pool->solvables, 0, 0, debs[i]);
594 if (S_ISREG(stb.st_mode))
595 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
597 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
600 if (!(flags & REPO_NO_INTERNALIZE))
601 repodata_internalize(data);
606 repo_add_deb(Repo *repo, const char *deb, int flags)
609 repo_add_debs(repo, &deb, 1, flags);
610 if (end == repo->end)
613 return repo->end - 1;