Imported Upstream version 0.6.12
[platform/upstream/libsolv.git] / ext / repo_mdk.c
1 /*
2  * Copyright (c) 2012, 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 <expat.h>
15
16 #include "pool.h"
17 #include "repo.h"
18 #include "util.h"
19 #include "chksum.h"
20 #include "repo_mdk.h"
21
22 static Offset
23 parse_deps(Solvable *s, char *bp, Id marker)
24 {
25   Pool *pool = s->repo->pool;
26   Offset deps = 0;
27   char *nbp, *ebp;
28   for (; bp; bp = nbp)
29     {
30       int ispre = 0;
31       Id id, evr = 0;
32       int flags = 0;
33
34       nbp = strchr(bp, '@');
35       if (!nbp)
36         ebp = bp + strlen(bp);
37       else
38         {
39           ebp = nbp;
40           *nbp++ = 0;
41         }
42       if (ebp[-1] == ']')
43         {
44           char *sbp = ebp - 1;
45           while (sbp >= bp && *sbp != '[')
46             sbp--;
47           if (sbp >= bp && sbp[1] != '*')
48             {
49               char *fbp;
50               for (fbp = sbp + 1;; fbp++)
51                 {
52                   if (*fbp == '>')
53                     flags |= REL_GT;
54                   else if (*fbp == '=')
55                     flags |= REL_EQ;
56                   else if (*fbp == '<')
57                     flags |= REL_LT;
58                   else
59                     break;
60                 }
61               if (*fbp == ' ')
62                 fbp++;
63               evr = pool_strn2id(pool, fbp, ebp - 1 - fbp, 1);
64               ebp = sbp;
65             }
66         }
67       if (ebp[-1] == ']' && ebp >= bp + 3 && !strncmp(ebp - 3, "[*]", 3))
68         {
69           ispre = 1;
70           ebp -= 3;
71         }
72       id = pool_strn2id(pool, bp, ebp - bp, 1);
73       if (evr)
74         id = pool_rel2id(pool, id, evr, flags, 1);
75       deps = repo_addid_dep(s->repo, deps, id, ispre ? marker : 0);
76       bp = nbp;
77     }
78   return deps;
79 }
80
81 int
82 repo_add_mdk(Repo *repo, FILE *fp, int flags)
83 {
84   Pool *pool = repo->pool;
85   Repodata *data;
86   Solvable *s;
87   char *buf;
88   int bufa, bufl;
89
90   data = repo_add_repodata(repo, flags);
91   bufa = 4096;
92   buf = solv_malloc(bufa);
93   bufl = 0;
94   s = 0;
95   while (fgets(buf + bufl, bufa - bufl, fp) > 0)
96     {
97       bufl += strlen(buf + bufl);
98       if (!bufl)
99         continue;
100       if (buf[bufl - 1] != '\n')
101         {
102           if (bufa - bufl < 256)
103             {
104               bufa += 4096;
105               buf = solv_realloc(buf, bufa);
106             }
107           continue;
108         }
109       buf[bufl - 1] = 0;
110       bufl = 0;
111       if (buf[0] != '@')
112         {
113           pool_debug(pool, SOLV_ERROR, "bad line <%s>\n", buf);
114           continue;
115         }
116       if (!s)
117         s = pool_id2solvable(pool, repo_add_solvable(repo));
118       if (!strncmp(buf + 1, "filesize@", 9))
119         repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, strtoull(buf + 10, 0, 10));
120       else if (!strncmp(buf + 1, "summary@", 8))
121         repodata_set_str(data, s - pool->solvables, SOLVABLE_SUMMARY, buf + 9);
122       else if (!strncmp(buf + 1, "provides@", 9))
123         s->provides = parse_deps(s, buf + 10, 0);
124       else if (!strncmp(buf + 1, "requires@", 9))
125         s->requires = parse_deps(s, buf + 10, SOLVABLE_PREREQMARKER);
126       else if (!strncmp(buf + 1, "suggests@", 9))
127         s->suggests = parse_deps(s, buf + 10, 0);
128       else if (!strncmp(buf + 1, "obsoletes@", 10))
129         s->obsoletes = parse_deps(s, buf + 11, 0);
130       else if (!strncmp(buf + 1, "conflicts@", 10))
131         s->conflicts = parse_deps(s, buf + 11, 0);
132       else if (!strncmp(buf + 1, "info@", 5))
133         {
134           char *nvra = buf + 6;
135           char *epochstr;
136           char *arch;
137           char *version;
138           char *filename;
139           char *disttag = 0;
140           char *distepoch = 0;
141           if ((epochstr = strchr(nvra, '@')) != 0)
142             {
143               char *sizestr;
144               *epochstr++ = 0;
145               if ((sizestr = strchr(epochstr, '@')) != 0)
146                 {
147                   char *groupstr;
148                   *sizestr++ = 0;
149                   if ((groupstr = strchr(sizestr, '@')) != 0)
150                     {
151                       *groupstr++ = 0;
152                       if ((disttag = strchr(groupstr, '@')) != 0)
153                         {
154                           *disttag++ = 0;
155                           if ((distepoch = strchr(disttag, '@')) != 0)
156                             {
157                               char *n;
158                               *distepoch++ = 0;
159                               if ((n = strchr(distepoch, '@')) != 0)
160                                 *n = 0;
161                             }
162                         }
163                       if (*groupstr)
164                         repodata_set_poolstr(data, s - pool->solvables, SOLVABLE_GROUP, groupstr);
165                     }
166                   if (*sizestr)
167                     repodata_set_num(data, s - pool->solvables, SOLVABLE_INSTALLSIZE, strtoull(sizestr, 0, 10));
168                 }
169             }
170           filename = pool_tmpjoin(pool, nvra, ".rpm", 0);
171           arch = strrchr(nvra, '.');
172           if (arch)
173             {
174               *arch++ = 0;
175               s->arch = pool_str2id(pool, arch, 1);
176             }
177           if (disttag && *disttag)
178             {
179               /* strip disttag from release */
180               char *n = strrchr(nvra, '-');
181               if (n && !strncmp(n + 1, disttag, strlen(disttag)))
182                 *n = 0;
183             }
184           if (distepoch && *distepoch)
185             {
186               /* add distepoch */
187               int le = strlen(distepoch);
188               int ln = strlen(nvra);
189               nvra[ln++] = ':';
190               memmove(nvra + ln, distepoch, le);        /* may overlap */
191               nvra[le + ln] = 0;
192             }
193           version = strrchr(nvra, '-');
194           if (version)
195             {
196               char *release = version;
197               *release = 0;
198               version = strrchr(nvra, '-');
199               *release = '-';
200               if (!version)
201                 version = release;
202               *version++ = 0;
203             }
204           else
205             version = "";
206           s->name = pool_str2id(pool, nvra, 1);
207           if (epochstr && *epochstr && strcmp(epochstr, "0") != 0)
208             {
209               char *evr = pool_tmpjoin(pool, epochstr, ":", version);
210               s->evr = pool_str2id(pool, evr, 1);
211             }
212           else
213             s->evr = pool_str2id(pool, version, 1);
214           repodata_set_location(data, s - pool->solvables, 0, 0, filename);
215           if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
216             s->provides = repo_addid_dep(s->repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
217           s = 0;
218         }
219       else
220         {
221           char *tagend = strchr(buf + 1, '@');
222           if (tagend)
223             *tagend = 0;
224           pool_debug(pool, SOLV_ERROR, "unknown tag <%s>\n", buf + 1);
225           continue;
226         }
227     }
228   if (s)
229     {
230       pool_debug(pool, SOLV_ERROR, "unclosed package at EOF\n");
231       repo_free_solvable(s->repo, s - pool->solvables, 1);
232     }
233   solv_free(buf);
234   if (!(flags & REPO_NO_INTERNALIZE))
235     repodata_internalize(data);
236   return 0;
237 }
238
239 enum state {
240   STATE_START,
241   STATE_MEDIA_INFO,
242   STATE_INFO,
243   STATE_FILES,
244   NUMSTATES
245 };
246
247 struct stateswitch {
248   enum state from;
249   char *ename;
250   enum state to;
251   int docontent;
252 };
253
254 /* must be sorted by first column */
255 static struct stateswitch stateswitches[] = {
256   { STATE_START, "media_info", STATE_MEDIA_INFO, 0 },
257   { STATE_MEDIA_INFO, "info", STATE_INFO, 1 },
258   { STATE_MEDIA_INFO, "files", STATE_FILES, 1 },
259   { NUMSTATES }
260 };
261
262 struct parsedata {
263   Pool *pool;
264   Repo *repo;
265   Repodata *data;
266   int depth;
267   enum state state;
268   int statedepth;
269   char *content;
270   int lcontent;
271   int acontent;
272   int docontent;
273   struct stateswitch *swtab[NUMSTATES];
274   enum state sbtab[NUMSTATES];
275   Solvable *solvable;
276   Hashtable joinhash;
277   Hashval joinhashmask;
278 };
279
280 static inline const char *
281 find_attr(const char *txt, const char **atts)
282 {
283   for (; *atts; atts += 2)
284     {
285       if (!strcmp(*atts, txt))
286         return atts[1];
287     }
288   return 0;
289 }
290
291 static Hashtable
292 joinhash_init(Repo *repo, Hashval *hmp)
293 {
294   Hashval hm = mkmask(repo->nsolvables);
295   Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
296   Hashval h, hh;
297   Solvable *s;
298   int i;
299
300   FOR_REPO_SOLVABLES(repo, i, s)
301     {
302       hh = HASHCHAIN_START;
303       h = s->name & hm;
304       while (ht[h])
305         h = HASHCHAIN_NEXT(h, hh, hm);
306       ht[h] = i;
307     }
308   *hmp = hm;
309   return ht;
310 }
311
312 static Solvable *
313 joinhash_lookup(Repo *repo, Hashtable ht, Hashval hm, const char *fn, const char *distepoch)
314 {
315   Hashval h, hh;
316   const char *p, *vrstart, *vrend;
317   Id name, arch;
318
319   if (!fn || !*fn)
320     return 0;
321   if (distepoch && !*distepoch)
322     distepoch = 0;
323   p = fn + strlen(fn);
324   while (--p > fn)
325     if (*p == '.')
326       break;
327   if (p == fn)
328     return 0;
329   arch = pool_str2id(repo->pool, p + 1, 0);
330   if (!arch)
331     return 0;
332   if (distepoch)
333     {
334       while (--p > fn)
335         if (*p == '-')
336           break;
337       if (p == fn)
338         return 0;
339     }
340   vrend = p;
341   while (--p > fn)
342     if (*p == '-')
343       break;
344   if (p == fn)
345     return 0;
346   while (--p > fn)
347     if (*p == '-')
348       break;
349   if (p == fn)
350     return 0;
351   vrstart = p + 1;
352   name = pool_strn2id(repo->pool, fn, p - fn, 0);
353   if (!name)
354     return 0;
355   hh = HASHCHAIN_START;
356   h = name & hm;
357   while (ht[h])
358     {
359       Solvable *s = repo->pool->solvables + ht[h];
360       if (s->name == name && s->arch == arch)
361         {
362           /* too bad we don't know the epoch... */
363           const char *evr = pool_id2str(repo->pool, s->evr);
364           for (p = evr; *p >= '0' && *p <= '9'; p++)
365             ;
366           if (p > evr && *p == ':')
367             evr = p + 1;
368           if (distepoch)
369             {
370               if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == ':' && !strcmp(distepoch, evr + (vrend - vrstart + 1)))
371                 return s;
372             }
373           else if (!strncmp(evr, vrstart, vrend - vrstart) && evr[vrend - vrstart] == 0)
374             return s;
375         }
376       h = HASHCHAIN_NEXT(h, hh, hm);
377     }
378   return 0;
379 }
380
381 static void XMLCALL
382 startElement(void *userData, const char *name, const char **atts)
383 {
384   struct parsedata *pd = userData;
385   Pool *pool = pd->pool;
386   struct stateswitch *sw;
387
388   if (pd->depth != pd->statedepth)
389     {
390       pd->depth++;
391       return;
392     }
393   pd->depth++;
394   if (!pd->swtab[pd->state])
395     return;
396   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)
397     if (!strcmp(sw->ename, name))
398       break;
399   if (sw->from != pd->state)
400     return;
401   pd->state = sw->to;
402   pd->docontent = sw->docontent;
403   pd->statedepth = pd->depth;
404   pd->lcontent = 0;
405   *pd->content = 0;
406   switch (pd->state)
407     {
408     case STATE_INFO:
409       {
410         const char *fn = find_attr("fn", atts);
411         const char *distepoch = find_attr("distepoch", atts);
412         const char *str;
413         pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
414         if (!pd->solvable)
415           break;
416         str = find_attr("url", atts);
417         if (str && *str)
418           repodata_set_str(pd->data, pd->solvable - pool->solvables, SOLVABLE_URL, str);
419         str = find_attr("license", atts);
420         if (str && *str)
421           repodata_set_poolstr(pd->data, pd->solvable - pool->solvables, SOLVABLE_LICENSE, str);
422         str = find_attr("sourcerpm", atts);
423         if (str && *str)
424           repodata_set_sourcepkg(pd->data, pd->solvable - pool->solvables, str);
425         break;
426       }
427     case STATE_FILES:
428       {
429         const char *fn = find_attr("fn", atts);
430         const char *distepoch = find_attr("distepoch", atts);
431         pd->solvable = joinhash_lookup(pd->repo, pd->joinhash, pd->joinhashmask, fn, distepoch);
432         break;
433       }
434     default:
435       break;
436     }
437 }
438
439 static void XMLCALL
440 endElement(void *userData, const char *name)
441 {
442   struct parsedata *pd = userData;
443   Solvable *s = pd->solvable;
444   if (pd->depth != pd->statedepth)
445     {
446       pd->depth--;
447       return;
448     }
449   pd->depth--;
450   pd->statedepth--;
451   switch (pd->state)
452     {
453     case STATE_INFO:
454       if (s && *pd->content)
455         repodata_set_str(pd->data, s - pd->pool->solvables, SOLVABLE_DESCRIPTION, pd->content);
456       break;
457     case STATE_FILES:
458       if (s && *pd->content)
459         {
460           char *np, *p, *sl;
461           for (p = pd->content; p && *p; p = np)
462             {
463               Id id;
464               np = strchr(p, '\n');
465               if (np)
466                 *np++ = 0;
467               if (!*p)
468                 continue;
469               sl = strrchr(p, '/');
470               if (sl)
471                 {
472                   *sl++ = 0;
473                   id = repodata_str2dir(pd->data, p, 1);
474                 }
475               else
476                 {
477                   sl = p;
478                   id = 0;
479                 }
480               if (!id)
481                 id = repodata_str2dir(pd->data, "/", 1);
482               repodata_add_dirstr(pd->data, s - pd->pool->solvables, SOLVABLE_FILELIST, id, sl);
483             }
484         }
485       break;
486     default:
487       break;
488     }
489   pd->state = pd->sbtab[pd->state];
490   pd->docontent = 0;
491 }
492
493 static void XMLCALL
494 characterData(void *userData, const XML_Char *s, int len)
495 {
496   struct parsedata *pd = userData;
497   int l;
498   char *c;
499   if (!pd->docontent)
500     return;
501   l = pd->lcontent + len + 1;
502   if (l > pd->acontent)
503     {
504       pd->content = solv_realloc(pd->content, l + 256);
505       pd->acontent = l + 256;
506     }
507   c = pd->content + pd->lcontent;
508   pd->lcontent += len;
509   while (len-- > 0)
510     *c++ = *s++;
511   *c = 0;
512 }
513
514 #define BUFF_SIZE 8192
515
516 int
517 repo_add_mdk_info(Repo *repo, FILE *fp, int flags)
518 {
519   Repodata *data;
520   struct parsedata pd;
521   char buf[BUFF_SIZE];
522   int i, l;
523   struct stateswitch *sw;
524   XML_Parser parser;
525
526   if (!(flags & REPO_EXTEND_SOLVABLES))
527     {
528       pool_debug(repo->pool, SOLV_ERROR, "repo_add_mdk_info: can only extend existing solvables\n");
529       return -1;
530     }
531
532   data = repo_add_repodata(repo, flags);
533
534   memset(&pd, 0, sizeof(pd));
535   pd.repo = repo;
536   pd.pool = repo->pool;
537   pd.data = data;
538
539   pd.content = solv_malloc(256);
540   pd.acontent = 256;
541
542   pd.joinhash = joinhash_init(repo, &pd.joinhashmask);
543
544   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
545     {
546       if (!pd.swtab[sw->from])
547         pd.swtab[sw->from] = sw;
548       pd.sbtab[sw->to] = sw->from;
549     }
550
551   parser = XML_ParserCreate(NULL);
552   XML_SetUserData(parser, &pd);
553   XML_SetElementHandler(parser, startElement, endElement);
554   XML_SetCharacterDataHandler(parser, characterData);
555   for (;;)
556     {
557       l = fread(buf, 1, sizeof(buf), fp);
558       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
559         {
560           pool_debug(pd.pool, SOLV_ERROR, "%s at line %u:%u\n", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
561           break;
562         }
563       if (l == 0)
564         break;
565     }
566   XML_ParserFree(parser);
567   solv_free(pd.content);
568   solv_free(pd.joinhash);
569   if (!(flags & REPO_NO_INTERNALIZE))
570     repodata_internalize(data);
571   return 0;
572 }