- no endless loops, please
[platform/upstream/libsolv.git] / ext / repo_deb.c
1 /*
2  * Copyright (c) 2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <zlib.h>
15
16 #include "pool.h"
17 #include "repo.h"
18 #include "util.h"
19 #include "chksum.h"
20 #include "repo_deb.h"
21
22 static unsigned char *
23 decompress(unsigned char *in, int inl, int *outlp)
24 {
25   z_stream strm;
26   int outl, ret;
27   unsigned char *out;
28
29   memset(&strm, 0, sizeof(strm));
30   strm.next_in = in;
31   strm.avail_in = inl;
32   out = sat_malloc(4096);
33   strm.next_out = out;
34   strm.avail_out = 4096;
35   outl = 0;
36   ret = inflateInit2(&strm, -MAX_WBITS);
37   if (ret != Z_OK)
38     {
39       free(out);
40       return 0;
41     }
42   for (;;)
43     {
44       if (strm.avail_out == 0)
45         {
46           outl += 4096;
47           out = sat_realloc(out, outl + 4096);
48           strm.next_out = out + outl;
49           strm.avail_out = 4096;
50         }
51       ret = inflate(&strm, Z_NO_FLUSH);
52       if (ret == Z_STREAM_END)
53         break;
54       if (ret != Z_OK)
55         {
56           free(out);
57           return 0;
58         }
59     }
60   outl += 4096 - strm.avail_out;
61   inflateEnd(&strm);
62   *outlp = outl;
63   return out;
64 }
65
66 static unsigned int
67 makedeps(Repo *repo, char *deps, unsigned int olddeps, Id marker)
68 {
69   Pool *pool = repo->pool;
70   char *p, *n, *ne, *e, *ee;
71   Id id, name, evr;
72   int flags;
73
74   while ((p = strchr(deps, ',')) != 0)
75     {
76       *p++ = 0;
77       olddeps = makedeps(repo, deps, olddeps, marker);
78       deps = p;
79     }
80   id = 0;
81   p = deps;
82   for (;;)
83     {
84       while (*p == ' ' || *p == '\t' || *p == '\n')
85         p++;
86       if (!*p || *p == '(')
87         break;
88       n = p;
89       while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '(' && *p != '|')
90         p++;
91       ne = p;
92       while (*p == ' ' || *p == '\t' || *p == '\n')
93         p++;
94       evr = 0;
95       flags = 0;
96       e = ee = 0;
97       if (*p == '(')
98         {
99           p++;
100           while (*p == ' ' || *p == '\t' || *p == '\n')
101             p++;
102           if (*p == '>')
103             flags |= REL_GT;
104           else if (*p == '=')
105             flags |= REL_EQ;
106           else if (*p == '<')
107             flags |= REL_LT;
108           if (flags)
109             {
110               p++;
111               if (*p == '>')
112                 flags |= REL_GT;
113               else if (*p == '=')
114                 flags |= REL_EQ;
115               else if (*p == '<')
116                 flags |= REL_LT;
117               else
118                 p--;
119               p++;
120             }
121           while (*p == ' ' || *p == '\t' || *p == '\n')
122             p++;
123           e = p;
124           while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != ')')
125             p++;
126           ee = p;
127           while (*p && *p != ')')
128             p++;
129           if (*p)
130             p++;
131           while (*p == ' ' || *p == '\t' || *p == '\n')
132             p++;
133         }
134       name = strn2id(pool, n, ne - n, 1);
135       if (e)
136         {
137           evr = strn2id(pool, e, ee - e, 1);
138           name = rel2id(pool, name, evr, flags, 1);
139         }
140       if (!id)
141         id = name;
142       else
143         id = rel2id(pool, id, name, REL_OR, 1);
144       if (*p != '|')
145         break;
146       p++;
147     }
148   if (!id)
149     return olddeps;
150   return repo_addid_dep(repo, olddeps, id, marker);
151 }
152
153
154 /* put data from control file into the solvable */
155 /* warning: does inplace changes */
156 static void
157 control2solvable(Solvable *s, Repodata *data, char *control)
158 {
159   Repo *repo = s->repo;
160   Pool *pool = repo->pool;
161   char *p, *q, *end, *tag;
162   int x, l;
163
164   p = control;
165   while (*p)
166     {
167       p = strchr(p, '\n');
168       if (!p)
169         break;
170       if (p[1] == ' ' || p[1] == '\t')
171         {
172           char *q;
173           /* continuation line */
174           q = p - 1;
175           while (q >= control && *q == ' ' && *q == '\t')
176             q--;
177           l = q + 1 - control;
178           if (l)
179             memmove(p + 1 - l, control, l);
180           control = p + 1 - l;
181           p[1] = '\n';
182           p += 2;
183           continue;
184         }
185       end = p - 1;
186       if (*p)
187         *p++ = 0;
188       /* strip trailing space */
189       while (end >= control && *end == ' ' && *end == '\t')
190         *end-- = 0;
191       tag = control;
192       control = p;
193       q = strchr(tag, ':');
194       if (!q || q - tag < 4)
195         continue;
196       *q++ = 0;
197       while (*q == ' ' || *q == '\t')
198         q++;
199       x = '@' + (tag[0] & 0x1f);
200       x = (x << 8) + '@' + (tag[1] & 0x1f);
201       switch(x)
202         {
203         case 'A' << 8 | 'R':
204           if (!strcasecmp(tag, "architecture"))
205             s->arch = str2id(pool, q, 1);
206           break;
207         case 'B' << 8 | 'R':
208           if (!strcasecmp(tag, "breaks"))
209             s->conflicts = makedeps(repo, q, s->conflicts, 0);
210           break;
211         case 'C' << 8 | 'O':
212           if (!strcasecmp(tag, "conflicts"))
213             s->conflicts = makedeps(repo, q, s->conflicts, 0);
214           break;
215         case 'D' << 8 | 'E':
216           if (!strcasecmp(tag, "depends"))
217             s->requires = makedeps(repo, q, s->requires, -SOLVABLE_PREREQMARKER);
218           else if (!strcasecmp(tag, "description"))
219             {
220               char *ld = strchr(q, '\n');
221               if (ld)
222                 {
223                   *ld++ = 0;
224                   repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, ld);
225                 }
226               else
227                 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, q);
228               repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, q);
229             }
230           break;
231         case 'E' << 8 | 'N':
232           if (!strcasecmp(tag, "enhances"))
233             s->enhances = makedeps(repo, q, s->enhances, 0);
234           break;
235         case 'I' << 8 | 'N':
236           if (!strcasecmp(tag, "installed-size"))
237             repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
238           break;
239         case 'P' << 8 | 'A':
240           if (!strcasecmp(tag, "package"))
241             s->name = str2id(pool, q, 1);
242           break;
243         case 'P' << 8 | 'R':
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);
248           break;
249         case 'R' << 8 | 'E':
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);
254           break;
255         case 'S' << 8 | 'U':
256           if (!strcasecmp(tag, "suggests"))
257             s->suggests = makedeps(repo, q, s->suggests, 0);
258           break;
259         case 'V' << 8 | 'E':
260           if (!strcasecmp(tag, "version"))
261             s->evr = str2id(pool, q, 1);
262           break;
263         }
264     }
265 }
266
267 void
268 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
269 {
270   Pool *pool = repo->pool;
271   Repodata *data;
272   unsigned char buf[4096], *bp;
273   int i, l, l2, vlen, clen, ctarlen;
274   unsigned char *ctgz;
275   unsigned char pkgid[16];
276   unsigned char *ctar;
277   int gotpkgid;
278   FILE *fp;
279   Solvable *s;
280   struct stat stb;
281
282   if (!(flags & REPO_REUSE_REPODATA))
283     data = repo_add_repodata(repo, 0);
284   else
285     data = repo_last_repodata(repo);
286   for (i = 0; i < ndebs; i++)
287     {
288       if ((fp = fopen(debs[i], "r")) == 0)
289         {
290           perror(debs[i]);
291           continue;
292         }
293       if (fstat(fileno(fp), &stb))
294         {
295           perror("stat");
296           continue;
297         }
298       l = fread(buf, 1, sizeof(buf), fp);
299       if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary   ", 8 + 16) != 0)
300         {
301           fprintf(stderr, "%s: not a deb package\n", debs[i]);
302           fclose(fp);
303           continue;
304         }
305       vlen = atoi((char *)buf + 8 + 48);
306       if (vlen < 0 || vlen > l)
307         {
308           fprintf(stderr, "%s: not a deb package\n", debs[i]);
309           fclose(fp);
310           continue;
311         }
312       vlen += vlen & 1;
313       if (l < 8 + 60 + vlen + 60)
314         {
315           fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
316           fclose(fp);
317           continue;
318         }
319       if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz  ", 16) != 0)
320         {
321           fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
322           fclose(fp);
323           continue;
324         }
325       clen = atoi((char *)buf + 8 + 60 + vlen + 48);
326       if (clen <= 0)
327         {
328           fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
329           fclose(fp);
330           continue;
331         }
332       ctgz = sat_calloc(1, clen + 4);
333       bp = buf + 8 + 60 + vlen + 60;
334       l -= 8 + 60 + vlen + 60;
335       if (l > clen)
336         l = clen;
337       if (l)
338         memcpy(ctgz, bp, l);
339       if (l < clen)
340         {
341           if (fread(ctgz + l, clen - l, 1, fp) != 1)
342             {
343               fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
344               sat_free(ctgz);
345               fclose(fp);
346               continue;
347             }
348         }
349       fclose(fp);
350       gotpkgid = 0;
351       if (flags & DEBS_ADD_WITH_PKGID)
352         {
353           void *handle = sat_chksum_create(REPOKEY_TYPE_MD5);
354           sat_chksum_add(handle, ctgz, clen);
355           sat_chksum_free(handle, pkgid);
356           gotpkgid = 1;
357         }
358       if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
359         {
360           fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
361           sat_free(ctgz);
362           continue;
363         }
364       if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
365         {
366           fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
367           sat_free(ctgz);
368           continue;
369         }
370       bp = ctgz + 4;
371       bp += 6;  /* skip time, xflags and OS code */
372       if (ctgz[3] & 0x04)
373         {
374           /* skip extra field */
375           l = bp[0] | bp[1] << 8;
376           bp += l + 2;
377           if (bp >= ctgz + clen)
378             {
379               fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
380               sat_free(ctgz);
381               continue;
382             }
383         }
384       if (ctgz[3] & 0x08)       /* orig filename */
385         while (*bp)
386           bp++;
387       if (ctgz[3] & 0x10)       /* file comment */
388         while (*bp)
389           bp++;
390       if (ctgz[3] & 0x02)       /* header crc */
391         bp += 2;
392       if (bp >= ctgz + clen)
393         {
394           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
395           sat_free(ctgz);
396           continue;
397         }
398       ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
399       sat_free(ctgz);
400       if (!ctar)
401         {
402           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
403           continue;
404         }
405       bp = ctar;
406       l = ctarlen;
407       while (l > 512)
408         {
409           int j;
410           l2 = 0;
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((char *)bp, "./control"))
415             break;
416           l2 = 512 + ((l2 + 511) & ~511);
417           l -= l2;
418           bp += l2;
419         }
420       if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
421         {
422           fprintf(stderr, "%s: control.tar.gz contains no ./control file\n", debs[i]);
423           free(ctar);
424           continue;
425         }
426       memmove(ctar, bp + 512, l2);
427       ctar = sat_realloc(ctar, l2 + 1);
428       ctar[l2] = 0;
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));
434       if (gotpkgid)
435         repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
436       sat_free(ctar);
437     }
438   if (!(flags & REPO_NO_INTERNALIZE))
439     repodata_internalize(data);
440 }