- add repo_deb to CMakeLists.txt
[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     }
147   if (!id)
148     return olddeps;
149   return repo_addid_dep(repo, olddeps, id, marker);
150 }
151
152
153 /* put data from control file into the solvable */
154 /* warning: does inplace changes */
155 static void
156 control2solvable(Solvable *s, Repodata *data, char *control)
157 {
158   Repo *repo = s->repo;
159   Pool *pool = repo->pool;
160   char *p, *q, *end, *tag;
161   int x, l;
162
163   p = control;
164   while (*p)
165     {
166       p = strchr(p, '\n');
167       if (!p)
168         break;
169       if (p[1] == ' ' || p[1] == '\t')
170         {
171           char *q;
172           /* continuation line */
173           q = p - 1;
174           while (q >= control && *q == ' ' && *q == '\t')
175             q--;
176           l = q + 1 - control;
177           if (l)
178             memmove(p + 1 - l, control, l);
179           control = p + 1 - l;
180           p[1] = '\n';
181           p += 2;
182           continue;
183         }
184       end = p - 1;
185       if (*p)
186         *p++ = 0;
187       /* strip trailing space */
188       while (end >= control && *end == ' ' && *end == '\t')
189         *end-- = 0;
190       tag = control;
191       control = p;
192       q = strchr(tag, ':');
193       if (!q || q - tag < 4)
194         continue;
195       *q++ = 0;
196       while (*q == ' ' || *q == '\t')
197         q++;
198       x = '@' + (tag[0] & 0x1f);
199       x = (x << 8) + '@' + (tag[1] & 0x1f);
200       switch(x)
201         {
202         case 'A' << 8 | 'R':
203           if (!strcasecmp(tag, "architecture"))
204             s->arch = str2id(pool, q, 1);
205           break;
206         case 'B' << 8 | 'R':
207           if (!strcasecmp(tag, "breaks"))
208             s->conflicts = makedeps(repo, q, s->conflicts, 0);
209           break;
210         case 'C' << 8 | 'O':
211           if (!strcasecmp(tag, "conflicts"))
212             s->conflicts = makedeps(repo, q, s->conflicts, 0);
213           break;
214         case 'D' << 8 | 'E':
215           if (!strcasecmp(tag, "depends"))
216             s->requires = makedeps(repo, q, s->requires, -SOLVABLE_PREREQMARKER);
217           else if (!strcasecmp(tag, "description"))
218             {
219               char *ld = strchr(q, '\n');
220               if (ld)
221                 {
222                   *ld++ = 0;
223                   repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, ld);
224                 }
225               else
226                 repodata_set_str(data, s - pool->solvables, SOLVABLE_DESCRIPTION, q);
227               repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, q);
228             }
229           break;
230         case 'E' << 8 | 'N':
231           if (!strcasecmp(tag, "enhances"))
232             s->enhances = makedeps(repo, q, s->enhances, 0);
233           break;
234         case 'I' << 8 | 'N':
235           if (!strcasecmp(tag, "installed-size"))
236             repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
237           break;
238         case 'P' << 8 | 'A':
239           if (!strcasecmp(tag, "package"))
240             s->name = str2id(pool, q, 1);
241           break;
242         case 'P' << 8 | 'R':
243           if (!strcasecmp(tag, "pre-depends"))
244             s->requires = makedeps(repo, q, s->requires, SOLVABLE_PREREQMARKER);
245           else if (!strcasecmp(tag, "provides"))
246             s->provides = makedeps(repo, q, s->provides, 0);
247           break;
248         case 'R' << 8 | 'E':
249           if (!strcasecmp(tag, "replaces"))
250             s->obsoletes = makedeps(repo, q, s->conflicts, 0);
251           else if (!strcasecmp(tag, "recommends"))
252             s->recommends = makedeps(repo, q, s->recommends, 0);
253           break;
254         case 'S' << 8 | 'U':
255           if (!strcasecmp(tag, "suggests"))
256             s->suggests = makedeps(repo, q, s->suggests, 0);
257           break;
258         case 'V' << 8 | 'E':
259           if (!strcasecmp(tag, "version"))
260             s->evr = str2id(pool, q, 1);
261           break;
262         }
263     }
264 }
265
266 void
267 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
268 {
269   Pool *pool = repo->pool;
270   Repodata *data;
271   unsigned char buf[4096], *bp;
272   int i, l, l2, vlen, clen, ctarlen;
273   unsigned char *ctgz;
274   unsigned char pkgid[16];
275   unsigned char *ctar;
276   int gotpkgid;
277   FILE *fp;
278   Solvable *s;
279   struct stat stb;
280
281   if (!(flags & REPO_REUSE_REPODATA))
282     data = repo_add_repodata(repo, 0);
283   else
284     data = repo_last_repodata(repo);
285   for (i = 0; i < ndebs; i++)
286     {
287       if ((fp = fopen(debs[i], "r")) == 0)
288         {
289           perror(debs[i]);
290           continue;
291         }
292       if (fstat(fileno(fp), &stb))
293         {
294           perror("stat");
295           continue;
296         }
297       l = fread(buf, 1, sizeof(buf), fp);
298       if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary   ", 8 + 16) != 0)
299         {
300           fprintf(stderr, "%s: not a deb package\n", debs[i]);
301           fclose(fp);
302           continue;
303         }
304       vlen = atoi((char *)buf + 8 + 48);
305       if (vlen < 0 || vlen > l)
306         {
307           fprintf(stderr, "%s: not a deb package\n", debs[i]);
308           fclose(fp);
309           continue;
310         }
311       vlen += vlen & 1;
312       if (l < 8 + 60 + vlen + 60)
313         {
314           fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
315           fclose(fp);
316           continue;
317         }
318       if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz  ", 16) != 0)
319         {
320           fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
321           fclose(fp);
322           continue;
323         }
324       clen = atoi((char *)buf + 8 + 60 + vlen + 48);
325       if (clen <= 0)
326         {
327           fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
328           fclose(fp);
329           continue;
330         }
331       ctgz = sat_calloc(1, clen + 4);
332       bp = buf + 8 + 60 + vlen + 60;
333       l -= 8 + 60 + vlen + 60;
334       if (l > clen)
335         l = clen;
336       if (l)
337         memcpy(ctgz, bp, l);
338       if (l < clen)
339         {
340           if (fread(ctgz + l, clen - l, 1, fp) != 1)
341             {
342               fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
343               sat_free(ctgz);
344               fclose(fp);
345               continue;
346             }
347         }
348       fclose(fp);
349       gotpkgid = 0;
350       if (flags & DEBS_ADD_WITH_PKGID)
351         {
352           void *handle = sat_chksum_create(REPOKEY_TYPE_MD5);
353           sat_chksum_add(handle, ctgz, clen);
354           sat_chksum_free(handle, pkgid);
355           gotpkgid = 1;
356         }
357       if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
358         {
359           fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
360           sat_free(ctgz);
361           continue;
362         }
363       if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
364         {
365           fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
366           sat_free(ctgz);
367           continue;
368         }
369       bp = ctgz + 4;
370       bp += 6;  /* skip time, xflags and OS code */
371       if (ctgz[3] & 0x04)
372         {
373           /* skip extra field */
374           l = bp[0] | bp[1] << 8;
375           bp += l + 2;
376           if (bp >= ctgz + clen)
377             {
378               fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
379               sat_free(ctgz);
380               continue;
381             }
382         }
383       if (ctgz[3] & 0x08)       /* orig filename */
384         while (*bp)
385           bp++;
386       if (ctgz[3] & 0x10)       /* file comment */
387         while (*bp)
388           bp++;
389       if (ctgz[3] & 0x02)       /* header crc */
390         bp += 2;
391       if (bp >= ctgz + clen)
392         {
393           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
394           sat_free(ctgz);
395           continue;
396         }
397       ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
398       sat_free(ctgz);
399       if (!ctar)
400         {
401           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
402           continue;
403         }
404       bp = ctar;
405       l = ctarlen;
406       while (l > 512)
407         {
408           int j;
409           l2 = 0;
410           for (j = 124; j < 124 + 12; j++)
411             if (bp[j] >= '0' && bp[j] <= '7')
412               l2 = l2 * 8 + (bp[j] - '0');
413           if (!strcmp((char *)bp, "./control"))
414             break;
415           l2 = 512 + ((l2 + 511) & ~511);
416           l -= l2;
417           bp += l2;
418         }
419       if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
420         {
421           fprintf(stderr, "%s: control.tar.gz contains no ./control file\n", debs[i]);
422           free(ctar);
423           continue;
424         }
425       memmove(ctar, bp + 512, l2);
426       ctar = sat_realloc(ctar, l2 + 1);
427       ctar[l2] = 0;
428       s = pool_id2solvable(pool, repo_add_solvable(repo));
429       control2solvable(s, data, (char *)ctar);
430       repodata_set_location(data, s - pool->solvables, 0, 0, debs[i]);
431       if (S_ISREG(stb.st_mode))
432         repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
433       if (gotpkgid)
434         repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
435       sat_free(ctar);
436     }
437   if (!(flags & REPO_NO_INTERNALIZE))
438     repodata_internalize(data);
439 }