- fix support for splitprovides
[platform/upstream/libsolv.git] / src / repo.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 /*
9  * repo.c
10  *
11  * Manage metadata coming from one repository
12  * 
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "repo.h"
20 #include "pool.h"
21 #include "poolid_private.h"
22 #include "util.h"
23 #include "attr_store_p.h"
24
25 #define IDARRAY_BLOCK     4095
26
27
28 /*
29  * create empty repo
30  * and add to pool
31  */
32
33 Repo *
34 repo_create(Pool *pool, const char *name)
35 {
36   Repo *repo;
37
38   pool_freewhatprovides(pool);
39   repo = (Repo *)sat_calloc(1, sizeof(*repo));
40   pool->repos = (Repo **)sat_realloc2(pool->repos, pool->nrepos + 1, sizeof(Repo *));
41   pool->repos[pool->nrepos++] = repo;
42   repo->name = name ? strdup(name) : 0;
43   repo->pool = pool;
44   repo->start = pool->nsolvables;
45   repo->end = pool->nsolvables;
46   repo->nsolvables = 0;
47   return repo;
48 }
49
50 static void
51 repo_freedata(Repo *repo)
52 {
53   sat_free(repo->idarraydata);
54   sat_free(repo->rpmdbid);
55   sat_free((char *)repo->name);
56   sat_free(repo);
57 }
58
59 /*
60  * add Id to repo
61  * olddeps = old array to extend
62  * 
63  */
64
65 Offset
66 repo_addid(Repo *repo, Offset olddeps, Id id)
67 {
68   Id *idarray;
69   int idarraysize;
70   int i;
71   
72   idarray = repo->idarraydata;
73   idarraysize = repo->idarraysize;
74
75   if (!idarray)                        /* alloc idarray if not done yet */
76     {
77       idarray = sat_malloc2(1 + IDARRAY_BLOCK, sizeof(Id));
78       idarray[0] = 0;
79       idarraysize = 1;
80       repo->lastoff = 0;
81     }
82
83   if (!olddeps)                         /* no deps yet */
84     {   
85       olddeps = idarraysize;
86       if ((idarraysize & IDARRAY_BLOCK) == 0)
87         idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
88     }   
89   else if (olddeps == repo->lastoff)    /* extend at end */
90     idarraysize--;
91   else                                  /* can't extend, copy old */
92     {
93       i = olddeps;
94       olddeps = idarraysize;
95       for (; idarray[i]; i++)
96         {
97           if ((idarraysize & IDARRAY_BLOCK) == 0)
98             idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
99           idarray[idarraysize++] = idarray[i];
100         }
101       if ((idarraysize & IDARRAY_BLOCK) == 0)
102         idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
103     }
104   
105   idarray[idarraysize++] = id;          /* insert Id into array */
106
107   if ((idarraysize & IDARRAY_BLOCK) == 0)   /* realloc if at block boundary */
108     idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
109
110   idarray[idarraysize++] = 0;           /* ensure NULL termination */
111
112   repo->idarraydata = idarray;
113   repo->idarraysize = idarraysize;
114   repo->lastoff = olddeps;
115
116   return olddeps;
117 }
118
119
120 /*
121  * add dependency (as Id) to repo, also unifies dependencies
122  * olddeps = offset into idarraydata
123  * marker= 0 for normal dep
124  * marker > 0 add dep after marker
125  * marker < 0 add dep after -marker
126  * 
127  */
128 Offset
129 repo_addid_dep(Repo *repo, Offset olddeps, Id id, Id marker)
130 {
131   Id oid, *oidp, *markerp;
132   int before;
133
134   if (!olddeps)
135     {
136       if (marker > 0)
137         olddeps = repo_addid(repo, olddeps, marker);
138       return repo_addid(repo, olddeps, id);
139     }
140
141   if (!marker)
142     {
143       for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
144         {
145           if (oid == id)
146             return olddeps;
147         }
148       return repo_addid(repo, olddeps, id);
149     }
150
151   before = 0;
152   markerp = 0;
153   if (marker < 0)
154     {
155       before = 1;
156       marker = -marker;
157     }
158   for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
159     {
160       if (oid == marker)
161         markerp = oidp;
162       else if (oid == id)
163         break;
164     }
165
166   if (oid)
167     {
168       if (markerp || before)
169         return olddeps;
170       /* we found it, but in the wrong half */
171       markerp = oidp++;
172       for (; (oid = *oidp) != ID_NULL; oidp++)
173         if (oid == marker)
174           break;
175       if (!oid)
176         {
177           /* no marker in array yet */
178           oidp--;
179           if (markerp < oidp)
180             memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
181           *oidp = marker;
182           return repo_addid(repo, olddeps, id);
183         }
184       while (oidp[1])
185         oidp++;
186       memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
187       *oidp = id;
188       return olddeps;
189     }
190   /* id not yet in array */
191   if (!before && !markerp)
192     olddeps = repo_addid(repo, olddeps, marker);
193   else if (before && markerp)
194     {
195       *markerp++ = id;
196       id = *--oidp;
197       if (markerp < oidp)
198         memmove(markerp + 1, markerp, (oidp - markerp) * sizeof(Id));
199       *markerp = marker;
200     }
201   return repo_addid(repo, olddeps, id);
202 }
203
204
205 /*
206  * reserve Ids
207  * make space for 'num' more dependencies
208  */
209
210 Offset
211 repo_reserve_ids(Repo *repo, Offset olddeps, int num)
212 {
213   num++;        /* room for trailing ID_NULL */
214
215   if (!repo->idarraysize)              /* ensure buffer space */
216     {
217       repo->idarraysize = 1;
218       repo->idarraydata = sat_malloc2((1 + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK, sizeof(Id));
219       repo->idarraydata[0] = 0;
220       repo->lastoff = 1;
221       return 1;
222     }
223
224   if (olddeps && olddeps != repo->lastoff)   /* if not appending */
225     {
226       /* can't insert into idarray, this would invalidate all 'larger' offsets
227        * so create new space at end and move existing deps there.
228        * Leaving 'hole' at old position.
229        */
230       
231       Id *idstart, *idend;
232       int count;
233
234       for (idstart = idend = repo->idarraydata + olddeps; *idend++; )   /* find end */
235         ;
236       count = idend - idstart - 1 + num;               /* new size */
237
238       /* realloc if crossing block boundary */
239       if (((repo->idarraysize - 1) | IDARRAY_BLOCK) != ((repo->idarraysize + count - 1) | IDARRAY_BLOCK))
240         repo->idarraydata = sat_realloc2(repo->idarraydata, (repo->idarraysize + count + IDARRAY_BLOCK) & ~IDARRAY_BLOCK, sizeof(Id));
241
242       /* move old deps to end */
243       olddeps = repo->lastoff = repo->idarraysize;
244       memcpy(repo->idarraydata + olddeps, idstart, count - num);
245       repo->idarraysize = olddeps + count - num;
246
247       return olddeps;
248     }
249
250   if (olddeps)                         /* appending */
251     repo->idarraysize--;
252
253   /* realloc if crossing block boundary */
254   if (((repo->idarraysize - 1) | IDARRAY_BLOCK) != ((repo->idarraysize + num - 1) | IDARRAY_BLOCK))
255     repo->idarraydata = sat_realloc2(repo->idarraydata, (repo->idarraysize + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK, sizeof(Id));
256
257   /* appending or new */
258   repo->lastoff = olddeps ? olddeps : repo->idarraysize;
259
260   return repo->lastoff;
261 }
262
263
264 /*
265  * remove repo from pool, zero out solvables 
266  * 
267  */
268
269 void
270 repo_free(Repo *repo, int reuseids)
271 {
272   Pool *pool = repo->pool;
273   Solvable *s;
274   int i;
275
276   pool_freewhatprovides(pool);
277
278   if (reuseids && repo->end == pool->nsolvables)
279     {
280       /* it's ok to reuse the ids. As this is the last repo, we can
281          just shrink the solvable array */
282       for (i = repo->end - 1, s = pool->solvables + i; i >= repo->start; i--, s--)
283         if (s->repo != repo)
284           break;
285       repo->end = i + 1;
286       pool->nsolvables = i + 1;
287     }
288   /* zero out solvables belonging to this repo */
289   for (i = repo->start, s = pool->solvables + i; i < repo->end; i++, s++)
290     if (s->repo == repo)
291       memset(s, 0, sizeof(*s));
292   for (i = 0; i < pool->nrepos; i++)    /* find repo in pool */
293     if (pool->repos[i] == repo)
294       break;
295   if (i == pool->nrepos)               /* repo not in pool, return */
296     return;
297   if (i < pool->nrepos - 1)
298     memmove(pool->repos + i, pool->repos + i + 1, (pool->nrepos - 1 - i) * sizeof(Repo *));
299   pool->nrepos--;
300   repo_freedata(repo);
301 }
302
303 void
304 repo_freeallrepos(Pool *pool, int reuseids)
305 {
306   int i;
307
308   pool_freewhatprovides(pool);
309   for (i = 0; i < pool->nrepos; i++)
310     repo_freedata(pool->repos[i]);
311   pool->repos = sat_free(pool->repos);
312   pool->nrepos = 0;
313   /* the first two solvables don't belong to a repo */
314   pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
315 }
316
317 Offset
318 repo_fix_legacy(Repo *repo, Offset provides, Offset supplements)
319 {
320   Pool *pool = repo->pool;
321   Id id, idp, idl;
322   char buf[1024], *p, *dep;
323   int i;
324
325   if (provides)
326     {
327       for (i = provides; repo->idarraydata[i]; i++)
328         {
329           id = repo->idarraydata[i];
330           if (ISRELDEP(id))
331             continue;
332           dep = (char *)id2str(pool, id);
333           if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
334             {
335               idp = 0;
336               strcpy(buf + 2, dep);
337               dep = buf + 2 + 7;
338               if ((p = strchr(dep, ':')) != 0 && p != dep)
339                 {
340                   *p++ = 0;
341                   idp = str2id(pool, dep, 1);
342                   dep = p;
343                 }
344               id = 0;
345               while ((p = strchr(dep, ';')) != 0)
346                 {
347                   if (p == dep)
348                     {
349                       dep = p + 1;
350                       continue;
351                     }
352                   strncpy(dep - 9, "language:", 9);
353                   *p++ = 0;
354                   idl = str2id(pool, dep - 9, 1);
355                   if (id)
356                     id = rel2id(pool, id, idl, REL_OR, 1);
357                   else
358                     id = idl;
359                   dep = p;
360                 }
361               if (dep[0] && dep[1])
362                 {
363                   for (p = dep; *p && *p != ')'; p++)
364                     ;
365                   *p = 0;
366                   strncpy(dep - 9, "language:", 9);
367                   idl = str2id(pool, dep - 9, 1);
368                   if (id)
369                     id = rel2id(pool, id, idl, REL_OR, 1);
370                   else
371                     id = idl;
372                 }
373               if (idp)
374                 id = rel2id(pool, idp, id, REL_AND, 1);
375               if (id)
376                 supplements = repo_addid_dep(repo, supplements, id, 0);
377             }
378           else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
379             {
380               strcpy(buf, dep);
381               p = buf + (p - dep);
382               *p++ = 0;
383               idp = str2id(pool, buf, 1);
384               id = str2id(pool, p, 1);
385               id = rel2id(pool, idp, id, REL_WITH, 1);
386               id = rel2id(pool, NAMESPACE_SPLITPROVIDES, id, REL_NAMESPACE, 1);
387               supplements = repo_addid_dep(repo, supplements, id, 0);
388             }
389         }
390     }
391   if (!supplements)
392     return 0;
393   for (i = supplements; repo->idarraydata[i]; i++)
394     {
395       id = repo->idarraydata[i];
396       if (ISRELDEP(id))
397         continue;
398       dep = (char *)id2str(pool, id);
399       if (!strncmp(dep, "system:modalias(", 16))
400         dep += 7;
401       if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
402         {
403           strcpy(buf, dep);
404           p = strchr(buf + 9, ':');
405           if (p && p != buf + 9 && strchr(p + 1, ':'))
406             {
407               *p++ = 0;
408               idp = str2id(pool, buf + 9, 1);
409               p[strlen(p) - 1] = 0;
410               id = str2id(pool, p, 1);
411               id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
412               id = rel2id(pool, idp, id, REL_AND, 1);
413             }
414           else
415             {
416               p = buf + 9;
417               p[strlen(p) - 1] = 0;
418               id = str2id(pool, p, 1);
419               id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
420             }
421           if (id)
422             repo->idarraydata[i] = id;
423         }
424       else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
425         {
426           strcpy(buf, dep);
427           id = 0;
428           dep = buf + 11;
429           while ((p = strchr(dep, ':')) != 0)
430             {
431               if (p == dep)
432                 {
433                   dep = p + 1;
434                   continue;
435                 }
436               *p++ = 0;
437               idp = str2id(pool, dep, 1);
438               if (id)
439                 id = rel2id(pool, id, idp, REL_AND, 1);
440               else
441                 id = idp;
442               dep = p;
443             }
444           if (dep[0] && dep[1])
445             {
446               dep[strlen(dep) - 1] = 0;
447               idp = str2id(pool, dep, 1);
448               if (id)
449                 id = rel2id(pool, id, idp, REL_AND, 1);
450               else
451                 id = idp;
452             }
453           if (id)
454             repo->idarraydata[i] = id;
455         }
456     }
457   return supplements;
458 }
459
460 static unsigned char *
461 data_read_id(unsigned char *dp, Id *idp)
462 {
463   Id x = 0;
464   unsigned char c;
465   for (;;)
466     {
467       c = *dp++;
468       if (!(c & 0x80))
469         {
470           *idp = (x << 7) ^ c;
471           return dp;
472         }
473       x = (x << 7) ^ c ^ 128;
474     }
475 }
476
477 static unsigned char *
478 data_skip(unsigned char *dp, int type)
479 {
480   switch (type)
481     {
482     case TYPE_VOID:
483       return dp;
484     case TYPE_ID:
485       while ((*dp & 0x80) != 0)
486         dp++;
487       return dp;
488     case TYPE_IDARRAY:
489     case TYPE_REL_IDARRAY:
490     case TYPE_IDVALUEARRAY:
491     case TYPE_IDVALUEVALUEARRAY:
492       while ((*dp & 0xc0) != 0)
493         dp++;
494       return dp;
495     default:
496       fprintf(stderr, "unknown type in data_skip\n");
497       exit(1);
498     }
499 }
500
501 static unsigned char *
502 forward_to_key(Repodata *data, Id key, Id schema, unsigned char *dp)
503 {
504   Id k, *keyp;
505
506   keyp = data->schemadata + schema;
507   while ((k = *keyp++) != 0)
508     {
509       if (k == key)
510         return dp;
511       if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
512         {
513           /* skip that offset */
514           dp = data_skip(dp, TYPE_ID);
515           continue;
516         }
517       if (data->keys[k].storage != KEY_STORAGE_INCORE)
518         continue;
519       dp = data_skip(dp, data->keys[k].type);
520     }
521   return 0;
522 }
523
524 static unsigned char *
525 load_page_range(Repodata *data, unsigned int pstart, unsigned int pend)
526 {
527   /* add smart paging here */
528   return 0;
529 }
530
531 static unsigned char *
532 get_data(Repodata *data, Repokey *key, unsigned char **dpp)
533 {
534   Id off;
535   unsigned char *dp = *dpp;
536   int i, max;
537   unsigned int pstart, pend, poff, plen;
538
539   if (!dp)
540     return 0;
541   if (key->storage == KEY_STORAGE_INCORE)
542     {
543       *dpp = data_skip(dp, key->type);
544       return dp;
545     }
546   if (key->storage != KEY_STORAGE_VERTICAL_OFFSET)
547     return 0;
548   if (!data->fp)
549     return 0;
550   dp = data_read_id(dp, &off);
551   *dpp = dp;
552   if (key->type == TYPE_VOID)
553     return 0;
554   max = key->size - off;
555   if (max <= 0)
556     return 0;
557   /* we now have the offset, go into vertical */
558   for (i = key - data->keys - 1; i > 0; i--)
559     if (data->keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
560       off += data->keys[i].size;
561   pstart = off / BLOB_PAGESIZE;
562   pend = pstart;
563   poff = off % BLOB_PAGESIZE;
564   plen = BLOB_PAGESIZE - poff;
565   for (;;)
566     {
567       if (plen > max)
568         plen = max;
569       dp = load_page_range(data, pstart, pend) + poff;
570       if (!dp)
571         return 0;
572       switch (key->type)
573         {
574         case TYPE_STR:
575           if (memchr(dp, 0, plen))
576             return dp;
577           break;
578         case TYPE_ID:
579           for (i = 0; i < plen; i++)
580             if ((dp[i] & 0x80) == 0)
581               return dp;
582           break;
583         case TYPE_IDARRAY:
584         case TYPE_REL_IDARRAY:
585         case TYPE_IDVALUEARRAY:
586         case TYPE_IDVALUEVALUEARRAY:
587           for (i = 0; i < plen; i++)
588             if ((dp[i] & 0xc0) == 0)
589               return dp;
590           break;
591         }
592       if (plen == max)
593         return 0;
594       pend++;
595       plen += BLOB_PAGESIZE;
596     }
597 }
598
599
600 const char *
601 repodata_lookup_str(Repodata *data, Id entry, Id key)
602 {
603   Id schema;
604   Id id, k, *kp, *keyp;
605   unsigned char *dp;
606
607   if (data->entryschemau8)
608     schema = data->entryschemau8[entry];
609   else
610     schema = data->entryschema[entry];
611   keyp = data->schemadata + schema;
612   /* make sure the schema of this solvable contains the key */
613   for (kp = keyp; (k = *kp++) != 0; )
614     if (k == key)
615       break;
616   if (k == 0)
617     return 0;
618   dp = forward_to_key(data, key, schema, data->incoredata + data->incoreoffset[entry]);
619   dp = get_data(data, data->keys + key, &dp);
620   if (!dp)
621     return 0;
622   if (data->keys[key].type == TYPE_STR)
623     return (const char *)dp;
624   /* id type, must either use global or local string strore*/
625   dp = data_read_id(dp, &id);
626 #if 0
627   /* not yet working */
628   return data->ss.stringspace + data->ss.strings[id];
629 #else
630   return id2str(data->repo->pool, id);
631 #endif
632 }
633
634 #define SEARCH_NEXT_KEY         1
635 #define SEARCH_NEXT_SOLVABLE    2
636 #define SEACH_STOP              3
637
638 struct matchdata
639 {
640   Pool *pool;
641   const char *matchstr;
642   int flags;
643 #if 0
644   regex_t regex;
645 #endif
646   int stop;
647   int (*callback)(void *data, Solvable *s, Id key, const char *str);
648   void *callback_data;
649 };
650
651 static void
652 domatch(Id p, Id key, struct matchdata *md, const char *str)
653 {
654   /* fill match code here */
655   md->stop = md->callback(md->callback_data, md->pool->solvables + p, key, str);
656 }
657
658 static void
659 repodata_search(Repodata *data, Id entry, Id key, struct matchdata *md)
660 {
661   Id schema;
662   Id id, k, *kp, *keyp;
663   unsigned char *dp, *ddp;
664   int onekey = 0;
665
666   if (data->entryschemau8)
667     schema = data->entryschemau8[entry];
668   else
669     schema = data->entryschema[entry];
670   keyp = data->schemadata + schema;
671   dp = data->incoredata + data->incoreoffset[entry];
672   if (key)
673     {
674       /* search in a specific key */
675       for (kp = keyp; (k = *kp++) != 0; )
676         if (k == key)
677           break;
678       if (k == 0)
679         return;
680       dp = forward_to_key(data, key, schema, dp);
681       if (!dp)
682         return;
683       keyp = kp - 1;
684       onekey = 1;
685     }
686   md->stop = 0;
687   while ((key = *keyp++) != 0)
688     {
689       ddp = get_data(data, data->keys + key, &dp);
690       while (ddp)
691         {
692           switch (data->keys[key].type)
693             {
694             case TYPE_STR:
695               domatch(data->start + entry, data->keys[key].name, md, (const char *)ddp);
696               ddp = 0;
697               break;
698             case TYPE_ID:
699               data_read_id(ddp, &id);
700               /* domatch */
701               ddp = 0;
702               break;
703             case TYPE_IDARRAY:
704               ddp = data_read_id(ddp, &id);
705               if ((id & 0x40) == 0)
706                 ddp = 0;
707               id = (id & 0x3f) | ((id >> 1) & ~0x3f);
708               /* domatch */
709               while (md->stop == SEARCH_NEXT_KEY && ddp)
710                 ddp = data_read_id(ddp, &id);
711               break;
712             default:
713               ddp = 0;
714             }
715         }
716       if (onekey || md->stop > SEARCH_NEXT_KEY)
717         return;
718     }
719 }
720
721 static void
722 domatch_idarray(Id p, Id key, struct matchdata *md, Id *ida)
723 {
724   for (; *ida && !md->stop; ida++)
725     domatch(p, key, md, id2str(md->pool, *ida));
726 }
727
728 static void
729 repo_search_md(Repo *repo, Id p, Id key, struct matchdata *md)
730 {
731   Pool *pool = repo->pool;
732   Repodata *data;
733   Solvable *s;
734   int i;
735
736   md->stop = 0;
737   if (!p)
738     {
739 #if 0
740       switch(key)
741         {
742         case 0:
743         case SOLVABLE_NAME:
744         case SOLVABLE_ARCH:
745         case SOLVABLE_EVR:
746         case SOLVABLE_VENDOR:
747         case SOLVABLE_PROVIDES:
748         case SOLVABLE_OBSOLETES:
749         case SOLVABLE_CONFLICTS:
750         case SOLVABLE_REQUIRES:
751         case SOLVABLE_RECOMMENDS:
752         case SOLVABLE_SUPPLEMENTS:
753         case SOLVABLE_SUGGESTS:
754         case SOLVABLE_ENHANCES:
755         case SOLVABLE_FRESHENS:
756           break;
757         default:
758           for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
759             repodata_search(data, -1, key, md);
760           return;
761         }
762 #endif
763       for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
764         {
765           if (s->repo == repo)
766             repo_search_md(repo, p, key, md);
767           if (md->stop > SEARCH_NEXT_SOLVABLE)
768             break;
769         }
770       return;
771     }
772   s = pool->solvables + p;
773   switch(key)
774     {
775       case 0:
776       case SOLVABLE_NAME:
777         if (s->name)
778           domatch(p, SOLVABLE_NAME, md, id2str(pool, s->name));
779         if (key || md->stop > SEARCH_NEXT_KEY)
780           return;
781       case SOLVABLE_ARCH:
782         if (s->arch)
783           domatch(p, SOLVABLE_ARCH, md, id2str(pool, s->arch));
784         if (key || md->stop > SEARCH_NEXT_KEY)
785           return;
786       case SOLVABLE_EVR:
787         if (s->evr)
788           domatch(p, SOLVABLE_EVR, md, id2str(pool, s->evr));
789         if (key || md->stop > SEARCH_NEXT_KEY)
790           return;
791       case SOLVABLE_VENDOR:
792         if (s->vendor)
793           domatch(p, SOLVABLE_VENDOR, md, id2str(pool, s->vendor));
794         if (key || md->stop > SEARCH_NEXT_KEY)
795           return;
796       case SOLVABLE_PROVIDES:
797         if (s->provides)
798           domatch_idarray(p, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
799         if (key || md->stop > SEARCH_NEXT_KEY)
800           return;
801       case SOLVABLE_OBSOLETES:
802         if (s->obsoletes)
803           domatch_idarray(p, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
804         if (key || md->stop > SEARCH_NEXT_KEY)
805           return;
806       case SOLVABLE_CONFLICTS:
807         if (s->obsoletes)
808           domatch_idarray(p, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
809         if (key || md->stop > SEARCH_NEXT_KEY)
810           return;
811       case SOLVABLE_REQUIRES:
812         if (s->requires)
813           domatch_idarray(p, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
814         if (key || md->stop > SEARCH_NEXT_KEY)
815           return;
816       case SOLVABLE_RECOMMENDS:
817         if (s->recommends)
818           domatch_idarray(p, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
819         if (key || md->stop > SEARCH_NEXT_KEY)
820           return;
821       case SOLVABLE_SUPPLEMENTS:
822         if (s->supplements)
823           domatch_idarray(p, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
824         if (key || md->stop > SEARCH_NEXT_KEY)
825           return;
826       case SOLVABLE_SUGGESTS:
827         if (s->suggests)
828           domatch_idarray(p, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
829         if (key || md->stop > SEARCH_NEXT_KEY)
830           return;
831       case SOLVABLE_ENHANCES:
832         if (s->enhances)
833           domatch_idarray(p, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
834         if (key || md->stop > SEARCH_NEXT_KEY)
835           return;
836       case SOLVABLE_FRESHENS:
837         if (s->freshens)
838           domatch_idarray(p, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
839         if (key || md->stop > SEARCH_NEXT_KEY)
840           return;
841       default:
842         break;
843     }
844
845   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
846     {
847       if (p < data->start || p >= data->end)
848         continue;
849       repodata_search(data, p - data->start, key, md);
850       if (md->stop > SEARCH_NEXT_KEY)
851         break;
852     }
853 }
854
855 void
856 repo_search(Repo *repo, Id p, Id key, const char *match, int flags, int (*callback)(void *data, Solvable *s, Id key, const char *str), void *callback_data)
857 {
858   struct matchdata md;
859
860   memset(&md, 0, sizeof(md));
861   md.pool = repo->pool;
862   md.matchstr = match;
863   md.flags = flags;
864   md.callback = callback;
865   md.callback_data = callback_data;
866   repo_search_md(repo, p, key, &md);
867 }
868
869 const char *
870 repo_lookup_str(Solvable *s, Id key)
871 {
872   Repo *repo = s->repo;
873   Pool *pool = repo->pool;
874   Repodata *data;
875   int i, j, n;
876
877   switch(key)
878     {
879     case SOLVABLE_NAME:
880       return id2str(pool, s->name);
881     case SOLVABLE_ARCH:
882       return id2str(pool, s->arch);
883     case SOLVABLE_EVR:
884       return id2str(pool, s->evr);
885     case SOLVABLE_VENDOR:
886       return id2str(pool, s->vendor);
887     }
888   n = s - pool->solvables;
889   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
890     {
891       if (n < data->start || n >= data->end)
892         continue;
893       for (j = 1; j < data->nkeys; j++)
894         {
895           if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
896             return repodata_lookup_str(data, n - data->start, j);
897         }
898     }
899   return 0;
900 }
901
902
903
904 static int
905 key_cmp (const void *pa, const void *pb)
906 {
907   Repokey *a = (Repokey *)pa;
908   Repokey *b = (Repokey *)pb;
909   return a->name - b->name;
910 }
911
912 void
913 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
914 {
915   unsigned i;
916   Repodata *data;
917   /* If this is meant to be the embedded attributes, make sure we don't
918      have them already.  */
919   if (!location)
920     {
921       for (i = 0; i < repo->nrepodata; i++)
922         if (repo->repodata[i].location == 0)
923           break;
924       if (i != repo->nrepodata)
925         {
926           pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
927           exit (1);
928         }
929     }
930   repo->nrepodata++;
931   repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
932   data = repo->repodata + repo->nrepodata - 1;
933   memset (data, 0, sizeof (*data));
934   data->s = s;
935   data->nkeys = s->nkeys;
936   if (data->nkeys)
937     {
938       data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
939       for (i = 1; i < data->nkeys; i++)
940         {
941           data->keys[i].name = s->keys[i].name;
942           data->keys[i].type = s->keys[i].type;
943         }
944       if (data->nkeys > 2)
945         qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
946     }
947   if (location)
948     data->location = strdup(location);
949 }
950
951 // EOF