- some pieces of code for the unified lookup/search interface
[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 *)xcalloc(1, sizeof(*repo));
40   pool->repos = (Repo **)xrealloc(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   xfree(repo->idarraydata);
54   xfree(repo->rpmdbid);
55   xfree((char *)repo->name);
56   xfree(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 = (Id *)xmalloc((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 = (Id *)xrealloc(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 = (Id *)xrealloc(idarray, (idarraysize + 1 + IDARRAY_BLOCK) * sizeof(Id));
99           idarray[idarraysize++] = idarray[i];
100         }
101       if ((idarraysize & IDARRAY_BLOCK) == 0)
102         idarray = (Id *)xrealloc(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 = (Id *)xrealloc(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
122  * olddeps = offset into idarraydata
123  * isreq = 0 for normal dep
124  * isreq = 1 for requires
125  * isreq = 2 for pre-requires
126  * 
127  */
128
129 Offset
130 repo_addid_dep(Repo *repo, Offset olddeps, Id id, int isreq)
131 {
132   Id oid, *oidp, *marker = 0;
133
134   if (!olddeps)
135     return repo_addid(repo, olddeps, id);
136
137   if (!isreq)
138     {
139       for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
140         {
141           if (oid == id)
142             return olddeps;
143         }
144       return repo_addid(repo, olddeps, id);
145     }
146
147   for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
148     {
149       if (oid == SOLVABLE_PREREQMARKER)
150         marker = oidp;
151       else if (oid == id)
152         break;
153     }
154
155   if (oid)
156     {
157       if (marker || isreq == 1)
158         return olddeps;
159       marker = oidp++;
160       for (; (oid = *oidp) != ID_NULL; oidp++)
161         if (oid == SOLVABLE_PREREQMARKER)
162           break;
163       if (!oid)
164         {
165           oidp--;
166           if (marker < oidp)
167             memmove(marker, marker + 1, (oidp - marker) * sizeof(Id));
168           *oidp = SOLVABLE_PREREQMARKER;
169           return repo_addid(repo, olddeps, id);
170         }
171       while (oidp[1])
172         oidp++;
173       memmove(marker, marker + 1, (oidp - marker) * sizeof(Id));
174       *oidp = id;
175       return olddeps;
176     }
177   if (isreq == 2 && !marker)
178     olddeps = repo_addid(repo, olddeps, SOLVABLE_PREREQMARKER);
179   else if (isreq == 1 && marker)
180     {
181       *marker++ = id;
182       id = *--oidp;
183       if (marker < oidp)
184         memmove(marker + 1, marker, (oidp - marker) * sizeof(Id));
185       *marker = SOLVABLE_PREREQMARKER;
186     }
187   return repo_addid(repo, olddeps, id);
188 }
189
190
191 /*
192  * reserve Ids
193  * make space for 'num' more dependencies
194  */
195
196 Offset
197 repo_reserve_ids(Repo *repo, Offset olddeps, int num)
198 {
199   num++;        /* room for trailing ID_NULL */
200
201   if (!repo->idarraysize)              /* ensure buffer space */
202     {
203       repo->idarraysize = 1;
204       repo->idarraydata = (Id *)xmalloc(((1 + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK) * sizeof(Id));
205       repo->idarraydata[0] = 0;
206       repo->lastoff = 1;
207       return 1;
208     }
209
210   if (olddeps && olddeps != repo->lastoff)   /* if not appending */
211     {
212       /* can't insert into idarray, this would invalidate all 'larger' offsets
213        * so create new space at end and move existing deps there.
214        * Leaving 'hole' at old position.
215        */
216       
217       Id *idstart, *idend;
218       int count;
219
220       for (idstart = idend = repo->idarraydata + olddeps; *idend++; )   /* find end */
221         ;
222       count = idend - idstart - 1 + num;               /* new size */
223
224       /* realloc if crossing block boundary */
225       if (((repo->idarraysize - 1) | IDARRAY_BLOCK) != ((repo->idarraysize + count - 1) | IDARRAY_BLOCK))
226         repo->idarraydata = (Id *)xrealloc(repo->idarraydata, ((repo->idarraysize + count + IDARRAY_BLOCK) & ~IDARRAY_BLOCK) * sizeof(Id));
227
228       /* move old deps to end */
229       olddeps = repo->lastoff = repo->idarraysize;
230       memcpy(repo->idarraydata + olddeps, idstart, count - num);
231       repo->idarraysize = olddeps + count - num;
232
233       return olddeps;
234     }
235
236   if (olddeps)                         /* appending */
237     repo->idarraysize--;
238
239   /* realloc if crossing block boundary */
240   if (((repo->idarraysize - 1) | IDARRAY_BLOCK) != ((repo->idarraysize + num - 1) | IDARRAY_BLOCK))
241     repo->idarraydata = (Id *)xrealloc(repo->idarraydata, ((repo->idarraysize + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK) * sizeof(Id));
242
243   /* appending or new */
244   repo->lastoff = olddeps ? olddeps : repo->idarraysize;
245
246   return repo->lastoff;
247 }
248
249
250 /*
251  * remove repo from pool, zero out solvables 
252  * 
253  */
254
255 void
256 repo_free(Repo *repo, int reuseids)
257 {
258   Pool *pool = repo->pool;
259   Solvable *s;
260   int i;
261
262   pool_freewhatprovides(pool);
263
264   if (reuseids && repo->end == pool->nsolvables)
265     {
266       /* it's ok to reuse the ids. As this is the last repo, we can
267          just shrink the solvable array */
268       for (i = repo->end - 1, s = pool->solvables + i; i >= repo->start; i--, s--)
269         if (s->repo != repo)
270           break;
271       repo->end = i + 1;
272       pool->nsolvables = i + 1;
273     }
274   /* zero out solvables belonging to this repo */
275   for (i = repo->start, s = pool->solvables + i; i < repo->end; i++, s++)
276     if (s->repo == repo)
277       memset(s, 0, sizeof(*s));
278   for (i = 0; i < pool->nrepos; i++)    /* find repo in pool */
279     if (pool->repos[i] == repo)
280       break;
281   if (i == pool->nrepos)               /* repo not in pool, return */
282     return;
283   if (i < pool->nrepos - 1)
284     memmove(pool->repos + i, pool->repos + i + 1, (pool->nrepos - 1 - i) * sizeof(Repo *));
285   pool->nrepos--;
286   repo_freedata(repo);
287 }
288
289 void
290 repo_freeallrepos(Pool *pool, int reuseids)
291 {
292   int i;
293
294   pool_freewhatprovides(pool);
295   for (i = 0; i < pool->nrepos; i++)
296     repo_freedata(pool->repos[i]);
297   pool->repos = xfree(pool->repos);
298   pool->nrepos = 0;
299   /* the first two solvables don't belong to a repo */
300   pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
301 }
302
303 Offset
304 repo_fix_legacy(Repo *repo, Offset provides, Offset supplements)
305 {
306   Pool *pool = repo->pool;
307   Id id, idp, idl, idns;
308   char buf[1024], *p, *dep;
309   int i;
310
311   if (provides)
312     {
313       for (i = provides; repo->idarraydata[i]; i++)
314         {
315           id = repo->idarraydata[i];
316           if (ISRELDEP(id))
317             continue;
318           dep = (char *)id2str(pool, id);
319           if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
320             {
321               idp = 0;
322               strcpy(buf + 2, dep);
323               dep = buf + 2 + 7;
324               if ((p = strchr(dep, ':')) != 0 && p != dep)
325                 {
326                   *p++ = 0;
327                   idp = str2id(pool, dep, 1);
328                   dep = p;
329                 }
330               id = 0;
331               while ((p = strchr(dep, ';')) != 0)
332                 {
333                   if (p == dep)
334                     {
335                       dep = p + 1;
336                       continue;
337                     }
338                   strncpy(dep - 9, "language:", 9);
339                   *p++ = 0;
340                   idl = str2id(pool, dep - 9, 1);
341                   if (id)
342                     id = rel2id(pool, id, idl, REL_OR, 1);
343                   else
344                     id = idl;
345                   dep = p;
346                 }
347               if (dep[0] && dep[1])
348                 {
349                   for (p = dep; *p && *p != ')'; p++)
350                     ;
351                   *p = 0;
352                   strncpy(dep - 9, "language:", 9);
353                   idl = str2id(pool, dep - 9, 1);
354                   if (id)
355                     id = rel2id(pool, id, idl, REL_OR, 1);
356                   else
357                     id = idl;
358                 }
359               if (idp)
360                 id = rel2id(pool, idp, id, REL_AND, 1);
361               if (id)
362                 supplements = repo_addid_dep(repo, supplements, id, 0);
363             }
364           else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
365             {
366               strcpy(buf, dep);
367               p = buf + (p - dep);
368               *p++ = 0;
369               idp = str2id(pool, buf, 1);
370               idns = str2id(pool, "namespace:installed", 1);
371               id = str2id(pool, p, 1);
372               id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
373               id = rel2id(pool, idp, id, REL_AND, 1);
374               supplements = repo_addid_dep(repo, supplements, id, 0);
375             }
376         }
377     }
378   if (!supplements)
379     return 0;
380   for (i = supplements; repo->idarraydata[i]; i++)
381     {
382       id = repo->idarraydata[i];
383       if (ISRELDEP(id))
384         continue;
385       dep = (char *)id2str(pool, id);
386       if (!strncmp(dep, "system:modalias(", 16))
387         dep += 7;
388       if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
389         {
390           strcpy(buf, dep);
391           p = strchr(buf + 9, ':');
392           idns = str2id(pool, "namespace:modalias", 1);
393           if (p && p != buf + 9 && strchr(p + 1, ':'))
394             {
395               *p++ = 0;
396               idp = str2id(pool, buf + 9, 1);
397               p[strlen(p) - 1] = 0;
398               id = str2id(pool, p, 1);
399               id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
400               id = rel2id(pool, idp, id, REL_AND, 1);
401             }
402           else
403             {
404               p = buf + 9;
405               p[strlen(p) - 1] = 0;
406               id = str2id(pool, p, 1);
407               id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
408             }
409           if (id)
410             repo->idarraydata[i] = id;
411         }
412       else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
413         {
414           strcpy(buf, dep);
415           id = 0;
416           dep = buf + 11;
417           while ((p = strchr(dep, ':')) != 0)
418             {
419               if (p == dep)
420                 {
421                   dep = p + 1;
422                   continue;
423                 }
424               *p++ = 0;
425               idp = str2id(pool, dep, 1);
426               if (id)
427                 id = rel2id(pool, id, idp, REL_AND, 1);
428               else
429                 id = idp;
430               dep = p;
431             }
432           if (dep[0] && dep[1])
433             {
434               dep[strlen(dep) - 1] = 0;
435               idp = str2id(pool, dep, 1);
436               if (id)
437                 id = rel2id(pool, id, idp, REL_AND, 1);
438               else
439                 id = idp;
440             }
441           if (id)
442             repo->idarraydata[i] = id;
443         }
444     }
445   return supplements;
446 }
447
448 void
449 repodata_search(Repodata *data, Id key)
450 {
451 }
452
453 unsigned char *
454 data_read_id(unsigned char *dp, Id *idp)
455 {
456   Id x = 0;
457   unsigned char c;
458   for (;;)
459     {
460       c = *dp++;
461       if (!(c & 0x80))
462         {
463           *idp = (x << 7) ^ c;
464           return dp;
465         }
466       x = (x << 7) ^ c ^ 128;
467     }
468 }
469
470 unsigned char *
471 data_skip(unsigned char *dp, int type)
472 {
473   switch (type)
474     {
475     case TYPE_VOID:
476       return dp;
477     case TYPE_ID:
478       while ((*dp & 0x80) != 0)
479         dp++;
480       return dp;
481     case TYPE_IDARRAY:
482     case TYPE_REL_IDARRAY:
483     case TYPE_IDVALUEARRAY:
484     case TYPE_IDVALUEVALUEARRAY:
485       while ((*dp & 0xc0) != 0)
486         dp++;
487       return dp;
488     default:
489       fprintf(stderr, "unknown type in data_skip\n");
490       exit(1);
491     }
492 }
493
494 const char *
495 repodata_lookup_str(Repodata *data, Id entry, Id key)
496 {
497   Id schema;
498   Id id, k, *kp, *keyp;
499   unsigned char *dp;
500
501   if (data->entryschemau8)
502     schema = data->entryschemau8[entry];
503   else
504     schema = data->entryschema[entry];
505   keyp = data->schemadata + schema;
506   /* make sure the schema of this solvable contains the key */
507   for (kp = keyp; (k = *kp++) != 0; )
508     if (k == key)
509       break;
510   if (k == 0)
511     return 0;
512   switch (data->keys[key].storage)
513     {
514     case KEY_STORAGE_VERTICAL_OFFSET:
515     case KEY_STORAGE_INCORE:
516       dp = data->incoredata + data->incoreoffset[entry];
517       while ((k = *keyp++) != 0)
518         {
519           if (k == key)
520             break;
521           if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
522             {
523               /* skip that offset */
524               dp = data_skip(dp, TYPE_ID);
525               continue;
526             }
527           if (data->keys[k].storage != KEY_STORAGE_INCORE)
528             continue;
529           dp = data_skip(dp, data->keys[k].type);
530         }
531       if (data->keys[key].storage == KEY_STORAGE_VERTICAL_OFFSET)
532         {
533           int i, oi, max;
534           if (!data->fp)
535             return 0;
536           dp = data_read_id(dp, &id);
537           max = data->keys[key].size - id;
538           if (max <= 0)
539             return 0;
540           /* we now have the offset, go into vertical */
541           for (i = 1; i < key; i++)
542             if (data->keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
543               id += data->keys[i].size;
544           if (fseek(data->fp, data->verticaloffset + id, SEEK_SET))
545             return 0;
546           i = max > 256 ? 256 : max;
547           for (oi = 0;; oi = i, i += 256)
548             {
549               if (i > max)
550                 i = max;
551               if (i == oi)
552                 return 0;
553               if (i > data->strbuflen)
554                 {
555                   data->strbuf = xrealloc(data->strbuf, i);
556                   data->strbuflen = i;
557                 }
558               if (fread(data->strbuf + oi, i - oi, 1, data->fp) != 1)
559                 return 0;
560               if (memchr(data->strbuf + oi, 0, i - oi))
561                 return data->strbuf;
562             }
563         }
564       if (data->keys[key].type == TYPE_STR)
565         return (const char *)dp;
566       /* id type, must either use global or local string strore*/
567       dp = data_read_id(dp, &id);
568 #if 0
569       /* not yet working */
570       return data->ss.stringspace + data->ss.strings[id];
571 #else
572       return id2str(data->repo->pool, id);
573 #endif
574     }
575   return 0;
576 }
577
578 const char *
579 repo_lookup_str(Solvable *s, Id key)
580 {
581   Repo *repo = s->repo;
582   Pool *pool = repo->pool;
583   Repodata *data;
584   int i, j, n;
585
586   switch(key)
587     {
588     case SOLVABLE_NAME:
589       return id2str(pool, s->name);
590     case SOLVABLE_ARCH:
591       return id2str(pool, s->arch);
592     case SOLVABLE_EVR:
593       return id2str(pool, s->evr);
594     case SOLVABLE_VENDOR:
595       return id2str(pool, s->vendor);
596     }
597   n = s - pool->solvables;
598   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
599     {
600       if (n < data->start || n >= data->end)
601         continue;
602       for (j = 1; j < data->nkeys; j++)
603         {
604           if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
605             return repodata_lookup_str(data, n - data->start, j);
606         }
607     }
608   return 0;
609 }
610
611
612 static int
613 key_cmp (const void *pa, const void *pb)
614 {
615   Repokey *a = (Repokey *)pa;
616   Repokey *b = (Repokey *)pb;
617   return a->name - b->name;
618 }
619
620 void
621 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
622 {
623   unsigned i;
624   Repodata *data;
625   /* If this is meant to be the embedded attributes, make sure we don't
626      have them already.  */
627   if (!location)
628     {
629       for (i = 0; i < repo->nrepodata; i++)
630         if (repo->repodata[i].location == 0)
631           break;
632       if (i != repo->nrepodata)
633         {
634           pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
635           exit (1);
636         }
637     }
638   repo->nrepodata++;
639   repo->repodata = xrealloc (repo->repodata, repo->nrepodata * sizeof (*data));
640   data = repo->repodata + repo->nrepodata - 1;
641   memset (data, 0, sizeof (*data));
642   data->s = s;
643   data->nkeys = s->nkeys;
644   if (data->nkeys)
645     {
646       data->keys = xmalloc(data->nkeys * sizeof(data->keys[0]));
647       for (i = 1; i < data->nkeys; i++)
648         {
649           data->keys[i].name = s->keys[i].name;
650           data->keys[i].type = s->keys[i].type;
651         }
652       if (data->nkeys > 2)
653         qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
654     }
655   if (location)
656     data->location = strdup(location);
657 }
658
659 // EOF