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