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;
75 while ((p = strchr(deps, ',')) != 0)
78 olddeps = makedeps(repo, deps, olddeps, marker);
85 while (*p == ' ' || *p == '\t' || *p == '\n')
90 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '(' && *p != '|')
93 while (*p == ' ' || *p == '\t' || *p == '\n')
101 while (*p == ' ' || *p == '\t' || *p == '\n')
122 while (*p == ' ' || *p == '\t' || *p == '\n')
125 while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != ')')
128 while (*p && *p != ')')
132 while (*p == ' ' || *p == '\t' || *p == '\n')
135 name = strn2id(pool, n, ne - n, 1);
138 evr = strn2id(pool, e, ee - e, 1);
139 name = rel2id(pool, name, evr, flags, 1);
144 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, "installed-size"))
237 repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
240 if (!strcasecmp(tag, "package"))
241 s->name = str2id(pool, q, 1);
244 if (!strcasecmp(tag, "pre-depends"))
245 s->requires = makedeps(repo, q, s->requires, SOLVABLE_PREREQMARKER);
246 else if (!strcasecmp(tag, "provides"))
247 s->provides = makedeps(repo, q, s->provides, 0);
250 if (!strcasecmp(tag, "replaces"))
251 s->obsoletes = makedeps(repo, q, s->conflicts, 0);
252 else if (!strcasecmp(tag, "recommends"))
253 s->recommends = makedeps(repo, q, s->recommends, 0);
256 if (!strcasecmp(tag, "suggests"))
257 s->suggests = makedeps(repo, q, s->suggests, 0);
260 if (!strcasecmp(tag, "version"))
261 s->evr = str2id(pool, q, 1);
268 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
270 Pool *pool = repo->pool;
272 unsigned char buf[4096], *bp;
273 int i, l, l2, vlen, clen, ctarlen;
275 unsigned char pkgid[16];
282 if (!(flags & REPO_REUSE_REPODATA))
283 data = repo_add_repodata(repo, 0);
285 data = repo_last_repodata(repo);
286 for (i = 0; i < ndebs; i++)
288 if ((fp = fopen(debs[i], "r")) == 0)
293 if (fstat(fileno(fp), &stb))
298 l = fread(buf, 1, sizeof(buf), fp);
299 if (l < 8 + 60 || strncmp(buf, "!<arch>\ndebian-binary ", 8 + 16) != 0)
301 fprintf(stderr, "%s: not a deb package\n", debs[i]);
305 vlen = atoi(buf + 8 + 48);
306 if (vlen < 0 || vlen > l)
308 fprintf(stderr, "%s: not a deb package\n", debs[i]);
313 if (l < 8 + 60 + vlen + 60)
315 fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
319 if (strncmp(buf + 8 + 60 + vlen, "control.tar.gz ", 16) != 0)
321 fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
325 clen = atoi(buf + 8 + 60 + vlen + 48);
328 fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
332 ctgz = sat_calloc(1, clen + 4);
333 bp = buf + 8 + 60 + vlen + 60;
334 l -= 8 + 60 + vlen + 60;
341 if (fread(ctgz + l, clen - l, 1, fp) != 1)
343 fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
351 if (flags & DEBS_ADD_WITH_PKGID)
353 void *handle = sat_chksum_create(REPOKEY_TYPE_MD5);
354 sat_chksum_add(handle, ctgz, clen);
355 sat_chksum_free(handle, pkgid);
358 if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
360 fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
364 if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
366 fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
371 bp += 6; /* skip time, xflags and OS code */
374 /* skip extra field */
375 l = bp[0] | bp[1] << 8;
377 if (bp >= ctgz + clen)
379 fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
384 if (ctgz[3] & 0x08) /* orig filename */
387 if (ctgz[3] & 0x10) /* file comment */
390 if (ctgz[3] & 0x02) /* header crc */
392 if (bp >= ctgz + clen)
394 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
398 ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
402 fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
411 for (j = 124; j < 124 + 12; j++)
412 if (bp[j] >= '0' && bp[j] <= '7')
413 l2 = l2 * 8 + (bp[j] - '0');
414 if (!strcmp(bp, "./control"))
416 l2 = 512 + ((l2 + 511) & ~511);
420 if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
422 fprintf(stderr, "%s: control.tar.gz contains no ./control file\n", debs[i]);
426 memmove(ctar, bp + 512, l2);
427 ctar = sat_realloc(ctar, l2 + 1);
429 s = pool_id2solvable(pool, repo_add_solvable(repo));
430 control2solvable(s, data, (char *)ctar);
431 repodata_set_location(data, s - pool->solvables, 0, 0, debs[i]);
432 if (S_ISREG(stb.st_mode))
433 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
435 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
438 if (!(flags & REPO_NO_INTERNALIZE))
439 repodata_internalize(data);