Add a couple of architectures.
[platform/upstream/libsolv.git] / ext / repo_susetags.c
1 /*
2  * Copyright (c) 2007, 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 <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "pool.h"
16 #include "repo.h"
17 #include "hash.h"
18 #include "chksum.h"
19 #include "tools_util.h"
20 #include "repo_susetags.h"
21
22 struct datashare {
23   Id name;
24   Id evr;
25   Id arch;
26 };
27
28 struct parsedata {
29   int ret;
30   Pool *pool;
31   Repo *repo;
32   Repodata *data;
33   char *kind;
34   int flags;
35   int last_found_source;
36   struct datashare *share_with;
37   int nshare;
38   Id (*dirs)[3];                        /* dirid, size, nfiles */
39   int ndirs;
40   struct joindata jd;
41   char *language;                       /* the default language */
42   Id langcache[ID_NUM_INTERNAL];        /* cache for the default language */
43   int lineno;
44   char *filelist;
45   int afilelist;                        /* allocated */
46   int nfilelist;                        /* used */
47 };
48
49 static char *flagtab[] = {
50   ">",
51   "=",
52   ">=",
53   "<",
54   "!=",
55   "<="
56 };
57
58
59 static Id
60 langtag(struct parsedata *pd, Id tag, const char *language)
61 {
62   if (language && *language)
63     return pool_id2langid(pd->repo->pool, tag, language, 1);
64   if (!pd->language)
65     return tag;
66   if (tag >= ID_NUM_INTERNAL)
67     return pool_id2langid(pd->repo->pool, tag, pd->language, 1);
68   if (!pd->langcache[tag])
69     pd->langcache[tag] = pool_id2langid(pd->repo->pool, tag, pd->language, 1);
70   return pd->langcache[tag];
71 }
72
73 /*
74  * adddep
75  * create and add dependency
76  */
77
78 static unsigned int
79 adddep(Pool *pool, struct parsedata *pd, unsigned int olddeps, char *line, Id marker, char *kind)
80 {
81   int i, flags;
82   Id id, evrid;
83   char *sp[4];
84
85   if (line[6] == '/')
86     {
87       /* A file dependency. Do not try to parse it */
88       id = pool_str2id(pool, line + 6, 1);
89     }
90   else
91     {
92       i = split(line + 6, sp, 4); /* name, <op>, evr, ? */
93       if (i != 1 && i != 3) /* expect either 'name' or 'name' <op> 'evr' */
94         {
95           pd->ret = pool_error(pool, -1, "susetags: line %d: bad dependency: '%s'\n", pd->lineno, line);
96           return olddeps;
97         }
98       if (kind)
99         id = pool_str2id(pool, join2(&pd->jd, kind, ":", sp[0]), 1);
100       else
101         id = pool_str2id(pool, sp[0], 1);
102       if (i == 3)
103         {
104           evrid = makeevr(pool, sp[2]);
105           for (flags = 0; flags < 6; flags++)
106             if (!strcmp(sp[1], flagtab[flags]))
107               break;
108           if (flags == 6)
109             {
110               if (!strcmp(sp[1], "<>"))
111                 flags = 4;
112               else
113                 {
114                   pd->ret = pool_error(pool, -1, "susetags: line %d: unknown relation: '%s'\n", pd->lineno, sp[1]);
115                   return olddeps;
116                 }
117             }
118           id = pool_rel2id(pool, id, evrid, flags + 1, 1);
119         }
120     }
121   return repo_addid_dep(pd->repo, olddeps, id, marker);
122 }
123
124
125 /*
126  * add_source
127  *
128  */
129
130 static void
131 add_source(struct parsedata *pd, char *line, Solvable *s, Id handle)
132 {
133   Pool *pool = s->repo->pool;
134   char *sp[5];
135   Id name;
136   Id arch;
137   const char *evr, *sevr;
138
139   if (split(line, sp, 5) != 4)
140     {
141       pd->ret = pool_error(pool, -1, "susetags: line %d: bad source line '%s'\n", pd->lineno, line);
142       return;
143     }
144
145   name = pool_str2id(pool, sp[0], 1);
146   arch = pool_str2id(pool, sp[3], 1);   /* do this before id2str */
147   evr = join2(&pd->jd, sp[1], "-", sp[2]);
148   sevr = pool_id2str(pool, s->evr);
149   if (sevr)
150     {
151       /* strip epoch */
152       const char *p;
153       for (p = sevr; *p >= '0' && *p <= '9'; p++)
154         ;
155       if (p != sevr && *p == ':' && p[1])
156         sevr = p;
157     }
158   if (name == s->name)
159     repodata_set_void(pd->data, handle, SOLVABLE_SOURCENAME);
160   else
161     repodata_set_id(pd->data, handle, SOLVABLE_SOURCENAME, name);
162   if (sevr && !strcmp(sevr, evr))
163     repodata_set_void(pd->data, handle, SOLVABLE_SOURCEEVR);
164   else
165     repodata_set_id(pd->data, handle, SOLVABLE_SOURCEEVR, pool_str2id(pool, evr, 1));
166   repodata_set_constantid(pd->data, handle, SOLVABLE_SOURCEARCH, arch);
167 }
168
169 /*
170  * add_dirline
171  * add a line with directory information
172  *
173  */
174
175 static void
176 add_dirline(struct parsedata *pd, char *line)
177 {
178   char *sp[6];
179   long filesz;
180   long filenum;
181   Id dirid;
182   if (split(line, sp, 6) != 5)
183     return;
184   pd->dirs = solv_extend(pd->dirs, pd->ndirs, 1, sizeof(pd->dirs[0]), 31);
185   filesz = strtol(sp[1], 0, 0);
186   filesz += strtol(sp[2], 0, 0);
187   filenum = strtol(sp[3], 0, 0);
188   filenum += strtol(sp[4], 0, 0);
189   /* hack: we know that there's room for a / */
190   if (*sp[0] != '/')
191     *--sp[0] = '/';
192   dirid = repodata_str2dir(pd->data, sp[0], 1);
193 #if 0
194 fprintf(stderr, "%s -> %d\n", sp[0], dirid);
195 #endif
196   pd->dirs[pd->ndirs][0] = dirid;
197   pd->dirs[pd->ndirs][1] = filesz;
198   pd->dirs[pd->ndirs][2] = filenum;
199   pd->ndirs++;
200 }
201
202 static void
203 set_checksum(struct parsedata *pd, Repodata *data, Id handle, Id keyname, char *line)
204 {
205   char *sp[3];
206   Id type;
207   if (split(line, sp, 3) != 2)
208     {
209       pd->ret = pool_error(pd->pool, -1, "susetags: line %d: bad checksum line '%s'\n", pd->lineno, line);
210       return;
211     }
212   type = solv_chksum_str2type(sp[0]);
213   if (!type)
214     {
215       pd->ret = pool_error(pd->pool, -1, "susetags: line %d: unknown checksum type: '%s'\n", pd->lineno, sp[0]);
216       return;
217     }
218   if (strlen(sp[1]) != 2 * solv_chksum_len(type))
219     {
220       pd->ret = pool_error(pd->pool, -1, "susetags: line %d: bad checksum length for type %s: '%s'\n", pd->lineno, sp[0], sp[1]);
221       return;
222     }
223   repodata_set_checksum(data, handle, keyname, type, sp[1]);
224 }
225
226
227 /*
228  * id3_cmp
229  * compare
230  *
231  */
232
233 static int
234 id3_cmp(const void *v1, const void *v2, void *dp)
235 {
236   Id *i1 = (Id*)v1;
237   Id *i2 = (Id*)v2;
238   return i1[0] - i2[0];
239 }
240
241
242 /*
243  * commit_diskusage
244  *
245  */
246
247 static void
248 commit_diskusage(struct parsedata *pd, Id handle)
249 {
250   int i;
251   Dirpool *dp = &pd->data->dirpool;
252   /* Now sort in dirid order.  This ensures that parents come before
253      their children.  */
254   if (pd->ndirs > 1)
255     solv_sort(pd->dirs, pd->ndirs, sizeof(pd->dirs[0]), id3_cmp, 0);
256   /* Substract leaf numbers from all parents to make the numbers
257      non-cumulative.  This must be done post-order (i.e. all leafs
258      adjusted before parents).  We ensure this by starting at the end of
259      the array moving to the start, hence seeing leafs before parents.  */
260   for (i = pd->ndirs; i--;)
261     {
262       Id p = dirpool_parent(dp, pd->dirs[i][0]);
263       int j = i;
264       for (; p; p = dirpool_parent(dp, p))
265         {
266           for (; j--;)
267             if (pd->dirs[j][0] == p)
268               break;
269           if (j >= 0)
270             {
271               if (pd->dirs[j][1] < pd->dirs[i][1])
272                 pd->dirs[j][1] = 0;
273               else
274                 pd->dirs[j][1] -= pd->dirs[i][1];
275               if (pd->dirs[j][2] < pd->dirs[i][2])
276                 pd->dirs[j][2] = 0;
277               else
278                 pd->dirs[j][2] -= pd->dirs[i][2];
279             }
280           else
281             /* Haven't found this parent in the list, look further if
282                we maybe find the parents parent.  */
283             j = i;
284         }
285     }
286 #if 0
287   char sbuf[1024];
288   char *buf = sbuf;
289   unsigned slen = sizeof(sbuf);
290   for (i = 0; i < pd->ndirs; i++)
291     {
292       dir2str(attr, pd->dirs[i][0], &buf, &slen);
293       fprintf(stderr, "have dir %d %d %d %s\n", pd->dirs[i][0], pd->dirs[i][1], pd->dirs[i][2], buf);
294     }
295   if (buf != sbuf)
296     free (buf);
297 #endif
298   for (i = 0; i < pd->ndirs; i++)
299     if (pd->dirs[i][1] || pd->dirs[i][2])
300       {
301         repodata_add_dirnumnum(pd->data, handle, SOLVABLE_DISKUSAGE, pd->dirs[i][0], pd->dirs[i][1], pd->dirs[i][2]);
302       }
303   pd->ndirs = 0;
304 }
305
306
307 /* Unfortunately "a"[0] is no constant expression in the C languages,
308    so we need to pass the four characters individually :-/  */
309 #define CTAG(a,b,c,d) ((unsigned)(((unsigned char)a) << 24) \
310  | ((unsigned char)b << 16) \
311  | ((unsigned char)c << 8) \
312  | ((unsigned char)d))
313
314 /*
315  * tag_from_string
316  *
317  */
318
319 static inline unsigned
320 tag_from_string(char *cs)
321 {
322   unsigned char *s = (unsigned char *)cs;
323   return ((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3]);
324 }
325
326
327 /*
328  * repo_add_susetags
329  * Parse susetags file passed in fp, fill solvables into repo
330  *
331  * susetags is key,value based
332  *  for short values
333  *    =key: value
334  *  is used
335  *  for long (multi-line) values,
336  *    +key:
337  *    value
338  *    value
339  *    -key:
340  *  is used
341  *
342  * See http://en.opensuse.org/Standards/YaST2_Repository_Metadata
343  * and http://en.opensuse.org/Standards/YaST2_Repository_Metadata/packages
344  * and http://en.opensuse.org/Standards/YaST2_Repository_Metadata/pattern
345  *
346  * Assumptions:
347  *   All keys have 3 characters and end in ':'
348  */
349
350 static void
351 finish_solvable(struct parsedata *pd, Solvable *s, Offset freshens)
352 {
353   Pool *pool = pd->repo->pool;
354   Id handle = s - pool->solvables;
355
356   if (pd->nfilelist)
357     {
358       int l;
359       Id did;
360       for (l = 0; l < pd->nfilelist; l += strlen(pd->filelist + l) + 1)
361         {
362           char *p = strrchr(pd->filelist + l, '/');
363           if (!p)
364             continue;
365           *p++ = 0;
366           did = repodata_str2dir(pd->data, pd->filelist + l, 1);
367           p[-1] = '/';
368           if (!did)
369             did = repodata_str2dir(pd->data, "/", 1);
370           repodata_add_dirstr(pd->data, handle, SOLVABLE_FILELIST, did, p);
371         }
372       pd->nfilelist = 0;
373     }
374   /* A self provide, except for source packages.  This is harmless
375      to do twice (in case we see the same package twice).  */
376   if (s->name && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
377     s->provides = repo_addid_dep(pd->repo, s->provides,
378                 pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
379   /* XXX This uses repo_addid_dep internally, so should also be
380      harmless to do twice.  */
381   s->supplements = repo_fix_supplements(pd->repo, s->provides, s->supplements, freshens);
382   s->conflicts = repo_fix_conflicts(pd->repo, s->conflicts);
383   if (pd->ndirs)
384     commit_diskusage(pd, handle);
385 }
386
387 static Hashtable
388 joinhash_init(Repo *repo, Hashval *hmp)
389 {
390   Hashval hm = mkmask(repo->nsolvables);
391   Hashtable ht = solv_calloc(hm + 1, sizeof(*ht));
392   Hashval h, hh;
393   Solvable *s;
394   int i;
395
396   FOR_REPO_SOLVABLES(repo, i, s)
397     {
398       hh = HASHCHAIN_START;
399       h = s->name & hm;
400       while (ht[h])
401         h = HASHCHAIN_NEXT(h, hh, hm);
402       ht[h] = i;
403     }
404   *hmp = hm;
405   return ht;
406 }
407
408 static Solvable *
409 joinhash_lookup(Repo *repo, Hashtable ht, Hashval hm, Id name, Id evr, Id arch, Id start)
410 {
411   Hashval h, hh;
412
413   if (!name || !arch || !evr)
414     return 0;
415   hh = HASHCHAIN_START;
416   h = name & hm;
417   while (ht[h])
418     {
419       Solvable *s = repo->pool->solvables + ht[h];
420       if (ht[h] >= start && s->name == name && s->evr == evr && s->arch == arch)
421         return s;
422       h = HASHCHAIN_NEXT(h, hh, hm);
423     }
424   return 0;
425 }
426
427 static Id
428 lookup_shared_id(Repodata *data, Id p, Id keyname, Id voidid, int uninternalized)
429 {
430   Id r;
431   r = repodata_lookup_type(data, p, keyname);
432   if (r)
433     {
434       if (r == REPOKEY_TYPE_VOID)
435         return voidid;
436       r = repodata_lookup_id(data, p, keyname);
437       if (r)
438         return r;
439     }
440   if (uninternalized)
441     return repodata_lookup_id_uninternalized(data, p, keyname, voidid);
442   return 0;
443 }
444
445 static inline Id
446 toevr(Pool *pool, struct parsedata *pd, const char *version, const char *release)
447 {
448   return makeevr(pool, !release || (release[0] == '-' && !release[1]) ?
449         version : join2(&pd->jd, version, "-", release));
450 }
451
452
453 /*
454  * parse susetags
455  *
456  * fp: file to read from
457  * defvendor: default vendor (0 if none)
458  * language: current language (0 if none)
459  * flags: flags
460  */
461
462 int
463 repo_add_susetags(Repo *repo, FILE *fp, Id defvendor, const char *language, int flags)
464 {
465   Pool *pool = repo->pool;
466   char *line, *linep;
467   int aline;
468   Solvable *s;
469   Offset freshens;
470   int intag = 0;
471   int cummulate = 0;
472   int indesc = 0;
473   int indelta = 0;
474   int last_found_pack = 0;
475   Id first_new_pkg = 0;
476   char *sp[5];
477   struct parsedata pd;
478   Repodata *data = 0;
479   Id handle = 0;
480   Hashtable joinhash = 0;
481   Hashval joinhashm = 0;
482   int createdpkgs = 0;
483
484   if ((flags & (SUSETAGS_EXTEND|REPO_EXTEND_SOLVABLES)) != 0 && repo->nrepodata)
485     {
486       joinhash = joinhash_init(repo, &joinhashm);
487       indesc = 1;
488     }
489
490   data = repo_add_repodata(repo, flags);
491
492   memset(&pd, 0, sizeof(pd));
493   line = solv_malloc(1024);
494   aline = 1024;
495
496   pd.pool = pool;
497   pd.repo = repo;
498   pd.data = data;
499   pd.flags = flags;
500   pd.language = language && *language ? solv_strdup(language) : 0;
501
502   linep = line;
503   s = 0;
504   freshens = 0;
505
506   /* if this is a join setup the recorded share data */
507   if (joinhash)
508     {
509       Repodata *sdata;
510       int i;
511
512       FOR_REPODATAS(repo, i, sdata)
513         {
514           int p;
515           if (!repodata_has_keyname(sdata, SUSETAGS_SHARE_NAME))
516             continue;
517           for (p = sdata->start; p < sdata->end; p++)
518             {
519               Id name, evr, arch;
520               name = lookup_shared_id(sdata, p, SUSETAGS_SHARE_NAME, pool->solvables[p].name, sdata == data);
521               if (!name)
522                 continue;
523               evr = lookup_shared_id(sdata, p, SUSETAGS_SHARE_EVR, pool->solvables[p].evr, sdata == data);
524               if (!evr)
525                 continue;
526               arch = lookup_shared_id(sdata, p, SUSETAGS_SHARE_ARCH, pool->solvables[p].arch, sdata == data);
527               if (!arch)
528                 continue;
529               if (p - repo->start >= pd.nshare)
530                 {
531                   pd.share_with = solv_realloc2(pd.share_with, p - repo->start + 256, sizeof(*pd.share_with));
532                   memset(pd.share_with + pd.nshare, 0, (p - repo->start + 256 - pd.nshare) * sizeof(*pd.share_with));
533                   pd.nshare = p - repo->start + 256;
534                 }
535               pd.share_with[p - repo->start].name = name;
536               pd.share_with[p - repo->start].evr = evr;
537               pd.share_with[p - repo->start].arch = arch;
538             }
539         }
540     }
541
542   /*
543    * read complete file
544    *
545    * collect values in 'struct parsedata pd'
546    * then build .solv (and .attr) file
547    */
548
549   for (;;)
550     {
551       unsigned tag;
552       char *olinep; /* old line pointer */
553       char line_lang[6];
554       int keylen = 3;
555
556       if (pd.ret)
557         break;
558       if (linep - line + 16 > aline)              /* (re-)alloc buffer */
559         {
560           aline = linep - line;
561           line = solv_realloc(line, aline + 512);
562           linep = line + aline;
563           aline += 512;
564         }
565       if (!fgets(linep, aline - (linep - line), fp)) /* read line */
566         break;
567       olinep = linep;
568       linep += strlen(linep);
569       if (linep == line || linep[-1] != '\n')
570         continue;
571       pd.lineno++;
572       *--linep = 0;
573       if (linep == olinep)
574         continue;
575
576       if (intag)
577         {
578           /* check for multi-line value tags (+Key:/-Key:) */
579
580           int is_end = (linep[-intag - keylen + 1] == '-')
581                       && (linep[-1] == ':')
582                       && (linep == line + 1 + intag + 1 + 1 + 1 + intag + 1 || linep[-intag - keylen] == '\n');
583           if (is_end
584               && strncmp(linep - 1 - intag, line + 1, intag))
585             {
586               pool_debug(pool, SOLV_ERROR, "susetags: Nonmatching multi-line tags: %d: '%s' '%s' %d\n", pd.lineno, linep - 1 - intag, line + 1, intag);
587             }
588           if (cummulate && !is_end)
589             {
590               *linep++ = '\n';
591               continue;
592             }
593           if (cummulate && is_end)
594             {
595               linep[-intag - keylen + 1] = 0;
596               if (linep[-intag - keylen] == '\n')
597                 linep[-intag - keylen] = 0;
598               linep = line;
599               intag = 0;
600             }
601           if (!cummulate && is_end)
602             {
603               intag = 0;
604               linep = line;
605               continue;
606             }
607           if (!cummulate && !is_end)
608             linep = line + intag + keylen;
609         }
610       else
611         linep = line;
612
613       if (!intag && line[0] == '+' && line[1] && line[1] != ':') /* start of +Key:/-Key: tag */
614         {
615           char *tagend = strchr(line, ':');
616           if (!tagend)
617             {
618               pd.ret = pool_error(pool, -1, "susetags: line %d: bad line '%s'\n", pd.lineno, line);
619               break;
620             }
621           intag = tagend - (line + 1);
622           cummulate = 0;
623           switch (tag_from_string(line))       /* check if accumulation is needed */
624             {
625               case CTAG('+', 'D', 'e', 's'):
626               case CTAG('+', 'E', 'u', 'l'):
627               case CTAG('+', 'I', 'n', 's'):
628               case CTAG('+', 'D', 'e', 'l'):
629               case CTAG('+', 'A', 'u', 't'):
630                 if (line[4] == ':' || line[4] == '.')
631                   cummulate = 1;
632                 break;
633               default:
634                 break;
635             }
636           line[0] = '=';                       /* handle lines between +Key:/-Key: as =Key: */
637           line[intag + keylen - 1] = ' ';
638           linep = line + intag + keylen;
639           continue;
640         }
641       if (*line == '#' || !*line)
642         continue;
643       if (! (line[0] && line[1] && line[2] && line[3] && (line[4] == ':' || line[4] == '.')))
644         continue;
645       line_lang[0] = 0;
646       if (line[4] == '.')
647         {
648           char *endlang;
649           endlang = strchr(line + 5, ':');
650           if (endlang)
651             {
652               int langsize = endlang - (line + 5);
653               keylen = endlang - line - 1;
654               if (langsize > 5)
655                 langsize = 5;
656               strncpy(line_lang, line + 5, langsize);
657               line_lang[langsize] = 0;
658             }
659         }
660       tag = tag_from_string(line);
661
662       if (indelta)
663         {
664           /* Example:
665             =Dlt: subversion 1.6.16 1.3.1 i586
666             =Dsq: subversion 1.6.15 4.2 i586 d57b3fc86e7a2f73796e8e35b96fa86212c910
667             =Cks: SHA1 14a8410cf741856a5d70d89dab62984dba6a1ca7
668             =Loc: 1 subversion-1.6.15_1.6.16-4.2_1.3.1.i586.delta.rpm
669             =Siz: 81558
670            */
671           switch (tag)
672             {
673             case CTAG('=', 'D', 's', 'q'):
674               {
675                 Id evr;
676                 if (split(line + 5, sp, 5) != 5)
677                   continue;
678                 repodata_set_id(data, handle, DELTA_SEQ_NAME, pool_str2id(pool, sp[0], 1));
679                 evr = toevr(pool, &pd, sp[1], sp[2]);
680                 repodata_set_id(data, handle, DELTA_SEQ_EVR, evr);
681                 /* repodata_set_id(data, handle, DELTA_SEQ_ARCH, pool_str2id(pool, sp[3], 1)); */
682                 repodata_set_str(data, handle, DELTA_SEQ_NUM, sp[4]);
683                 repodata_set_id(data, handle, DELTA_BASE_EVR, evr);
684                 continue;
685               }
686             case CTAG('=', 'C', 'k', 's'):
687               set_checksum(&pd, data, handle, DELTA_CHECKSUM, line + 6);
688               continue;
689             case CTAG('=', 'L', 'o', 'c'):
690               {
691                 int i = split(line + 6, sp, 3);
692                 if (i != 2 && i != 3)
693                   {
694                     pd.ret = pool_error(pool, -1, "susetags: line %d: bad location line '%s'\n", pd.lineno, line);
695                     continue;
696                   }
697                 repodata_set_deltalocation(data, handle, atoi(sp[0]), i == 3 ? sp[2] : 0, sp[1]);
698                 continue;
699               }
700             case CTAG('=', 'S', 'i', 'z'):
701               if (split(line + 6, sp, 3) == 2)
702                 repodata_set_num(data, handle, DELTA_DOWNLOADSIZE, strtoull(sp[0], 0, 10));
703               continue;
704             case CTAG('=', 'P', 'k', 'g'):
705             case CTAG('=', 'P', 'a', 't'):
706             case CTAG('=', 'D', 'l', 't'):
707               handle = 0;
708               indelta = 0;
709               break;
710             default:
711               pool_debug(pool, SOLV_ERROR, "susetags: unknown line: %d: %s\n", pd.lineno, line);
712               continue;
713             }
714         }
715
716       /*
717        * start of (next) package or pattern or delta
718        *
719        * =Pkg: <name> <version> <release> <architecture>
720        * (=Pat: ...)
721        */
722       if (tag == CTAG('=', 'D', 'l', 't'))
723         {
724           if (s)
725             finish_solvable(&pd, s, freshens);
726           s = 0;
727           pd.kind = 0;
728           if (split(line + 5, sp, 5) != 4)
729             {
730               pd.ret = pool_error(pool, -1, "susetags: line %d: bad line '%s'\n", pd.lineno, line);
731               break;
732             }
733           handle = repodata_new_handle(data);
734           repodata_set_id(data, handle, DELTA_PACKAGE_NAME, pool_str2id(pool, sp[0], 1));
735           repodata_set_id(data, handle, DELTA_PACKAGE_EVR, toevr(pool, &pd, sp[1], sp[2]));
736           repodata_set_id(data, handle, DELTA_PACKAGE_ARCH, pool_str2id(pool, sp[3], 1));
737           repodata_add_flexarray(data, SOLVID_META, REPOSITORY_DELTAINFO, handle);
738           indelta = 1;
739           continue;
740         }
741       if (tag == CTAG('=', 'P', 'k', 'g')
742            || tag == CTAG('=', 'P', 'a', 't'))
743         {
744           /* If we have an old solvable, complete it by filling in some
745              default stuff.  */
746           if (s)
747             finish_solvable(&pd, s, freshens);
748
749           /*
750            * define kind
751            */
752
753           pd.kind = 0;
754           if (line[3] == 't')
755             pd.kind = "pattern";
756
757           /*
758            * parse nevra
759            */
760
761           if (split(line + 5, sp, 5) != 4)
762             {
763               pd.ret = pool_error(pool, -1, "susetags: line %d: bad line '%s'\n", pd.lineno, line);
764               break;
765             }
766           s = 0;
767           freshens = 0;
768
769           if (joinhash)
770             {
771               /* data join operation. find solvable matching name/arch/evr and
772                * add data to it */
773               Id name, evr, arch;
774               /* we don't use the create flag here as a simple pre-check for existance */
775               if (pd.kind)
776                 name = pool_str2id(pool, join2(&pd.jd, pd.kind, ":", sp[0]), 0);
777               else
778                 name = pool_str2id(pool, sp[0], 0);
779               evr = toevr(pool, &pd, sp[1], sp[2]);
780               arch = pool_str2id(pool, sp[3], 0);
781               if (name && arch)
782                 {
783                   Id start = (flags & REPO_EXTEND_SOLVABLES) ? 0 : first_new_pkg;
784                   if (repo->start + last_found_pack + 1 >= start && repo->start + last_found_pack + 1 < repo->end)
785                     {
786                       s = pool->solvables + repo->start + last_found_pack + 1;
787                       if (s->repo != repo || s->name != name || s->evr != evr || s->arch != arch)
788                         s = 0;
789                     }
790                   if (!s)
791                     s = joinhash_lookup(repo, joinhash, joinhashm, name, evr, arch, start);
792                 }
793               /* do not create new packages in EXTEND_SOLVABLES mode */
794               if (!s && (flags & REPO_EXTEND_SOLVABLES) != 0)
795                 continue;
796               /* fallthrough to package creation */
797             }
798           if (!s)
799             {
800               /* normal operation. create a new solvable. */
801               s = pool_id2solvable(pool, repo_add_solvable(repo));
802               if (pd.kind)
803                 s->name = pool_str2id(pool, join2(&pd.jd, pd.kind, ":", sp[0]), 1);
804               else
805                 s->name = pool_str2id(pool, sp[0], 1);
806               s->evr = toevr(pool, &pd, sp[1], sp[2]);
807               s->arch = pool_str2id(pool, sp[3], 1);
808               s->vendor = defvendor;
809               if (!first_new_pkg)
810                 first_new_pkg = s - pool->solvables;
811               createdpkgs = 1;
812             }
813           last_found_pack = (s - pool->solvables) - repo->start;
814           if (data)
815             handle = s - pool->solvables;
816         }
817
818       /* If we have no current solvable to add to, ignore all further lines
819          for it.  Probably invalid input data in the second set of
820          solvables.  */
821       if (indesc >= 2 && !s)
822         {
823 #if 0
824           pool_debug(pool, SOLV_ERROR, "susetags: huh %d: %s?\n", pd.lineno, line);
825 #endif
826           continue;
827         }
828       switch (tag)
829         {
830           case CTAG('=', 'P', 'r', 'v'):                                        /* provides */
831             if (line[6] == '/')
832               {
833                 /* probably a filelist entry. stash it away for now */
834                 int l = strlen(line + 6) + 1;
835                 if (pd.nfilelist + l > pd.afilelist)
836                   {
837                     pd.afilelist = pd.nfilelist + l + 512;
838                     pd.filelist = solv_realloc(pd.filelist, pd.afilelist);
839                   }
840                 memcpy(pd.filelist + pd.nfilelist, line + 6, l);
841                 pd.nfilelist += l;
842                 break;
843               }
844             if (pd.nfilelist)
845               {
846                 int l;
847                 for (l = 0; l < pd.nfilelist; l += strlen(pd.filelist + l) + 1)
848                   s->provides = repo_addid_dep(pd.repo, s->provides, pool_str2id(pool, pd.filelist + l, 1), 0);
849                 pd.nfilelist = 0;
850               }
851             s->provides = adddep(pool, &pd, s->provides, line, 0, pd.kind);
852             continue;
853           case CTAG('=', 'R', 'e', 'q'):                                        /* requires */
854             s->requires = adddep(pool, &pd, s->requires, line, -SOLVABLE_PREREQMARKER, pd.kind);
855             continue;
856           case CTAG('=', 'P', 'r', 'q'):                                        /* pre-requires / packages required */
857             if (pd.kind)
858               {
859                 s->requires = adddep(pool, &pd, s->requires, line, 0, 0);           /* patterns: a required package */
860               }
861             else
862               s->requires = adddep(pool, &pd, s->requires, line, SOLVABLE_PREREQMARKER, 0); /* package: pre-requires */
863             continue;
864           case CTAG('=', 'O', 'b', 's'):                                        /* obsoletes */
865             s->obsoletes = adddep(pool, &pd, s->obsoletes, line, 0, pd.kind);
866             continue;
867           case CTAG('=', 'C', 'o', 'n'):                                        /* conflicts */
868             s->conflicts = adddep(pool, &pd, s->conflicts, line, 0, pd.kind);
869             continue;
870           case CTAG('=', 'R', 'e', 'c'):                                        /* recommends */
871             s->recommends = adddep(pool, &pd, s->recommends, line, 0, pd.kind);
872             continue;
873           case CTAG('=', 'S', 'u', 'p'):                                        /* supplements */
874             s->supplements = adddep(pool, &pd, s->supplements, line, 0, pd.kind);
875             continue;
876           case CTAG('=', 'E', 'n', 'h'):                                        /* enhances */
877             s->enhances = adddep(pool, &pd, s->enhances, line, 0, pd.kind);
878             continue;
879           case CTAG('=', 'S', 'u', 'g'):                                        /* suggests */
880             s->suggests = adddep(pool, &pd, s->suggests, line, 0, pd.kind);
881             continue;
882           case CTAG('=', 'F', 'r', 'e'):                                        /* freshens */
883             freshens = adddep(pool, &pd, freshens, line, 0, pd.kind);
884             continue;
885           case CTAG('=', 'P', 'r', 'c'):                                        /* packages recommended */
886             s->recommends = adddep(pool, &pd, s->recommends, line, 0, 0);
887             continue;
888           case CTAG('=', 'P', 's', 'g'):                                        /* packages suggested */
889             s->suggests = adddep(pool, &pd, s->suggests, line, 0, 0);
890             continue;
891           case CTAG('=', 'P', 'c', 'n'):                                        /* pattern: package conflicts */
892             s->conflicts = adddep(pool, &pd, s->conflicts, line, 0, 0);
893             continue;
894           case CTAG('=', 'P', 'o', 'b'):                                        /* pattern: package obsoletes */
895             s->obsoletes = adddep(pool, &pd, s->obsoletes, line, 0, 0);
896             continue;
897           case CTAG('=', 'P', 'f', 'r'):                                        /* pattern: package freshens */
898             freshens = adddep(pool, &pd, freshens, line, 0, 0);
899             continue;
900           case CTAG('=', 'P', 's', 'p'):                                        /* pattern: package supplements */
901             s->supplements = adddep(pool, &pd, s->supplements, line, 0, 0);
902             continue;
903           case CTAG('=', 'P', 'e', 'n'):                                        /* pattern: package enhances */
904             s->enhances = adddep(pool, &pd, s->enhances, line, 0, 0);
905             continue;
906           case CTAG('=', 'V', 'e', 'r'):                                        /* - version - */
907             last_found_pack = 0;
908             handle = 0;
909             indesc++;
910             if (createdpkgs)
911               {
912                 solv_free(joinhash);
913                 joinhash = joinhash_init(repo, &joinhashm);
914                 createdpkgs = 0;
915               }
916             continue;
917           case CTAG('=', 'V', 'n', 'd'):                                        /* vendor */
918             s->vendor = pool_str2id(pool, line + 6, 1);
919             continue;
920
921         /* From here it's the attribute tags.  */
922           case CTAG('=', 'G', 'r', 'p'):
923             repodata_set_poolstr(data, handle, SOLVABLE_GROUP, line + 6);
924             continue;
925           case CTAG('=', 'L', 'i', 'c'):
926             repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, line + 6);
927             continue;
928           case CTAG('=', 'L', 'o', 'c'):
929             {
930               int i = split(line + 6, sp, 3);
931               if (i != 2 && i != 3)
932                 {
933                   pd.ret = pool_error(pool, -1, "susetags: line %d: bad location line '%s'\n", pd.lineno, line);
934                   continue;
935                 }
936               repodata_set_location(data, handle, atoi(sp[0]), i == 3 ? sp[2] : pool_id2str(pool, s->arch), sp[1]);
937             }
938             continue;
939           case CTAG('=', 'S', 'r', 'c'):
940             add_source(&pd, line + 6, s, handle);
941             continue;
942           case CTAG('=', 'S', 'i', 'z'):
943             if (split(line + 6, sp, 3) == 2)
944               {
945                 repodata_set_num(data, handle, SOLVABLE_DOWNLOADSIZE, strtoull(sp[0], 0, 10));
946                 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, strtoull(sp[1], 0, 10));
947               }
948             continue;
949           case CTAG('=', 'T', 'i', 'm'):
950             {
951               unsigned int t = atoi(line + 6);
952               if (t)
953                 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, t);
954             }
955             continue;
956           case CTAG('=', 'K', 'w', 'd'):
957             repodata_add_poolstr_array(data, handle, SOLVABLE_KEYWORDS, line + 6);
958             continue;
959           case CTAG('=', 'A', 'u', 't'):
960             repodata_set_str(data, handle, SOLVABLE_AUTHORS, line + 6);
961             continue;
962           case CTAG('=', 'S', 'u', 'm'):
963             repodata_set_str(data, handle, langtag(&pd, SOLVABLE_SUMMARY, line_lang), line + 3 + keylen);
964             continue;
965           case CTAG('=', 'D', 'e', 's'):
966             repodata_set_str(data, handle, langtag(&pd, SOLVABLE_DESCRIPTION, line_lang), line + 3 + keylen);
967             continue;
968           case CTAG('=', 'E', 'u', 'l'):
969             repodata_set_str(data, handle, langtag(&pd, SOLVABLE_EULA, line_lang), line + 6);
970             continue;
971           case CTAG('=', 'I', 'n', 's'):
972             repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEINS, line_lang), line + 6);
973             continue;
974           case CTAG('=', 'D', 'e', 'l'):
975             repodata_set_str(data, handle, langtag(&pd, SOLVABLE_MESSAGEDEL, line_lang), line + 6);
976             continue;
977           case CTAG('=', 'V', 'i', 's'):
978             {
979               /* Accept numbers and textual bools.  */
980               int k;
981               k = atoi(line + 6);
982               if (k || !strcasecmp(line + 6, "true"))
983                 repodata_set_void(data, handle, SOLVABLE_ISVISIBLE);
984             }
985             continue;
986           case CTAG('=', 'S', 'h', 'r'):
987             {
988               Id name, evr, arch;
989               if (split(line + 6, sp, 5) != 4)
990                 {
991                   pd.ret = pool_error(pool, -1, "susetags: line %d: bad =Shr line '%s'\n", pd.lineno, line);
992                   continue;
993                 }
994               name = pool_str2id(pool, sp[0], 1);
995               evr = toevr(pool, &pd, sp[1], sp[2]);
996               arch = pool_str2id(pool, sp[3], 1);
997               if (last_found_pack >= pd.nshare)
998                 {
999                   pd.share_with = solv_realloc2(pd.share_with, last_found_pack + 256, sizeof(*pd.share_with));
1000                   memset(pd.share_with + pd.nshare, 0, (last_found_pack + 256 - pd.nshare) * sizeof(*pd.share_with));
1001                   pd.nshare = last_found_pack + 256;
1002                 }
1003               pd.share_with[last_found_pack].name = name;
1004               pd.share_with[last_found_pack].evr = evr;
1005               pd.share_with[last_found_pack].arch = arch;
1006               if ((flags & SUSETAGS_RECORD_SHARES) != 0)
1007                 {
1008                   if (s->name == name)
1009                     repodata_set_void(data, handle, SUSETAGS_SHARE_NAME);
1010                   else
1011                     repodata_set_id(data, handle, SUSETAGS_SHARE_NAME, name);
1012                   if (s->evr == evr)
1013                     repodata_set_void(data, handle, SUSETAGS_SHARE_EVR);
1014                   else
1015                     repodata_set_id(data, handle, SUSETAGS_SHARE_EVR, evr);
1016                   if (s->arch == arch)
1017                     repodata_set_void(data, handle, SUSETAGS_SHARE_ARCH);
1018                   else
1019                     repodata_set_id(data, handle, SUSETAGS_SHARE_ARCH, arch);
1020                 }
1021               continue;
1022             }
1023           case CTAG('=', 'D', 'i', 'r'):
1024             add_dirline(&pd, line + 6);
1025             continue;
1026           case CTAG('=', 'C', 'a', 't'):
1027             repodata_set_poolstr(data, handle, langtag(&pd, SOLVABLE_CATEGORY, line_lang), line + 3 + keylen);
1028             break;
1029           case CTAG('=', 'O', 'r', 'd'):
1030             /* Order is a string not a number, so we can retroactively insert
1031                new patterns in the middle, i.e. 1 < 15 < 2.  */
1032             repodata_set_str(data, handle, SOLVABLE_ORDER, line + 6);
1033             break;
1034           case CTAG('=', 'I', 'c', 'o'):
1035             repodata_set_str(data, handle, SOLVABLE_ICON, line + 6);
1036             break;
1037           case CTAG('=', 'E', 'x', 't'):
1038             repodata_add_poolstr_array(data, handle, SOLVABLE_EXTENDS, join2(&pd.jd, "pattern", ":", line + 6));
1039             break;
1040           case CTAG('=', 'I', 'n', 'c'):
1041             repodata_add_poolstr_array(data, handle, SOLVABLE_INCLUDES, join2(&pd.jd, "pattern", ":", line + 6));
1042             break;
1043           case CTAG('=', 'C', 'k', 's'):
1044             set_checksum(&pd, data, handle, SOLVABLE_CHECKSUM, line + 6);
1045             break;
1046           case CTAG('=', 'L', 'a', 'n'):
1047             pd.language = solv_free(pd.language);
1048             memset(pd.langcache, 0, sizeof(pd.langcache));
1049             if (line[6])
1050               pd.language = solv_strdup(line + 6);
1051             break;
1052
1053           case CTAG('=', 'F', 'l', 's'):
1054             {
1055               char *p = strrchr(line + 6, '/');
1056               Id did;
1057               /* strip trailing slash */
1058               if (p && p != line + 6 && !p[1])
1059                 {
1060                   *p = 0;
1061                   p = strrchr(line + 6, '/');
1062                 }
1063               if (p)
1064                 {
1065                   *p++ = 0;
1066                   did = repodata_str2dir(data, line + 6, 1);
1067                 }
1068               else
1069                 {
1070                   p = line + 6;
1071                   did = 0;
1072                 }
1073               if (!did)
1074                 did = repodata_str2dir(data, "/", 1);
1075               repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, p);
1076               break;
1077             }
1078           case CTAG('=', 'H', 'd', 'r'):
1079             /* rpm header range */
1080             if (split(line + 6, sp, 3) == 2)
1081               {
1082                 /* we ignore the start value */
1083                 unsigned int end = (unsigned int)atoi(sp[1]);
1084                 if (end)
1085                   repodata_set_num(data, handle, SOLVABLE_HEADEREND, end);
1086               }
1087             break;
1088
1089           case CTAG('=', 'P', 'a', 't'):
1090           case CTAG('=', 'P', 'k', 'g'):
1091             break;
1092
1093           default:
1094 #if 0
1095             pool_debug(pool, SOLV_WARN, "susetags: unknown line: %d: %s\n", pd.lineno, line);
1096 #endif
1097             break;
1098         }
1099
1100     }
1101
1102   if (s)
1103     finish_solvable(&pd, s, freshens);
1104   solv_free(pd.filelist);
1105
1106   /* Shared attributes
1107    *  (e.g. multiple binaries built from same source)
1108    */
1109   if (pd.nshare)
1110     {
1111       int i, last_found;
1112       Map keyidmap;
1113
1114       map_init(&keyidmap, data->nkeys);
1115       for (i = 1; i < data->nkeys; i++)
1116         {
1117           Id keyname = data->keys[i].name;
1118           if (keyname == SOLVABLE_INSTALLSIZE || keyname == SOLVABLE_DISKUSAGE || keyname == SOLVABLE_FILELIST)
1119             continue;
1120           if (keyname == SOLVABLE_MEDIADIR || keyname == SOLVABLE_MEDIAFILE || keyname == SOLVABLE_MEDIANR)
1121             continue;
1122           if (keyname == SOLVABLE_DOWNLOADSIZE || keyname == SOLVABLE_CHECKSUM)
1123             continue;
1124           if (keyname == SOLVABLE_SOURCENAME || keyname == SOLVABLE_SOURCEARCH || keyname == SOLVABLE_SOURCEEVR)
1125             continue;
1126           if (keyname == SOLVABLE_PKGID || keyname == SOLVABLE_HDRID || keyname == SOLVABLE_LEADSIGID)
1127             continue;
1128           if (keyname == SUSETAGS_SHARE_NAME || keyname == SUSETAGS_SHARE_EVR || keyname == SUSETAGS_SHARE_ARCH)
1129             continue;
1130           MAPSET(&keyidmap, i);
1131         }
1132       last_found = 0;
1133       for (i = 0; i < pd.nshare; i++)
1134         {
1135           unsigned int n, nn;
1136           Solvable *found = 0;
1137           if (!pd.share_with[i].name)
1138             continue;
1139           for (n = repo->start, nn = repo->start + last_found; n < repo->end; n++, nn++)
1140             {
1141               if (nn >= repo->end)
1142                 nn = repo->start;
1143               found = pool->solvables + nn;
1144               if (found->repo == repo
1145                   && found->name == pd.share_with[i].name
1146                   && found->evr == pd.share_with[i].evr
1147                   && found->arch == pd.share_with[i].arch)
1148                 {
1149                   last_found = nn - repo->start;
1150                   break;
1151                 }
1152             }
1153           if (n != repo->end)
1154             repodata_merge_some_attrs(data, repo->start + i, repo->start + last_found, &keyidmap, 0);
1155         }
1156       free(pd.share_with);
1157       map_free(&keyidmap);
1158     }
1159
1160   solv_free(joinhash);
1161   repodata_free_dircache(data);
1162   if (!(flags & REPO_NO_INTERNALIZE))
1163     repodata_internalize(data);
1164
1165   solv_free(pd.language);
1166   solv_free(line);
1167   join_freemem(&pd.jd);
1168   return pd.ret;
1169 }