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 *)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;
44 repo->start = pool->nsolvables;
45 repo->end = pool->nsolvables;
51 repo_freedata(Repo *repo)
53 xfree(repo->idarraydata);
55 xfree((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 = (Id *)xmalloc((1 + IDARRAY_BLOCK) * sizeof(Id));
83 if (!olddeps) /* no deps yet */
85 olddeps = idarraysize;
86 if ((idarraysize & IDARRAY_BLOCK) == 0)
87 idarray = (Id *)xrealloc(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 = (Id *)xrealloc(idarray, (idarraysize + 1 + IDARRAY_BLOCK) * sizeof(Id));
99 idarray[idarraysize++] = idarray[i];
101 if ((idarraysize & IDARRAY_BLOCK) == 0)
102 idarray = (Id *)xrealloc(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 = (Id *)xrealloc(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
122 * olddeps = offset into idarraydata
123 * isreq = 0 for normal dep
124 * isreq = 1 for requires
125 * isreq = 2 for pre-requires
130 repo_addid_dep(Repo *repo, Offset olddeps, Id id, int isreq)
132 Id oid, *oidp, *marker = 0;
135 return repo_addid(repo, olddeps, id);
139 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
144 return repo_addid(repo, olddeps, id);
147 for (oidp = repo->idarraydata + olddeps; (oid = *oidp) != ID_NULL; oidp++)
149 if (oid == SOLVABLE_PREREQMARKER)
157 if (marker || isreq == 1)
160 for (; (oid = *oidp) != ID_NULL; oidp++)
161 if (oid == SOLVABLE_PREREQMARKER)
167 memmove(marker, marker + 1, (oidp - marker) * sizeof(Id));
168 *oidp = SOLVABLE_PREREQMARKER;
169 return repo_addid(repo, olddeps, id);
173 memmove(marker, marker + 1, (oidp - marker) * sizeof(Id));
177 if (isreq == 2 && !marker)
178 olddeps = repo_addid(repo, olddeps, SOLVABLE_PREREQMARKER);
179 else if (isreq == 1 && marker)
184 memmove(marker + 1, marker, (oidp - marker) * sizeof(Id));
185 *marker = SOLVABLE_PREREQMARKER;
187 return repo_addid(repo, olddeps, id);
193 * make space for 'num' more dependencies
197 repo_reserve_ids(Repo *repo, Offset olddeps, int num)
199 num++; /* room for trailing ID_NULL */
201 if (!repo->idarraysize) /* ensure buffer space */
203 repo->idarraysize = 1;
204 repo->idarraydata = (Id *)xmalloc(((1 + num + IDARRAY_BLOCK) & ~IDARRAY_BLOCK) * sizeof(Id));
205 repo->idarraydata[0] = 0;
210 if (olddeps && olddeps != repo->lastoff) /* if not appending */
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.
220 for (idstart = idend = repo->idarraydata + olddeps; *idend++; ) /* find end */
222 count = idend - idstart - 1 + num; /* new size */
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));
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;
236 if (olddeps) /* appending */
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));
243 /* appending or new */
244 repo->lastoff = olddeps ? olddeps : repo->idarraysize;
246 return repo->lastoff;
251 * remove repo from pool, zero out solvables
256 repo_free(Repo *repo, int reuseids)
258 Pool *pool = repo->pool;
262 pool_freewhatprovides(pool);
264 if (reuseids && repo->end == pool->nsolvables)
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--)
272 pool->nsolvables = i + 1;
274 /* zero out solvables belonging to this repo */
275 for (i = repo->start, s = pool->solvables + i; i < repo->end; i++, s++)
277 memset(s, 0, sizeof(*s));
278 for (i = 0; i < pool->nrepos; i++) /* find repo in pool */
279 if (pool->repos[i] == repo)
281 if (i == pool->nrepos) /* repo not in pool, return */
283 if (i < pool->nrepos - 1)
284 memmove(pool->repos + i, pool->repos + i + 1, (pool->nrepos - 1 - i) * sizeof(Repo *));
290 repo_freeallrepos(Pool *pool, int reuseids)
294 pool_freewhatprovides(pool);
295 for (i = 0; i < pool->nrepos; i++)
296 repo_freedata(pool->repos[i]);
297 pool->repos = xfree(pool->repos);
299 /* the first two solvables don't belong to a repo */
300 pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
304 repo_fix_legacy(Repo *repo, Offset provides, Offset supplements)
306 Pool *pool = repo->pool;
307 Id id, idp, idl, idns;
308 char buf[1024], *p, *dep;
313 for (i = provides; repo->idarraydata[i]; i++)
315 id = repo->idarraydata[i];
318 dep = (char *)id2str(pool, id);
319 if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
322 strcpy(buf + 2, dep);
324 if ((p = strchr(dep, ':')) != 0 && p != dep)
327 idp = str2id(pool, dep, 1);
331 while ((p = strchr(dep, ';')) != 0)
338 strncpy(dep - 9, "language:", 9);
340 idl = str2id(pool, dep - 9, 1);
342 id = rel2id(pool, id, idl, REL_OR, 1);
347 if (dep[0] && dep[1])
349 for (p = dep; *p && *p != ')'; p++)
352 strncpy(dep - 9, "language:", 9);
353 idl = str2id(pool, dep - 9, 1);
355 id = rel2id(pool, id, idl, REL_OR, 1);
360 id = rel2id(pool, idp, id, REL_AND, 1);
362 supplements = repo_addid_dep(repo, supplements, id, 0);
364 else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
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);
380 for (i = supplements; repo->idarraydata[i]; i++)
382 id = repo->idarraydata[i];
385 dep = (char *)id2str(pool, id);
386 if (!strncmp(dep, "system:modalias(", 16))
388 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
391 p = strchr(buf + 9, ':');
392 idns = str2id(pool, "namespace:modalias", 1);
393 if (p && p != buf + 9 && strchr(p + 1, ':'))
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);
405 p[strlen(p) - 1] = 0;
406 id = str2id(pool, p, 1);
407 id = rel2id(pool, idns, id, REL_NAMESPACE, 1);
410 repo->idarraydata[i] = id;
412 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
417 while ((p = strchr(dep, ':')) != 0)
425 idp = str2id(pool, dep, 1);
427 id = rel2id(pool, id, idp, REL_AND, 1);
432 if (dep[0] && dep[1])
434 dep[strlen(dep) - 1] = 0;
435 idp = str2id(pool, dep, 1);
437 id = rel2id(pool, id, idp, REL_AND, 1);
442 repo->idarraydata[i] = id;
448 static unsigned char *
449 data_read_id(unsigned char *dp, Id *idp)
461 x = (x << 7) ^ c ^ 128;
465 static unsigned char *
466 data_skip(unsigned char *dp, int type)
473 while ((*dp & 0x80) != 0)
477 case TYPE_REL_IDARRAY:
478 case TYPE_IDVALUEARRAY:
479 case TYPE_IDVALUEVALUEARRAY:
480 while ((*dp & 0xc0) != 0)
484 fprintf(stderr, "unknown type in data_skip\n");
489 static unsigned char *
490 forward_to_key(Repodata *data, Id key, Id schema, unsigned char *dp)
494 keyp = data->schemadata + schema;
495 while ((k = *keyp++) != 0)
499 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
501 /* skip that offset */
502 dp = data_skip(dp, TYPE_ID);
505 if (data->keys[k].storage != KEY_STORAGE_INCORE)
507 dp = data_skip(dp, data->keys[k].type);
512 static unsigned char *
513 load_page_range(Repodata *data, unsigned int pstart, unsigned int pend)
515 /* add smart paging here */
519 static unsigned char *
520 get_data(Repodata *data, Repokey *key, unsigned char **dpp)
523 unsigned char *dp = *dpp;
525 unsigned int pstart, pend, poff, plen;
529 if (key->storage == KEY_STORAGE_INCORE)
531 if (key->storage != KEY_STORAGE_VERTICAL_OFFSET)
535 dp = data_read_id(dp, &off);
537 if (key->type == TYPE_VOID)
539 max = key->size - off;
542 /* we now have the offset, go into vertical */
543 for (i = key - data->keys - 1; i > 0; i--)
544 if (data->keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
545 off += data->keys[i].size;
546 pstart = off / BLOB_PAGESIZE;
548 poff = off % BLOB_PAGESIZE;
549 plen = BLOB_PAGESIZE - poff;
554 dp = load_page_range(data, pstart, pend) + poff;
560 if (memchr(dp, 0, plen))
564 for (i = 0; i < plen; i++)
565 if ((dp[i] & 0x80) == 0)
569 case TYPE_REL_IDARRAY:
570 case TYPE_IDVALUEARRAY:
571 case TYPE_IDVALUEVALUEARRAY:
572 for (i = 0; i < plen; i++)
573 if ((dp[i] & 0xc0) == 0)
580 plen += BLOB_PAGESIZE;
586 repodata_lookup_str(Repodata *data, Id entry, Id key)
589 Id id, k, *kp, *keyp;
592 if (data->entryschemau8)
593 schema = data->entryschemau8[entry];
595 schema = data->entryschema[entry];
596 keyp = data->schemadata + schema;
597 /* make sure the schema of this solvable contains the key */
598 for (kp = keyp; (k = *kp++) != 0; )
603 dp = forward_to_key(data, key, schema, data->incoredata + data->incoreoffset[entry]);
604 dp = get_data(data, data->keys + key, &dp);
607 if (data->keys[key].type == TYPE_STR)
608 return (const char *)dp;
609 /* id type, must either use global or local string strore*/
610 dp = data_read_id(dp, &id);
612 /* not yet working */
613 return data->ss.stringspace + data->ss.strings[id];
615 return id2str(data->repo->pool, id);
622 const char *matchstr;
628 int (*callback)(void *data, Solvable *s, Id key, const char *str);
633 domatch(Id p, Id key, struct matchdata *md, const char *str)
635 /* fill match code here */
636 md->callback(md->callback_data, md->pool->solvables + p, key, str);
640 repodata_search(Repodata *data, Id entry, Id key, struct matchdata *md)
643 Id id, k, *kp, *keyp;
644 unsigned char *dp, *ddp;
647 if (data->entryschemau8)
648 schema = data->entryschemau8[entry];
650 schema = data->entryschema[entry];
651 keyp = data->schemadata + schema;
652 dp = data->incoredata + data->incoreoffset[entry];
655 /* search in a specific key */
656 for (kp = keyp; (k = *kp++) != 0; )
661 dp = forward_to_key(data, key, schema, dp);
667 while ((key = *keyp++) != 0)
669 ddp = get_data(data, data->keys + key, &dp);
672 switch (data->keys[key].type)
675 domatch(data->start + entry, data->keys[key].name, md, (const char *)ddp);
679 data_read_id(ddp, &id);
684 ddp = data_read_id(ddp, &id);
685 if ((id & 0x40) == 0)
687 id = (id & 0x3f) | ((id >> 1) & ~0x3f);
694 if (onekey || md->stop)
700 repo_search_md(Repo *repo, Id p, Id key, struct matchdata *md)
702 Pool *pool = repo->pool;
709 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
711 repo_search_md(repo, p, key, md);
714 s = pool->solvables + p;
715 if ((!key || key == SOLVABLE_NAME) && s->name)
716 domatch(p, SOLVABLE_NAME, md, id2str(pool, s->name));
717 if ((!key || key == SOLVABLE_ARCH) && s->arch)
718 domatch(p, SOLVABLE_ARCH, md, id2str(pool, s->arch));
719 if ((!key || key == SOLVABLE_EVR) && s->evr)
720 domatch(p, SOLVABLE_EVR, md, id2str(pool, s->evr));
721 if ((!key || key == SOLVABLE_VENDOR) && s->vendor)
722 domatch(p, SOLVABLE_VENDOR, md, id2str(pool, s->vendor));
723 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
725 if (p < data->start || p >= data->end)
727 repodata_search(data, p - data->start, key, md);
732 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)
736 memset(&md, 0, sizeof(md));
737 md.pool = repo->pool;
740 md.callback = callback;
741 md.callback_data = callback_data;
742 repo_search_md(repo, p, key, &md);
746 repo_lookup_str(Solvable *s, Id key)
748 Repo *repo = s->repo;
749 Pool *pool = repo->pool;
756 return id2str(pool, s->name);
758 return id2str(pool, s->arch);
760 return id2str(pool, s->evr);
761 case SOLVABLE_VENDOR:
762 return id2str(pool, s->vendor);
764 n = s - pool->solvables;
765 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
767 if (n < data->start || n >= data->end)
769 for (j = 1; j < data->nkeys; j++)
771 if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
772 return repodata_lookup_str(data, n - data->start, j);
781 key_cmp (const void *pa, const void *pb)
783 Repokey *a = (Repokey *)pa;
784 Repokey *b = (Repokey *)pb;
785 return a->name - b->name;
789 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
793 /* If this is meant to be the embedded attributes, make sure we don't
794 have them already. */
797 for (i = 0; i < repo->nrepodata; i++)
798 if (repo->repodata[i].location == 0)
800 if (i != repo->nrepodata)
802 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
807 repo->repodata = xrealloc (repo->repodata, repo->nrepodata * sizeof (*data));
808 data = repo->repodata + repo->nrepodata - 1;
809 memset (data, 0, sizeof (*data));
811 data->nkeys = s->nkeys;
814 data->keys = xmalloc(data->nkeys * sizeof(data->keys[0]));
815 for (i = 1; i < data->nkeys; i++)
817 data->keys[i].name = s->keys[i].name;
818 data->keys[i].type = s->keys[i].type;
821 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
824 data->location = strdup(location);