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 *dpp = data_skip(dp, key->type);
534 if (key->storage != KEY_STORAGE_VERTICAL_OFFSET)
538 dp = data_read_id(dp, &off);
540 if (key->type == TYPE_VOID)
542 max = key->size - off;
545 /* we now have the offset, go into vertical */
546 for (i = key - data->keys - 1; i > 0; i--)
547 if (data->keys[i].storage == KEY_STORAGE_VERTICAL_OFFSET)
548 off += data->keys[i].size;
549 pstart = off / BLOB_PAGESIZE;
551 poff = off % BLOB_PAGESIZE;
552 plen = BLOB_PAGESIZE - poff;
557 dp = load_page_range(data, pstart, pend) + poff;
563 if (memchr(dp, 0, plen))
567 for (i = 0; i < plen; i++)
568 if ((dp[i] & 0x80) == 0)
572 case TYPE_REL_IDARRAY:
573 case TYPE_IDVALUEARRAY:
574 case TYPE_IDVALUEVALUEARRAY:
575 for (i = 0; i < plen; i++)
576 if ((dp[i] & 0xc0) == 0)
583 plen += BLOB_PAGESIZE;
589 repodata_lookup_str(Repodata *data, Id entry, Id key)
592 Id id, k, *kp, *keyp;
595 if (data->entryschemau8)
596 schema = data->entryschemau8[entry];
598 schema = data->entryschema[entry];
599 keyp = data->schemadata + schema;
600 /* make sure the schema of this solvable contains the key */
601 for (kp = keyp; (k = *kp++) != 0; )
606 dp = forward_to_key(data, key, schema, data->incoredata + data->incoreoffset[entry]);
607 dp = get_data(data, data->keys + key, &dp);
610 if (data->keys[key].type == TYPE_STR)
611 return (const char *)dp;
612 /* id type, must either use global or local string strore*/
613 dp = data_read_id(dp, &id);
615 /* not yet working */
616 return data->ss.stringspace + data->ss.strings[id];
618 return id2str(data->repo->pool, id);
622 #define SEARCH_NEXT_KEY 1
623 #define SEARCH_NEXT_SOLVABLE 2
629 const char *matchstr;
635 int (*callback)(void *data, Solvable *s, Id key, const char *str);
640 domatch(Id p, Id key, struct matchdata *md, const char *str)
642 /* fill match code here */
643 md->stop = md->callback(md->callback_data, md->pool->solvables + p, key, str);
647 repodata_search(Repodata *data, Id entry, Id key, struct matchdata *md)
650 Id id, k, *kp, *keyp;
651 unsigned char *dp, *ddp;
654 if (data->entryschemau8)
655 schema = data->entryschemau8[entry];
657 schema = data->entryschema[entry];
658 keyp = data->schemadata + schema;
659 dp = data->incoredata + data->incoreoffset[entry];
662 /* search in a specific key */
663 for (kp = keyp; (k = *kp++) != 0; )
668 dp = forward_to_key(data, key, schema, dp);
675 while ((key = *keyp++) != 0)
677 ddp = get_data(data, data->keys + key, &dp);
680 switch (data->keys[key].type)
683 domatch(data->start + entry, data->keys[key].name, md, (const char *)ddp);
687 data_read_id(ddp, &id);
692 ddp = data_read_id(ddp, &id);
693 if ((id & 0x40) == 0)
695 id = (id & 0x3f) | ((id >> 1) & ~0x3f);
697 while (md->stop == SEARCH_NEXT_KEY && ddp)
698 ddp = data_read_id(ddp, &id);
704 if (onekey || md->stop > SEARCH_NEXT_KEY)
710 domatch_idarray(Id p, Id key, struct matchdata *md, Id *ida)
712 for (; *ida && !md->stop; ida++)
713 domatch(p, key, md, id2str(md->pool, *ida));
717 repo_search_md(Repo *repo, Id p, Id key, struct matchdata *md)
719 Pool *pool = repo->pool;
727 for (p = repo->start, s = repo->pool->solvables + p; p < repo->end; p++, s++)
730 repo_search_md(repo, p, key, md);
731 if (md->stop > SEARCH_NEXT_SOLVABLE)
736 s = pool->solvables + p;
742 domatch(p, SOLVABLE_NAME, md, id2str(pool, s->name));
743 if (key || md->stop > SEARCH_NEXT_KEY)
747 domatch(p, SOLVABLE_ARCH, md, id2str(pool, s->arch));
748 if (key || md->stop > SEARCH_NEXT_KEY)
752 domatch(p, SOLVABLE_EVR, md, id2str(pool, s->evr));
753 if (key || md->stop > SEARCH_NEXT_KEY)
755 case SOLVABLE_VENDOR:
757 domatch(p, SOLVABLE_VENDOR, md, id2str(pool, s->vendor));
758 if (key || md->stop > SEARCH_NEXT_KEY)
760 case SOLVABLE_PROVIDES:
762 domatch_idarray(p, SOLVABLE_PROVIDES, md, repo->idarraydata + s->provides);
763 if (key || md->stop > SEARCH_NEXT_KEY)
765 case SOLVABLE_OBSOLETES:
767 domatch_idarray(p, SOLVABLE_OBSOLETES, md, repo->idarraydata + s->obsoletes);
768 if (key || md->stop > SEARCH_NEXT_KEY)
770 case SOLVABLE_CONFLICTS:
772 domatch_idarray(p, SOLVABLE_CONFLICTS, md, repo->idarraydata + s->conflicts);
773 if (key || md->stop > SEARCH_NEXT_KEY)
775 case SOLVABLE_REQUIRES:
777 domatch_idarray(p, SOLVABLE_REQUIRES, md, repo->idarraydata + s->requires);
778 if (key || md->stop > SEARCH_NEXT_KEY)
780 case SOLVABLE_RECOMMENDS:
782 domatch_idarray(p, SOLVABLE_RECOMMENDS, md, repo->idarraydata + s->recommends);
783 if (key || md->stop > SEARCH_NEXT_KEY)
785 case SOLVABLE_SUPPLEMENTS:
787 domatch_idarray(p, SOLVABLE_SUPPLEMENTS, md, repo->idarraydata + s->supplements);
788 if (key || md->stop > SEARCH_NEXT_KEY)
790 case SOLVABLE_SUGGESTS:
792 domatch_idarray(p, SOLVABLE_SUGGESTS, md, repo->idarraydata + s->suggests);
793 if (key || md->stop > SEARCH_NEXT_KEY)
795 case SOLVABLE_ENHANCES:
797 domatch_idarray(p, SOLVABLE_ENHANCES, md, repo->idarraydata + s->enhances);
798 if (key || md->stop > SEARCH_NEXT_KEY)
800 case SOLVABLE_FRESHENS:
802 domatch_idarray(p, SOLVABLE_FRESHENS, md, repo->idarraydata + s->freshens);
803 if (key || md->stop > SEARCH_NEXT_KEY)
809 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
811 if (p < data->start || p >= data->end)
813 repodata_search(data, p - data->start, key, md);
814 if (md->stop > SEARCH_NEXT_KEY)
820 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)
824 memset(&md, 0, sizeof(md));
825 md.pool = repo->pool;
828 md.callback = callback;
829 md.callback_data = callback_data;
830 repo_search_md(repo, p, key, &md);
834 repo_lookup_str(Solvable *s, Id key)
836 Repo *repo = s->repo;
837 Pool *pool = repo->pool;
844 return id2str(pool, s->name);
846 return id2str(pool, s->arch);
848 return id2str(pool, s->evr);
849 case SOLVABLE_VENDOR:
850 return id2str(pool, s->vendor);
852 n = s - pool->solvables;
853 for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
855 if (n < data->start || n >= data->end)
857 for (j = 1; j < data->nkeys; j++)
859 if (data->keys[j].name == key && (data->keys[j].type == TYPE_ID || data->keys[j].type == TYPE_STR))
860 return repodata_lookup_str(data, n - data->start, j);
869 key_cmp (const void *pa, const void *pb)
871 Repokey *a = (Repokey *)pa;
872 Repokey *b = (Repokey *)pb;
873 return a->name - b->name;
877 repo_add_attrstore (Repo *repo, Attrstore *s, const char *location)
881 /* If this is meant to be the embedded attributes, make sure we don't
882 have them already. */
885 for (i = 0; i < repo->nrepodata; i++)
886 if (repo->repodata[i].location == 0)
888 if (i != repo->nrepodata)
890 pool_debug (repo->pool, SAT_FATAL, "embedded attribs added twice\n");
895 repo->repodata = xrealloc (repo->repodata, repo->nrepodata * sizeof (*data));
896 data = repo->repodata + repo->nrepodata - 1;
897 memset (data, 0, sizeof (*data));
899 data->nkeys = s->nkeys;
902 data->keys = xmalloc(data->nkeys * sizeof(data->keys[0]));
903 for (i = 1; i < data->nkeys; i++)
905 data->keys[i].name = s->keys[i].name;
906 data->keys[i].type = s->keys[i].type;
909 qsort(data->keys + 1, data->nkeys - 1, sizeof(data->keys[0]), key_cmp);
912 data->location = strdup(location);