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