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