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