- only add selfprovides if name is set
[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 'H' << 8 | 'O':
236           if (!strcasecmp(tag, "homepage"))
237             repodata_set_str(data, s - pool->solvables, SOLVABLE_URL, q);
238           break;
239         case 'I' << 8 | 'N':
240           if (!strcasecmp(tag, "installed-size"))
241             repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, atoi(q));
242           break;
243         case 'P' << 8 | 'A':
244           if (!strcasecmp(tag, "package"))
245             s->name = str2id(pool, q, 1);
246           break;
247         case 'P' << 8 | 'R':
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);
252           break;
253         case 'R' << 8 | 'E':
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);
258           break;
259         case 'S' << 8 | 'U':
260           if (!strcasecmp(tag, "suggests"))
261             s->suggests = makedeps(repo, q, s->suggests, 0);
262           break;
263         case 'V' << 8 | 'E':
264           if (!strcasecmp(tag, "version"))
265             s->evr = str2id(pool, q, 1);
266           break;
267         }
268     }
269   if (!s->arch)
270     s->arch = ARCH_ALL;
271   if (!s->evr)
272     s->evr = ID_EMPTY;
273   if (s->name)
274     s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
275 }
276
277 void
278 repo_add_debs(Repo *repo, const char **debs, int ndebs, int flags)
279 {
280   Pool *pool = repo->pool;
281   Repodata *data;
282   unsigned char buf[4096], *bp;
283   int i, l, l2, vlen, clen, ctarlen;
284   unsigned char *ctgz;
285   unsigned char pkgid[16];
286   unsigned char *ctar;
287   int gotpkgid;
288   FILE *fp;
289   Solvable *s;
290   struct stat stb;
291
292   if (!(flags & REPO_REUSE_REPODATA))
293     data = repo_add_repodata(repo, 0);
294   else
295     data = repo_last_repodata(repo);
296   for (i = 0; i < ndebs; i++)
297     {
298       if ((fp = fopen(debs[i], "r")) == 0)
299         {
300           perror(debs[i]);
301           continue;
302         }
303       if (fstat(fileno(fp), &stb))
304         {
305           perror("stat");
306           continue;
307         }
308       l = fread(buf, 1, sizeof(buf), fp);
309       if (l < 8 + 60 || strncmp((char *)buf, "!<arch>\ndebian-binary   ", 8 + 16) != 0)
310         {
311           fprintf(stderr, "%s: not a deb package\n", debs[i]);
312           fclose(fp);
313           continue;
314         }
315       vlen = atoi((char *)buf + 8 + 48);
316       if (vlen < 0 || vlen > l)
317         {
318           fprintf(stderr, "%s: not a deb package\n", debs[i]);
319           fclose(fp);
320           continue;
321         }
322       vlen += vlen & 1;
323       if (l < 8 + 60 + vlen + 60)
324         {
325           fprintf(stderr, "%s: unhandled deb package\n", debs[i]);
326           fclose(fp);
327           continue;
328         }
329       if (strncmp((char *)buf + 8 + 60 + vlen, "control.tar.gz  ", 16) != 0)
330         {
331           fprintf(stderr, "%s: control.tar.gz is not second entry\n", debs[i]);
332           fclose(fp);
333           continue;
334         }
335       clen = atoi((char *)buf + 8 + 60 + vlen + 48);
336       if (clen <= 0)
337         {
338           fprintf(stderr, "%s: control.tar.gz has illegal size\n", debs[i]);
339           fclose(fp);
340           continue;
341         }
342       ctgz = sat_calloc(1, clen + 4);
343       bp = buf + 8 + 60 + vlen + 60;
344       l -= 8 + 60 + vlen + 60;
345       if (l > clen)
346         l = clen;
347       if (l)
348         memcpy(ctgz, bp, l);
349       if (l < clen)
350         {
351           if (fread(ctgz + l, clen - l, 1, fp) != 1)
352             {
353               fprintf(stderr, "%s: unexpected EOF\n", debs[i]);
354               sat_free(ctgz);
355               fclose(fp);
356               continue;
357             }
358         }
359       fclose(fp);
360       gotpkgid = 0;
361       if (flags & DEBS_ADD_WITH_PKGID)
362         {
363           void *handle = sat_chksum_create(REPOKEY_TYPE_MD5);
364           sat_chksum_add(handle, ctgz, clen);
365           sat_chksum_free(handle, pkgid);
366           gotpkgid = 1;
367         }
368       if (ctgz[0] != 0x1f || ctgz[1] != 0x8b)
369         {
370           fprintf(stderr, "%s: control.tar.gz is not gzipped\n", debs[i]);
371           sat_free(ctgz);
372           continue;
373         }
374       if (ctgz[2] != 8 || (ctgz[3] & 0xe0) != 0)
375         {
376           fprintf(stderr, "%s: control.tar.gz is compressed in a strange way\n", debs[i]);
377           sat_free(ctgz);
378           continue;
379         }
380       bp = ctgz + 4;
381       bp += 6;  /* skip time, xflags and OS code */
382       if (ctgz[3] & 0x04)
383         {
384           /* skip extra field */
385           l = bp[0] | bp[1] << 8;
386           bp += l + 2;
387           if (bp >= ctgz + clen)
388             {
389               fprintf(stderr, "%s: corrupt gzip\n", debs[i]);
390               sat_free(ctgz);
391               continue;
392             }
393         }
394       if (ctgz[3] & 0x08)       /* orig filename */
395         while (*bp)
396           bp++;
397       if (ctgz[3] & 0x10)       /* file comment */
398         while (*bp)
399           bp++;
400       if (ctgz[3] & 0x02)       /* header crc */
401         bp += 2;
402       if (bp >= ctgz + clen)
403         {
404           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
405           sat_free(ctgz);
406           continue;
407         }
408       ctar = decompress(bp, ctgz + clen - bp, &ctarlen);
409       sat_free(ctgz);
410       if (!ctar)
411         {
412           fprintf(stderr, "%s: corrupt control.tar.gz\n", debs[i]);
413           continue;
414         }
415       bp = ctar;
416       l = ctarlen;
417       while (l > 512)
418         {
419           int j;
420           l2 = 0;
421           for (j = 124; j < 124 + 12; j++)
422             if (bp[j] >= '0' && bp[j] <= '7')
423               l2 = l2 * 8 + (bp[j] - '0');
424           if (!strcmp((char *)bp, "./control"))
425             break;
426           l2 = 512 + ((l2 + 511) & ~511);
427           l -= l2;
428           bp += l2;
429         }
430       if (l <= 512 || l - 512 - l2 <= 0 || l2 <= 0)
431         {
432           fprintf(stderr, "%s: control.tar.gz contains no ./control file\n", debs[i]);
433           free(ctar);
434           continue;
435         }
436       memmove(ctar, bp + 512, l2);
437       ctar = sat_realloc(ctar, l2 + 1);
438       ctar[l2] = 0;
439       s = pool_id2solvable(pool, repo_add_solvable(repo));
440       control2solvable(s, data, (char *)ctar);
441       repodata_set_location(data, s - pool->solvables, 0, 0, debs[i]);
442       if (S_ISREG(stb.st_mode))
443         repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned int)((stb.st_size + 1023) / 1024));
444       if (gotpkgid)
445         repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, pkgid);
446       sat_free(ctar);
447     }
448   if (!(flags & REPO_NO_INTERNALIZE))
449     repodata_internalize(data);
450 }