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