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
26 #include "poolid_private.h"
29 #include "attr_store_p.h"
32 #define IDARRAY_BLOCK 4095
41 repo_create(Pool *pool, const char *name)
45 pool_freewhatprovides(pool);
46 repo = (Repo *)sat_calloc(1, sizeof(*repo));
47 pool->repos = (Repo **)sat_realloc2(pool->repos, pool->nrepos + 1, sizeof(Repo *));
48 pool->repos[pool->nrepos++] = repo;
49 repo->name = name ? strdup(name) : 0;
51 repo->start = pool->nsolvables;
52 repo->end = pool->nsolvables;
58 repo_freedata(Repo *repo)
60 sat_free(repo->idarraydata);
61 sat_free(repo->rpmdbid);
62 sat_free((char *)repo->name);
68 * olddeps = old array to extend
73 repo_addid(Repo *repo, Offset olddeps, Id id)
79 idarray = repo->idarraydata;
80 idarraysize = repo->idarraysize;
82 if (!idarray) /* alloc idarray if not done yet */
85 idarray = sat_extend_resize(0, 1, sizeof(Id), IDARRAY_BLOCK);
90 if (!olddeps) /* no deps yet */
92 olddeps = idarraysize;
93 idarray = sat_extend(idarray, idarraysize, 1, sizeof(Id), IDARRAY_BLOCK);
95 else if (olddeps == repo->lastoff) /* extend at end */
97 else /* can't extend, copy old */
100 olddeps = idarraysize;
101 for (; idarray[i]; i++)
103 idarray = sat_extend(idarray, idarraysize, 1, sizeof(Id), IDARRAY_BLOCK);
104 idarray[idarraysize++] = idarray[i];
106 idarray = sat_extend(idarray, idarraysize, 1, sizeof(Id), IDARRAY_BLOCK);
109 idarray[idarraysize++] = id; /* insert Id into array */
110 idarray = sat_extend(idarray, idarraysize, 1, sizeof(Id), IDARRAY_BLOCK);
111 idarray[idarraysize++] = 0; /* ensure NULL termination */
113 repo->idarraydata = idarray;
114 repo->idarraysize = idarraysize;
115 repo->lastoff = olddeps;
122 * add dependency (as Id) to repo, also unifies dependencies
123 * olddeps = offset into idarraydata
124 * marker= 0 for normal dep
125 * marker > 0 add dep after marker
126 * marker < 0 add dep after -marker
130 repo_addid_dep(Repo *repo, Offset olddeps, Id id, Id marker)
132 Id oid, *oidp, *markerp;
138 olddeps = repo_addid(repo, olddeps, marker);
139 return repo_addid(repo, olddeps, id);
144 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
149 return repo_addid(repo, olddeps, id);
159 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
169 if (markerp || before)
171 /* we found it, but in the wrong half */
173 for (; (oid = *oidp) != ID_NULL; oidp++)
178 /* no marker in array yet */
181 memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
183 return repo_addid(repo, olddeps, id);
187 memmove(markerp, markerp + 1, (oidp - markerp) * sizeof(Id));
191 /* id not yet in array */
192 if (!before && !markerp)
193 olddeps = repo_addid(repo, olddeps, marker);
194 else if (before && markerp)
199 memmove(markerp + 1, markerp, (oidp - markerp) * sizeof(Id));
202 return repo_addid(repo, olddeps, id);
208 * make space for 'num' more dependencies
212 repo_reserve_ids(Repo *repo, Offset olddeps, int num)
214 num++; /* room for trailing ID_NULL */
216 if (!repo->idarraysize) /* ensure buffer space */
218 repo->idarraysize = 1;
219 repo->idarraydata = sat_extend_resize(0, 1 + num, sizeof(Id), IDARRAY_BLOCK);
220 repo->idarraydata[0] = 0;
225 if (olddeps && olddeps != repo->lastoff) /* if not appending */
227 /* can't insert into idarray, this would invalidate all 'larger' offsets
228 * so create new space at end and move existing deps there.
229 * Leaving 'hole' at old position.
235 for (idstart = idend = repo->idarraydata + olddeps; *idend++; ) /* find end */
237 count = idend - idstart - 1 + num; /* new size */
239 repo->idarraydata = sat_extend(repo->idarraydata, repo->idarraysize, count, sizeof(Id), IDARRAY_BLOCK);
240 /* move old deps to end */
241 olddeps = repo->lastoff = repo->idarraysize;
242 memcpy(repo->idarraydata + olddeps, idstart, count - num);
243 repo->idarraysize = olddeps + count - num;
248 if (olddeps) /* appending */
252 repo->idarraydata = sat_extend(repo->idarraydata, repo->idarraysize, num, sizeof(Id), IDARRAY_BLOCK);
254 /* appending or new */
255 repo->lastoff = olddeps ? olddeps : repo->idarraysize;
257 return repo->lastoff;
262 * remove repo from pool, zero out solvables
267 repo_free(Repo *repo, int reuseids)
269 Pool *pool = repo->pool;
273 pool_freewhatprovides(pool);
275 if (reuseids && repo->end == pool->nsolvables)
277 /* it's ok to reuse the ids. As this is the last repo, we can
278 just shrink the solvable array */
279 for (i = repo->end - 1, s = pool->solvables + i; i >= repo->start; i--, s--)
283 pool->nsolvables = i + 1;
285 /* zero out solvables belonging to this repo */
286 for (i = repo->start, s = pool->solvables + i; i < repo->end; i++, s++)
288 memset(s, 0, sizeof(*s));
289 for (i = 0; i < pool->nrepos; i++) /* find repo in pool */
290 if (pool->repos[i] == repo)
292 if (i == pool->nrepos) /* repo not in pool, return */
294 if (i < pool->nrepos - 1)
295 memmove(pool->repos + i, pool->repos + i + 1, (pool->nrepos - 1 - i) * sizeof(Repo *));
301 repo_freeallrepos(Pool *pool, int reuseids)
305 pool_freewhatprovides(pool);
306 for (i = 0; i < pool->nrepos; i++)
307 repo_freedata(pool->repos[i]);
308 pool->repos = sat_free(pool->repos);
310 /* the first two solvables don't belong to a repo */
311 pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
315 repo_fix_legacy(Repo *repo, Offset provides, Offset supplements)
317 Pool *pool = repo->pool;
319 char buf[1024], *p, *dep;
324 for (i = provides; repo->idarraydata[i]; i++)
326 id = repo->idarraydata[i];
329 dep = (char *)id2str(pool, id);
330 if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
333 strcpy(buf + 2, dep);
335 if ((p = strchr(dep, ':')) != 0 && p != dep)
338 idp = str2id(pool, dep, 1);
342 while ((p = strchr(dep, ';')) != 0)
349 strncpy(dep - 9, "language:", 9);
351 idl = str2id(pool, dep - 9, 1);
353 id = rel2id(pool, id, idl, REL_OR, 1);
358 if (dep[0] && dep[1])
360 for (p = dep; *p && *p != ')'; p++)
363 strncpy(dep - 9, "language:", 9);
364 idl = str2id(pool, dep - 9, 1);
366 id = rel2id(pool, id, idl, REL_OR, 1);
371 id = rel2id(pool, idp, id, REL_AND, 1);
373 supplements = repo_addid_dep(repo, supplements, id, 0);
375 else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
380 idp = str2id(pool, buf, 1);
381 id = str2id(pool, p, 1);
382 id = rel2id(pool, idp, id, REL_WITH, 1);
383 id = rel2id(pool, NAMESPACE_SPLITPROVIDES, id, REL_NAMESPACE, 1);
384 supplements = repo_addid_dep(repo, supplements, id, 0);
390 for (i = supplements; repo->idarraydata[i]; i++)
392 id = repo->idarraydata[i];
395 dep = (char *)id2str(pool, id);
396 if (!strncmp(dep, "system:modalias(", 16))
398 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
401 p = strchr(buf + 9, ':');
402 if (p && p != buf + 9 && strchr(p + 1, ':'))
405 idp = str2id(pool, buf + 9, 1);
406 p[strlen(p) - 1] = 0;
407 id = str2id(pool, p, 1);
408 id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
409 id = rel2id(pool, idp, id, REL_AND, 1);
414 p[strlen(p) - 1] = 0;
415 id = str2id(pool, p, 1);
416 id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
419 repo->idarraydata[i] = id;
421 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
426 while ((p = strchr(dep, ':')) != 0)
434 idp = str2id(pool, dep, 1);
436 id = rel2id(pool, id, idp, REL_AND, 1);
441 if (dep[0] && dep[1])
443 dep[strlen(dep) - 1] = 0;
444 idp = str2id(pool, dep, 1);
446 id = rel2id(pool, id, idp, REL_AND, 1);
451 repo->idarraydata[i] = id;
466 int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv);
471 repo_matchvalue(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
473 struct matchdata *md = cbdata;
474 int flags = md->flags;
476 if ((flags & SEARCH_STRINGMASK) != 0)
483 kv->str = stringpool_id2str(&data->spool, kv->id);
485 kv->str = id2str(data->repo->pool, kv->id);
492 switch ((flags & SEARCH_STRINGMASK))
494 case SEARCH_SUBSTRING:
495 if (flags & SEARCH_NOCASE)
497 if (!strcasestr(kv->str, md->match))
502 if (!strstr(kv->str, md->match))
507 if (flags & SEARCH_NOCASE)
509 if (strcasecmp(md->match, kv->str))
514 if (strcmp(md->match, kv->str))
519 if (fnmatch(md->match, kv->str, (flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0))
524 if (regexec(&md->regexp, kv->str, 0, NULL, 0))
531 md->stop = md->callback(md->callback_data, s, data, key, kv);
536 static Repokey solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1] = {
537 { SOLVABLE_NAME, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
538 { SOLVABLE_ARCH, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
539 { SOLVABLE_EVR, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
540 { SOLVABLE_VENDOR, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
541 { SOLVABLE_PROVIDES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
542 { SOLVABLE_OBSOLETES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
543 { SOLVABLE_CONFLICTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
544 { SOLVABLE_REQUIRES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
545 { SOLVABLE_RECOMMENDS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
546 { SOLVABLE_SUGGESTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
547 { SOLVABLE_SUPPLEMENTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
548 { SOLVABLE_ENHANCES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
549 { SOLVABLE_FRESHENS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
550 { RPM_RPMDBID, TYPE_U32, 0, KEY_STORAGE_SOLVABLE },
554 domatch_idarray(Solvable *s, Id keyname, struct matchdata *md, Id *ida)
557 for (; *ida && !md->stop; ida++)
560 kv.eof = ida[1] ? 0 : 1;
561 repo_matchvalue(md, s, 0, solvablekeys + (keyname - SOLVABLE_NAME), &kv);
566 repo_search_md(Repo *repo, Id p, Id keyname, struct matchdata *md)
569 Pool *pool = repo->pool;
577 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
580 repo_search_md(repo, p, keyname, md);
581 if (md->stop > SEARCH_NEXT_SOLVABLE)
586 s = pool->solvables + p;
588 if (!(flags & SEARCH_NO_STORAGE_SOLVABLE))
597 repo_matchvalue(md, s, 0, solvablekeys + 0, &kv);
599 if (keyname || md->stop > SEARCH_NEXT_KEY)
605 repo_matchvalue(md, s, 0, solvablekeys + 1, &kv);
607 if (keyname || md->stop > SEARCH_NEXT_KEY)
613 repo_matchvalue(md, s, 0, solvablekeys + 2, &kv);
615 if (keyname || md->stop > SEARCH_NEXT_KEY)
617 case SOLVABLE_VENDOR:
621 repo_matchvalue(md, s, 0, solvablekeys + 3, &kv);
623 if (keyname || md->stop > SEARCH_NEXT_KEY)
625 case SOLVABLE_PROVIDES:
627 domatch_idarray(s, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
628 if (keyname || md->stop > SEARCH_NEXT_KEY)
630 case SOLVABLE_OBSOLETES:
632 domatch_idarray(s, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
633 if (keyname || md->stop > SEARCH_NEXT_KEY)
635 case SOLVABLE_CONFLICTS:
637 domatch_idarray(s, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
638 if (keyname || md->stop > SEARCH_NEXT_KEY)
640 case SOLVABLE_REQUIRES:
642 domatch_idarray(s, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
643 if (keyname || md->stop > SEARCH_NEXT_KEY)
645 case SOLVABLE_RECOMMENDS:
647 domatch_idarray(s, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
648 if (keyname || md->stop > SEARCH_NEXT_KEY)
650 case SOLVABLE_SUPPLEMENTS:
652 domatch_idarray(s, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
653 if (keyname || md->stop > SEARCH_NEXT_KEY)
655 case SOLVABLE_SUGGESTS:
657 domatch_idarray(s, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
658 if (keyname || md->stop > SEARCH_NEXT_KEY)
660 case SOLVABLE_ENHANCES:
662 domatch_idarray(s, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
663 if (keyname || md->stop > SEARCH_NEXT_KEY)
665 case SOLVABLE_FRESHENS:
667 domatch_idarray(s, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
668 if (keyname || md->stop > SEARCH_NEXT_KEY)
673 kv.num = repo->rpmdbid[p - repo->start];
674 repo_matchvalue(md, s, 0, solvablekeys + (RPM_RPMDBID - SOLVABLE_NAME), &kv);
676 if (keyname || md->stop > SEARCH_NEXT_KEY)
684 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
686 if (p < data->start || p >= data->end)
688 if (data->state == REPODATA_STUB)
692 for (j = 1; j < data->nkeys; j++)
693 if (keyname == data->keys[j].name)
695 if (j == data->nkeys)
699 if (data->loadcallback)
700 data->loadcallback(data);
702 data->state = REPODATA_ERROR;
704 if (data->state == REPODATA_ERROR)
706 repodata_search(data, p - data->start, keyname, repo_matchvalue, md);
707 if (md->stop > SEARCH_NEXT_KEY)
713 repo_search(Repo *repo, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
717 memset(&md, 0, sizeof(md));
718 md.pool = repo->pool;
721 md.callback = callback;
722 md.callback_data = cbdata;
723 repo_search_md(repo, p, key, &md);
727 repo_lookup_str(Solvable *s, Id key)
729 Repo *repo = s->repo;
730 Pool *pool = repo->pool;
737 return id2str(pool, s->name);
739 return id2str(pool, s->arch);
741 return id2str(pool, s->evr);
742 case SOLVABLE_VENDOR:
743 return id2str(pool, s->vendor);
745 n = s - pool->solvables;
746 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
748 if (n < data->start || n >= data->end)
750 for (j = 1; j < data->nkeys; j++)
752 if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
753 return repodata_lookup_str(data, n - data->start, j);
760 repo_add_repodata(Repo *repo)
765 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
766 data = repo->repodata + repo->nrepodata - 1;
767 memset(data, 0, sizeof (*data));
769 data->start = repo->start;
770 data->end = repo->end;
772 data->keys = sat_calloc(1, sizeof(Repokey));
774 data->schemata = sat_calloc(1, sizeof(Id));
775 data->schemadata = sat_calloc(1, sizeof(Id));
777 data->schemadatalen = 1;
778 data->entryschemau8 = sat_calloc(data->end - data->start, 1);
779 data->incoreoffset = sat_calloc(data->end - data->start, sizeof(Id));
783 static Repodata *findrepodata(Repo *repo, Id p, Id keyname)
788 /* FIXME: enter nice code here */
789 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
790 if (p >= data->start && p < data->end)
792 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
795 if (i < repo->nrepodata)
797 repodata_extend(data, p);
800 return repo_add_repodata(repo);
804 repo_set_id(Repo *repo, Id p, Id keyname, Id id)
806 Repodata *data = findrepodata(repo, p, keyname);
807 repodata_set_id(data, p - data->start, keyname, id);
811 repo_set_num(Repo *repo, Id p, Id keyname, Id num)
813 Repodata *data = findrepodata(repo, p, keyname);
814 repodata_set_num(data, p - data->start, keyname, num);
818 repo_set_str(Repo *repo, Id p, Id keyname, const char *str)
820 Repodata *data = findrepodata(repo, p, keyname);
821 repodata_set_str(data, p - data->start, keyname, str);
825 repo_set_poolstr(Repo *repo, Id p, Id keyname, const char *str)
827 Repodata *data = findrepodata(repo, p, keyname);
828 repodata_set_poolstr(data, p - data->start, keyname, str);
832 repo_internalize(Repo *repo)
837 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
839 repodata_internalize(data);
846 key_cmp (const void *pa, const void *pb)
848 Repokey *a = (Repokey *)pa;
849 Repokey *b = (Repokey *)pb;
850 return a->name - b->name;
854 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
858 /* If this is meant to be the embedded attributes, make sure we don't
859 have them already. */
862 for (i = 0; i < repo->nrepodata; i++)
863 if (repo->repodata[i].location == 0)
865 if (i != repo->nrepodata)
867 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
872 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
873 data = repo->repodata + repo->nrepodata - 1;
874 memset (data, 0, sizeof (*data));
876 data->nkeys = s->nkeys;
879 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
880 for (i = 1; i < data->nkeys; i++)
882 data->keys[i].name = s->keys[i].name;
883 data->keys[i].type = s->keys[i].type;
886 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
889 data->location = strdup(location);