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 = sat_malloc(4096);
34 strm.avail_out = 4096;
36 ret = inflateInit2(&strm, -MAX_WBITS);
44 if (strm.avail_out == 0)
47 out = sat_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 makedeps(Repo *repo, char *deps, unsigned int olddeps, Id marker)
69 Pool *pool = repo->pool;
70 char *p, *n, *ne, *e, *ee;
74 while ((p = strchr(deps, ',')) != 0)
77 olddeps = makedeps(repo, deps, olddeps, marker);
84 while (*p == ' ' || *p == '\t' || *p == '\n')
89 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '(' && *p != '|')
92 while (*p == ' ' || *p == '\t' || *p == '\n')
100 while (*p == ' ' || *p == '\t' || *p == '\n')
121 while (*p == ' ' || *p == '\t' || *p == '\n')
124 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != ')')
127 while (*p && *p != ')')
131 while (*p == ' ' || *p == '\t' || *p == '\n')
134 name = strn2id(pool, n, ne - n, 1);
137 evr = strn2id(pool, e, ee - e, 1);
138 name = rel2id(pool, name, evr, flags, 1);
143 id = rel2id(pool, id, name, REL_OR, 1);
150 return repo_addid_dep(repo, olddeps, id, marker);
154 /* put data from control file into the solvable */
155 /* warning: does inplace changes */
157 control2solvable(Solvable *s, Repodata *data, char *control)
159 Repo *repo = s->repo;
160 Pool *pool = repo->pool;
161 char *p, *q, *end, *tag;
170 if (p[1] == ' ' || p[1] == '\t')
173 /* continuation line */
175 while (q >= control && *q == ' ' && *q == '\t')
179 memmove(p + 1 - l, control, l);
188 /* strip trailing space */
189 while (end >= control && *end == ' ' && *end == '\t')
193 q = strchr(tag, ':');
194 if (!q || q - tag < 4)
197 while (*q == ' ' || *q == '\t')
199 x = '@' + (tag[0] & 0x1f);
200 x = (x << 8) + '@' + (tag[1] & 0x1f);
204 if (!strcasecmp(tag, "architecture"))
205 s->arch = str2id(pool, q, 1);
208 if (!strcasecmp(tag, "breaks"))
209 s->conflicts = makedeps(repo, q, s->conflicts, 0);
212 if (!strcasecmp(tag, "conflicts"))
213 s->conflicts = makedeps(repo, q, s->conflicts, 0);
216 if (!strcasecmp(tag, "depends"))
217 s->requires = makedeps(repo, q, s->requires, -SOLVABLE_PREREQMARKER);
218 else if (!strcasecmp(tag, "description"))
220 char *ld = strchr(q, '\n');
224 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, ld);
227 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, q);
228 repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, q);
232 if (!strcasecmp(tag, "enhances"))
233 s->enhances = makedeps(repo, q, s->enhances, 0);
236 if (!strcasecmp(tag, "homepage"))
237 repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, q);
240 if (!strcasecmp(tag, "installed-size"))
241 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
244 if (!strcasecmp(tag, "package"))
245 s->name = str2id(pool, q, 1);
248 if (!strcasecmp(tag, "pre-depends"))
249 s->requires = makedeps(repo, q, s->requires, SOLVABLE_PREREQMARKER);
250 else if (!strcasecmp(tag, "provides"))
251 s->provides = makedeps(repo, q, s->provides, 0);
254 if (!strcasecmp(tag, "replaces"))
255 s->obsoletes = makedeps(repo, q, s->conflicts, 0);
256 else if (!strcasecmp(tag, "recommends"))
257 s->recommends = makedeps(repo, q, s->recommends, 0);
260 if (!strcasecmp(tag, "suggests"))
261 s->suggests = makedeps(repo, q, s->suggests, 0);
264 if (!strcasecmp(tag, "version"))
265 s->evr = str2id(pool, q, 1);
272 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
274 Pool *pool = repo->pool;
276 unsigned char buf[4096], *bp;
277 int i, l, l2, vlen, clen, ctarlen;
279 unsigned char pkgid[16];
286 if (!(flags & REPO_REUSE_REPODATA))
287 data = repo_add_repodata(repo, 0);
289 data = repo_last_repodata(repo);
290 for (i = 0; i < ndebs; i++)
292 if ((fp = fopen(debs[i], "r")) == 0)
297 if (fstat(fileno(fp), &stb))
302 l = fread(buf, 1, sizeof(buf), fp);
303 if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary ", 8 + 16) != 0)
305 fprintf(stderr, "%s: not a deb package\n", debs[i]);
309 vlen = atoi((char *)buf + 8 + 48);
310 if (vlen < 0 || vlen > l)
312 fprintf(stderr, "%s: not a deb package\n", debs[i]);
317 if (l < 8 + 60 + vlen + 60)
319 fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
323 if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz ", 16) != 0)
325 fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
329 clen = atoi((char *)buf + 8 + 60 + vlen + 48);
332 fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
336 ctgz = sat_calloc(1, clen + 4);
337 bp = buf + 8 + 60 + vlen + 60;
338 l -= 8 + 60 + vlen + 60;
345 if (fread(ctgz + l, clen - l, 1, fp) != 1)
347 fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
355 if (flags & DEBS_ADD_WITH_PKGID)
357 void *handle = sat_chksum_create(REPOKEY_TYPE_MD5);
358 sat_chksum_add(handle, ctgz, clen);
359 sat_chksum_free(handle, pkgid);
362 if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
364 fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
368 if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
370 fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
375 bp += 6; /* skip time, xflags and OS code */
378 /* skip extra field */
379 l = bp[0] | bp[1] << 8;
381 if (bp >= ctgz + clen)
383 fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
388 if (ctgz[3] & 0x08) /* orig filename */
391 if (ctgz[3] & 0x10) /* file comment */
394 if (ctgz[3] & 0x02) /* header crc */
396 if (bp >= ctgz + clen)
398 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
402 ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
406 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
415 for (j = 124; j < 124 + 12; j++)
416 if (bp[j] >= '0' && bp[j] <= '7')
417 l2 = l2 * 8 + (bp[j] - '0');
418 if (!strcmp((char *)bp, "./control"))
420 l2 = 512 + ((l2 + 511) & ~511);
424 if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
426 fprintf(stderr, "%s: control.tar.gz contains no ./control file\n", debs[i]);
430 memmove(ctar, bp + 512, l2);
431 ctar = sat_realloc(ctar, l2 + 1);
433 s = pool_id2solvable(pool, repo_add_solvable(repo));
434 control2solvable(s, data, (char *)ctar);
435 repodata_set_location(data, s - pool->solvables, 0, 0, debs[i]);
436 if (S_ISREG(stb.st_mode))
437 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
439 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
442 if (!(flags & REPO_NO_INTERNALIZE))
443 repodata_internalize(data);