2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * Manage data coming from one repository
13 * a repository can contain multiple repodata entries, consisting of
14 * different sets of keys and different sets of solvables
29 #include "poolid_private.h"
37 #define REPODATA_BLOCK 255
39 static unsigned char *data_skip_key(Repodata *data, unsigned char *dp, Repokey *key);
42 repodata_initdata(Repodata *data, Repo *repo, int localpool)
44 memset(data, 0, sizeof (*data));
45 data->repodataid = data - repo->repodata;
47 data->localpool = localpool;
49 stringpool_init_empty(&data->spool);
50 /* dirpool_init(&data->dirpool); just zeros out again */
51 data->keys = solv_calloc(1, sizeof(Repokey));
53 data->schemata = solv_calloc(1, sizeof(Id));
54 data->schemadata = solv_calloc(1, sizeof(Id));
56 data->schemadatalen = 1;
57 repopagestore_init(&data->store);
61 repodata_freedata(Repodata *data)
65 solv_free(data->keys);
67 solv_free(data->schemata);
68 solv_free(data->schemadata);
69 solv_free(data->schematahash);
71 stringpool_free(&data->spool);
72 dirpool_free(&data->dirpool);
74 solv_free(data->mainschemaoffsets);
75 solv_free(data->incoredata);
76 solv_free(data->incoreoffset);
77 solv_free(data->verticaloffset);
79 repopagestore_free(&data->store);
81 solv_free(data->vincore);
84 for (i = 0; i < data->end - data->start; i++)
85 solv_free(data->attrs[i]);
86 solv_free(data->attrs);
88 for (i = 0; i < data->nxattrs; i++)
89 solv_free(data->xattrs[i]);
90 solv_free(data->xattrs);
92 solv_free(data->attrdata);
93 solv_free(data->attriddata);
94 solv_free(data->attrnum64data);
96 solv_free(data->dircache);
100 repodata_free(Repodata *data)
102 Repo *repo = data->repo;
103 int i = data - repo->repodata;
106 repodata_freedata(data);
107 if (i < repo->nrepodata - 1)
109 /* whoa! this changes the repodataids! */
110 memmove(repo->repodata + i, repo->repodata + i + 1, (repo->nrepodata - 1 - i) * sizeof(Repodata));
111 for (; i < repo->nrepodata - 1; i++)
112 repo->repodata[i].repodataid = i;
115 if (repo->nrepodata == 1)
117 repo->repodata = solv_free(repo->repodata);
123 repodata_empty(Repodata *data, int localpool)
125 void (*loadcallback)(Repodata *) = data->loadcallback;
126 int state = data->state;
127 repodata_freedata(data);
128 repodata_initdata(data, data->repo, localpool);
130 data->loadcallback = loadcallback;
134 /***************************************************************
135 * key pool management
138 /* this is not so time critical that we need a hash, so we do a simple
141 repodata_key2id(Repodata *data, Repokey *key, int create)
145 for (keyid = 1; keyid < data->nkeys; keyid++)
146 if (data->keys[keyid].name == key->name && data->keys[keyid].type == key->type)
148 if ((key->type == REPOKEY_TYPE_CONSTANT || key->type == REPOKEY_TYPE_CONSTANTID) && key->size != data->keys[keyid].size)
152 if (keyid == data->nkeys)
156 /* allocate new key */
157 data->keys = solv_realloc2(data->keys, data->nkeys + 1, sizeof(Repokey));
158 data->keys[data->nkeys++] = *key;
159 if (data->verticaloffset)
161 data->verticaloffset = solv_realloc2(data->verticaloffset, data->nkeys, sizeof(Id));
162 data->verticaloffset[data->nkeys - 1] = 0;
164 data->keybits[(key->name >> 3) & (sizeof(data->keybits) - 1)] |= 1 << (key->name & 7);
170 /***************************************************************
171 * schema pool management
174 #define SCHEMATA_BLOCK 31
175 #define SCHEMATADATA_BLOCK 255
178 repodata_schema2id(Repodata *data, Id *schema, int create)
185 return 0; /* XXX: allow empty schema? */
186 if ((schematahash = data->schematahash) == 0)
188 data->schematahash = schematahash = solv_calloc(256, sizeof(Id));
189 for (i = 1; i < data->nschemata; i++)
191 for (sp = data->schemadata + data->schemata[i], h = 0; *sp;)
196 data->schemadata = solv_extend_resize(data->schemadata, data->schemadatalen, sizeof(Id), SCHEMATADATA_BLOCK);
197 data->schemata = solv_extend_resize(data->schemata, data->nschemata, sizeof(Id), SCHEMATA_BLOCK);
200 for (sp = schema, len = 0, h = 0; *sp; len++)
205 cid = schematahash[h];
208 if (!memcmp(data->schemadata + data->schemata[cid], schema, len * sizeof(Id)))
210 /* cache conflict, do a slow search */
211 for (cid = 1; cid < data->nschemata; cid++)
212 if (!memcmp(data->schemadata + data->schemata[cid], schema, len * sizeof(Id)))
218 data->schemadata = solv_extend(data->schemadata, data->schemadatalen, len, sizeof(Id), SCHEMATADATA_BLOCK);
219 data->schemata = solv_extend(data->schemata, data->nschemata, 1, sizeof(Id), SCHEMATA_BLOCK);
221 memcpy(data->schemadata + data->schemadatalen, schema, len * sizeof(Id));
222 data->schemata[data->nschemata] = data->schemadatalen;
223 data->schemadatalen += len;
224 schematahash[h] = data->nschemata;
226 fprintf(stderr, "schema2id: new schema\n");
228 return data->nschemata++;
232 repodata_free_schemahash(Repodata *data)
234 data->schematahash = solv_free(data->schematahash);
236 data->schemata = solv_realloc2(data->schemata, data->nschemata, sizeof(Id));
237 data->schemadata = solv_realloc2(data->schemadata, data->schemadatalen, sizeof(Id));
241 /***************************************************************
242 * dir pool management
245 #ifndef HAVE_STRCHRNUL
246 static inline const char *strchrnul(const char *str, char x)
248 const char *p = strchr(str, x);
249 return p ? p : str + strlen(str);
253 #define DIRCACHE_SIZE 41 /* < 1k */
257 Id ids[DIRCACHE_SIZE];
258 char str[(DIRCACHE_SIZE * (DIRCACHE_SIZE - 1)) / 2];
263 repodata_str2dir(Repodata *data, const char *dir, int create)
274 while (*dir == '/' && dir[1] == '/')
276 if (*dir == '/' && !dir[1])
278 if (data->dirpool.ndirs)
280 return dirpool_add_dir(&data->dirpool, 0, 1, create);
287 struct dircache *dircache = data->dircache;
291 if (l < DIRCACHE_SIZE && dircache->ids[l] && !memcmp(dircache->str + l * (l - 1) / 2, dir, l))
293 parent = dircache->ids[l];
309 dire = strchrnul(dir, '/');
311 id = stringpool_strn2id(&data->spool, dir, dire - dir, create);
313 id = pool_strn2id(data->repo->pool, dir, dire - dir, create);
316 parent = dirpool_add_dir(&data->dirpool, parent, id, create);
321 data->dircache = solv_calloc(1, sizeof(struct dircache));
325 if (l < DIRCACHE_SIZE)
327 data->dircache->ids[l] = parent;
328 memcpy(data->dircache->str + l * (l - 1) / 2, dirs, l);
342 repodata_free_dircache(Repodata *data)
344 data->dircache = solv_free(data->dircache);
348 repodata_dir2str(Repodata *data, Id did, const char *suf)
350 Pool *pool = data->repo->pool;
357 return suf ? suf : "";
361 comp = dirpool_compid(&data->dirpool, parent);
362 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
364 parent = dirpool_parent(&data->dirpool, parent);
369 l += strlen(suf) + 1;
370 p = pool_alloctmpspace(pool, l + 1) + l;
381 comp = dirpool_compid(&data->dirpool, parent);
382 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
385 strncpy(p, comps, l);
386 parent = dirpool_parent(&data->dirpool, parent);
394 /***************************************************************
398 static inline unsigned char *
399 data_skip_schema(Repodata *data, unsigned char *dp, Id schema)
401 Id *keyp = data->schemadata + data->schemata[schema];
402 for (; *keyp; keyp++)
403 dp = data_skip_key(data, dp, data->keys + *keyp);
407 static unsigned char *
408 data_skip_key(Repodata *data, unsigned char *dp, Repokey *key)
410 int nentries, schema;
413 case REPOKEY_TYPE_FIXARRAY:
414 dp = data_read_id(dp, &nentries);
417 dp = data_read_id(dp, &schema);
419 dp = data_skip_schema(data, dp, schema);
421 case REPOKEY_TYPE_FLEXARRAY:
422 dp = data_read_id(dp, &nentries);
425 dp = data_read_id(dp, &schema);
426 dp = data_skip_schema(data, dp, schema);
430 if (key->storage == KEY_STORAGE_INCORE)
431 dp = data_skip(dp, key->type);
432 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
434 dp = data_skip(dp, REPOKEY_TYPE_ID);
435 dp = data_skip(dp, REPOKEY_TYPE_ID);
441 static unsigned char *
442 forward_to_key(Repodata *data, Id keyid, Id *keyp, unsigned char *dp)
448 if (data->mainschemaoffsets && dp == data->incoredata + data->mainschemaoffsets[0] && keyp == data->schemadata + data->schemata[data->mainschema])
451 for (i = 0; (k = *keyp++) != 0; i++)
453 return data->incoredata + data->mainschemaoffsets[i];
456 while ((k = *keyp++) != 0)
460 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
462 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip offset */
463 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip length */
466 if (data->keys[k].storage != KEY_STORAGE_INCORE)
468 dp = data_skip_key(data, dp, data->keys + k);
473 static unsigned char *
474 get_vertical_data(Repodata *data, Repokey *key, Id off, Id len)
479 if (off >= data->lastverticaloffset)
481 off -= data->lastverticaloffset;
482 if ((unsigned int)off + len > data->vincorelen)
484 return data->vincore + off;
486 if ((unsigned int)off + len > key->size)
488 /* we now have the offset, go into vertical */
489 off += data->verticaloffset[key - data->keys];
490 /* fprintf(stderr, "key %d page %d\n", key->name, off / REPOPAGE_BLOBSIZE); */
491 dp = repopagestore_load_page_range(&data->store, off / REPOPAGE_BLOBSIZE, (off + len - 1) / REPOPAGE_BLOBSIZE);
494 dp += off % REPOPAGE_BLOBSIZE;
498 static inline unsigned char *
499 get_data(Repodata *data, Repokey *key, unsigned char **dpp, int advance)
501 unsigned char *dp = *dpp;
505 if (key->storage == KEY_STORAGE_INCORE)
508 *dpp = data_skip_key(data, dp, key);
511 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
514 dp = data_read_id(dp, &off);
515 dp = data_read_id(dp, &len);
518 return get_vertical_data(data, key, off, len);
524 load_repodata(Repodata *data)
526 if (data->loadcallback)
528 data->loadcallback(data);
529 if (data->state == REPODATA_AVAILABLE)
532 data->state = REPODATA_ERROR;
537 maybe_load_repodata(Repodata *data, Id keyname)
539 if (keyname && !repodata_precheck_keyname(data, keyname))
540 return 0; /* do not bother... */
547 for (i = 1; i < data->nkeys; i++)
548 if (keyname == data->keys[i].name)
550 if (i == data->nkeys)
553 return load_repodata(data);
556 case REPODATA_AVAILABLE:
557 case REPODATA_LOADING:
560 data->state = REPODATA_ERROR;
565 static inline unsigned char *
566 solvid2data(Repodata *data, Id solvid, Id *schemap)
568 unsigned char *dp = data->incoredata;
571 if (solvid == SOLVID_META)
572 dp += 1; /* offset of "meta" solvable */
573 else if (solvid == SOLVID_POS)
575 Pool *pool = data->repo->pool;
576 if (data->repo != pool->pos.repo)
578 if (data != data->repo->repodata + pool->pos.repodataid)
581 if (pool->pos.dp != 1)
583 *schemap = pool->pos.schema;
589 if (solvid < data->start || solvid >= data->end)
591 dp += data->incoreoffset[solvid - data->start];
593 return data_read_id(dp, schemap);
596 /************************************************************************
600 static unsigned char *
601 find_key_data(Repodata *data, Id solvid, Id keyname, Repokey **keypp)
604 Id schema, *keyp, *kp;
607 if (!maybe_load_repodata(data, keyname))
609 dp = solvid2data(data, solvid, &schema);
612 keyp = data->schemadata + data->schemata[schema];
613 for (kp = keyp; *kp; kp++)
614 if (data->keys[*kp].name == keyname)
618 *keypp = key = data->keys + *kp;
619 if (key->type == REPOKEY_TYPE_DELETED)
621 if (key->type == REPOKEY_TYPE_VOID || key->type == REPOKEY_TYPE_CONSTANT || key->type == REPOKEY_TYPE_CONSTANTID)
622 return dp; /* no need to forward... */
623 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
624 return 0; /* get_data will not work, no need to forward */
625 dp = forward_to_key(data, *kp, keyp, dp);
628 return get_data(data, key, &dp, 0);
632 repodata_lookup_type(Repodata *data, Id solvid, Id keyname)
634 Id schema, *keyp, *kp;
635 if (!maybe_load_repodata(data, keyname))
637 if (!solvid2data(data, solvid, &schema))
639 keyp = data->schemadata + data->schemata[schema];
640 for (kp = keyp; *kp; kp++)
641 if (data->keys[*kp].name == keyname)
642 return data->keys[*kp].type;
647 repodata_lookup_id(Repodata *data, Id solvid, Id keyname)
653 dp = find_key_data(data, solvid, keyname, &key);
656 if (key->type == REPOKEY_TYPE_CONSTANTID)
658 if (key->type != REPOKEY_TYPE_ID)
660 dp = data_read_id(dp, &id);
665 repodata_lookup_str(Repodata *data, Id solvid, Id keyname)
671 dp = find_key_data(data, solvid, keyname, &key);
674 if (key->type == REPOKEY_TYPE_STR)
675 return (const char *)dp;
676 if (key->type == REPOKEY_TYPE_CONSTANTID)
678 else if (key->type == REPOKEY_TYPE_ID)
679 dp = data_read_id(dp, &id);
683 return stringpool_id2str(&data->spool, id);
684 return pool_id2str(data->repo->pool, id);
688 repodata_lookup_num(Repodata *data, Id solvid, Id keyname, unsigned long long *value)
692 unsigned int high, low;
695 dp = find_key_data(data, solvid, keyname, &key);
700 case REPOKEY_TYPE_NUM:
701 data_read_num64(dp, &low, &high);
702 *value = (unsigned long long)high << 32 | low;
704 case REPOKEY_TYPE_U32:
705 data_read_u32(dp, &low);
708 case REPOKEY_TYPE_CONSTANT:
717 repodata_lookup_void(Repodata *data, Id solvid, Id keyname)
723 if (!maybe_load_repodata(data, keyname))
725 dp = solvid2data(data, solvid, &schema);
728 /* can't use find_key_data as we need to test the type */
729 for (keyp = data->schemadata + data->schemata[schema]; *keyp; keyp++)
730 if (data->keys[*keyp].name == keyname && data->keys[*keyp].type == REPOKEY_TYPE_VOID)
735 const unsigned char *
736 repodata_lookup_bin_checksum(Repodata *data, Id solvid, Id keyname, Id *typep)
741 dp = find_key_data(data, solvid, keyname, &key);
756 repodata_lookup_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
764 dp = find_key_data(data, solvid, keyname, &key);
767 if (key->type != REPOKEY_TYPE_IDARRAY)
771 dp = data_read_ideof(dp, &id, &eof);
780 repodata_lookup_binary(Repodata *data, Id solvid, Id keyname, int *lenp)
786 dp = find_key_data(data, solvid, keyname, &key);
787 if (!dp || key->type != REPOKEY_TYPE_BINARY)
792 dp = data_read_id(dp, &len);
798 repodata_globalize_id(Repodata *data, Id id, int create)
800 if (!id || !data || !data->localpool)
802 return pool_str2id(data->repo->pool, stringpool_id2str(&data->spool, id), create);
806 repodata_localize_id(Repodata *data, Id id, int create)
808 if (!id || !data || !data->localpool)
810 return stringpool_str2id(&data->spool, pool_id2str(data->repo->pool, id), create);
814 repodata_translate_id(Repodata *data, Repodata *fromdata, Id id, int create)
816 if (!id || !data || !fromdata)
818 if (!data->localpool || !fromdata->localpool)
820 if (fromdata->localpool)
821 id = repodata_globalize_id(fromdata, id, create);
823 id = repodata_localize_id(data, id, create);
826 /* localpool is set in both data and fromdata */
827 return stringpool_str2id(&data->spool, stringpool_id2str(&fromdata->spool, id), create);
831 repodata_lookup_id_uninternalized(Repodata *data, Id solvid, Id keyname, Id voidid)
836 ap = data->attrs[solvid - data->start];
841 if (data->keys[*ap].name != keyname)
843 if (data->keys[*ap].type == REPOKEY_TYPE_VOID)
845 if (data->keys[*ap].type == REPOKEY_TYPE_ID)
853 repodata_lookup_dirstrarray_uninternalized(Repodata *data, Id solvid, Id keyname, Id *didp, Id *iterp)
857 if (iter == 0) /* find key data */
861 ap = data->attrs[solvid - data->start];
865 if (data->keys[*ap].name == keyname && data->keys[*ap].type == REPOKEY_TYPE_DIRSTRARRAY)
872 for (ap = data->attriddata + iter; *ap; ap += 2)
874 if (did && ap[0] != did)
877 *iterp = ap - data->attriddata + 2;
878 return (const char *)data->attrdata + ap[1];
884 const unsigned char *
885 repodata_lookup_bin_checksum_uninternalized(Repodata *data, Id solvid, Id keyname, Id *typep)
890 ap = data->attrs[solvid - data->start];
895 if (data->keys[*ap].name != keyname)
897 switch (data->keys[*ap].type)
900 *typep = data->keys[*ap].type;
901 return (const unsigned char *)data->attrdata + ap[1];
909 /************************************************************************
915 repodata_stringify(Pool *pool, Repodata *data, Repokey *key, KeyValue *kv, int flags)
919 case REPOKEY_TYPE_ID:
920 case REPOKEY_TYPE_CONSTANTID:
921 case REPOKEY_TYPE_IDARRAY:
922 if (data && data->localpool)
923 kv->str = stringpool_id2str(&data->spool, kv->id);
925 kv->str = pool_id2str(pool, kv->id);
926 if ((flags & SEARCH_SKIP_KIND) != 0 && key->storage == KEY_STORAGE_SOLVABLE)
929 for (s = kv->str; *s >= 'a' && *s <= 'z'; s++)
931 if (*s == ':' && s > kv->str)
935 case REPOKEY_TYPE_STR:
937 case REPOKEY_TYPE_DIRSTRARRAY:
938 if (!(flags & SEARCH_FILES))
939 return kv->str; /* match just the basename */
941 return kv->str; /* already stringified */
942 /* Put the full filename into kv->str. */
943 kv->str = repodata_dir2str(data, kv->id, kv->str);
944 kv->num = 1; /* mark stringification */
947 if (!(flags & SEARCH_CHECKSUMS))
948 return 0; /* skip em */
950 return kv->str; /* already stringified */
951 kv->str = repodata_chk2str(data, key->type, (const unsigned char *)kv->str);
952 kv->num = 1; /* mark stringification */
960 struct subschema_data {
966 /* search a specific repodata */
968 repodata_search(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
972 Id keyid, *kp, *keyp;
973 unsigned char *dp, *ddp;
979 if (!maybe_load_repodata(data, keyname))
981 if (solvid == SOLVID_SUBSCHEMA)
983 struct subschema_data *subd = cbdata;
984 cbdata = subd->cbdata;
986 schema = subd->parent->id;
987 dp = (unsigned char *)subd->parent->str;
988 kv.parent = subd->parent;
993 dp = solvid2data(data, solvid, &schema);
996 s = data->repo->pool->solvables + solvid;
999 keyp = data->schemadata + data->schemata[schema];
1002 /* search for a specific key */
1003 for (kp = keyp; *kp; kp++)
1004 if (data->keys[*kp].name == keyname)
1008 dp = forward_to_key(data, *kp, keyp, dp);
1014 while ((keyid = *keyp++) != 0)
1017 key = data->keys + keyid;
1018 ddp = get_data(data, key, &dp, *keyp ? 1 : 0);
1020 if (key->type == REPOKEY_TYPE_DELETED)
1022 if (key->type == REPOKEY_TYPE_FLEXARRAY || key->type == REPOKEY_TYPE_FIXARRAY)
1024 struct subschema_data subd;
1028 subd.cbdata = cbdata;
1031 ddp = data_read_id(ddp, &nentries);
1035 while (ddp && nentries > 0)
1039 if (key->type == REPOKEY_TYPE_FLEXARRAY || !kv.entry)
1040 ddp = data_read_id(ddp, &schema);
1042 kv.str = (char *)ddp;
1043 stop = callback(cbdata, s, data, key, &kv);
1044 if (stop > SEARCH_NEXT_KEY)
1046 if (stop && stop != SEARCH_ENTERSUB)
1048 if ((flags & SEARCH_SUB) != 0 || stop == SEARCH_ENTERSUB)
1049 repodata_search(data, SOLVID_SUBSCHEMA, 0, flags, callback, &subd);
1050 ddp = data_skip_schema(data, ddp, schema);
1053 if (!nentries && (flags & SEARCH_ARRAYSENTINEL) != 0)
1057 kv.str = (char *)ddp;
1058 stop = callback(cbdata, s, data, key, &kv);
1059 if (stop > SEARCH_NEXT_KEY)
1069 ddp = data_fetch(ddp, &kv, key);
1072 stop = callback(cbdata, s, data, key, &kv);
1075 while (!kv.eof && !stop);
1076 if (onekey || stop > SEARCH_NEXT_KEY)
1082 repodata_setpos_kv(Repodata *data, KeyValue *kv)
1084 Pool *pool = data->repo->pool;
1086 pool_clear_pos(pool);
1089 pool->pos.repo = data->repo;
1090 pool->pos.repodataid = data - data->repo->repodata;
1091 pool->pos.dp = (unsigned char *)kv->str - data->incoredata;
1092 pool->pos.schema = kv->id;
1096 /************************************************************************
1097 * data iterator functions
1101 solvabledata_fetch(Solvable *s, KeyValue *kv, Id keyname)
1115 case SOLVABLE_VENDOR:
1118 case SOLVABLE_PROVIDES:
1120 return s->provides ? s->repo->idarraydata + s->provides : 0;
1121 case SOLVABLE_OBSOLETES:
1123 return s->obsoletes ? s->repo->idarraydata + s->obsoletes : 0;
1124 case SOLVABLE_CONFLICTS:
1126 return s->conflicts ? s->repo->idarraydata + s->conflicts : 0;
1127 case SOLVABLE_REQUIRES:
1129 return s->requires ? s->repo->idarraydata + s->requires : 0;
1130 case SOLVABLE_RECOMMENDS:
1132 return s->recommends ? s->repo->idarraydata + s->recommends : 0;
1133 case SOLVABLE_SUPPLEMENTS:
1135 return s->supplements ? s->repo->idarraydata + s->supplements : 0;
1136 case SOLVABLE_SUGGESTS:
1138 return s->suggests ? s->repo->idarraydata + s->suggests : 0;
1139 case SOLVABLE_ENHANCES:
1141 return s->enhances ? s->repo->idarraydata + s->enhances : 0;
1144 return s->repo->rpmdbid ? s->repo->rpmdbid + (s - s->repo->pool->solvables - s->repo->start) : 0;
1151 datamatcher_init(Datamatcher *ma, const char *match, int flags)
1153 match = match ? solv_strdup(match) : 0;
1158 if ((flags & SEARCH_STRINGMASK) == SEARCH_REGEX)
1160 ma->matchdata = solv_calloc(1, sizeof(regex_t));
1161 ma->error = regcomp((regex_t *)ma->matchdata, match, REG_EXTENDED | REG_NOSUB | REG_NEWLINE | ((flags & SEARCH_NOCASE) ? REG_ICASE : 0));
1164 solv_free(ma->matchdata);
1165 ma->flags = (flags & ~SEARCH_STRINGMASK) | SEARCH_ERROR;
1168 if ((flags & SEARCH_FILES) != 0 && match)
1170 /* prepare basename check */
1171 if ((flags & SEARCH_STRINGMASK) == SEARCH_STRING || (flags & SEARCH_STRINGMASK) == SEARCH_STRINGEND)
1173 const char *p = strrchr(match, '/');
1174 ma->matchdata = (void *)(p ? p + 1 : match);
1176 else if ((flags & SEARCH_STRINGMASK) == SEARCH_GLOB)
1179 for (p = match + strlen(match) - 1; p >= match; p--)
1180 if (*p == '[' || *p == ']' || *p == '*' || *p == '?' || *p == '/')
1182 ma->matchdata = (void *)(p + 1);
1189 datamatcher_free(Datamatcher *ma)
1192 ma->match = solv_free((char *)ma->match);
1193 if ((ma->flags & SEARCH_STRINGMASK) == SEARCH_REGEX && ma->matchdata)
1195 regfree(ma->matchdata);
1196 solv_free(ma->matchdata);
1202 datamatcher_match(Datamatcher *ma, const char *str)
1205 switch ((ma->flags & SEARCH_STRINGMASK))
1207 case SEARCH_SUBSTRING:
1208 if (ma->flags & SEARCH_NOCASE)
1209 return strcasestr(str, ma->match) != 0;
1211 return strstr(str, ma->match) != 0;
1213 if (ma->flags & SEARCH_NOCASE)
1214 return !strcasecmp(ma->match, str);
1216 return !strcmp(ma->match, str);
1217 case SEARCH_STRINGSTART:
1218 if (ma->flags & SEARCH_NOCASE)
1219 return !strncasecmp(ma->match, str, strlen(ma->match));
1221 return !strncmp(ma->match, str, strlen(ma->match));
1222 case SEARCH_STRINGEND:
1223 l = strlen(str) - strlen(ma->match);
1226 if (ma->flags & SEARCH_NOCASE)
1227 return !strcasecmp(ma->match, str + l);
1229 return !strcmp(ma->match, str + l);
1231 return !fnmatch(ma->match, str, (ma->flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0);
1233 return !regexec((const regex_t *)ma->matchdata, str, 0, NULL, 0);
1239 /* check if the matcher can match the provides basename */
1242 datamatcher_checkbasename(Datamatcher *ma, const char *basename)
1245 const char *match = ma->matchdata;
1248 switch (ma->flags & SEARCH_STRINGMASK)
1252 case SEARCH_STRINGEND:
1253 if (match != ma->match)
1254 break; /* had slash, do exact match on basename */
1257 /* check if the basename ends with match */
1258 l = strlen(basename) - strlen(match);
1264 return 1; /* maybe matches */
1266 if ((ma->flags & SEARCH_NOCASE) != 0)
1267 return !strcasecmp(match, basename);
1269 return !strcmp(match, basename);
1273 repodata_filelistfilter_matches(Repodata *data, const char *str)
1275 /* '.*bin\/.*', '^\/etc\/.*', '^\/usr\/lib\/sendmail$' */
1276 /* for now hardcoded */
1277 if (strstr(str, "bin/"))
1279 if (!strncmp(str, "/etc/", 5))
1281 if (!strcmp(str, "/usr/lib/sendmail"))
1303 di_nextarrayelement,
1309 di_entersolvablekey,
1313 /* see dataiterator.h for documentation */
1315 dataiterator_init(Dataiterator *di, Pool *pool, Repo *repo, Id p, Id keyname, const char *match, int flags)
1317 memset(di, 0, sizeof(*di));
1319 di->flags = flags & ~SEARCH_THISSOLVID;
1320 if (!pool || (repo && repo->pool != pool))
1328 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1334 di->keyname = keyname;
1335 di->keynames[0] = keyname;
1336 dataiterator_set_search(di, repo, p);
1341 dataiterator_init_clone(Dataiterator *di, Dataiterator *from)
1346 if (di->dupstr == di->kv.str)
1347 di->dupstr = solv_memdup(di->dupstr, di->dupstrn);
1354 memset(&di->matcher, 0, sizeof(di->matcher));
1355 if (from->matcher.match)
1356 datamatcher_init(&di->matcher, from->matcher.match, from->matcher.flags);
1361 for (i = 1; i < di->nparents; i++)
1362 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1363 di->kv.parent = &di->parents[di->nparents - 1].kv;
1368 dataiterator_set_match(Dataiterator *di, const char *match, int flags)
1370 di->flags = (flags & ~SEARCH_THISSOLVID) | (di->flags & SEARCH_THISSOLVID);
1371 datamatcher_free(&di->matcher);
1372 memset(&di->matcher, 0, sizeof(di->matcher));
1376 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1386 dataiterator_set_search(Dataiterator *di, Repo *repo, Id p)
1390 di->flags &= ~SEARCH_THISSOLVID;
1394 if (!di->pool->urepos)
1402 di->repo = di->pool->repos[di->repoid];
1404 di->state = di_enterrepo;
1406 dataiterator_jump_to_solvid(di, p);
1410 dataiterator_set_keyname(Dataiterator *di, Id keyname)
1413 di->keyname = keyname;
1414 di->keynames[0] = keyname;
1418 dataiterator_prepend_keyname(Dataiterator *di, Id keyname)
1422 if (di->nkeynames >= sizeof(di->keynames)/sizeof(*di->keynames) - 2)
1424 di->state = di_bye; /* sorry */
1427 for (i = di->nkeynames + 1; i > 0; i--)
1428 di->keynames[i] = di->keynames[i - 1];
1429 di->keynames[0] = di->keyname = keyname;
1434 dataiterator_free(Dataiterator *di)
1436 if (di->matcher.match)
1437 datamatcher_free(&di->matcher);
1439 solv_free(di->dupstr);
1442 static unsigned char *
1443 dataiterator_find_keyname(Dataiterator *di, Id keyname)
1446 Repokey *keys = di->data->keys, *key;
1449 for (keyp = di->keyp; *keyp; keyp++)
1450 if (keys[*keyp].name == keyname)
1455 if (key->type == REPOKEY_TYPE_DELETED)
1457 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
1458 return 0; /* get_data will not work, no need to forward */
1459 dp = forward_to_key(di->data, *keyp, di->keyp, di->dp);
1467 is_filelist_extension(Repodata *data)
1470 if (!repodata_precheck_keyname(data, SOLVABLE_FILELIST))
1472 for (j = 1; j < data->nkeys; j++)
1473 if (data->keys[j].name == SOLVABLE_FILELIST)
1475 if (j == data->nkeys)
1477 if (data->state != REPODATA_AVAILABLE)
1479 for (j = 1; j < data->nkeys; j++)
1480 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1486 dataiterator_filelistcheck(Dataiterator *di)
1489 int needcomplete = 0;
1490 Repodata *data = di->data;
1492 if ((di->flags & SEARCH_COMPLETE_FILELIST) != 0)
1493 if (!di->matcher.match
1494 || ((di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_STRING
1495 && (di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_GLOB)
1496 || !repodata_filelistfilter_matches(data, di->matcher.match))
1498 if (data->state != REPODATA_AVAILABLE)
1499 return needcomplete ? 1 : 0;
1502 /* we don't need the complete filelist, so ignore all stubs */
1503 if (data->repo->nrepodata == 2)
1505 for (j = 1; j < data->nkeys; j++)
1506 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1512 /* we need the complete filelist. check if we habe a filtered filelist and there's
1513 * a extension with the complete filelist later on */
1514 for (j = 1; j < data->nkeys; j++)
1515 if (data->keys[j].name == SOLVABLE_FILELIST)
1517 if (j == data->nkeys)
1518 return 0; /* does not have filelist */
1519 for (j = 1; j < data->nkeys; j++)
1520 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1522 if (j == data->nkeys)
1523 return 1; /* this is the externsion */
1524 while (data - data->repo->repodata + 1 < data->repo->nrepodata)
1527 if (is_filelist_extension(data))
1535 dataiterator_step(Dataiterator *di)
1539 if (di->state == di_nextattr && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET && di->vert_ddp && di->vert_storestate != di->data->storestate) {
1540 unsigned int ddpoff = di->ddp - di->vert_ddp;
1541 di->vert_off += ddpoff;
1542 di->vert_len -= ddpoff;
1543 di->ddp = di->vert_ddp = get_vertical_data(di->data, di->key, di->vert_off, di->vert_len);
1544 di->vert_storestate = di->data->storestate;
1546 di->state = di_nextkey;
1552 case di_enterrepo: di_enterrepo:
1553 if (!di->repo || (di->repo->disabled && !(di->flags & SEARCH_DISABLED_REPOS)))
1555 if (!(di->flags & SEARCH_THISSOLVID))
1557 di->solvid = di->repo->start - 1; /* reset solvid iterator */
1558 goto di_nextsolvable;
1562 case di_entersolvable: di_entersolvable:
1565 di->repodataid = 1; /* reset repodata iterator */
1566 if (di->solvid > 0 && !(di->flags & SEARCH_NO_STORAGE_SOLVABLE) && (!di->keyname || (di->keyname >= SOLVABLE_NAME && di->keyname <= RPM_RPMDBID)) && di->nparents - di->rootlevel == di->nkeynames)
1568 extern Repokey repo_solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1];
1570 di->key = repo_solvablekeys + (di->keyname ? di->keyname - SOLVABLE_NAME : 0);
1572 goto di_entersolvablekey;
1577 case di_enterrepodata: di_enterrepodata:
1580 if (di->repodataid >= di->repo->nrepodata)
1581 goto di_nextsolvable;
1582 di->data = di->repo->repodata + di->repodataid;
1584 if (di->repodataid && di->keyname == SOLVABLE_FILELIST && !dataiterator_filelistcheck(di))
1585 goto di_nextrepodata;
1586 if (!maybe_load_repodata(di->data, di->keyname))
1587 goto di_nextrepodata;
1588 di->dp = solvid2data(di->data, di->solvid, &schema);
1590 goto di_nextrepodata;
1591 if (di->solvid == SOLVID_POS)
1592 di->solvid = di->pool->pos.solvid;
1593 /* reset key iterator */
1594 di->keyp = di->data->schemadata + di->data->schemata[schema];
1597 case di_enterschema: di_enterschema:
1599 di->dp = dataiterator_find_keyname(di, di->keyname);
1600 if (!di->dp || !*di->keyp)
1604 goto di_nextrepodata;
1608 case di_enterkey: di_enterkey:
1610 di->key = di->data->keys + *di->keyp;
1613 /* this is get_data() modified to store vert_ data */
1614 if (di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
1617 di->dp = data_read_id(di->dp, &off);
1618 di->dp = data_read_id(di->dp, &len);
1619 di->vert_ddp = di->ddp = get_vertical_data(di->data, di->key, off, len);
1622 di->vert_storestate = di->data->storestate;
1624 else if (di->key->storage == KEY_STORAGE_INCORE)
1627 if (di->keyp[1] && (!di->keyname || (di->flags & SEARCH_SUB) != 0))
1628 di->dp = data_skip_key(di->data, di->dp, di->key);
1634 if (di->key->type == REPOKEY_TYPE_DELETED)
1636 if (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY)
1638 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1644 di->ddp = data_fetch(di->ddp, &di->kv, di->key);
1646 di->state = di_nextkey;
1648 di->state = di_nextattr;
1651 case di_nextkey: di_nextkey:
1652 if (!di->keyname && *++di->keyp)
1658 case di_nextrepodata: di_nextrepodata:
1659 if (di->repodataid && ++di->repodataid < di->repo->nrepodata)
1660 goto di_enterrepodata;
1663 case di_nextsolvable: di_nextsolvable:
1664 if (!(di->flags & SEARCH_THISSOLVID))
1667 di->solvid = di->repo->start;
1670 for (; di->solvid < di->repo->end; di->solvid++)
1672 if (di->pool->solvables[di->solvid].repo == di->repo)
1673 goto di_entersolvable;
1678 case di_nextrepo: di_nextrepo:
1683 if (di->repoid < di->pool->nrepos)
1685 di->repo = di->pool->repos[di->repoid];
1691 case di_bye: di_bye:
1695 case di_enterarray: di_enterarray:
1696 if (di->key->name == REPOSITORY_SOLVABLES)
1698 di->ddp = data_read_id(di->ddp, (Id *)&di->kv.num);
1703 case di_nextarrayelement: di_nextarrayelement:
1706 di->ddp = data_skip_schema(di->data, di->ddp, di->kv.id);
1707 if (di->kv.entry == di->kv.num)
1709 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1711 if (!(di->flags & SEARCH_ARRAYSENTINEL))
1713 di->kv.str = (char *)di->ddp;
1715 di->state = di_nextkey;
1718 if (di->kv.entry == di->kv.num - 1)
1720 if (di->key->type == REPOKEY_TYPE_FLEXARRAY || !di->kv.entry)
1721 di->ddp = data_read_id(di->ddp, &di->kv.id);
1722 di->kv.str = (char *)di->ddp;
1723 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1725 if ((di->flags & SEARCH_SUB) != 0)
1726 di->state = di_entersub;
1728 di->state = di_nextarrayelement;
1731 case di_entersub: di_entersub:
1732 if (di->nparents == sizeof(di->parents)/sizeof(*di->parents) - 1)
1733 goto di_nextarrayelement; /* sorry, full */
1734 di->parents[di->nparents].kv = di->kv;
1735 di->parents[di->nparents].dp = di->dp;
1736 di->parents[di->nparents].keyp = di->keyp;
1737 di->dp = (unsigned char *)di->kv.str;
1738 di->keyp = di->data->schemadata + di->data->schemata[di->kv.id];
1739 memset(&di->kv, 0, sizeof(di->kv));
1740 di->kv.parent = &di->parents[di->nparents].kv;
1742 di->keyname = di->keynames[di->nparents - di->rootlevel];
1743 goto di_enterschema;
1745 case di_leavesub: di_leavesub:
1746 if (di->nparents - 1 < di->rootlevel)
1749 di->dp = di->parents[di->nparents].dp;
1750 di->kv = di->parents[di->nparents].kv;
1751 di->keyp = di->parents[di->nparents].keyp;
1752 di->key = di->data->keys + *di->keyp;
1753 di->ddp = (unsigned char *)di->kv.str;
1754 di->keyname = di->keynames[di->nparents - di->rootlevel];
1755 goto di_nextarrayelement;
1757 /* special solvable attr handling follows */
1759 case di_nextsolvablekey: di_nextsolvablekey:
1760 if (di->keyname || di->key->name == RPM_RPMDBID)
1761 goto di_enterrepodata;
1765 case di_entersolvablekey: di_entersolvablekey:
1766 di->idp = solvabledata_fetch(di->pool->solvables + di->solvid, &di->kv, di->key->name);
1767 if (!di->idp || !*di->idp)
1768 goto di_nextsolvablekey;
1772 di->kv.id = *di->idp;
1773 di->kv.num = *di->idp; /* for rpmdbid */
1774 di->kv.num2 = 0; /* for rpmdbid */
1776 di->state = di_nextsolvablekey;
1782 case di_nextsolvableattr:
1783 di->state = di_nextsolvableattr;
1784 di->kv.id = *di->idp++;
1789 di->state = di_nextsolvablekey;
1795 if (di->matcher.match)
1798 /* simple pre-check so that we don't need to stringify */
1799 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->matcher.flags & SEARCH_FILES) != 0)
1800 if (!datamatcher_checkbasename(&di->matcher, di->kv.str))
1802 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
1804 if (di->keyname && (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY))
1808 if (!datamatcher_match(&di->matcher, str))
1813 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->flags & SEARCH_FILES) != 0)
1814 repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags);
1816 /* found something! */
1822 dataiterator_entersub(Dataiterator *di)
1824 if (di->state == di_nextarrayelement)
1825 di->state = di_entersub;
1829 dataiterator_setpos(Dataiterator *di)
1831 if (di->kv.eof == 2)
1833 pool_clear_pos(di->pool);
1836 di->pool->pos.solvid = di->solvid;
1837 di->pool->pos.repo = di->repo;
1838 di->pool->pos.repodataid = di->data - di->repo->repodata;
1839 di->pool->pos.schema = di->kv.id;
1840 di->pool->pos.dp = (unsigned char *)di->kv.str - di->data->incoredata;
1844 dataiterator_setpos_parent(Dataiterator *di)
1846 if (!di->kv.parent || di->kv.parent->eof == 2)
1848 pool_clear_pos(di->pool);
1851 di->pool->pos.solvid = di->solvid;
1852 di->pool->pos.repo = di->repo;
1853 di->pool->pos.repodataid = di->data - di->repo->repodata;
1854 di->pool->pos.schema = di->kv.parent->id;
1855 di->pool->pos.dp = (unsigned char *)di->kv.parent->str - di->data->incoredata;
1858 /* clones just the position, not the search keys/matcher */
1860 dataiterator_clonepos(Dataiterator *di, Dataiterator *from)
1862 di->state = from->state;
1863 di->flags &= ~SEARCH_THISSOLVID;
1864 di->flags |= (from->flags & SEARCH_THISSOLVID);
1865 di->repo = from->repo;
1866 di->data = from->data;
1868 di->ddp = from->ddp;
1869 di->idp = from->idp;
1870 di->keyp = from->keyp;
1871 di->key = from->key;
1873 di->repodataid = from->repodataid;
1874 di->solvid = from->solvid;
1875 di->repoid = from->repoid;
1876 di->rootlevel = from->rootlevel;
1877 memcpy(di->parents, from->parents, sizeof(from->parents));
1878 di->nparents = from->nparents;
1882 for (i = 1; i < di->nparents; i++)
1883 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1884 di->kv.parent = &di->parents[di->nparents - 1].kv;
1888 if (from->dupstr && from->dupstr == from->kv.str)
1890 di->dupstrn = from->dupstrn;
1891 di->dupstr = solv_memdup(from->dupstr, from->dupstrn);
1896 dataiterator_seek(Dataiterator *di, int whence)
1898 if ((whence & DI_SEEK_STAY) != 0)
1899 di->rootlevel = di->nparents;
1900 switch (whence & ~DI_SEEK_STAY)
1903 if (di->state != di_nextarrayelement)
1905 if ((whence & DI_SEEK_STAY) != 0)
1906 di->rootlevel = di->nparents + 1; /* XXX: dangerous! */
1907 di->state = di_entersub;
1909 case DI_SEEK_PARENT:
1916 if (di->rootlevel > di->nparents)
1917 di->rootlevel = di->nparents;
1918 di->dp = di->parents[di->nparents].dp;
1919 di->kv = di->parents[di->nparents].kv;
1920 di->keyp = di->parents[di->nparents].keyp;
1921 di->key = di->data->keys + *di->keyp;
1922 di->ddp = (unsigned char *)di->kv.str;
1923 di->keyname = di->keynames[di->nparents - di->rootlevel];
1924 di->state = di_nextarrayelement;
1926 case DI_SEEK_REWIND:
1932 di->dp = (unsigned char *)di->kv.parent->str;
1933 di->keyp = di->data->schemadata + di->data->schemata[di->kv.parent->id];
1934 di->state = di_enterschema;
1942 dataiterator_skip_attribute(Dataiterator *di)
1944 if (di->state == di_nextsolvableattr)
1945 di->state = di_nextsolvablekey;
1947 di->state = di_nextkey;
1951 dataiterator_skip_solvable(Dataiterator *di)
1956 di->keyname = di->keynames[0];
1957 di->state = di_nextsolvable;
1961 dataiterator_skip_repo(Dataiterator *di)
1966 di->keyname = di->keynames[0];
1967 di->state = di_nextrepo;
1971 dataiterator_jump_to_solvid(Dataiterator *di, Id solvid)
1976 di->keyname = di->keynames[0];
1977 if (solvid == SOLVID_POS)
1979 di->repo = di->pool->pos.repo;
1986 if (!di->pool->pos.repodataid && di->pool->pos.solvid == SOLVID_META) {
1987 solvid = SOLVID_META; /* META pos hack */
1989 di->data = di->repo->repodata + di->pool->pos.repodataid;
1993 else if (solvid > 0)
1995 di->repo = di->pool->solvables[solvid].repo;
2000 if (!di->pool->urepos)
2006 di->repo = di->pool->repos[di->repoid];
2008 if (solvid != SOLVID_POS)
2010 di->solvid = solvid;
2012 di->flags |= SEARCH_THISSOLVID;
2013 di->state = di_enterrepo;
2017 dataiterator_jump_to_repo(Dataiterator *di, Repo *repo)
2023 di->repoid = 0; /* 0 means stay at repo */
2026 di->flags &= ~SEARCH_THISSOLVID;
2027 di->state = di_enterrepo;
2031 dataiterator_match(Dataiterator *di, Datamatcher *ma)
2034 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
2036 return ma ? datamatcher_match(ma, str) : 1;
2040 dataiterator_strdup(Dataiterator *di)
2044 if (!di->kv.str || di->kv.str == di->dupstr)
2046 switch (di->key->type)
2049 case REPOKEY_TYPE_DIRSTRARRAY:
2050 if (di->kv.num) /* was it stringified into tmp space? */
2051 l = strlen(di->kv.str) + 1;
2056 if (l < 0 && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
2058 switch (di->key->type)
2060 case REPOKEY_TYPE_STR:
2061 case REPOKEY_TYPE_DIRSTRARRAY:
2062 l = strlen(di->kv.str) + 1;
2065 l = solv_chksum_len(di->key->type);
2067 case REPOKEY_TYPE_BINARY:
2074 if (!di->dupstrn || di->dupstrn < l)
2076 di->dupstrn = l + 16;
2077 di->dupstr = solv_realloc(di->dupstr, di->dupstrn);
2080 memcpy(di->dupstr, di->kv.str, l);
2081 di->kv.str = di->dupstr;
2085 /************************************************************************
2086 * data modify functions
2089 /* extend repodata so that it includes solvables p */
2091 repodata_extend(Repodata *data, Id p)
2093 if (data->start == data->end)
2094 data->start = data->end = p;
2097 int old = data->end - data->start;
2098 int new = p - data->end + 1;
2101 data->attrs = solv_extend(data->attrs, old, new, sizeof(Id *), REPODATA_BLOCK);
2102 memset(data->attrs + old, 0, new * sizeof(Id *));
2104 data->incoreoffset = solv_extend(data->incoreoffset, old, new, sizeof(Id), REPODATA_BLOCK);
2105 memset(data->incoreoffset + old, 0, new * sizeof(Id));
2108 if (p < data->start)
2110 int old = data->end - data->start;
2111 int new = data->start - p;
2114 data->attrs = solv_extend_resize(data->attrs, old + new, sizeof(Id *), REPODATA_BLOCK);
2115 memmove(data->attrs + new, data->attrs, old * sizeof(Id *));
2116 memset(data->attrs, 0, new * sizeof(Id *));
2118 data->incoreoffset = solv_extend_resize(data->incoreoffset, old + new, sizeof(Id), REPODATA_BLOCK);
2119 memmove(data->incoreoffset + new, data->incoreoffset, old * sizeof(Id));
2120 memset(data->incoreoffset, 0, new * sizeof(Id));
2125 /* shrink end of repodata */
2127 repodata_shrink(Repodata *data, int end)
2131 if (data->end <= end)
2133 if (data->start >= end)
2137 for (i = 0; i < data->end - data->start; i++)
2138 solv_free(data->attrs[i]);
2139 data->attrs = solv_free(data->attrs);
2141 data->incoreoffset = solv_free(data->incoreoffset);
2142 data->start = data->end = 0;
2147 for (i = end; i < data->end; i++)
2148 solv_free(data->attrs[i - data->start]);
2149 data->attrs = solv_extend_resize(data->attrs, end - data->start, sizeof(Id *), REPODATA_BLOCK);
2151 if (data->incoreoffset)
2152 data->incoreoffset = solv_extend_resize(data->incoreoffset, end - data->start, sizeof(Id), REPODATA_BLOCK);
2156 /* extend repodata so that it includes solvables from start to start + num - 1 */
2158 repodata_extend_block(Repodata *data, Id start, Id num)
2162 if (!data->incoreoffset)
2164 /* this also means that data->attrs is NULL */
2165 data->incoreoffset = solv_calloc_block(num, sizeof(Id), REPODATA_BLOCK);
2166 data->start = start;
2167 data->end = start + num;
2170 repodata_extend(data, start);
2172 repodata_extend(data, start + num - 1);
2175 /**********************************************************************/
2178 #define REPODATA_ATTRS_BLOCK 31
2179 #define REPODATA_ATTRDATA_BLOCK 1023
2180 #define REPODATA_ATTRIDDATA_BLOCK 63
2181 #define REPODATA_ATTRNUM64DATA_BLOCK 15
2185 repodata_new_handle(Repodata *data)
2189 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2190 data->nxattrs = 2; /* -1: SOLVID_META */
2192 data->xattrs = solv_extend(data->xattrs, data->nxattrs, 1, sizeof(Id *), REPODATA_BLOCK);
2193 data->xattrs[data->nxattrs] = 0;
2194 return -(data->nxattrs++);
2198 repodata_get_attrp(Repodata *data, Id handle)
2202 if (handle == SOLVID_META && !data->xattrs)
2204 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2207 return data->xattrs - handle;
2209 if (handle < data->start || handle >= data->end)
2210 repodata_extend(data, handle);
2212 data->attrs = solv_calloc_block(data->end - data->start, sizeof(Id *), REPODATA_BLOCK);
2213 return data->attrs + (handle - data->start);
2217 repodata_insert_keyid(Repodata *data, Id handle, Id keyid, Id val, int overwrite)
2223 app = repodata_get_attrp(data, handle);
2228 /* Determine equality based on the name only, allows us to change
2229 type (when overwrite is set), and makes TYPE_CONSTANT work. */
2230 for (pp = ap; *pp; pp += 2)
2231 if (data->keys[*pp].name == data->keys[keyid].name)
2235 if (overwrite || data->keys[*pp].type == REPOKEY_TYPE_DELETED)
2244 ap = solv_extend(ap, i, 3, sizeof(Id), REPODATA_ATTRS_BLOCK);
2254 repodata_set(Repodata *data, Id solvid, Repokey *key, Id val)
2258 keyid = repodata_key2id(data, key, 1);
2259 repodata_insert_keyid(data, solvid, keyid, val, 1);
2263 repodata_set_id(Repodata *data, Id solvid, Id keyname, Id id)
2267 key.type = REPOKEY_TYPE_ID;
2269 key.storage = KEY_STORAGE_INCORE;
2270 repodata_set(data, solvid, &key, id);
2274 repodata_set_num(Repodata *data, Id solvid, Id keyname, unsigned long long num)
2278 key.type = REPOKEY_TYPE_NUM;
2280 key.storage = KEY_STORAGE_INCORE;
2281 if (num >= 0x80000000)
2283 data->attrnum64data = solv_extend(data->attrnum64data, data->attrnum64datalen, 1, sizeof(unsigned long long), REPODATA_ATTRNUM64DATA_BLOCK);
2284 data->attrnum64data[data->attrnum64datalen] = num;
2285 num = 0x80000000 | data->attrnum64datalen++;
2287 repodata_set(data, solvid, &key, (Id)num);
2291 repodata_set_poolstr(Repodata *data, Id solvid, Id keyname, const char *str)
2295 if (data->localpool)
2296 id = stringpool_str2id(&data->spool, str, 1);
2298 id = pool_str2id(data->repo->pool, str, 1);
2300 key.type = REPOKEY_TYPE_ID;
2302 key.storage = KEY_STORAGE_INCORE;
2303 repodata_set(data, solvid, &key, id);
2307 repodata_set_constant(Repodata *data, Id solvid, Id keyname, unsigned int constant)
2311 key.type = REPOKEY_TYPE_CONSTANT;
2312 key.size = constant;
2313 key.storage = KEY_STORAGE_INCORE;
2314 repodata_set(data, solvid, &key, 0);
2318 repodata_set_constantid(Repodata *data, Id solvid, Id keyname, Id id)
2322 key.type = REPOKEY_TYPE_CONSTANTID;
2324 key.storage = KEY_STORAGE_INCORE;
2325 repodata_set(data, solvid, &key, 0);
2329 repodata_set_void(Repodata *data, Id solvid, Id keyname)
2333 key.type = REPOKEY_TYPE_VOID;
2335 key.storage = KEY_STORAGE_INCORE;
2336 repodata_set(data, solvid, &key, 0);
2340 repodata_set_str(Repodata *data, Id solvid, Id keyname, const char *str)
2345 l = strlen(str) + 1;
2347 key.type = REPOKEY_TYPE_STR;
2349 key.storage = KEY_STORAGE_INCORE;
2350 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2351 memcpy(data->attrdata + data->attrdatalen, str, l);
2352 repodata_set(data, solvid, &key, data->attrdatalen);
2353 data->attrdatalen += l;
2357 repodata_set_binary(Repodata *data, Id solvid, Id keyname, void *buf, int len)
2365 key.type = REPOKEY_TYPE_BINARY;
2367 key.storage = KEY_STORAGE_INCORE;
2368 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, len + 5, 1, REPODATA_ATTRDATA_BLOCK);
2369 dp = data->attrdata + data->attrdatalen;
2370 if (len >= (1 << 14))
2372 if (len >= (1 << 28))
2373 *dp++ = (len >> 28) | 128;
2374 if (len >= (1 << 21))
2375 *dp++ = (len >> 21) | 128;
2376 *dp++ = (len >> 14) | 128;
2378 if (len >= (1 << 7))
2379 *dp++ = (len >> 7) | 128;
2382 memcpy(dp, buf, len);
2383 repodata_set(data, solvid, &key, data->attrdatalen);
2384 data->attrdatalen = dp + len - data->attrdata;
2387 /* add an array element consisting of entrysize Ids to the repodata. modifies attriddata
2388 * so that the caller can append entrysize new elements plus the termination zero there */
2390 repodata_add_array(Repodata *data, Id handle, Id keyname, Id keytype, int entrysize)
2393 Id *ida, *pp, **ppp;
2395 /* check if it is the same as last time, this speeds things up a lot */
2396 if (handle == data->lasthandle && data->keys[data->lastkey].name == keyname && data->keys[data->lastkey].type == keytype && data->attriddatalen == data->lastdatalen)
2398 /* great! just append the new data */
2399 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2400 data->attriddatalen--; /* overwrite terminating 0 */
2401 data->lastdatalen += entrysize;
2405 ppp = repodata_get_attrp(data, handle);
2409 for (; *pp; pp += 2)
2410 if (data->keys[*pp].name == keyname)
2413 if (!pp || !*pp || data->keys[*pp].type != keytype)
2415 /* not found. allocate new key */
2421 key.storage = KEY_STORAGE_INCORE;
2422 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2423 keyid = repodata_key2id(data, &key, 1);
2424 repodata_insert_keyid(data, handle, keyid, data->attriddatalen, 1);
2425 data->lasthandle = handle;
2426 data->lastkey = keyid;
2427 data->lastdatalen = data->attriddatalen + entrysize + 1;
2431 for (ida = data->attriddata + pp[1]; *ida; ida += entrysize)
2432 oldsize += entrysize;
2433 if (ida + 1 == data->attriddata + data->attriddatalen)
2435 /* this was the last entry, just append it */
2436 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2437 data->attriddatalen--; /* overwrite terminating 0 */
2441 /* too bad. move to back. */
2442 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, oldsize + entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2443 memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
2444 pp[1] = data->attriddatalen;
2445 data->attriddatalen += oldsize;
2447 data->lasthandle = handle;
2448 data->lastkey = *pp;
2449 data->lastdatalen = data->attriddatalen + entrysize + 1;
2453 repodata_set_bin_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2454 const unsigned char *str)
2459 if (!(l = solv_chksum_len(type)))
2464 key.storage = KEY_STORAGE_INCORE;
2465 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2466 memcpy(data->attrdata + data->attrdatalen, str, l);
2467 repodata_set(data, solvid, &key, data->attrdatalen);
2468 data->attrdatalen += l;
2472 repodata_set_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2475 unsigned char buf[64];
2478 if (!(l = solv_chksum_len(type)))
2480 if (l > sizeof(buf) || solv_hex2bin(&str, buf, l) != l)
2482 repodata_set_bin_checksum(data, solvid, keyname, type, buf);
2486 repodata_chk2str(Repodata *data, Id type, const unsigned char *buf)
2490 if (!(l = solv_chksum_len(type)))
2492 return pool_bin2hex(data->repo->pool, buf, l);
2495 /* rpm filenames don't contain the epoch, so strip it */
2496 static inline const char *
2497 evrid2vrstr(Pool *pool, Id evrid)
2499 const char *p, *evr = pool_id2str(pool, evrid);
2502 for (p = evr; *p >= '0' && *p <= '9'; p++)
2504 return p != evr && *p == ':' && p[1] ? p + 1 : evr;
2508 repodata_set_poolstrn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2511 if (data->localpool)
2512 id = stringpool_strn2id(&data->spool, str, l, 1);
2514 id = pool_strn2id(data->repo->pool, str, l, 1);
2515 repodata_set_id(data, solvid, keyname, id);
2519 repodata_set_strn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2522 repodata_set_str(data, solvid, keyname, str);
2525 char *s = solv_strdup(str);
2527 repodata_set_str(data, solvid, keyname, s);
2533 repodata_set_location(Repodata *data, Id solvid, int medianr, const char *dir, const char *file)
2535 Pool *pool = data->repo->pool;
2537 const char *str, *fp;
2541 repodata_set_constant(data, solvid, SOLVABLE_MEDIANR, medianr);
2544 if ((dir = strrchr(file, '/')) != 0)
2555 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2560 if (l == 1 && dir[0] == '.')
2562 s = pool->solvables + solvid;
2565 str = pool_id2str(pool, s->arch);
2566 if (!strncmp(dir, str, l) && !str[l])
2567 repodata_set_void(data, solvid, SOLVABLE_MEDIADIR);
2569 repodata_set_strn(data, solvid, SOLVABLE_MEDIADIR, dir, l);
2572 str = pool_id2str(pool, s->name);
2574 if ((!l || !strncmp(fp, str, l)) && fp[l] == '-')
2577 str = evrid2vrstr(pool, s->evr);
2579 if ((!l || !strncmp(fp, str, l)) && fp[l] == '.')
2582 str = pool_id2str(pool, s->arch);
2584 if ((!l || !strncmp(fp, str, l)) && !strcmp(fp + l, ".rpm"))
2586 repodata_set_void(data, solvid, SOLVABLE_MEDIAFILE);
2591 repodata_set_str(data, solvid, SOLVABLE_MEDIAFILE, file);
2594 /* XXX: medianr is currently not stored */
2596 repodata_set_deltalocation(Repodata *data, Id handle, int medianr, const char *dir, const char *file)
2599 const char *evr, *suf, *s;
2603 if ((dir = strrchr(file, '/')) != 0)
2614 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2619 if (l == 1 && dir[0] == '.')
2622 repodata_set_poolstrn(data, handle, DELTA_LOCATION_DIR, dir, l);
2623 evr = strchr(file, '-');
2626 for (s = evr - 1; s > file; s--)
2633 suf = strrchr(file, '.');
2636 for (s = suf - 1; s > file; s--)
2642 if (!strcmp(suf, ".delta.rpm") || !strcmp(suf, ".patch.rpm"))
2644 /* We accept one more item as suffix. */
2645 for (s = suf - 1; s > file; s--)
2655 if (suf && evr && suf < evr)
2657 repodata_set_poolstrn(data, handle, DELTA_LOCATION_NAME, file, evr ? evr - file : strlen(file));
2659 repodata_set_poolstrn(data, handle, DELTA_LOCATION_EVR, evr + 1, suf ? suf - evr - 1: strlen(evr + 1));
2661 repodata_set_poolstr(data, handle, DELTA_LOCATION_SUFFIX, suf + 1);
2665 repodata_set_sourcepkg(Repodata *data, Id solvid, const char *sourcepkg)
2667 Pool *pool = data->repo->pool;
2668 Solvable *s = pool->solvables + solvid;
2669 const char *p, *sevr, *sarch, *name, *evr;
2671 p = strrchr(sourcepkg, '.');
2672 if (!p || strcmp(p, ".rpm") != 0)
2675 repodata_set_str(data, solvid, SOLVABLE_SOURCENAME, sourcepkg);
2679 while (p > sourcepkg && *p != '.')
2681 if (*p != '.' || p == sourcepkg)
2684 while (p > sourcepkg && *p != '-')
2686 if (*p != '-' || p == sourcepkg)
2689 while (p > sourcepkg && *p != '-')
2691 if (*p != '-' || p == sourcepkg)
2694 pool = s->repo->pool;
2696 name = pool_id2str(pool, s->name);
2697 if (name && !strncmp(sourcepkg, name, sevr - sourcepkg - 1) && name[sevr - sourcepkg - 1] == 0)
2698 repodata_set_void(data, solvid, SOLVABLE_SOURCENAME);
2700 repodata_set_id(data, solvid, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcepkg, sevr - sourcepkg - 1, 1));
2702 evr = evrid2vrstr(pool, s->evr);
2703 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
2704 repodata_set_void(data, solvid, SOLVABLE_SOURCEEVR);
2706 repodata_set_id(data, solvid, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
2708 if (!strcmp(sarch, "src.rpm"))
2709 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_SRC);
2710 else if (!strcmp(sarch, "nosrc.rpm"))
2711 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
2713 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
2717 repodata_set_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
2723 key.type = REPOKEY_TYPE_IDARRAY;
2725 key.storage = KEY_STORAGE_INCORE;
2726 repodata_set(data, solvid, &key, data->attriddatalen);
2727 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, q->count + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2728 for (i = 0; i < q->count; i++)
2729 data->attriddata[data->attriddatalen++] = q->elements[i];
2730 data->attriddata[data->attriddatalen++] = 0;
2734 repodata_add_dirnumnum(Repodata *data, Id solvid, Id keyname, Id dir, Id num, Id num2)
2738 fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", solvid, dir, num, num2, data->attriddatalen);
2740 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRNUMNUMARRAY, 3);
2741 data->attriddata[data->attriddatalen++] = dir;
2742 data->attriddata[data->attriddatalen++] = num;
2743 data->attriddata[data->attriddatalen++] = num2;
2744 data->attriddata[data->attriddatalen++] = 0;
2748 repodata_add_dirstr(Repodata *data, Id solvid, Id keyname, Id dir, const char *str)
2754 l = strlen(str) + 1;
2755 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2756 memcpy(data->attrdata + data->attrdatalen, str, l);
2757 stroff = data->attrdatalen;
2758 data->attrdatalen += l;
2761 fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", solvid, dir, str, data->attriddatalen);
2763 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRSTRARRAY, 2);
2764 data->attriddata[data->attriddatalen++] = dir;
2765 data->attriddata[data->attriddatalen++] = stroff;
2766 data->attriddata[data->attriddatalen++] = 0;
2770 repodata_add_idarray(Repodata *data, Id solvid, Id keyname, Id id)
2773 fprintf(stderr, "repodata_add_idarray %d %d (%d)\n", solvid, id, data->attriddatalen);
2775 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_IDARRAY, 1);
2776 data->attriddata[data->attriddatalen++] = id;
2777 data->attriddata[data->attriddatalen++] = 0;
2781 repodata_add_poolstr_array(Repodata *data, Id solvid, Id keyname,
2785 if (data->localpool)
2786 id = stringpool_str2id(&data->spool, str, 1);
2788 id = pool_str2id(data->repo->pool, str, 1);
2789 repodata_add_idarray(data, solvid, keyname, id);
2793 repodata_add_fixarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2795 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FIXARRAY, 1);
2796 data->attriddata[data->attriddatalen++] = ghandle;
2797 data->attriddata[data->attriddatalen++] = 0;
2801 repodata_add_flexarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2803 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FLEXARRAY, 1);
2804 data->attriddata[data->attriddatalen++] = ghandle;
2805 data->attriddata[data->attriddatalen++] = 0;
2809 repodata_unset_uninternalized(Repodata *data, Id solvid, Id keyname)
2812 app = repodata_get_attrp(data, solvid);
2816 for (; *ap; ap += 2)
2817 if (data->keys[*ap].name == keyname)
2823 for (; *ap; ap += 2)
2825 if (data->keys[*ap].name == keyname)
2833 /* XXX: does not work correctly, needs fix in iterators! */
2835 repodata_unset(Repodata *data, Id solvid, Id keyname)
2839 key.type = REPOKEY_TYPE_DELETED;
2841 key.storage = KEY_STORAGE_INCORE;
2842 repodata_set(data, solvid, &key, 0);
2845 /* add all (uninternalized) attrs from src to dest */
2847 repodata_merge_attrs(Repodata *data, Id dest, Id src)
2850 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2852 for (; *keyp; keyp += 2)
2853 repodata_insert_keyid(data, dest, keyp[0], keyp[1], 0);
2856 /* add some (uninternalized) attrs from src to dest */
2858 repodata_merge_some_attrs(Repodata *data, Id dest, Id src, Map *keyidmap, int overwrite)
2861 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2863 for (; *keyp; keyp += 2)
2864 if (!keyidmap || MAPTST(keyidmap, keyp[0]))
2865 repodata_insert_keyid(data, dest, keyp[0], keyp[1], overwrite);
2868 /* swap (uninternalized) attrs from src and dest */
2870 repodata_swap_attrs(Repodata *data, Id dest, Id src)
2873 if (!data->attrs || dest == src)
2875 if (dest < data->start || dest >= data->end)
2876 repodata_extend(data, dest);
2877 if (src < data->start || src >= data->end)
2878 repodata_extend(data, src);
2879 tmpattrs = data->attrs[dest - data->start];
2880 data->attrs[dest - data->start] = data->attrs[src - data->start];
2881 data->attrs[src - data->start] = tmpattrs;
2885 /**********************************************************************/
2887 /* TODO: unify with repo_write and repo_solv! */
2889 #define EXTDATA_BLOCK 1023
2897 data_addid(struct extdata *xd, Id sx)
2899 unsigned int x = (unsigned int)sx;
2902 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2903 dp = xd->buf + xd->len;
2908 *dp++ = (x >> 28) | 128;
2910 *dp++ = (x >> 21) | 128;
2911 *dp++ = (x >> 14) | 128;
2914 *dp++ = (x >> 7) | 128;
2916 xd->len = dp - xd->buf;
2920 data_addid64(struct extdata *xd, unsigned long long x)
2922 if (x >= 0x100000000)
2926 data_addid(xd, (Id)(x >> 35));
2927 xd->buf[xd->len - 1] |= 128;
2929 data_addid(xd, (Id)((unsigned int)x | 0x80000000));
2930 xd->buf[xd->len - 5] = (x >> 28) | 128;
2933 data_addid(xd, (Id)x);
2937 data_addideof(struct extdata *xd, Id sx, int eof)
2939 unsigned int x = (unsigned int)sx;
2942 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2943 dp = xd->buf + xd->len;
2948 *dp++ = (x >> 27) | 128;
2950 *dp++ = (x >> 20) | 128;
2951 *dp++ = (x >> 13) | 128;
2954 *dp++ = (x >> 6) | 128;
2955 *dp++ = eof ? (x & 63) : (x & 63) | 64;
2956 xd->len = dp - xd->buf;
2960 data_addblob(struct extdata *xd, unsigned char *blob, int len)
2962 xd->buf = solv_extend(xd->buf, xd->len, len, 1, EXTDATA_BLOCK);
2963 memcpy(xd->buf + xd->len, blob, len);
2967 /*********************************/
2969 /* this is to reduct memory usage when internalizing oversized repos */
2971 compact_attrdata(Repodata *data, int entry, int nentry)
2974 unsigned int attrdatastart = data->attrdatalen;
2975 unsigned int attriddatastart = data->attriddatalen;
2976 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
2978 for (i = entry; i < nentry; i++)
2980 Id v, *attrs = data->attrs[i];
2983 for (; *attrs; attrs += 2)
2985 switch (data->keys[*attrs].type)
2987 case REPOKEY_TYPE_STR:
2988 case REPOKEY_TYPE_BINARY:
2990 if ((unsigned int)attrs[1] < attrdatastart)
2991 attrdatastart = attrs[1];
2993 case REPOKEY_TYPE_DIRSTRARRAY:
2994 for (v = attrs[1]; data->attriddata[v] ; v += 2)
2995 if (data->attriddata[v + 1] < attrdatastart)
2996 attrdatastart = data->attriddata[v + 1];
2998 case REPOKEY_TYPE_IDARRAY:
2999 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3000 if ((unsigned int)attrs[1] < attriddatastart)
3001 attriddatastart = attrs[1];
3003 case REPOKEY_TYPE_FIXARRAY:
3004 case REPOKEY_TYPE_FLEXARRAY:
3012 printf("compact_attrdata %d %d\n", entry, nentry);
3013 printf("attrdatastart: %d\n", attrdatastart);
3014 printf("attriddatastart: %d\n", attriddatastart);
3016 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
3018 for (i = entry; i < nentry; i++)
3020 Id v, *attrs = data->attrs[i];
3023 for (; *attrs; attrs += 2)
3025 switch (data->keys[*attrs].type)
3027 case REPOKEY_TYPE_STR:
3028 case REPOKEY_TYPE_BINARY:
3030 attrs[1] -= attrdatastart;
3032 case REPOKEY_TYPE_DIRSTRARRAY:
3033 for (v = attrs[1]; data->attriddata[v] ; v += 2)
3034 data->attriddata[v + 1] -= attrdatastart;
3036 case REPOKEY_TYPE_IDARRAY:
3037 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3038 attrs[1] -= attriddatastart;
3047 data->attrdatalen -= attrdatastart;
3048 memmove(data->attrdata, data->attrdata + attrdatastart, data->attrdatalen);
3049 data->attrdata = solv_extend_resize(data->attrdata, data->attrdatalen, 1, REPODATA_ATTRDATA_BLOCK);
3051 if (attriddatastart)
3053 data->attriddatalen -= attriddatastart;
3054 memmove(data->attriddata, data->attriddata + attriddatastart, data->attriddatalen * sizeof(Id));
3055 data->attriddata = solv_extend_resize(data->attriddata, data->attriddatalen, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
3059 /* internalalize some key into incore/vincore data */
3062 repodata_serialize_key(Repodata *data, struct extdata *newincore,
3063 struct extdata *newvincore,
3065 Repokey *key, Id val)
3069 unsigned int oldvincorelen = 0;
3073 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3076 oldvincorelen = xd->len;
3080 case REPOKEY_TYPE_VOID:
3081 case REPOKEY_TYPE_CONSTANT:
3082 case REPOKEY_TYPE_CONSTANTID:
3083 case REPOKEY_TYPE_DELETED:
3085 case REPOKEY_TYPE_STR:
3086 data_addblob(xd, data->attrdata + val, strlen((char *)(data->attrdata + val)) + 1);
3088 case REPOKEY_TYPE_MD5:
3089 data_addblob(xd, data->attrdata + val, SIZEOF_MD5);
3091 case REPOKEY_TYPE_SHA1:
3092 data_addblob(xd, data->attrdata + val, SIZEOF_SHA1);
3094 case REPOKEY_TYPE_SHA224:
3095 data_addblob(xd, data->attrdata + val, SIZEOF_SHA224);
3097 case REPOKEY_TYPE_SHA256:
3098 data_addblob(xd, data->attrdata + val, SIZEOF_SHA256);
3100 case REPOKEY_TYPE_SHA384:
3101 data_addblob(xd, data->attrdata + val, SIZEOF_SHA384);
3103 case REPOKEY_TYPE_SHA512:
3104 data_addblob(xd, data->attrdata + val, SIZEOF_SHA512);
3106 case REPOKEY_TYPE_NUM:
3107 if (val & 0x80000000)
3109 data_addid64(xd, data->attrnum64data[val ^ 0x80000000]);
3113 case REPOKEY_TYPE_ID:
3114 case REPOKEY_TYPE_DIR:
3115 data_addid(xd, val);
3117 case REPOKEY_TYPE_BINARY:
3120 unsigned char *dp = data_read_id(data->attrdata + val, &len);
3121 dp += (unsigned int)len;
3122 data_addblob(xd, data->attrdata + val, dp - (data->attrdata + val));
3125 case REPOKEY_TYPE_IDARRAY:
3126 for (ida = data->attriddata + val; *ida; ida++)
3127 data_addideof(xd, ida[0], ida[1] ? 0 : 1);
3129 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3130 for (ida = data->attriddata + val; *ida; ida += 3)
3132 data_addid(xd, ida[0]);
3133 data_addid(xd, ida[1]);
3134 data_addideof(xd, ida[2], ida[3] ? 0 : 1);
3137 case REPOKEY_TYPE_DIRSTRARRAY:
3138 for (ida = data->attriddata + val; *ida; ida += 2)
3140 data_addideof(xd, ida[0], ida[2] ? 0 : 1);
3141 data_addblob(xd, data->attrdata + ida[1], strlen((char *)(data->attrdata + ida[1])) + 1);
3144 case REPOKEY_TYPE_FIXARRAY:
3148 for (ida = data->attriddata + val; *ida; ida++)
3152 kp = data->xattrs[-*ida];
3154 continue; /* ignore empty elements */
3156 for (; *kp; kp += 2)
3160 schemaid = repodata_schema2id(data, schema, 1);
3161 else if (schemaid != repodata_schema2id(data, schema, 0))
3163 pool_debug(data->repo->pool, SOLV_ERROR, "repodata_serialize_key: fixarray substructs with different schemas\n");
3168 data_addid(xd, num);
3171 data_addid(xd, schemaid);
3172 for (ida = data->attriddata + val; *ida; ida++)
3174 Id *kp = data->xattrs[-*ida];
3177 for (; *kp; kp += 2)
3178 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3182 case REPOKEY_TYPE_FLEXARRAY:
3185 for (ida = data->attriddata + val; *ida; ida++)
3187 data_addid(xd, num);
3188 for (ida = data->attriddata + val; *ida; ida++)
3190 Id *kp = data->xattrs[-*ida];
3193 data_addid(xd, 0); /* XXX */
3200 schemaid = repodata_schema2id(data, schema, 1);
3201 data_addid(xd, schemaid);
3202 kp = data->xattrs[-*ida];
3204 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3209 pool_debug(data->repo->pool, SOLV_FATAL, "repodata_serialize_key: don't know how to handle type %d\n", key->type);
3212 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3214 /* put offset/len in incore */
3215 data_addid(newincore, data->lastverticaloffset + oldvincorelen);
3216 oldvincorelen = xd->len - oldvincorelen;
3217 data_addid(newincore, oldvincorelen);
3221 /* create a circular linked list of all keys that share
3222 * the same keyname */
3224 calculate_keylink(Repodata *data)
3228 Id maxkeyname = 0, *keytable = 0;
3229 link = solv_calloc(data->nkeys, sizeof(Id));
3230 if (data->nkeys <= 2)
3232 for (i = 1; i < data->nkeys; i++)
3234 Id n = data->keys[i].name;
3235 if (n >= maxkeyname)
3237 keytable = solv_realloc2(keytable, n + 128, sizeof(Id));
3238 memset(keytable + maxkeyname, 0, (n + 128 - maxkeyname) * sizeof(Id));
3239 maxkeyname = n + 128;
3249 /* remove links that just point to themselfs */
3250 for (i = 1; i < data->nkeys; i++)
3253 solv_free(keytable);
3258 repodata_internalize(Repodata *data)
3260 Repokey *key, solvkey;
3262 Id schemaid, keyid, *schema, *sp, oldschemaid, *keyp, *seen;
3263 Offset *oldincoreoffs = 0;
3265 unsigned char *dp, *ndp;
3267 struct extdata newincore;
3268 struct extdata newvincore;
3273 if (!data->attrs && !data->xattrs)
3277 printf("repodata_internalize %d\n", data->repodataid);
3278 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3279 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3281 newvincore.buf = data->vincore;
3282 newvincore.len = data->vincorelen;
3284 /* find the solvables key, create if needed */
3285 memset(&solvkey, 0, sizeof(solvkey));
3286 solvkey.name = REPOSITORY_SOLVABLES;
3287 solvkey.type = REPOKEY_TYPE_FLEXARRAY;
3289 solvkey.storage = KEY_STORAGE_INCORE;
3290 solvkeyid = repodata_key2id(data, &solvkey, data->end != data->start ? 1 : 0);
3292 schema = solv_malloc2(data->nkeys, sizeof(Id));
3293 seen = solv_malloc2(data->nkeys, sizeof(Id));
3295 /* Merge the data already existing (in data->schemata, ->incoredata and
3296 friends) with the new attributes in data->attrs[]. */
3297 nentry = data->end - data->start;
3298 memset(&newincore, 0, sizeof(newincore));
3299 data_addid(&newincore, 0); /* start data at offset 1 */
3301 data->mainschema = 0;
3302 data->mainschemaoffsets = solv_free(data->mainschemaoffsets);
3304 keylink = calculate_keylink(data);
3305 /* join entry data */
3306 /* we start with the meta data, entry -1 */
3307 for (entry = -1; entry < nentry; entry++)
3310 dp = data->incoredata;
3313 dp += entry >= 0 ? data->incoreoffset[entry] : 1;
3314 dp = data_read_id(dp, &oldschemaid);
3316 memset(seen, 0, data->nkeys * sizeof(Id));
3318 fprintf(stderr, "oldschemaid %d\n", oldschemaid);
3319 fprintf(stderr, "schemata %d\n", data->schemata[oldschemaid]);
3320 fprintf(stderr, "schemadata %p\n", data->schemadata);
3323 /* seen: -1: old data, 0: skipped, >0: id + 1 */
3327 for (keyp = data->schemadata + data->schemata[oldschemaid]; *keyp; keyp++)
3331 /* oops, should not happen */
3335 seen[*keyp] = -1; /* use old marker */
3338 haveoldkl = 1; /* potential keylink conflict */
3341 /* strip solvables key */
3342 if (entry < 0 && solvkeyid && seen[solvkeyid])
3345 for (sp = keyp = schema; *sp; sp++)
3346 if (*sp != solvkeyid)
3349 seen[solvkeyid] = 0;
3353 /* add new entries */
3355 keyp = data->attrs ? data->attrs[entry] : 0;
3357 keyp = data->xattrs ? data->xattrs[1] : 0;
3359 for (; *keyp; keyp += 2)
3365 if (haveoldkl && keylink[*keyp]) /* this should be pretty rare */
3368 for (kl = keylink[*keyp]; kl != *keyp; kl = keylink[kl])
3371 /* replacing old key kl, remove from schema and seen */
3373 for (osp = schema; osp < sp; osp++)
3376 memmove(osp, osp + 1, (sp - osp) * sizeof(Id));
3384 seen[*keyp] = keyp[1] + 1;
3387 /* add solvables key if needed */
3388 if (entry < 0 && data->end != data->start)
3390 *sp++ = solvkeyid; /* always last in schema */
3397 /* Ideally we'd like to sort the new schema here, to ensure
3398 schema equality independend of the ordering. */
3399 schemaid = repodata_schema2id(data, schema, 1);
3401 schemaid = oldschemaid;
3405 data->mainschemaoffsets = solv_calloc(sp - schema, sizeof(Id));
3406 data->mainschema = schemaid;
3409 /* find offsets in old incore data */
3413 for (sp = data->schemadata + data->schemata[oldschemaid]; *sp; sp++)
3414 if (seen[*sp] == -1)
3415 lastneeded = sp + 1;
3419 oldincoreoffs = solv_malloc2(data->nkeys, 2 * sizeof(Offset));
3420 for (sp = data->schemadata + data->schemata[oldschemaid]; sp != lastneeded; sp++)
3422 /* Skip the data associated with this old key. */
3423 key = data->keys + *sp;
3425 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3427 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3428 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3430 else if (key->storage == KEY_STORAGE_INCORE)
3431 ndp = data_skip_key(data, ndp, key);
3432 oldincoreoffs[*sp * 2] = dp - data->incoredata;
3433 oldincoreoffs[*sp * 2 + 1] = ndp - dp;
3439 /* just copy over the complete old entry (including the schemaid) if there was no new data */
3440 if (entry >= 0 && !neednewschema && oldschemaid && (!data->attrs || !data->attrs[entry]) && dp)
3442 ndp = data->incoredata + data->incoreoffset[entry];
3443 data->incoreoffset[entry] = newincore.len;
3444 data_addblob(&newincore, ndp, dp - ndp);
3448 /* Now create data blob. We walk through the (possibly new) schema
3449 and either copy over old data, or insert the new. */
3451 data->incoreoffset[entry] = newincore.len;
3452 data_addid(&newincore, schemaid);
3454 /* we don't use a pointer to the schemadata here as repodata_serialize_key
3455 * may call repodata_schema2id() which might realloc our schemadata */
3456 for (schemaidx = data->schemata[schemaid]; (keyid = data->schemadata[schemaidx]) != 0; schemaidx++)
3460 data->mainschemaoffsets[schemaidx - data->schemata[schemaid]] = newincore.len;
3461 if (keyid == solvkeyid)
3463 /* add flexarray entry count */
3464 data_addid(&newincore, data->end - data->start);
3465 break; /* always the last entry */
3468 if (seen[keyid] == -1)
3470 if (oldincoreoffs[keyid * 2 + 1])
3471 data_addblob(&newincore, data->incoredata + oldincoreoffs[keyid * 2], oldincoreoffs[keyid * 2 + 1]);
3473 else if (seen[keyid])
3474 repodata_serialize_key(data, &newincore, &newvincore, schema, data->keys + keyid, seen[keyid] - 1);
3479 if (entry >= 0 && data->attrs)
3481 if (data->attrs[entry])
3482 data->attrs[entry] = solv_free(data->attrs[entry]);
3483 if (entry && entry % 4096 == 0 && data->nxattrs <= 2 && entry + 64 < nentry)
3485 compact_attrdata(data, entry + 1, nentry); /* try to free some memory */
3487 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3488 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3489 printf(" incore data: %d K\n", newincore.len / 1024);
3490 printf(" sum: %d K\n", (newincore.len + data->attrdatalen + data->attriddatalen * 4) / 1024);
3491 /* malloc_stats(); */
3496 /* free all xattrs */
3497 for (entry = 0; entry < data->nxattrs; entry++)
3498 if (data->xattrs[entry])
3499 solv_free(data->xattrs[entry]);
3500 data->xattrs = solv_free(data->xattrs);
3503 data->lasthandle = 0;
3505 data->lastdatalen = 0;
3509 solv_free(oldincoreoffs);
3510 repodata_free_schemahash(data);
3512 solv_free(data->incoredata);
3513 data->incoredata = newincore.buf;
3514 data->incoredatalen = newincore.len;
3515 data->incoredatafree = 0;
3517 solv_free(data->vincore);
3518 data->vincore = newvincore.buf;
3519 data->vincorelen = newvincore.len;
3521 data->attrs = solv_free(data->attrs);
3522 data->attrdata = solv_free(data->attrdata);
3523 data->attriddata = solv_free(data->attriddata);
3524 data->attrnum64data = solv_free(data->attrnum64data);
3525 data->attrdatalen = 0;
3526 data->attriddatalen = 0;
3527 data->attrnum64datalen = 0;
3529 printf("repodata_internalize %d done\n", data->repodataid);
3530 printf(" incore data: %d K\n", data->incoredatalen / 1024);
3535 repodata_disable_paging(Repodata *data)
3537 if (maybe_load_repodata(data, 0))
3539 repopagestore_disable_paging(&data->store);
3545 repodata_load_stub(Repodata *data)
3547 Repo *repo = data->repo;
3548 Pool *pool = repo->pool;
3550 struct _Pool_tmpspace oldtmpspace;
3553 if (!pool->loadcallback)
3555 data->state = REPODATA_ERROR;
3558 data->state = REPODATA_LOADING;
3560 /* save tmp space and pos */
3561 oldtmpspace = pool->tmpspace;
3562 memset(&pool->tmpspace, 0, sizeof(pool->tmpspace));
3565 r = pool->loadcallback(pool, data, pool->loadcallbackdata);
3567 /* restore tmp space and pos */
3568 for (i = 0; i < POOL_TMPSPACEBUF; i++)
3569 solv_free(pool->tmpspace.buf[i]);
3570 pool->tmpspace = oldtmpspace;
3571 if (r && oldpos.repo == repo && oldpos.repodataid == data->repodataid)
3572 memset(&oldpos, 0, sizeof(oldpos));
3575 data->state = r ? REPODATA_AVAILABLE : REPODATA_ERROR;
3579 repodata_add_stubkey(Repodata *data, Id keyname, Id keytype)
3583 xkey.name = keyname;
3584 xkey.type = keytype;
3585 xkey.storage = KEY_STORAGE_INCORE;
3587 repodata_key2id(data, &xkey, 1);
3591 repodata_add_stub(Repodata **datap)
3593 Repodata *data = *datap;
3594 Repo *repo = data->repo;
3595 Id repodataid = data - repo->repodata;
3596 Repodata *sdata = repo_add_repodata(repo, 0);
3597 data = repo->repodata + repodataid;
3598 if (data->end > data->start)
3599 repodata_extend_block(sdata, data->start, data->end - data->start);
3600 sdata->state = REPODATA_STUB;
3601 sdata->loadcallback = repodata_load_stub;
3607 repodata_create_stubs(Repodata *data)
3609 Repo *repo = data->repo;
3610 Pool *pool = repo->pool;
3617 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3618 while (dataiterator_step(&di))
3619 if (di.data == data)
3621 dataiterator_free(&di);
3624 stubdataids = solv_calloc(cnt, sizeof(*stubdataids));
3625 for (i = 0; i < cnt; i++)
3627 sdata = repodata_add_stub(&data);
3628 stubdataids[i] = sdata - repo->repodata;
3631 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3633 while (dataiterator_step(&di))
3635 if (di.data != data)
3637 if (di.key->name == REPOSITORY_EXTERNAL && !di.nparents)
3639 dataiterator_entersub(&di);
3640 sdata = repo->repodata + stubdataids[i++];
3644 switch (di.key->type)
3646 case REPOKEY_TYPE_ID:
3647 repodata_set_id(sdata, SOLVID_META, di.key->name, di.kv.id);
3649 case REPOKEY_TYPE_CONSTANTID:
3650 repodata_set_constantid(sdata, SOLVID_META, di.key->name, di.kv.id);
3652 case REPOKEY_TYPE_STR:
3653 repodata_set_str(sdata, SOLVID_META, di.key->name, di.kv.str);
3655 case REPOKEY_TYPE_VOID:
3656 repodata_set_void(sdata, SOLVID_META, di.key->name);
3658 case REPOKEY_TYPE_NUM:
3659 repodata_set_num(sdata, SOLVID_META, di.key->name, SOLV_KV_NUM64(&di.kv));
3662 repodata_set_bin_checksum(sdata, SOLVID_META, di.key->name, di.key->type, (const unsigned char *)di.kv.str);
3664 case REPOKEY_TYPE_IDARRAY:
3665 repodata_add_idarray(sdata, SOLVID_META, di.key->name, di.kv.id);
3666 if (di.key->name == REPOSITORY_KEYS)
3671 xkeyname = di.kv.id;
3675 repodata_add_stubkey(sdata, xkeyname, di.kv.id);
3684 dataiterator_free(&di);
3685 for (i = 0; i < cnt; i++)
3686 repodata_internalize(repo->repodata + stubdataids[i]);
3687 solv_free(stubdataids);
3692 repodata_memused(Repodata *data)
3694 return data->incoredatalen + data->vincorelen;