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;
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 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);
393 for (i = supplements; repo->idarraydata[i]; i++)
395 id = repo->idarraydata[i];
398 dep = (char *)id2str(pool, id);
399 if (!strncmp(dep, "system:modalias(", 16))
401 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
404 p = strchr(buf + 9, ':');
405 if (p && p != buf + 9 && strchr(p + 1, ':'))
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);
417 p[strlen(p) - 1] = 0;
418 id = str2id(pool, p, 1);
419 id = rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
422 repo->idarraydata[i] = id;
424 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
429 while ((p = strchr(dep, ':')) != 0)
437 idp = str2id(pool, dep, 1);
439 id = rel2id(pool, id, idp, REL_AND, 1);
444 if (dep[0] && dep[1])
446 dep[strlen(dep) - 1] = 0;
447 idp = str2id(pool, dep, 1);
449 id = rel2id(pool, id, idp, REL_AND, 1);
454 repo->idarraydata[i] = id;
460 static unsigned char *
461 data_read_id(unsigned char *dp, Id *idp)
473 x = (x << 7) ^ c ^ 128;
477 static unsigned char *
478 data_skip(unsigned char *dp, int type)
485 while ((*dp & 0x80) != 0)
489 case TYPE_REL_IDARRAY:
490 case TYPE_IDVALUEARRAY:
491 case TYPE_IDVALUEVALUEARRAY:
492 while ((*dp & 0xc0) != 0)
496 fprintf(stderr, "unknown type in data_skip\n");
501 static unsigned char *
502 forward_to_key(Repodata *data, Id key, Id schema, unsigned char *dp)
506 keyp = data->schemadata + schema;
507 while ((k = *keyp++) != 0)
511 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
513 /* skip that offset */
514 dp = data_skip(dp, TYPE_ID);
517 if (data->keys[k].storage != KEY_STORAGE_INCORE)
519 dp = data_skip(dp, data->keys[k].type);
524 static unsigned char *
525 load_page_range(Repodata *data, unsigned int pstart, unsigned int pend)
527 /* add smart paging here */
531 static unsigned char *
532 get_data(Repodata *data, Repokey *key, unsigned char **dpp)
535 unsigned char *dp = *dpp;
537 unsigned int pstart, pend, poff, plen;
541 if (key->storage == KEY_STORAGE_INCORE)
543 *dpp = data_skip(dp, key->type);
546 if (key->storage != KEY_STORAGE_VERTICAL_OFFSET)
550 dp = data_read_id(dp, &off);
552 if (key->type == TYPE_VOID)
554 max = key->size - off;
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;
563 poff = off % BLOB_PAGESIZE;
564 plen = BLOB_PAGESIZE - poff;
569 dp = load_page_range(data, pstart, pend) + poff;
575 if (memchr(dp, 0, plen))
579 for (i = 0; i < plen; i++)
580 if ((dp[i] & 0x80) == 0)
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)
595 plen += BLOB_PAGESIZE;
601 repodata_lookup_str(Repodata *data, Id entry, Id key)
604 Id id, k, *kp, *keyp;
607 if (data->entryschemau8)
608 schema = data->entryschemau8[entry];
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; )
618 dp = forward_to_key(data, key, schema, data->incoredata + data->incoreoffset[entry]);
619 dp = get_data(data, data->keys + key, &dp);
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);
627 /* not yet working */
628 return data->ss.stringspace + data->ss.strings[id];
630 return id2str(data->repo->pool, id);
634 #define SEARCH_NEXT_KEY 1
635 #define SEARCH_NEXT_SOLVABLE 2
641 const char *matchstr;
647 int (*callback)(void *data, Solvable *s, Id key, const char *str);
652 domatch(Id p, Id key, struct matchdata *md, const char *str)
654 /* fill match code here */
655 md->stop = md->callback(md->callback_data, md->pool->solvables + p, key, str);
659 repodata_search(Repodata *data, Id entry, Id key, struct matchdata *md)
662 Id id, k, *kp, *keyp;
663 unsigned char *dp, *ddp;
666 if (data->entryschemau8)
667 schema = data->entryschemau8[entry];
669 schema = data->entryschema[entry];
670 keyp = data->schemadata + schema;
671 dp = data->incoredata + data->incoreoffset[entry];
674 /* search in a specific key */
675 for (kp = keyp; (k = *kp++) != 0; )
680 dp = forward_to_key(data, key, schema, dp);
687 while ((key = *keyp++) != 0)
689 ddp = get_data(data, data->keys + key, &dp);
692 switch (data->keys[key].type)
695 domatch(data->start + entry, data->keys[key].name, md, (const char *)ddp);
699 data_read_id(ddp, &id);
704 ddp = data_read_id(ddp, &id);
705 if ((id & 0x40) == 0)
707 id = (id & 0x3f) | ((id >> 1) & ~0x3f);
709 while (md->stop == SEARCH_NEXT_KEY && ddp)
710 ddp = data_read_id(ddp, &id);
716 if (onekey || md->stop > SEARCH_NEXT_KEY)
722 domatch_idarray(Id p, Id key, struct matchdata *md, Id *ida)
724 for (; *ida && !md->stop; ida++)
725 domatch(p, key, md, id2str(md->pool, *ida));
729 repo_search_md(Repo *repo, Id p, Id key, struct matchdata *md)
731 Pool *pool = repo->pool;
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:
758 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
759 repodata_search(data, -1, key, md);
763 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
766 repo_search_md(repo, p, key, md);
767 if (md->stop > SEARCH_NEXT_SOLVABLE)
772 s = pool->solvables + p;
778 domatch(p, SOLVABLE_NAME, md, id2str(pool, s->name));
779 if (key || md->stop > SEARCH_NEXT_KEY)
783 domatch(p, SOLVABLE_ARCH, md, id2str(pool, s->arch));
784 if (key || md->stop > SEARCH_NEXT_KEY)
788 domatch(p, SOLVABLE_EVR, md, id2str(pool, s->evr));
789 if (key || md->stop > SEARCH_NEXT_KEY)
791 case SOLVABLE_VENDOR:
793 domatch(p, SOLVABLE_VENDOR, md, id2str(pool, s->vendor));
794 if (key || md->stop > SEARCH_NEXT_KEY)
796 case SOLVABLE_PROVIDES:
798 domatch_idarray(p, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
799 if (key || md->stop > SEARCH_NEXT_KEY)
801 case SOLVABLE_OBSOLETES:
803 domatch_idarray(p, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
804 if (key || md->stop > SEARCH_NEXT_KEY)
806 case SOLVABLE_CONFLICTS:
808 domatch_idarray(p, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
809 if (key || md->stop > SEARCH_NEXT_KEY)
811 case SOLVABLE_REQUIRES:
813 domatch_idarray(p, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
814 if (key || md->stop > SEARCH_NEXT_KEY)
816 case SOLVABLE_RECOMMENDS:
818 domatch_idarray(p, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
819 if (key || md->stop > SEARCH_NEXT_KEY)
821 case SOLVABLE_SUPPLEMENTS:
823 domatch_idarray(p, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
824 if (key || md->stop > SEARCH_NEXT_KEY)
826 case SOLVABLE_SUGGESTS:
828 domatch_idarray(p, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
829 if (key || md->stop > SEARCH_NEXT_KEY)
831 case SOLVABLE_ENHANCES:
833 domatch_idarray(p, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
834 if (key || md->stop > SEARCH_NEXT_KEY)
836 case SOLVABLE_FRESHENS:
838 domatch_idarray(p, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
839 if (key || md->stop > SEARCH_NEXT_KEY)
845 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
847 if (p < data->start || p >= data->end)
849 repodata_search(data, p - data->start, key, md);
850 if (md->stop > SEARCH_NEXT_KEY)
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)
860 memset(&md, 0, sizeof(md));
861 md.pool = repo->pool;
864 md.callback = callback;
865 md.callback_data = callback_data;
866 repo_search_md(repo, p, key, &md);
870 repo_lookup_str(Solvable *s, Id key)
872 Repo *repo = s->repo;
873 Pool *pool = repo->pool;
880 return id2str(pool, s->name);
882 return id2str(pool, s->arch);
884 return id2str(pool, s->evr);
885 case SOLVABLE_VENDOR:
886 return id2str(pool, s->vendor);
888 n = s - pool->solvables;
889 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
891 if (n < data->start || n >= data->end)
893 for (j = 1; j < data->nkeys; j++)
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);
905 key_cmp (const void *pa, const void *pb)
907 Repokey *a = (Repokey *)pa;
908 Repokey *b = (Repokey *)pb;
909 return a->name - b->name;
913 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
917 /* If this is meant to be the embedded attributes, make sure we don't
918 have them already. */
921 for (i = 0; i < repo->nrepodata; i++)
922 if (repo->repodata[i].location == 0)
924 if (i != repo->nrepodata)
926 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
931 repo->repodata = sat_realloc2(repo->repodata, repo->nrepodata, sizeof(*data));
932 data = repo->repodata + repo->nrepodata - 1;
933 memset (data, 0, sizeof (*data));
935 data->nkeys = s->nkeys;
938 data->keys = sat_malloc2(data->nkeys, sizeof(data->keys[0]));
939 for (i = 1; i < data->nkeys; i++)
941 data->keys[i].name = s->keys[i].name;
942 data->keys[i].type = s->keys[i].type;
945 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
948 data->location = strdup(location);