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)
351 strncpy(dep - 9, "language:", 9);
352 idl = str2id(pool, dep - 9, 1);
354 idl = str2id(pool, dep, 1);
355 idl = rel2id(pool, NAMESPACE_LANGUAGE, idl, REL_NAMESPACE, 1);
358 id = rel2id(pool, id, idl, REL_OR, 1);
363 if (dep[0] && dep[1])
365 for (p = dep; *p && *p != ')'; p++)
369 strncpy(dep - 9, "language:", 9);
370 idl = str2id(pool, dep - 9, 1);
372 idl = str2id(pool, dep, 1);
373 idl = rel2id(pool, NAMESPACE_LANGUAGE, idl, REL_NAMESPACE, 1);
376 id = rel2id(pool, id, idl, REL_OR, 1);
381 id = rel2id(pool, idp, id, REL_AND, 1);
383 supplements = repo_addid_dep(repo, supplements, id, 0);
385 else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
390 idp = str2id(pool, buf, 1);
391 id = str2id(pool, p, 1);
392 id = rel2id(pool, idp, id, REL_WITH, 1);
393 id = rel2id(pool, NAMESPACE_SPLITPROVIDES, id, REL_NAMESPACE, 1);
394 supplements = repo_addid_dep(repo, supplements, id, 0);
400 for (i = supplements; repo->idarraydata[i]; i++)
402 id = repo->idarraydata[i];
405 dep = (char *)id2str(pool, id);
406 if (!strncmp(dep, "system:modalias(", 16))
408 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
411 p = strchr(buf + 9, ':');
412 if (p && p != buf + 9 && strchr(p + 1, ':'))
415 idp = str2id(pool, buf + 9, 1);
416 p[strlen(p) - 1] = 0;
417 id = str2id(pool, p, 1);
418 id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
419 id = rel2id(pool, idp, id, REL_AND, 1);
424 p[strlen(p) - 1] = 0;
425 id = str2id(pool, p, 1);
426 id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
429 repo->idarraydata[i] = id;
431 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
436 while ((p = strchr(dep, ':')) != 0)
444 idp = str2id(pool, dep, 1);
446 id = rel2id(pool, id, idp, REL_AND, 1);
451 if (dep[0] && dep[1])
453 dep[strlen(dep) - 1] = 0;
454 idp = str2id(pool, dep, 1);
456 id = rel2id(pool, id, idp, REL_AND, 1);
461 repo->idarraydata[i] = id;
476 int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv);
481 repo_matchvalue(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
483 struct matchdata *md = cbdata;
484 int flags = md->flags;
486 if ((flags & SEARCH_STRINGMASK) != 0)
492 if (data && data->localpool)
493 kv->str = stringpool_id2str(&data->spool, kv->id);
495 kv->str = id2str(s->repo->pool, kv->id);
502 switch ((flags & SEARCH_STRINGMASK))
504 case SEARCH_SUBSTRING:
505 if (flags & SEARCH_NOCASE)
507 if (!strcasestr(kv->str, md->match))
512 if (!strstr(kv->str, md->match))
517 if (flags & SEARCH_NOCASE)
519 if (strcasecmp(md->match, kv->str))
524 if (strcmp(md->match, kv->str))
529 if (fnmatch(md->match, kv->str, (flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0))
534 if (regexec(&md->regexp, kv->str, 0, NULL, 0))
541 md->stop = md->callback(md->callback_data, s, data, key, kv);
546 static Repokey solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1] = {
547 { SOLVABLE_NAME, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
548 { SOLVABLE_ARCH, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
549 { SOLVABLE_EVR, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
550 { SOLVABLE_VENDOR, TYPE_ID, 0, KEY_STORAGE_SOLVABLE },
551 { SOLVABLE_PROVIDES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
552 { SOLVABLE_OBSOLETES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
553 { SOLVABLE_CONFLICTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
554 { SOLVABLE_REQUIRES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
555 { SOLVABLE_RECOMMENDS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
556 { SOLVABLE_SUGGESTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
557 { SOLVABLE_SUPPLEMENTS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
558 { SOLVABLE_ENHANCES, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
559 { SOLVABLE_FRESHENS, TYPE_IDARRAY, 0, KEY_STORAGE_SOLVABLE },
560 { RPM_RPMDBID, TYPE_U32, 0, KEY_STORAGE_SOLVABLE },
564 domatch_idarray(Solvable *s, Id keyname, struct matchdata *md, Id *ida)
567 for (; *ida && !md->stop; ida++)
570 kv.eof = ida[1] ? 0 : 1;
571 repo_matchvalue(md, s, 0, solvablekeys + (keyname - SOLVABLE_NAME), &kv);
576 repo_search_md(Repo *repo, Id p, Id keyname, struct matchdata *md)
579 Pool *pool = repo->pool;
587 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
590 repo_search_md(repo, p, keyname, md);
591 if (md->stop > SEARCH_NEXT_SOLVABLE)
596 s = pool->solvables + p;
598 if (!(flags & SEARCH_NO_STORAGE_SOLVABLE))
607 repo_matchvalue(md, s, 0, solvablekeys + 0, &kv);
609 if (keyname || md->stop > SEARCH_NEXT_KEY)
615 repo_matchvalue(md, s, 0, solvablekeys + 1, &kv);
617 if (keyname || md->stop > SEARCH_NEXT_KEY)
623 repo_matchvalue(md, s, 0, solvablekeys + 2, &kv);
625 if (keyname || md->stop > SEARCH_NEXT_KEY)
627 case SOLVABLE_VENDOR:
631 repo_matchvalue(md, s, 0, solvablekeys + 3, &kv);
633 if (keyname || md->stop > SEARCH_NEXT_KEY)
635 case SOLVABLE_PROVIDES:
637 domatch_idarray(s, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
638 if (keyname || md->stop > SEARCH_NEXT_KEY)
640 case SOLVABLE_OBSOLETES:
642 domatch_idarray(s, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
643 if (keyname || md->stop > SEARCH_NEXT_KEY)
645 case SOLVABLE_CONFLICTS:
647 domatch_idarray(s, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
648 if (keyname || md->stop > SEARCH_NEXT_KEY)
650 case SOLVABLE_REQUIRES:
652 domatch_idarray(s, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
653 if (keyname || md->stop > SEARCH_NEXT_KEY)
655 case SOLVABLE_RECOMMENDS:
657 domatch_idarray(s, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
658 if (keyname || md->stop > SEARCH_NEXT_KEY)
660 case SOLVABLE_SUPPLEMENTS:
662 domatch_idarray(s, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
663 if (keyname || md->stop > SEARCH_NEXT_KEY)
665 case SOLVABLE_SUGGESTS:
667 domatch_idarray(s, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
668 if (keyname || md->stop > SEARCH_NEXT_KEY)
670 case SOLVABLE_ENHANCES:
672 domatch_idarray(s, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
673 if (keyname || md->stop > SEARCH_NEXT_KEY)
675 case SOLVABLE_FRESHENS:
677 domatch_idarray(s, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
678 if (keyname || md->stop > SEARCH_NEXT_KEY)
683 kv.num = repo->rpmdbid[p - repo->start];
684 repo_matchvalue(md, s, 0, solvablekeys + (RPM_RPMDBID - SOLVABLE_NAME), &kv);
686 if (keyname || md->stop > SEARCH_NEXT_KEY)
694 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
696 if (p < data->start || p >= data->end)
698 if (data->state == REPODATA_STUB)
702 for (j = 1; j < data->nkeys; j++)
703 if (keyname == data->keys[j].name)
705 if (j == data->nkeys)
709 if (data->loadcallback)
710 data->loadcallback(data);
712 data->state = REPODATA_ERROR;
714 if (data->state == REPODATA_ERROR)
716 repodata_search(data, p - data->start, keyname, repo_matchvalue, md);
717 if (md->stop > SEARCH_NEXT_KEY)
723 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)
727 memset(&md, 0, sizeof(md));
728 md.pool = repo->pool;
731 md.callback = callback;
732 md.callback_data = cbdata;
733 repo_search_md(repo, p, key, &md);
737 repo_lookup_str(Solvable *s, Id key)
739 Repo *repo = s->repo;
740 Pool *pool = repo->pool;
747 return id2str(pool, s->name);
749 return id2str(pool, s->arch);
751 return id2str(pool, s->evr);
752 case SOLVABLE_VENDOR:
753 return id2str(pool, s->vendor);
755 n = s - pool->solvables;
756 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
758 if (n < data->start || n >= data->end)
760 for (j = 1; j < data->nkeys; j++)
762 if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
763 return repodata_lookup_str(data, n - data->start, j);
770 repo_lookup_num(Solvable *s, Id key)
772 Repo *repo = s->repo;
773 Pool *pool = repo->pool;
777 n = s - pool->solvables;
778 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
780 if (n < data->start || n >= data->end)
782 for (j = 1; j < data->nkeys; j++)
784 if (data->keys[j].name == key
785 && (data->keys[j].type == TYPE_U32
786 || data->keys[j].type == TYPE_NUM
787 || data->keys[j].type == TYPE_CONSTANT))
790 if (repodata_lookup_num(data, n - data->start, j, &value))
800 * generic attribute lookup
801 * returns non-zero if found
806 repo_lookup(Solvable *s, Id key, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
808 Repo *repo = s->repo;
809 Pool *pool = repo->pool;
813 s_id = s - pool->solvables;
814 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
816 if (s_id < data->start || s_id >= data->end)
818 repodata_search (data, s_id - data->start, key, callback, cbdata);
826 repo_add_repodata(Repo *repo)
831 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
832 data = repo->repodata + repo->nrepodata - 1;
833 memset(data, 0, sizeof (*data));
835 data->start = repo->start;
836 data->end = repo->end;
838 data->keys = sat_calloc(1, sizeof(Repokey));
840 data->schemata = sat_calloc(1, sizeof(Id));
841 data->schemadata = sat_calloc(1, sizeof(Id));
843 data->schemadatalen = 1;
844 data->incoreoffset = sat_calloc(data->end - data->start, sizeof(Id));
849 findrepodata(Repo *repo, Id p, Id keyname)
854 /* FIXME: enter nice code here */
855 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
856 if (p >= data->start && p < data->end)
858 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
861 if (i < repo->nrepodata)
863 repodata_extend(data, p);
866 return repo_add_repodata(repo);
870 repo_set_id(Repo *repo, Id p, Id keyname, Id id)
872 Repodata *data = findrepodata(repo, p, keyname);
873 repodata_set_id(data, p - data->start, keyname, id);
877 repo_set_num(Repo *repo, Id p, Id keyname, Id num)
879 Repodata *data = findrepodata(repo, p, keyname);
880 repodata_set_num(data, p - data->start, keyname, num);
884 repo_set_str(Repo *repo, Id p, Id keyname, const char *str)
886 Repodata *data = findrepodata(repo, p, keyname);
887 repodata_set_str(data, p - data->start, keyname, str);
891 repo_set_poolstr(Repo *repo, Id p, Id keyname, const char *str)
893 Repodata *data = findrepodata(repo, p, keyname);
894 repodata_set_poolstr(data, p - data->start, keyname, str);
898 repo_internalize(Repo *repo)
903 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
905 repodata_internalize(data);
912 key_cmp (const void *pa, const void *pb)
914 Repokey *a = (Repokey *)pa;
915 Repokey *b = (Repokey *)pb;
916 return a->name - b->name;
920 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
924 /* If this is meant to be the embedded attributes, make sure we don't
925 have them already. */
928 for (i = 0; i < repo->nrepodata; i++)
929 if (repo->repodata[i].location == 0)
931 if (i != repo->nrepodata)
933 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
938 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
939 data = repo->repodata + repo->nrepodata - 1;
940 memset (data, 0, sizeof (*data));
942 data->nkeys = s->nkeys;
945 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
946 for (i = 1; i < data->nkeys; i++)
948 data->keys[i].name = s->keys[i].name;
949 data->keys[i].type = s->keys[i].type;
952 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
955 data->location = strdup(location);