2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Manage metadata coming from one repository
21 #include "poolid_private.h"
23 #include "attr_store_p.h"
25 #define IDARRAY_BLOCK 4095
34 repo_create(Pool *pool, const char *name)
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;
44 repo->start = pool->nsolvables;
45 repo->end = pool->nsolvables;
51 repo_freedata(Repo *repo)
53 sat_free(repo->idarraydata);
54 sat_free(repo->rpmdbid);
55 sat_free((char *)repo->name);
61 * olddeps = old array to extend
66 repo_addid(Repo *repo, Offset olddeps, Id id)
72 idarray = repo->idarraydata;
73 idarraysize = repo->idarraysize;
75 if (!idarray) /* alloc idarray if not done yet */
77 idarray = sat_malloc2(1 + IDARRAY_BLOCK, sizeof(Id));
83 if (!olddeps) /* no deps yet */
85 olddeps = idarraysize;
86 if ((idarraysize & IDARRAY_BLOCK) == 0)
87 idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
89 else if (olddeps == repo->lastoff) /* extend at end */
91 else /* can't extend, copy old */
94 olddeps = idarraysize;
95 for (; idarray[i]; i++)
97 if ((idarraysize & IDARRAY_BLOCK) == 0)
98 idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
99 idarray[idarraysize++] = idarray[i];
101 if ((idarraysize & IDARRAY_BLOCK) == 0)
102 idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
105 idarray[idarraysize++] = id; /* insert Id into array */
107 if ((idarraysize & IDARRAY_BLOCK) == 0) /* realloc if at block boundary */
108 idarray = sat_realloc2(idarray, idarraysize + 1 + IDARRAY_BLOCK, sizeof(Id));
110 idarray[idarraysize++] = 0; /* ensure NULL termination */
112 repo->idarraydata = idarray;
113 repo->idarraysize = idarraysize;
114 repo->lastoff = olddeps;
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
129 repo_addid_dep(Repo *repo, Offset olddeps, Id id, Id marker)
131 Id oid, *oidp, *markerp;
137 olddeps = repo_addid(repo, olddeps, marker);
138 return repo_addid(repo, olddeps, id);
143 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
148 return repo_addid(repo, olddeps, id);
158 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
168 if (markerp || before)
170 /* we found it, but in the wrong half */
172 for (; (oid = *oidp) != ID_NULL; oidp++)
177 /* no marker in array yet */
180 memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
182 return repo_addid(repo, olddeps, id);
186 memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
190 /* id not yet in array */
191 if (!before && !markerp)
192 olddeps = repo_addid(repo, olddeps, marker);
193 else if (before && markerp)
198 memmove(markerp + 1, markerp, (oidp - markerp) * sizeof(Id));
201 return repo_addid(repo, olddeps, id);
207 * make space for 'num' more dependencies
211 repo_reserve_ids(Repo *repo, Offset olddeps, int num)
213 num++; /* room for trailing ID_NULL */
215 if (!repo->idarraysize) /* ensure buffer space */
217 repo->idarraysize = 1;
218 repo->idarraydata = sat_malloc2((1 + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK, sizeof(Id));
219 repo->idarraydata[0] = 0;
224 if (olddeps && olddeps != repo->lastoff) /* if not appending */
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.
234 for (idstart = idend = repo->idarraydata + olddeps; *idend++; ) /* find end */
236 count = idend - idstart - 1 + num; /* new size */
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));
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;
250 if (olddeps) /* appending */
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));
257 /* appending or new */
258 repo->lastoff = olddeps ? olddeps : repo->idarraysize;
260 return repo->lastoff;
265 * remove repo from pool, zero out solvables
270 repo_free(Repo *repo, int reuseids)
272 Pool *pool = repo->pool;
276 pool_freewhatprovides(pool);
278 if (reuseids && repo->end == pool->nsolvables)
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--)
286 pool->nsolvables = i + 1;
288 /* zero out solvables belonging to this repo */
289 for (i = repo->start, s = pool->solvables + i; i < repo->end; i++, s++)
291 memset(s, 0, sizeof(*s));
292 for (i = 0; i < pool->nrepos; i++) /* find repo in pool */
293 if (pool->repos[i] == repo)
295 if (i == pool->nrepos) /* repo not in pool, return */
297 if (i < pool->nrepos - 1)
298 memmove(pool->repos + i, pool->repos + i + 1, (pool->nrepos - 1 - i) * sizeof(Repo *));
304 repo_freeallrepos(Pool *pool, int reuseids)
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);
313 /* the first two solvables don't belong to a repo */
314 pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
318 repo_fix_legacy(Repo *repo, Offset provides, Offset supplements)
320 Pool *pool = repo->pool;
321 Id id, idp, idl, idns;
322 char buf[1024], *p, *dep;
327 for (i = provides; repo->idarraydata[i]; i++)
329 id = repo->idarraydata[i];
332 dep = (char *)id2str(pool, id);
333 if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
336 strcpy(buf + 2, dep);
338 if ((p = strchr(dep, ':')) != 0 && p != dep)
341 idp = str2id(pool, dep, 1);
345 while ((p = strchr(dep, ';')) != 0)
352 strncpy(dep - 9, "language:", 9);
354 idl = str2id(pool, dep - 9, 1);
356 id = rel2id(pool, id, idl, REL_OR, 1);
361 if (dep[0] && dep[1])
363 for (p = dep; *p && *p != ')'; p++)
366 strncpy(dep - 9, "language:", 9);
367 idl = str2id(pool, dep - 9, 1);
369 id = rel2id(pool, id, idl, REL_OR, 1);
374 id = rel2id(pool, idp, id, REL_AND, 1);
376 supplements = repo_addid_dep(repo, supplements, id, 0);
378 else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
383 idp = str2id(pool, buf, 1);
384 idns = str2id(pool, "namespace:installed", 1);
385 id = str2id(pool, p, 1);
386 id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
387 id = rel2id(pool, idp, id, REL_AND, 1);
388 supplements = repo_addid_dep(repo, supplements, id, 0);
394 for (i = supplements; repo->idarraydata[i]; i++)
396 id = repo->idarraydata[i];
399 dep = (char *)id2str(pool, id);
400 if (!strncmp(dep, "system:modalias(", 16))
402 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
405 p = strchr(buf + 9, ':');
406 idns = str2id(pool, "namespace:modalias", 1);
407 if (p && p != buf + 9 && strchr(p + 1, ':'))
410 idp = str2id(pool, buf + 9, 1);
411 p[strlen(p) - 1] = 0;
412 id = str2id(pool, p, 1);
413 id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
414 id = rel2id(pool, idp, id, REL_AND, 1);
419 p[strlen(p) - 1] = 0;
420 id = str2id(pool, p, 1);
421 id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
424 repo->idarraydata[i] = id;
426 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
431 while ((p = strchr(dep, ':')) != 0)
439 idp = str2id(pool, dep, 1);
441 id = rel2id(pool, id, idp, REL_AND, 1);
446 if (dep[0] && dep[1])
448 dep[strlen(dep) - 1] = 0;
449 idp = str2id(pool, dep, 1);
451 id = rel2id(pool, id, idp, REL_AND, 1);
456 repo->idarraydata[i] = id;
462 static unsigned char *
463 data_read_id(unsigned char *dp, Id *idp)
475 x = (x << 7) ^ c ^ 128;
479 static unsigned char *
480 data_skip(unsigned char *dp, int type)
487 while ((*dp & 0x80) != 0)
491 case TYPE_REL_IDARRAY:
492 case TYPE_IDVALUEARRAY:
493 case TYPE_IDVALUEVALUEARRAY:
494 while ((*dp & 0xc0) != 0)
498 fprintf(stderr, "unknown type in data_skip\n");
503 static unsigned char *
504 forward_to_key(Repodata *data, Id key, Id schema, unsigned char *dp)
508 keyp = data->schemadata + schema;
509 while ((k = *keyp++) != 0)
513 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
515 /* skip that offset */
516 dp = data_skip(dp, TYPE_ID);
519 if (data->keys[k].storage != KEY_STORAGE_INCORE)
521 dp = data_skip(dp, data->keys[k].type);
526 static unsigned char *
527 load_page_range(Repodata *data, unsigned int pstart, unsigned int pend)
529 /* add smart paging here */
533 static unsigned char *
534 get_data(Repodata *data, Repokey *key, unsigned char **dpp)
537 unsigned char *dp = *dpp;
539 unsigned int pstart, pend, poff, plen;
543 if (key->storage == KEY_STORAGE_INCORE)
545 *dpp = data_skip(dp, key->type);
548 if (key->storage != KEY_STORAGE_VERTICAL_OFFSET)
552 dp = data_read_id(dp, &off);
554 if (key->type == TYPE_VOID)
556 max = key->size - off;
559 /* we now have the offset, go into vertical */
560 for (i = key - data->keys - 1; i > 0; i--)
561 if (data->keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
562 off += data->keys[i].size;
563 pstart = off / BLOB_PAGESIZE;
565 poff = off % BLOB_PAGESIZE;
566 plen = BLOB_PAGESIZE - poff;
571 dp = load_page_range(data, pstart, pend) + poff;
577 if (memchr(dp, 0, plen))
581 for (i = 0; i < plen; i++)
582 if ((dp[i] & 0x80) == 0)
586 case TYPE_REL_IDARRAY:
587 case TYPE_IDVALUEARRAY:
588 case TYPE_IDVALUEVALUEARRAY:
589 for (i = 0; i < plen; i++)
590 if ((dp[i] & 0xc0) == 0)
597 plen += BLOB_PAGESIZE;
603 repodata_lookup_str(Repodata *data, Id entry, Id key)
606 Id id, k, *kp, *keyp;
609 if (data->entryschemau8)
610 schema = data->entryschemau8[entry];
612 schema = data->entryschema[entry];
613 keyp = data->schemadata + schema;
614 /* make sure the schema of this solvable contains the key */
615 for (kp = keyp; (k = *kp++) != 0; )
620 dp = forward_to_key(data, key, schema, data->incoredata + data->incoreoffset[entry]);
621 dp = get_data(data, data->keys + key, &dp);
624 if (data->keys[key].type == TYPE_STR)
625 return (const char *)dp;
626 /* id type, must either use global or local string strore*/
627 dp = data_read_id(dp, &id);
629 /* not yet working */
630 return data->ss.stringspace + data->ss.strings[id];
632 return id2str(data->repo->pool, id);
636 #define SEARCH_NEXT_KEY 1
637 #define SEARCH_NEXT_SOLVABLE 2
643 const char *matchstr;
649 int (*callback)(void *data, Solvable *s, Id key, const char *str);
654 domatch(Id p, Id key, struct matchdata *md, const char *str)
656 /* fill match code here */
657 md->stop = md->callback(md->callback_data, md->pool->solvables + p, key, str);
661 repodata_search(Repodata *data, Id entry, Id key, struct matchdata *md)
664 Id id, k, *kp, *keyp;
665 unsigned char *dp, *ddp;
668 if (data->entryschemau8)
669 schema = data->entryschemau8[entry];
671 schema = data->entryschema[entry];
672 keyp = data->schemadata + schema;
673 dp = data->incoredata + data->incoreoffset[entry];
676 /* search in a specific key */
677 for (kp = keyp; (k = *kp++) != 0; )
682 dp = forward_to_key(data, key, schema, dp);
689 while ((key = *keyp++) != 0)
691 ddp = get_data(data, data->keys + key, &dp);
694 switch (data->keys[key].type)
697 domatch(data->start + entry, data->keys[key].name, md, (const char *)ddp);
701 data_read_id(ddp, &id);
706 ddp = data_read_id(ddp, &id);
707 if ((id & 0x40) == 0)
709 id = (id & 0x3f) | ((id >> 1) & ~0x3f);
711 while (md->stop == SEARCH_NEXT_KEY && ddp)
712 ddp = data_read_id(ddp, &id);
718 if (onekey || md->stop > SEARCH_NEXT_KEY)
724 domatch_idarray(Id p, Id key, struct matchdata *md, Id *ida)
726 for (; *ida && !md->stop; ida++)
727 domatch(p, key, md, id2str(md->pool, *ida));
731 repo_search_md(Repo *repo, Id p, Id key, struct matchdata *md)
733 Pool *pool = repo->pool;
741 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
744 repo_search_md(repo, p, key, md);
745 if (md->stop > SEARCH_NEXT_SOLVABLE)
750 s = pool->solvables + p;
756 domatch(p, SOLVABLE_NAME, md, id2str(pool, s->name));
757 if (key || md->stop > SEARCH_NEXT_KEY)
761 domatch(p, SOLVABLE_ARCH, md, id2str(pool, s->arch));
762 if (key || md->stop > SEARCH_NEXT_KEY)
766 domatch(p, SOLVABLE_EVR, md, id2str(pool, s->evr));
767 if (key || md->stop > SEARCH_NEXT_KEY)
769 case SOLVABLE_VENDOR:
771 domatch(p, SOLVABLE_VENDOR, md, id2str(pool, s->vendor));
772 if (key || md->stop > SEARCH_NEXT_KEY)
774 case SOLVABLE_PROVIDES:
776 domatch_idarray(p, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
777 if (key || md->stop > SEARCH_NEXT_KEY)
779 case SOLVABLE_OBSOLETES:
781 domatch_idarray(p, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
782 if (key || md->stop > SEARCH_NEXT_KEY)
784 case SOLVABLE_CONFLICTS:
786 domatch_idarray(p, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
787 if (key || md->stop > SEARCH_NEXT_KEY)
789 case SOLVABLE_REQUIRES:
791 domatch_idarray(p, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
792 if (key || md->stop > SEARCH_NEXT_KEY)
794 case SOLVABLE_RECOMMENDS:
796 domatch_idarray(p, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
797 if (key || md->stop > SEARCH_NEXT_KEY)
799 case SOLVABLE_SUPPLEMENTS:
801 domatch_idarray(p, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
802 if (key || md->stop > SEARCH_NEXT_KEY)
804 case SOLVABLE_SUGGESTS:
806 domatch_idarray(p, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
807 if (key || md->stop > SEARCH_NEXT_KEY)
809 case SOLVABLE_ENHANCES:
811 domatch_idarray(p, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
812 if (key || md->stop > SEARCH_NEXT_KEY)
814 case SOLVABLE_FRESHENS:
816 domatch_idarray(p, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
817 if (key || md->stop > SEARCH_NEXT_KEY)
823 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
825 if (p < data->start || p >= data->end)
827 repodata_search(data, p - data->start, key, md);
828 if (md->stop > SEARCH_NEXT_KEY)
834 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)
838 memset(&md, 0, sizeof(md));
839 md.pool = repo->pool;
842 md.callback = callback;
843 md.callback_data = callback_data;
844 repo_search_md(repo, p, key, &md);
848 repo_lookup_str(Solvable *s, Id key)
850 Repo *repo = s->repo;
851 Pool *pool = repo->pool;
858 return id2str(pool, s->name);
860 return id2str(pool, s->arch);
862 return id2str(pool, s->evr);
863 case SOLVABLE_VENDOR:
864 return id2str(pool, s->vendor);
866 n = s - pool->solvables;
867 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
869 if (n < data->start || n >= data->end)
871 for (j = 1; j < data->nkeys; j++)
873 if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
874 return repodata_lookup_str(data, n - data->start, j);
883 key_cmp (const void *pa, const void *pb)
885 Repokey *a = (Repokey *)pa;
886 Repokey *b = (Repokey *)pb;
887 return a->name - b->name;
891 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
895 /* If this is meant to be the embedded attributes, make sure we don't
896 have them already. */
899 for (i = 0; i < repo->nrepodata; i++)
900 if (repo->repodata[i].location == 0)
902 if (i != repo->nrepodata)
904 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
909 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
910 data = repo->repodata + repo->nrepodata - 1;
911 memset (data, 0, sizeof (*data));
913 data->nkeys = s->nkeys;
916 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
917 for (i = 1; i < data->nkeys; i++)
919 data->keys[i].name = s->keys[i].name;
920 data->keys[i].type = s->keys[i].type;
923 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
926 data->location = strdup(location);