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)
272 return data->dirpool.ndirs ? 0 : dirpool_add_dir(&data->dirpool, 0, 0, create);
273 while (*dir == '/' && dir[1] == '/')
275 if (*dir == '/' && !dir[1])
276 return data->dirpool.ndirs ? 1 : dirpool_add_dir(&data->dirpool, 0, 1, create);
283 struct dircache *dircache = data->dircache;
287 if (l < DIRCACHE_SIZE && dircache->ids[l] && !memcmp(dircache->str + l * (l - 1) / 2, dir, l))
289 parent = dircache->ids[l];
305 dire = strchrnul(dir, '/');
307 id = stringpool_strn2id(&data->spool, dir, dire - dir, create);
309 id = pool_strn2id(data->repo->pool, dir, dire - dir, create);
312 parent = dirpool_add_dir(&data->dirpool, parent, id, create);
317 data->dircache = solv_calloc(1, sizeof(struct dircache));
321 if (l < DIRCACHE_SIZE)
323 data->dircache->ids[l] = parent;
324 memcpy(data->dircache->str + l * (l - 1) / 2, dirs, l);
338 repodata_free_dircache(Repodata *data)
340 data->dircache = solv_free(data->dircache);
344 repodata_dir2str(Repodata *data, Id did, const char *suf)
346 Pool *pool = data->repo->pool;
353 return suf ? suf : "";
357 comp = dirpool_compid(&data->dirpool, parent);
358 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
360 parent = dirpool_parent(&data->dirpool, parent);
365 l += strlen(suf) + 1;
366 p = pool_alloctmpspace(pool, l + 1) + l;
377 comp = dirpool_compid(&data->dirpool, parent);
378 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
381 strncpy(p, comps, l);
382 parent = dirpool_parent(&data->dirpool, parent);
390 /***************************************************************
394 static inline unsigned char *
395 data_skip_schema(Repodata *data, unsigned char *dp, Id schema)
397 Id *keyp = data->schemadata + data->schemata[schema];
398 for (; *keyp; keyp++)
399 dp = data_skip_key(data, dp, data->keys + *keyp);
403 static unsigned char *
404 data_skip_key(Repodata *data, unsigned char *dp, Repokey *key)
406 int nentries, schema;
409 case REPOKEY_TYPE_FIXARRAY:
410 dp = data_read_id(dp, &nentries);
413 dp = data_read_id(dp, &schema);
415 dp = data_skip_schema(data, dp, schema);
417 case REPOKEY_TYPE_FLEXARRAY:
418 dp = data_read_id(dp, &nentries);
421 dp = data_read_id(dp, &schema);
422 dp = data_skip_schema(data, dp, schema);
426 if (key->storage == KEY_STORAGE_INCORE)
427 dp = data_skip(dp, key->type);
428 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
430 dp = data_skip(dp, REPOKEY_TYPE_ID);
431 dp = data_skip(dp, REPOKEY_TYPE_ID);
437 static unsigned char *
438 forward_to_key(Repodata *data, Id keyid, Id *keyp, unsigned char *dp)
444 if (data->mainschemaoffsets && dp == data->incoredata + data->mainschemaoffsets[0] && keyp == data->schemadata + data->schemata[data->mainschema])
447 for (i = 0; (k = *keyp++) != 0; i++)
449 return data->incoredata + data->mainschemaoffsets[i];
452 while ((k = *keyp++) != 0)
456 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
458 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip offset */
459 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip length */
462 if (data->keys[k].storage != KEY_STORAGE_INCORE)
464 dp = data_skip_key(data, dp, data->keys + k);
469 static unsigned char *
470 get_vertical_data(Repodata *data, Repokey *key, Id off, Id len)
475 if (off >= data->lastverticaloffset)
477 off -= data->lastverticaloffset;
478 if ((unsigned int)off + len > data->vincorelen)
480 return data->vincore + off;
482 if ((unsigned int)off + len > key->size)
484 /* we now have the offset, go into vertical */
485 off += data->verticaloffset[key - data->keys];
486 /* fprintf(stderr, "key %d page %d\n", key->name, off / REPOPAGE_BLOBSIZE); */
487 dp = repopagestore_load_page_range(&data->store, off / REPOPAGE_BLOBSIZE, (off + len - 1) / REPOPAGE_BLOBSIZE);
490 dp += off % REPOPAGE_BLOBSIZE;
494 static inline unsigned char *
495 get_data(Repodata *data, Repokey *key, unsigned char **dpp, int advance)
497 unsigned char *dp = *dpp;
501 if (key->storage == KEY_STORAGE_INCORE)
504 *dpp = data_skip_key(data, dp, key);
507 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
510 dp = data_read_id(dp, &off);
511 dp = data_read_id(dp, &len);
514 return get_vertical_data(data, key, off, len);
520 load_repodata(Repodata *data)
522 if (data->loadcallback)
524 data->loadcallback(data);
525 if (data->state == REPODATA_AVAILABLE)
528 data->state = REPODATA_ERROR;
533 maybe_load_repodata(Repodata *data, Id keyname)
535 if (keyname && !repodata_precheck_keyname(data, keyname))
536 return 0; /* do not bother... */
543 for (i = 1; i < data->nkeys; i++)
544 if (keyname == data->keys[i].name)
546 if (i == data->nkeys)
549 return load_repodata(data);
552 case REPODATA_AVAILABLE:
553 case REPODATA_LOADING:
556 data->state = REPODATA_ERROR;
561 static inline unsigned char *
562 solvid2data(Repodata *data, Id solvid, Id *schemap)
564 unsigned char *dp = data->incoredata;
567 if (solvid == SOLVID_META)
568 dp += 1; /* offset of "meta" solvable */
569 else if (solvid == SOLVID_POS)
571 Pool *pool = data->repo->pool;
572 if (data->repo != pool->pos.repo)
574 if (data != data->repo->repodata + pool->pos.repodataid)
577 if (pool->pos.dp != 1)
579 *schemap = pool->pos.schema;
585 if (solvid < data->start || solvid >= data->end)
587 dp += data->incoreoffset[solvid - data->start];
589 return data_read_id(dp, schemap);
592 /************************************************************************
596 static unsigned char *
597 find_key_data(Repodata *data, Id solvid, Id keyname, Repokey **keypp)
600 Id schema, *keyp, *kp;
603 if (!maybe_load_repodata(data, keyname))
605 dp = solvid2data(data, solvid, &schema);
608 keyp = data->schemadata + data->schemata[schema];
609 for (kp = keyp; *kp; kp++)
610 if (data->keys[*kp].name == keyname)
614 *keypp = key = data->keys + *kp;
615 if (key->type == REPOKEY_TYPE_DELETED)
617 if (key->type == REPOKEY_TYPE_VOID || key->type == REPOKEY_TYPE_CONSTANT || key->type == REPOKEY_TYPE_CONSTANTID)
618 return dp; /* no need to forward... */
619 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
620 return 0; /* get_data will not work, no need to forward */
621 dp = forward_to_key(data, *kp, keyp, dp);
624 return get_data(data, key, &dp, 0);
628 repodata_lookup_type(Repodata *data, Id solvid, Id keyname)
630 Id schema, *keyp, *kp;
631 if (!maybe_load_repodata(data, keyname))
633 if (!solvid2data(data, solvid, &schema))
635 keyp = data->schemadata + data->schemata[schema];
636 for (kp = keyp; *kp; kp++)
637 if (data->keys[*kp].name == keyname)
638 return data->keys[*kp].type;
643 repodata_lookup_id(Repodata *data, Id solvid, Id keyname)
649 dp = find_key_data(data, solvid, keyname, &key);
652 if (key->type == REPOKEY_TYPE_CONSTANTID)
654 if (key->type != REPOKEY_TYPE_ID)
656 dp = data_read_id(dp, &id);
661 repodata_lookup_str(Repodata *data, Id solvid, Id keyname)
667 dp = find_key_data(data, solvid, keyname, &key);
670 if (key->type == REPOKEY_TYPE_STR)
671 return (const char *)dp;
672 if (key->type == REPOKEY_TYPE_CONSTANTID)
674 else if (key->type == REPOKEY_TYPE_ID)
675 dp = data_read_id(dp, &id);
679 return stringpool_id2str(&data->spool, id);
680 return pool_id2str(data->repo->pool, id);
684 repodata_lookup_num(Repodata *data, Id solvid, Id keyname, unsigned long long *value)
688 unsigned int high, low;
691 dp = find_key_data(data, solvid, keyname, &key);
696 case REPOKEY_TYPE_NUM:
697 data_read_num64(dp, &low, &high);
698 *value = (unsigned long long)high << 32 | low;
700 case REPOKEY_TYPE_U32:
701 data_read_u32(dp, &low);
704 case REPOKEY_TYPE_CONSTANT:
713 repodata_lookup_void(Repodata *data, Id solvid, Id keyname)
719 if (!maybe_load_repodata(data, keyname))
721 dp = solvid2data(data, solvid, &schema);
724 /* can't use find_key_data as we need to test the type */
725 for (keyp = data->schemadata + data->schemata[schema]; *keyp; keyp++)
726 if (data->keys[*keyp].name == keyname && data->keys[*keyp].type == REPOKEY_TYPE_VOID)
731 const unsigned char *
732 repodata_lookup_bin_checksum(Repodata *data, Id solvid, Id keyname, Id *typep)
737 dp = find_key_data(data, solvid, keyname, &key);
752 repodata_lookup_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
760 dp = find_key_data(data, solvid, keyname, &key);
763 if (key->type != REPOKEY_TYPE_IDARRAY)
767 dp = data_read_ideof(dp, &id, &eof);
776 repodata_lookup_binary(Repodata *data, Id solvid, Id keyname, int *lenp)
782 dp = find_key_data(data, solvid, keyname, &key);
783 if (!dp || key->type != REPOKEY_TYPE_BINARY)
788 dp = data_read_id(dp, &len);
794 repodata_globalize_id(Repodata *data, Id id, int create)
796 if (!id || !data || !data->localpool)
798 return pool_str2id(data->repo->pool, stringpool_id2str(&data->spool, id), create);
802 repodata_localize_id(Repodata *data, Id id, int create)
804 if (!id || !data || !data->localpool)
806 return stringpool_str2id(&data->spool, pool_id2str(data->repo->pool, id), create);
810 repodata_translate_id(Repodata *data, Repodata *fromdata, Id id, int create)
812 if (!id || !data || !fromdata)
814 if (!data->localpool || !fromdata->localpool)
816 if (fromdata->localpool)
817 id = repodata_globalize_id(fromdata, id, create);
819 id = repodata_localize_id(data, id, create);
822 /* localpool is set in both data and fromdata */
823 return stringpool_str2id(&data->spool, stringpool_id2str(&fromdata->spool, id), create);
827 repodata_lookup_id_uninternalized(Repodata *data, Id solvid, Id keyname, Id voidid)
832 ap = data->attrs[solvid - data->start];
837 if (data->keys[*ap].name != keyname)
839 if (data->keys[*ap].type == REPOKEY_TYPE_VOID)
841 if (data->keys[*ap].type == REPOKEY_TYPE_ID)
849 repodata_lookup_dirstrarray_uninternalized(Repodata *data, Id solvid, Id keyname, Id *didp, Id *iterp)
853 if (iter == 0) /* find key data */
857 ap = data->attrs[solvid - data->start];
861 if (data->keys[*ap].name == keyname && data->keys[*ap].type == REPOKEY_TYPE_DIRSTRARRAY)
868 for (ap = data->attriddata + iter; *ap; ap += 2)
870 if (did && ap[0] != did)
873 *iterp = ap - data->attriddata + 2;
874 return (const char *)data->attrdata + ap[1];
880 const unsigned char *
881 repodata_lookup_bin_checksum_uninternalized(Repodata *data, Id solvid, Id keyname, Id *typep)
886 ap = data->attrs[solvid - data->start];
891 if (data->keys[*ap].name != keyname)
893 switch (data->keys[*ap].type)
896 *typep = data->keys[*ap].type;
897 return (const unsigned char *)data->attrdata + ap[1];
905 /************************************************************************
911 repodata_stringify(Pool *pool, Repodata *data, Repokey *key, KeyValue *kv, int flags)
915 case REPOKEY_TYPE_ID:
916 case REPOKEY_TYPE_CONSTANTID:
917 case REPOKEY_TYPE_IDARRAY:
918 if (data && data->localpool)
919 kv->str = stringpool_id2str(&data->spool, kv->id);
921 kv->str = pool_id2str(pool, kv->id);
922 if ((flags & SEARCH_SKIP_KIND) != 0 && key->storage == KEY_STORAGE_SOLVABLE)
925 for (s = kv->str; *s >= 'a' && *s <= 'z'; s++)
927 if (*s == ':' && s > kv->str)
931 case REPOKEY_TYPE_STR:
933 case REPOKEY_TYPE_DIRSTRARRAY:
934 if (!(flags & SEARCH_FILES))
935 return kv->str; /* match just the basename */
937 return kv->str; /* already stringified */
938 /* Put the full filename into kv->str. */
939 kv->str = repodata_dir2str(data, kv->id, kv->str);
940 kv->num = 1; /* mark stringification */
943 if (!(flags & SEARCH_CHECKSUMS))
944 return 0; /* skip em */
946 return kv->str; /* already stringified */
947 kv->str = repodata_chk2str(data, key->type, (const unsigned char *)kv->str);
948 kv->num = 1; /* mark stringification */
956 struct subschema_data {
962 /* search a specific repodata */
964 repodata_search(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
968 Id keyid, *kp, *keyp;
969 unsigned char *dp, *ddp;
975 if (!maybe_load_repodata(data, keyname))
977 if (solvid == SOLVID_SUBSCHEMA)
979 struct subschema_data *subd = cbdata;
980 cbdata = subd->cbdata;
982 schema = subd->parent->id;
983 dp = (unsigned char *)subd->parent->str;
984 kv.parent = subd->parent;
989 dp = solvid2data(data, solvid, &schema);
992 s = data->repo->pool->solvables + solvid;
995 keyp = data->schemadata + data->schemata[schema];
998 /* search for a specific key */
999 for (kp = keyp; *kp; kp++)
1000 if (data->keys[*kp].name == keyname)
1004 dp = forward_to_key(data, *kp, keyp, dp);
1010 while ((keyid = *keyp++) != 0)
1013 key = data->keys + keyid;
1014 ddp = get_data(data, key, &dp, *keyp ? 1 : 0);
1016 if (key->type == REPOKEY_TYPE_DELETED)
1018 if (key->type == REPOKEY_TYPE_FLEXARRAY || key->type == REPOKEY_TYPE_FIXARRAY)
1020 struct subschema_data subd;
1024 subd.cbdata = cbdata;
1027 ddp = data_read_id(ddp, &nentries);
1031 while (ddp && nentries > 0)
1035 if (key->type == REPOKEY_TYPE_FLEXARRAY || !kv.entry)
1036 ddp = data_read_id(ddp, &schema);
1038 kv.str = (char *)ddp;
1039 stop = callback(cbdata, s, data, key, &kv);
1040 if (stop > SEARCH_NEXT_KEY)
1042 if (stop && stop != SEARCH_ENTERSUB)
1044 if ((flags & SEARCH_SUB) != 0 || stop == SEARCH_ENTERSUB)
1045 repodata_search(data, SOLVID_SUBSCHEMA, 0, flags, callback, &subd);
1046 ddp = data_skip_schema(data, ddp, schema);
1049 if (!nentries && (flags & SEARCH_ARRAYSENTINEL) != 0)
1053 kv.str = (char *)ddp;
1054 stop = callback(cbdata, s, data, key, &kv);
1055 if (stop > SEARCH_NEXT_KEY)
1065 ddp = data_fetch(ddp, &kv, key);
1068 stop = callback(cbdata, s, data, key, &kv);
1071 while (!kv.eof && !stop);
1072 if (onekey || stop > SEARCH_NEXT_KEY)
1078 repodata_setpos_kv(Repodata *data, KeyValue *kv)
1080 Pool *pool = data->repo->pool;
1082 pool_clear_pos(pool);
1085 pool->pos.repo = data->repo;
1086 pool->pos.repodataid = data - data->repo->repodata;
1087 pool->pos.dp = (unsigned char *)kv->str - data->incoredata;
1088 pool->pos.schema = kv->id;
1092 /************************************************************************
1093 * data iterator functions
1097 solvabledata_fetch(Solvable *s, KeyValue *kv, Id keyname)
1111 case SOLVABLE_VENDOR:
1114 case SOLVABLE_PROVIDES:
1116 return s->provides ? s->repo->idarraydata + s->provides : 0;
1117 case SOLVABLE_OBSOLETES:
1119 return s->obsoletes ? s->repo->idarraydata + s->obsoletes : 0;
1120 case SOLVABLE_CONFLICTS:
1122 return s->conflicts ? s->repo->idarraydata + s->conflicts : 0;
1123 case SOLVABLE_REQUIRES:
1125 return s->requires ? s->repo->idarraydata + s->requires : 0;
1126 case SOLVABLE_RECOMMENDS:
1128 return s->recommends ? s->repo->idarraydata + s->recommends : 0;
1129 case SOLVABLE_SUPPLEMENTS:
1131 return s->supplements ? s->repo->idarraydata + s->supplements : 0;
1132 case SOLVABLE_SUGGESTS:
1134 return s->suggests ? s->repo->idarraydata + s->suggests : 0;
1135 case SOLVABLE_ENHANCES:
1137 return s->enhances ? s->repo->idarraydata + s->enhances : 0;
1140 return s->repo->rpmdbid ? s->repo->rpmdbid + (s - s->repo->pool->solvables - s->repo->start) : 0;
1147 datamatcher_init(Datamatcher *ma, const char *match, int flags)
1149 match = match ? solv_strdup(match) : 0;
1154 if ((flags & SEARCH_STRINGMASK) == SEARCH_REGEX)
1156 ma->matchdata = solv_calloc(1, sizeof(regex_t));
1157 ma->error = regcomp((regex_t *)ma->matchdata, match, REG_EXTENDED | REG_NOSUB | REG_NEWLINE | ((flags & SEARCH_NOCASE) ? REG_ICASE : 0));
1160 solv_free(ma->matchdata);
1161 ma->flags = (flags & ~SEARCH_STRINGMASK) | SEARCH_ERROR;
1164 if ((flags & SEARCH_FILES) != 0 && match)
1166 /* prepare basename check */
1167 if ((flags & SEARCH_STRINGMASK) == SEARCH_STRING || (flags & SEARCH_STRINGMASK) == SEARCH_STRINGEND)
1169 const char *p = strrchr(match, '/');
1170 ma->matchdata = (void *)(p ? p + 1 : match);
1172 else if ((flags & SEARCH_STRINGMASK) == SEARCH_GLOB)
1175 for (p = match + strlen(match) - 1; p >= match; p--)
1176 if (*p == '[' || *p == ']' || *p == '*' || *p == '?' || *p == '/')
1178 ma->matchdata = (void *)(p + 1);
1185 datamatcher_free(Datamatcher *ma)
1188 ma->match = solv_free((char *)ma->match);
1189 if ((ma->flags & SEARCH_STRINGMASK) == SEARCH_REGEX && ma->matchdata)
1191 regfree(ma->matchdata);
1192 solv_free(ma->matchdata);
1198 datamatcher_match(Datamatcher *ma, const char *str)
1201 switch ((ma->flags & SEARCH_STRINGMASK))
1203 case SEARCH_SUBSTRING:
1204 if (ma->flags & SEARCH_NOCASE)
1205 return strcasestr(str, ma->match) != 0;
1207 return strstr(str, ma->match) != 0;
1209 if (ma->flags & SEARCH_NOCASE)
1210 return !strcasecmp(ma->match, str);
1212 return !strcmp(ma->match, str);
1213 case SEARCH_STRINGSTART:
1214 if (ma->flags & SEARCH_NOCASE)
1215 return !strncasecmp(ma->match, str, strlen(ma->match));
1217 return !strncmp(ma->match, str, strlen(ma->match));
1218 case SEARCH_STRINGEND:
1219 l = strlen(str) - strlen(ma->match);
1222 if (ma->flags & SEARCH_NOCASE)
1223 return !strcasecmp(ma->match, str + l);
1225 return !strcmp(ma->match, str + l);
1227 return !fnmatch(ma->match, str, (ma->flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0);
1229 return !regexec((const regex_t *)ma->matchdata, str, 0, NULL, 0);
1235 /* check if the matcher can match the provides basename */
1238 datamatcher_checkbasename(Datamatcher *ma, const char *basename)
1241 const char *match = ma->matchdata;
1244 switch (ma->flags & SEARCH_STRINGMASK)
1248 case SEARCH_STRINGEND:
1249 if (match != ma->match)
1250 break; /* had slash, do exact match on basename */
1253 /* check if the basename ends with match */
1254 l = strlen(basename) - strlen(match);
1260 return 1; /* maybe matches */
1262 if ((ma->flags & SEARCH_NOCASE) != 0)
1263 return !strcasecmp(match, basename);
1265 return !strcmp(match, basename);
1269 repodata_filelistfilter_matches(Repodata *data, const char *str)
1271 /* '.*bin\/.*', '^\/etc\/.*', '^\/usr\/lib\/sendmail$' */
1272 /* for now hardcoded */
1273 if (strstr(str, "bin/"))
1275 if (!strncmp(str, "/etc/", 5))
1277 if (!strcmp(str, "/usr/lib/sendmail"))
1299 di_nextarrayelement,
1305 di_entersolvablekey,
1309 /* see dataiterator.h for documentation */
1311 dataiterator_init(Dataiterator *di, Pool *pool, Repo *repo, Id p, Id keyname, const char *match, int flags)
1313 memset(di, 0, sizeof(*di));
1315 di->flags = flags & ~SEARCH_THISSOLVID;
1316 if (!pool || (repo && repo->pool != pool))
1324 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1330 di->keyname = keyname;
1331 di->keynames[0] = keyname;
1332 dataiterator_set_search(di, repo, p);
1337 dataiterator_init_clone(Dataiterator *di, Dataiterator *from)
1342 if (di->dupstr == di->kv.str)
1343 di->dupstr = solv_memdup(di->dupstr, di->dupstrn);
1350 memset(&di->matcher, 0, sizeof(di->matcher));
1351 if (from->matcher.match)
1352 datamatcher_init(&di->matcher, from->matcher.match, from->matcher.flags);
1357 for (i = 1; i < di->nparents; i++)
1358 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1359 di->kv.parent = &di->parents[di->nparents - 1].kv;
1364 dataiterator_set_match(Dataiterator *di, const char *match, int flags)
1366 di->flags = (flags & ~SEARCH_THISSOLVID) | (di->flags & SEARCH_THISSOLVID);
1367 datamatcher_free(&di->matcher);
1368 memset(&di->matcher, 0, sizeof(di->matcher));
1372 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1382 dataiterator_set_search(Dataiterator *di, Repo *repo, Id p)
1386 di->flags &= ~SEARCH_THISSOLVID;
1390 if (!di->pool->urepos)
1398 di->repo = di->pool->repos[di->repoid];
1400 di->state = di_enterrepo;
1402 dataiterator_jump_to_solvid(di, p);
1406 dataiterator_set_keyname(Dataiterator *di, Id keyname)
1409 di->keyname = keyname;
1410 di->keynames[0] = keyname;
1414 dataiterator_prepend_keyname(Dataiterator *di, Id keyname)
1418 if (di->nkeynames >= sizeof(di->keynames)/sizeof(*di->keynames) - 2)
1420 di->state = di_bye; /* sorry */
1423 for (i = di->nkeynames + 1; i > 0; i--)
1424 di->keynames[i] = di->keynames[i - 1];
1425 di->keynames[0] = di->keyname = keyname;
1430 dataiterator_free(Dataiterator *di)
1432 if (di->matcher.match)
1433 datamatcher_free(&di->matcher);
1435 solv_free(di->dupstr);
1438 static unsigned char *
1439 dataiterator_find_keyname(Dataiterator *di, Id keyname)
1442 Repokey *keys = di->data->keys, *key;
1445 for (keyp = di->keyp; *keyp; keyp++)
1446 if (keys[*keyp].name == keyname)
1451 if (key->type == REPOKEY_TYPE_DELETED)
1453 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
1454 return 0; /* get_data will not work, no need to forward */
1455 dp = forward_to_key(di->data, *keyp, di->keyp, di->dp);
1463 is_filelist_extension(Repodata *data)
1466 if (!repodata_precheck_keyname(data, SOLVABLE_FILELIST))
1468 for (j = 1; j < data->nkeys; j++)
1469 if (data->keys[j].name == SOLVABLE_FILELIST)
1471 if (j == data->nkeys)
1473 if (data->state != REPODATA_AVAILABLE)
1475 for (j = 1; j < data->nkeys; j++)
1476 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1482 dataiterator_filelistcheck(Dataiterator *di)
1485 int needcomplete = 0;
1486 Repodata *data = di->data;
1488 if ((di->flags & SEARCH_COMPLETE_FILELIST) != 0)
1489 if (!di->matcher.match
1490 || ((di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_STRING
1491 && (di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_GLOB)
1492 || !repodata_filelistfilter_matches(data, di->matcher.match))
1494 if (data->state != REPODATA_AVAILABLE)
1495 return needcomplete ? 1 : 0;
1498 /* we don't need the complete filelist, so ignore all stubs */
1499 if (data->repo->nrepodata == 2)
1501 for (j = 1; j < data->nkeys; j++)
1502 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1508 /* we need the complete filelist. check if we habe a filtered filelist and there's
1509 * a extension with the complete filelist later on */
1510 for (j = 1; j < data->nkeys; j++)
1511 if (data->keys[j].name == SOLVABLE_FILELIST)
1513 if (j == data->nkeys)
1514 return 0; /* does not have filelist */
1515 for (j = 1; j < data->nkeys; j++)
1516 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1518 if (j == data->nkeys)
1519 return 1; /* this is the externsion */
1520 while (data - data->repo->repodata + 1 < data->repo->nrepodata)
1523 if (is_filelist_extension(data))
1531 dataiterator_step(Dataiterator *di)
1535 if (di->state == di_nextattr && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET && di->vert_ddp && di->vert_storestate != di->data->storestate) {
1536 unsigned int ddpoff = di->ddp - di->vert_ddp;
1537 di->vert_off += ddpoff;
1538 di->vert_len -= ddpoff;
1539 di->ddp = di->vert_ddp = get_vertical_data(di->data, di->key, di->vert_off, di->vert_len);
1540 di->vert_storestate = di->data->storestate;
1542 di->state = di_nextkey;
1548 case di_enterrepo: di_enterrepo:
1549 if (!di->repo || (di->repo->disabled && !(di->flags & SEARCH_DISABLED_REPOS)))
1551 if (!(di->flags & SEARCH_THISSOLVID))
1553 di->solvid = di->repo->start - 1; /* reset solvid iterator */
1554 goto di_nextsolvable;
1558 case di_entersolvable: di_entersolvable:
1561 di->repodataid = 1; /* reset repodata iterator */
1562 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)
1564 extern Repokey repo_solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1];
1566 di->key = repo_solvablekeys + (di->keyname ? di->keyname - SOLVABLE_NAME : 0);
1568 goto di_entersolvablekey;
1573 case di_enterrepodata: di_enterrepodata:
1576 if (di->repodataid >= di->repo->nrepodata)
1577 goto di_nextsolvable;
1578 di->data = di->repo->repodata + di->repodataid;
1580 if (di->repodataid && di->keyname == SOLVABLE_FILELIST && !dataiterator_filelistcheck(di))
1581 goto di_nextrepodata;
1582 if (!maybe_load_repodata(di->data, di->keyname))
1583 goto di_nextrepodata;
1584 di->dp = solvid2data(di->data, di->solvid, &schema);
1586 goto di_nextrepodata;
1587 if (di->solvid == SOLVID_POS)
1588 di->solvid = di->pool->pos.solvid;
1589 /* reset key iterator */
1590 di->keyp = di->data->schemadata + di->data->schemata[schema];
1593 case di_enterschema: di_enterschema:
1595 di->dp = dataiterator_find_keyname(di, di->keyname);
1596 if (!di->dp || !*di->keyp)
1600 goto di_nextrepodata;
1604 case di_enterkey: di_enterkey:
1606 di->key = di->data->keys + *di->keyp;
1609 /* this is get_data() modified to store vert_ data */
1610 if (di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
1613 di->dp = data_read_id(di->dp, &off);
1614 di->dp = data_read_id(di->dp, &len);
1615 di->vert_ddp = di->ddp = get_vertical_data(di->data, di->key, off, len);
1618 di->vert_storestate = di->data->storestate;
1620 else if (di->key->storage == KEY_STORAGE_INCORE)
1623 if (di->keyp[1] && (!di->keyname || (di->flags & SEARCH_SUB) != 0))
1624 di->dp = data_skip_key(di->data, di->dp, di->key);
1630 if (di->key->type == REPOKEY_TYPE_DELETED)
1632 if (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY)
1634 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1640 di->ddp = data_fetch(di->ddp, &di->kv, di->key);
1642 di->state = di_nextkey;
1644 di->state = di_nextattr;
1647 case di_nextkey: di_nextkey:
1648 if (!di->keyname && *++di->keyp)
1654 case di_nextrepodata: di_nextrepodata:
1655 if (di->repodataid && ++di->repodataid < di->repo->nrepodata)
1656 goto di_enterrepodata;
1659 case di_nextsolvable: di_nextsolvable:
1660 if (!(di->flags & SEARCH_THISSOLVID))
1663 di->solvid = di->repo->start;
1666 for (; di->solvid < di->repo->end; di->solvid++)
1668 if (di->pool->solvables[di->solvid].repo == di->repo)
1669 goto di_entersolvable;
1674 case di_nextrepo: di_nextrepo:
1679 if (di->repoid < di->pool->nrepos)
1681 di->repo = di->pool->repos[di->repoid];
1687 case di_bye: di_bye:
1691 case di_enterarray: di_enterarray:
1692 if (di->key->name == REPOSITORY_SOLVABLES)
1694 di->ddp = data_read_id(di->ddp, (Id *)&di->kv.num);
1699 case di_nextarrayelement: di_nextarrayelement:
1702 di->ddp = data_skip_schema(di->data, di->ddp, di->kv.id);
1703 if (di->kv.entry == di->kv.num)
1705 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1707 if (!(di->flags & SEARCH_ARRAYSENTINEL))
1709 di->kv.str = (char *)di->ddp;
1711 di->state = di_nextkey;
1714 if (di->kv.entry == di->kv.num - 1)
1716 if (di->key->type == REPOKEY_TYPE_FLEXARRAY || !di->kv.entry)
1717 di->ddp = data_read_id(di->ddp, &di->kv.id);
1718 di->kv.str = (char *)di->ddp;
1719 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1721 if ((di->flags & SEARCH_SUB) != 0)
1722 di->state = di_entersub;
1724 di->state = di_nextarrayelement;
1727 case di_entersub: di_entersub:
1728 if (di->nparents == sizeof(di->parents)/sizeof(*di->parents) - 1)
1729 goto di_nextarrayelement; /* sorry, full */
1730 di->parents[di->nparents].kv = di->kv;
1731 di->parents[di->nparents].dp = di->dp;
1732 di->parents[di->nparents].keyp = di->keyp;
1733 di->dp = (unsigned char *)di->kv.str;
1734 di->keyp = di->data->schemadata + di->data->schemata[di->kv.id];
1735 memset(&di->kv, 0, sizeof(di->kv));
1736 di->kv.parent = &di->parents[di->nparents].kv;
1738 di->keyname = di->keynames[di->nparents - di->rootlevel];
1739 goto di_enterschema;
1741 case di_leavesub: di_leavesub:
1742 if (di->nparents - 1 < di->rootlevel)
1745 di->dp = di->parents[di->nparents].dp;
1746 di->kv = di->parents[di->nparents].kv;
1747 di->keyp = di->parents[di->nparents].keyp;
1748 di->key = di->data->keys + *di->keyp;
1749 di->ddp = (unsigned char *)di->kv.str;
1750 di->keyname = di->keynames[di->nparents - di->rootlevel];
1751 goto di_nextarrayelement;
1753 /* special solvable attr handling follows */
1755 case di_nextsolvablekey: di_nextsolvablekey:
1756 if (di->keyname || di->key->name == RPM_RPMDBID)
1757 goto di_enterrepodata;
1761 case di_entersolvablekey: di_entersolvablekey:
1762 di->idp = solvabledata_fetch(di->pool->solvables + di->solvid, &di->kv, di->key->name);
1763 if (!di->idp || !*di->idp)
1764 goto di_nextsolvablekey;
1768 di->kv.id = *di->idp;
1769 di->kv.num = *di->idp; /* for rpmdbid */
1770 di->kv.num2 = 0; /* for rpmdbid */
1772 di->state = di_nextsolvablekey;
1778 case di_nextsolvableattr:
1779 di->state = di_nextsolvableattr;
1780 di->kv.id = *di->idp++;
1785 di->state = di_nextsolvablekey;
1791 if (di->matcher.match)
1794 /* simple pre-check so that we don't need to stringify */
1795 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->matcher.flags & SEARCH_FILES) != 0)
1796 if (!datamatcher_checkbasename(&di->matcher, di->kv.str))
1798 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
1800 if (di->keyname && (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY))
1804 if (!datamatcher_match(&di->matcher, str))
1809 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->flags & SEARCH_FILES) != 0)
1810 repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags);
1812 /* found something! */
1818 dataiterator_entersub(Dataiterator *di)
1820 if (di->state == di_nextarrayelement)
1821 di->state = di_entersub;
1825 dataiterator_setpos(Dataiterator *di)
1827 if (di->kv.eof == 2)
1829 pool_clear_pos(di->pool);
1832 di->pool->pos.solvid = di->solvid;
1833 di->pool->pos.repo = di->repo;
1834 di->pool->pos.repodataid = di->data - di->repo->repodata;
1835 di->pool->pos.schema = di->kv.id;
1836 di->pool->pos.dp = (unsigned char *)di->kv.str - di->data->incoredata;
1840 dataiterator_setpos_parent(Dataiterator *di)
1842 if (!di->kv.parent || di->kv.parent->eof == 2)
1844 pool_clear_pos(di->pool);
1847 di->pool->pos.solvid = di->solvid;
1848 di->pool->pos.repo = di->repo;
1849 di->pool->pos.repodataid = di->data - di->repo->repodata;
1850 di->pool->pos.schema = di->kv.parent->id;
1851 di->pool->pos.dp = (unsigned char *)di->kv.parent->str - di->data->incoredata;
1854 /* clones just the position, not the search keys/matcher */
1856 dataiterator_clonepos(Dataiterator *di, Dataiterator *from)
1858 di->state = from->state;
1859 di->flags &= ~SEARCH_THISSOLVID;
1860 di->flags |= (from->flags & SEARCH_THISSOLVID);
1861 di->repo = from->repo;
1862 di->data = from->data;
1864 di->ddp = from->ddp;
1865 di->idp = from->idp;
1866 di->keyp = from->keyp;
1867 di->key = from->key;
1869 di->repodataid = from->repodataid;
1870 di->solvid = from->solvid;
1871 di->repoid = from->repoid;
1872 di->rootlevel = from->rootlevel;
1873 memcpy(di->parents, from->parents, sizeof(from->parents));
1874 di->nparents = from->nparents;
1878 for (i = 1; i < di->nparents; i++)
1879 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1880 di->kv.parent = &di->parents[di->nparents - 1].kv;
1884 if (from->dupstr && from->dupstr == from->kv.str)
1886 di->dupstrn = from->dupstrn;
1887 di->dupstr = solv_memdup(from->dupstr, from->dupstrn);
1892 dataiterator_seek(Dataiterator *di, int whence)
1894 if ((whence & DI_SEEK_STAY) != 0)
1895 di->rootlevel = di->nparents;
1896 switch (whence & ~DI_SEEK_STAY)
1899 if (di->state != di_nextarrayelement)
1901 if ((whence & DI_SEEK_STAY) != 0)
1902 di->rootlevel = di->nparents + 1; /* XXX: dangerous! */
1903 di->state = di_entersub;
1905 case DI_SEEK_PARENT:
1912 if (di->rootlevel > di->nparents)
1913 di->rootlevel = di->nparents;
1914 di->dp = di->parents[di->nparents].dp;
1915 di->kv = di->parents[di->nparents].kv;
1916 di->keyp = di->parents[di->nparents].keyp;
1917 di->key = di->data->keys + *di->keyp;
1918 di->ddp = (unsigned char *)di->kv.str;
1919 di->keyname = di->keynames[di->nparents - di->rootlevel];
1920 di->state = di_nextarrayelement;
1922 case DI_SEEK_REWIND:
1928 di->dp = (unsigned char *)di->kv.parent->str;
1929 di->keyp = di->data->schemadata + di->data->schemata[di->kv.parent->id];
1930 di->state = di_enterschema;
1938 dataiterator_skip_attribute(Dataiterator *di)
1940 if (di->state == di_nextsolvableattr)
1941 di->state = di_nextsolvablekey;
1943 di->state = di_nextkey;
1947 dataiterator_skip_solvable(Dataiterator *di)
1952 di->keyname = di->keynames[0];
1953 di->state = di_nextsolvable;
1957 dataiterator_skip_repo(Dataiterator *di)
1962 di->keyname = di->keynames[0];
1963 di->state = di_nextrepo;
1967 dataiterator_jump_to_solvid(Dataiterator *di, Id solvid)
1972 di->keyname = di->keynames[0];
1973 if (solvid == SOLVID_POS)
1975 di->repo = di->pool->pos.repo;
1982 if (!di->pool->pos.repodataid && di->pool->pos.solvid == SOLVID_META) {
1983 solvid = SOLVID_META; /* META pos hack */
1985 di->data = di->repo->repodata + di->pool->pos.repodataid;
1989 else if (solvid > 0)
1991 di->repo = di->pool->solvables[solvid].repo;
1996 if (!di->pool->urepos)
2002 di->repo = di->pool->repos[di->repoid];
2004 if (solvid != SOLVID_POS)
2006 di->solvid = solvid;
2008 di->flags |= SEARCH_THISSOLVID;
2009 di->state = di_enterrepo;
2013 dataiterator_jump_to_repo(Dataiterator *di, Repo *repo)
2019 di->repoid = 0; /* 0 means stay at repo */
2022 di->flags &= ~SEARCH_THISSOLVID;
2023 di->state = di_enterrepo;
2027 dataiterator_match(Dataiterator *di, Datamatcher *ma)
2030 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
2032 return ma ? datamatcher_match(ma, str) : 1;
2036 dataiterator_strdup(Dataiterator *di)
2040 if (!di->kv.str || di->kv.str == di->dupstr)
2042 switch (di->key->type)
2045 case REPOKEY_TYPE_DIRSTRARRAY:
2046 if (di->kv.num) /* was it stringified into tmp space? */
2047 l = strlen(di->kv.str) + 1;
2052 if (l < 0 && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
2054 switch (di->key->type)
2056 case REPOKEY_TYPE_STR:
2057 case REPOKEY_TYPE_DIRSTRARRAY:
2058 l = strlen(di->kv.str) + 1;
2061 l = solv_chksum_len(di->key->type);
2063 case REPOKEY_TYPE_BINARY:
2070 if (!di->dupstrn || di->dupstrn < l)
2072 di->dupstrn = l + 16;
2073 di->dupstr = solv_realloc(di->dupstr, di->dupstrn);
2076 memcpy(di->dupstr, di->kv.str, l);
2077 di->kv.str = di->dupstr;
2081 /************************************************************************
2082 * data modify functions
2085 /* extend repodata so that it includes solvables p */
2087 repodata_extend(Repodata *data, Id p)
2089 if (data->start == data->end)
2090 data->start = data->end = p;
2093 int old = data->end - data->start;
2094 int new = p - data->end + 1;
2097 data->attrs = solv_extend(data->attrs, old, new, sizeof(Id *), REPODATA_BLOCK);
2098 memset(data->attrs + old, 0, new * sizeof(Id *));
2100 data->incoreoffset = solv_extend(data->incoreoffset, old, new, sizeof(Id), REPODATA_BLOCK);
2101 memset(data->incoreoffset + old, 0, new * sizeof(Id));
2104 if (p < data->start)
2106 int old = data->end - data->start;
2107 int new = data->start - p;
2110 data->attrs = solv_extend_resize(data->attrs, old + new, sizeof(Id *), REPODATA_BLOCK);
2111 memmove(data->attrs + new, data->attrs, old * sizeof(Id *));
2112 memset(data->attrs, 0, new * sizeof(Id *));
2114 data->incoreoffset = solv_extend_resize(data->incoreoffset, old + new, sizeof(Id), REPODATA_BLOCK);
2115 memmove(data->incoreoffset + new, data->incoreoffset, old * sizeof(Id));
2116 memset(data->incoreoffset, 0, new * sizeof(Id));
2121 /* shrink end of repodata */
2123 repodata_shrink(Repodata *data, int end)
2127 if (data->end <= end)
2129 if (data->start >= end)
2133 for (i = 0; i < data->end - data->start; i++)
2134 solv_free(data->attrs[i]);
2135 data->attrs = solv_free(data->attrs);
2137 data->incoreoffset = solv_free(data->incoreoffset);
2138 data->start = data->end = 0;
2143 for (i = end; i < data->end; i++)
2144 solv_free(data->attrs[i - data->start]);
2145 data->attrs = solv_extend_resize(data->attrs, end - data->start, sizeof(Id *), REPODATA_BLOCK);
2147 if (data->incoreoffset)
2148 data->incoreoffset = solv_extend_resize(data->incoreoffset, end - data->start, sizeof(Id), REPODATA_BLOCK);
2152 /* extend repodata so that it includes solvables from start to start + num - 1 */
2154 repodata_extend_block(Repodata *data, Id start, Id num)
2158 if (!data->incoreoffset)
2160 /* this also means that data->attrs is NULL */
2161 data->incoreoffset = solv_calloc_block(num, sizeof(Id), REPODATA_BLOCK);
2162 data->start = start;
2163 data->end = start + num;
2166 repodata_extend(data, start);
2168 repodata_extend(data, start + num - 1);
2171 /**********************************************************************/
2174 #define REPODATA_ATTRS_BLOCK 31
2175 #define REPODATA_ATTRDATA_BLOCK 1023
2176 #define REPODATA_ATTRIDDATA_BLOCK 63
2177 #define REPODATA_ATTRNUM64DATA_BLOCK 15
2181 repodata_new_handle(Repodata *data)
2185 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2186 data->nxattrs = 2; /* -1: SOLVID_META */
2188 data->xattrs = solv_extend(data->xattrs, data->nxattrs, 1, sizeof(Id *), REPODATA_BLOCK);
2189 data->xattrs[data->nxattrs] = 0;
2190 return -(data->nxattrs++);
2194 repodata_get_attrp(Repodata *data, Id handle)
2198 if (handle == SOLVID_META && !data->xattrs)
2200 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2203 return data->xattrs - handle;
2205 if (handle < data->start || handle >= data->end)
2206 repodata_extend(data, handle);
2208 data->attrs = solv_calloc_block(data->end - data->start, sizeof(Id *), REPODATA_BLOCK);
2209 return data->attrs + (handle - data->start);
2213 repodata_insert_keyid(Repodata *data, Id handle, Id keyid, Id val, int overwrite)
2219 app = repodata_get_attrp(data, handle);
2224 /* Determine equality based on the name only, allows us to change
2225 type (when overwrite is set), and makes TYPE_CONSTANT work. */
2226 for (pp = ap; *pp; pp += 2)
2227 if (data->keys[*pp].name == data->keys[keyid].name)
2231 if (overwrite || data->keys[*pp].type == REPOKEY_TYPE_DELETED)
2240 ap = solv_extend(ap, i, 3, sizeof(Id), REPODATA_ATTRS_BLOCK);
2250 repodata_set(Repodata *data, Id solvid, Repokey *key, Id val)
2254 keyid = repodata_key2id(data, key, 1);
2255 repodata_insert_keyid(data, solvid, keyid, val, 1);
2259 repodata_set_id(Repodata *data, Id solvid, Id keyname, Id id)
2263 key.type = REPOKEY_TYPE_ID;
2265 key.storage = KEY_STORAGE_INCORE;
2266 repodata_set(data, solvid, &key, id);
2270 repodata_set_num(Repodata *data, Id solvid, Id keyname, unsigned long long num)
2274 key.type = REPOKEY_TYPE_NUM;
2276 key.storage = KEY_STORAGE_INCORE;
2277 if (num >= 0x80000000)
2279 data->attrnum64data = solv_extend(data->attrnum64data, data->attrnum64datalen, 1, sizeof(unsigned long long), REPODATA_ATTRNUM64DATA_BLOCK);
2280 data->attrnum64data[data->attrnum64datalen] = num;
2281 num = 0x80000000 | data->attrnum64datalen++;
2283 repodata_set(data, solvid, &key, (Id)num);
2287 repodata_set_poolstr(Repodata *data, Id solvid, Id keyname, const char *str)
2291 if (data->localpool)
2292 id = stringpool_str2id(&data->spool, str, 1);
2294 id = pool_str2id(data->repo->pool, str, 1);
2296 key.type = REPOKEY_TYPE_ID;
2298 key.storage = KEY_STORAGE_INCORE;
2299 repodata_set(data, solvid, &key, id);
2303 repodata_set_constant(Repodata *data, Id solvid, Id keyname, unsigned int constant)
2307 key.type = REPOKEY_TYPE_CONSTANT;
2308 key.size = constant;
2309 key.storage = KEY_STORAGE_INCORE;
2310 repodata_set(data, solvid, &key, 0);
2314 repodata_set_constantid(Repodata *data, Id solvid, Id keyname, Id id)
2318 key.type = REPOKEY_TYPE_CONSTANTID;
2320 key.storage = KEY_STORAGE_INCORE;
2321 repodata_set(data, solvid, &key, 0);
2325 repodata_set_void(Repodata *data, Id solvid, Id keyname)
2329 key.type = REPOKEY_TYPE_VOID;
2331 key.storage = KEY_STORAGE_INCORE;
2332 repodata_set(data, solvid, &key, 0);
2336 repodata_set_str(Repodata *data, Id solvid, Id keyname, const char *str)
2341 l = strlen(str) + 1;
2343 key.type = REPOKEY_TYPE_STR;
2345 key.storage = KEY_STORAGE_INCORE;
2346 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2347 memcpy(data->attrdata + data->attrdatalen, str, l);
2348 repodata_set(data, solvid, &key, data->attrdatalen);
2349 data->attrdatalen += l;
2353 repodata_set_binary(Repodata *data, Id solvid, Id keyname, void *buf, int len)
2361 key.type = REPOKEY_TYPE_BINARY;
2363 key.storage = KEY_STORAGE_INCORE;
2364 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, len + 5, 1, REPODATA_ATTRDATA_BLOCK);
2365 dp = data->attrdata + data->attrdatalen;
2366 if (len >= (1 << 14))
2368 if (len >= (1 << 28))
2369 *dp++ = (len >> 28) | 128;
2370 if (len >= (1 << 21))
2371 *dp++ = (len >> 21) | 128;
2372 *dp++ = (len >> 14) | 128;
2374 if (len >= (1 << 7))
2375 *dp++ = (len >> 7) | 128;
2378 memcpy(dp, buf, len);
2379 repodata_set(data, solvid, &key, data->attrdatalen);
2380 data->attrdatalen = dp + len - data->attrdata;
2383 /* add an array element consisting of entrysize Ids to the repodata. modifies attriddata
2384 * so that the caller can append entrysize new elements plus the termination zero there */
2386 repodata_add_array(Repodata *data, Id handle, Id keyname, Id keytype, int entrysize)
2389 Id *ida, *pp, **ppp;
2391 /* check if it is the same as last time, this speeds things up a lot */
2392 if (handle == data->lasthandle && data->keys[data->lastkey].name == keyname && data->keys[data->lastkey].type == keytype && data->attriddatalen == data->lastdatalen)
2394 /* great! just append the new data */
2395 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2396 data->attriddatalen--; /* overwrite terminating 0 */
2397 data->lastdatalen += entrysize;
2401 ppp = repodata_get_attrp(data, handle);
2405 for (; *pp; pp += 2)
2406 if (data->keys[*pp].name == keyname)
2409 if (!pp || !*pp || data->keys[*pp].type != keytype)
2411 /* not found. allocate new key */
2417 key.storage = KEY_STORAGE_INCORE;
2418 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2419 keyid = repodata_key2id(data, &key, 1);
2420 repodata_insert_keyid(data, handle, keyid, data->attriddatalen, 1);
2421 data->lasthandle = handle;
2422 data->lastkey = keyid;
2423 data->lastdatalen = data->attriddatalen + entrysize + 1;
2427 for (ida = data->attriddata + pp[1]; *ida; ida += entrysize)
2428 oldsize += entrysize;
2429 if (ida + 1 == data->attriddata + data->attriddatalen)
2431 /* this was the last entry, just append it */
2432 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2433 data->attriddatalen--; /* overwrite terminating 0 */
2437 /* too bad. move to back. */
2438 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, oldsize + entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2439 memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
2440 pp[1] = data->attriddatalen;
2441 data->attriddatalen += oldsize;
2443 data->lasthandle = handle;
2444 data->lastkey = *pp;
2445 data->lastdatalen = data->attriddatalen + entrysize + 1;
2449 repodata_set_bin_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2450 const unsigned char *str)
2455 if (!(l = solv_chksum_len(type)))
2460 key.storage = KEY_STORAGE_INCORE;
2461 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2462 memcpy(data->attrdata + data->attrdatalen, str, l);
2463 repodata_set(data, solvid, &key, data->attrdatalen);
2464 data->attrdatalen += l;
2468 repodata_set_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2471 unsigned char buf[64];
2474 if (!(l = solv_chksum_len(type)))
2476 if (l > sizeof(buf) || solv_hex2bin(&str, buf, l) != l)
2478 repodata_set_bin_checksum(data, solvid, keyname, type, buf);
2482 repodata_chk2str(Repodata *data, Id type, const unsigned char *buf)
2486 if (!(l = solv_chksum_len(type)))
2488 return pool_bin2hex(data->repo->pool, buf, l);
2491 /* rpm filenames don't contain the epoch, so strip it */
2492 static inline const char *
2493 evrid2vrstr(Pool *pool, Id evrid)
2495 const char *p, *evr = pool_id2str(pool, evrid);
2498 for (p = evr; *p >= '0' && *p <= '9'; p++)
2500 return p != evr && *p == ':' && p[1] ? p + 1 : evr;
2504 repodata_set_poolstrn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2507 if (data->localpool)
2508 id = stringpool_strn2id(&data->spool, str, l, 1);
2510 id = pool_strn2id(data->repo->pool, str, l, 1);
2511 repodata_set_id(data, solvid, keyname, id);
2515 repodata_set_strn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2518 repodata_set_str(data, solvid, keyname, str);
2521 char *s = solv_strdup(str);
2523 repodata_set_str(data, solvid, keyname, s);
2529 repodata_set_location(Repodata *data, Id solvid, int medianr, const char *dir, const char *file)
2531 Pool *pool = data->repo->pool;
2533 const char *str, *fp;
2537 repodata_set_constant(data, solvid, SOLVABLE_MEDIANR, medianr);
2540 if ((dir = strrchr(file, '/')) != 0)
2551 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2556 if (l == 1 && dir[0] == '.')
2558 s = pool->solvables + solvid;
2561 str = pool_id2str(pool, s->arch);
2562 if (!strncmp(dir, str, l) && !str[l])
2563 repodata_set_void(data, solvid, SOLVABLE_MEDIADIR);
2565 repodata_set_strn(data, solvid, SOLVABLE_MEDIADIR, dir, l);
2568 str = pool_id2str(pool, s->name);
2570 if ((!l || !strncmp(fp, str, l)) && fp[l] == '-')
2573 str = evrid2vrstr(pool, s->evr);
2575 if ((!l || !strncmp(fp, str, l)) && fp[l] == '.')
2578 str = pool_id2str(pool, s->arch);
2580 if ((!l || !strncmp(fp, str, l)) && !strcmp(fp + l, ".rpm"))
2582 repodata_set_void(data, solvid, SOLVABLE_MEDIAFILE);
2587 repodata_set_str(data, solvid, SOLVABLE_MEDIAFILE, file);
2590 /* XXX: medianr is currently not stored */
2592 repodata_set_deltalocation(Repodata *data, Id handle, int medianr, const char *dir, const char *file)
2595 const char *evr, *suf, *s;
2599 if ((dir = strrchr(file, '/')) != 0)
2610 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2615 if (l == 1 && dir[0] == '.')
2618 repodata_set_poolstrn(data, handle, DELTA_LOCATION_DIR, dir, l);
2619 evr = strchr(file, '-');
2622 for (s = evr - 1; s > file; s--)
2629 suf = strrchr(file, '.');
2632 for (s = suf - 1; s > file; s--)
2638 if (!strcmp(suf, ".delta.rpm") || !strcmp(suf, ".patch.rpm"))
2640 /* We accept one more item as suffix. */
2641 for (s = suf - 1; s > file; s--)
2651 if (suf && evr && suf < evr)
2653 repodata_set_poolstrn(data, handle, DELTA_LOCATION_NAME, file, evr ? evr - file : strlen(file));
2655 repodata_set_poolstrn(data, handle, DELTA_LOCATION_EVR, evr + 1, suf ? suf - evr - 1: strlen(evr + 1));
2657 repodata_set_poolstr(data, handle, DELTA_LOCATION_SUFFIX, suf + 1);
2661 repodata_set_sourcepkg(Repodata *data, Id solvid, const char *sourcepkg)
2663 Pool *pool = data->repo->pool;
2664 Solvable *s = pool->solvables + solvid;
2665 const char *p, *sevr, *sarch, *name, *evr;
2667 p = strrchr(sourcepkg, '.');
2668 if (!p || strcmp(p, ".rpm") != 0)
2671 repodata_set_str(data, solvid, SOLVABLE_SOURCENAME, sourcepkg);
2675 while (p > sourcepkg && *p != '.')
2677 if (*p != '.' || p == sourcepkg)
2680 while (p > sourcepkg && *p != '-')
2682 if (*p != '-' || p == sourcepkg)
2685 while (p > sourcepkg && *p != '-')
2687 if (*p != '-' || p == sourcepkg)
2690 pool = s->repo->pool;
2692 name = pool_id2str(pool, s->name);
2693 if (name && !strncmp(sourcepkg, name, sevr - sourcepkg - 1) && name[sevr - sourcepkg - 1] == 0)
2694 repodata_set_void(data, solvid, SOLVABLE_SOURCENAME);
2696 repodata_set_id(data, solvid, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcepkg, sevr - sourcepkg - 1, 1));
2698 evr = evrid2vrstr(pool, s->evr);
2699 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
2700 repodata_set_void(data, solvid, SOLVABLE_SOURCEEVR);
2702 repodata_set_id(data, solvid, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
2704 if (!strcmp(sarch, "src.rpm"))
2705 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_SRC);
2706 else if (!strcmp(sarch, "nosrc.rpm"))
2707 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
2709 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
2713 repodata_set_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
2719 key.type = REPOKEY_TYPE_IDARRAY;
2721 key.storage = KEY_STORAGE_INCORE;
2722 repodata_set(data, solvid, &key, data->attriddatalen);
2723 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, q->count + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2724 for (i = 0; i < q->count; i++)
2725 data->attriddata[data->attriddatalen++] = q->elements[i];
2726 data->attriddata[data->attriddatalen++] = 0;
2730 repodata_add_dirnumnum(Repodata *data, Id solvid, Id keyname, Id dir, Id num, Id num2)
2734 fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", solvid, dir, num, num2, data->attriddatalen);
2736 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRNUMNUMARRAY, 3);
2737 data->attriddata[data->attriddatalen++] = dir;
2738 data->attriddata[data->attriddatalen++] = num;
2739 data->attriddata[data->attriddatalen++] = num2;
2740 data->attriddata[data->attriddatalen++] = 0;
2744 repodata_add_dirstr(Repodata *data, Id solvid, Id keyname, Id dir, const char *str)
2750 l = strlen(str) + 1;
2751 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2752 memcpy(data->attrdata + data->attrdatalen, str, l);
2753 stroff = data->attrdatalen;
2754 data->attrdatalen += l;
2757 fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", solvid, dir, str, data->attriddatalen);
2759 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRSTRARRAY, 2);
2760 data->attriddata[data->attriddatalen++] = dir;
2761 data->attriddata[data->attriddatalen++] = stroff;
2762 data->attriddata[data->attriddatalen++] = 0;
2766 repodata_add_idarray(Repodata *data, Id solvid, Id keyname, Id id)
2769 fprintf(stderr, "repodata_add_idarray %d %d (%d)\n", solvid, id, data->attriddatalen);
2771 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_IDARRAY, 1);
2772 data->attriddata[data->attriddatalen++] = id;
2773 data->attriddata[data->attriddatalen++] = 0;
2777 repodata_add_poolstr_array(Repodata *data, Id solvid, Id keyname,
2781 if (data->localpool)
2782 id = stringpool_str2id(&data->spool, str, 1);
2784 id = pool_str2id(data->repo->pool, str, 1);
2785 repodata_add_idarray(data, solvid, keyname, id);
2789 repodata_add_fixarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2791 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FIXARRAY, 1);
2792 data->attriddata[data->attriddatalen++] = ghandle;
2793 data->attriddata[data->attriddatalen++] = 0;
2797 repodata_add_flexarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2799 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FLEXARRAY, 1);
2800 data->attriddata[data->attriddatalen++] = ghandle;
2801 data->attriddata[data->attriddatalen++] = 0;
2805 repodata_unset_uninternalized(Repodata *data, Id solvid, Id keyname)
2808 app = repodata_get_attrp(data, solvid);
2812 for (; *ap; ap += 2)
2813 if (data->keys[*ap].name == keyname)
2819 for (; *ap; ap += 2)
2821 if (data->keys[*ap].name == keyname)
2829 /* XXX: does not work correctly, needs fix in iterators! */
2831 repodata_unset(Repodata *data, Id solvid, Id keyname)
2835 key.type = REPOKEY_TYPE_DELETED;
2837 key.storage = KEY_STORAGE_INCORE;
2838 repodata_set(data, solvid, &key, 0);
2841 /* add all (uninternalized) attrs from src to dest */
2843 repodata_merge_attrs(Repodata *data, Id dest, Id src)
2846 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2848 for (; *keyp; keyp += 2)
2849 repodata_insert_keyid(data, dest, keyp[0], keyp[1], 0);
2852 /* add some (uninternalized) attrs from src to dest */
2854 repodata_merge_some_attrs(Repodata *data, Id dest, Id src, Map *keyidmap, int overwrite)
2857 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2859 for (; *keyp; keyp += 2)
2860 if (!keyidmap || MAPTST(keyidmap, keyp[0]))
2861 repodata_insert_keyid(data, dest, keyp[0], keyp[1], overwrite);
2864 /* swap (uninternalized) attrs from src and dest */
2866 repodata_swap_attrs(Repodata *data, Id dest, Id src)
2869 if (!data->attrs || dest == src)
2871 if (dest < data->start || dest >= data->end)
2872 repodata_extend(data, dest);
2873 if (src < data->start || src >= data->end)
2874 repodata_extend(data, src);
2875 tmpattrs = data->attrs[dest - data->start];
2876 data->attrs[dest - data->start] = data->attrs[src - data->start];
2877 data->attrs[src - data->start] = tmpattrs;
2881 /**********************************************************************/
2883 /* TODO: unify with repo_write and repo_solv! */
2885 #define EXTDATA_BLOCK 1023
2893 data_addid(struct extdata *xd, Id sx)
2895 unsigned int x = (unsigned int)sx;
2898 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2899 dp = xd->buf + xd->len;
2904 *dp++ = (x >> 28) | 128;
2906 *dp++ = (x >> 21) | 128;
2907 *dp++ = (x >> 14) | 128;
2910 *dp++ = (x >> 7) | 128;
2912 xd->len = dp - xd->buf;
2916 data_addid64(struct extdata *xd, unsigned long long x)
2918 if (x >= 0x100000000)
2922 data_addid(xd, (Id)(x >> 35));
2923 xd->buf[xd->len - 1] |= 128;
2925 data_addid(xd, (Id)((unsigned int)x | 0x80000000));
2926 xd->buf[xd->len - 5] = (x >> 28) | 128;
2929 data_addid(xd, (Id)x);
2933 data_addideof(struct extdata *xd, Id sx, int eof)
2935 unsigned int x = (unsigned int)sx;
2938 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2939 dp = xd->buf + xd->len;
2944 *dp++ = (x >> 27) | 128;
2946 *dp++ = (x >> 20) | 128;
2947 *dp++ = (x >> 13) | 128;
2950 *dp++ = (x >> 6) | 128;
2951 *dp++ = eof ? (x & 63) : (x & 63) | 64;
2952 xd->len = dp - xd->buf;
2956 data_addblob(struct extdata *xd, unsigned char *blob, int len)
2958 xd->buf = solv_extend(xd->buf, xd->len, len, 1, EXTDATA_BLOCK);
2959 memcpy(xd->buf + xd->len, blob, len);
2963 /*********************************/
2965 /* this is to reduct memory usage when internalizing oversized repos */
2967 compact_attrdata(Repodata *data, int entry, int nentry)
2970 unsigned int attrdatastart = data->attrdatalen;
2971 unsigned int attriddatastart = data->attriddatalen;
2972 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
2974 for (i = entry; i < nentry; i++)
2976 Id v, *attrs = data->attrs[i];
2979 for (; *attrs; attrs += 2)
2981 switch (data->keys[*attrs].type)
2983 case REPOKEY_TYPE_STR:
2984 case REPOKEY_TYPE_BINARY:
2986 if ((unsigned int)attrs[1] < attrdatastart)
2987 attrdatastart = attrs[1];
2989 case REPOKEY_TYPE_DIRSTRARRAY:
2990 for (v = attrs[1]; data->attriddata[v] ; v += 2)
2991 if (data->attriddata[v + 1] < attrdatastart)
2992 attrdatastart = data->attriddata[v + 1];
2994 case REPOKEY_TYPE_IDARRAY:
2995 case REPOKEY_TYPE_DIRNUMNUMARRAY:
2996 if ((unsigned int)attrs[1] < attriddatastart)
2997 attriddatastart = attrs[1];
2999 case REPOKEY_TYPE_FIXARRAY:
3000 case REPOKEY_TYPE_FLEXARRAY:
3008 printf("compact_attrdata %d %d\n", entry, nentry);
3009 printf("attrdatastart: %d\n", attrdatastart);
3010 printf("attriddatastart: %d\n", attriddatastart);
3012 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
3014 for (i = entry; i < nentry; i++)
3016 Id v, *attrs = data->attrs[i];
3019 for (; *attrs; attrs += 2)
3021 switch (data->keys[*attrs].type)
3023 case REPOKEY_TYPE_STR:
3024 case REPOKEY_TYPE_BINARY:
3026 attrs[1] -= attrdatastart;
3028 case REPOKEY_TYPE_DIRSTRARRAY:
3029 for (v = attrs[1]; data->attriddata[v] ; v += 2)
3030 data->attriddata[v + 1] -= attrdatastart;
3032 case REPOKEY_TYPE_IDARRAY:
3033 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3034 attrs[1] -= attriddatastart;
3043 data->attrdatalen -= attrdatastart;
3044 memmove(data->attrdata, data->attrdata + attrdatastart, data->attrdatalen);
3045 data->attrdata = solv_extend_resize(data->attrdata, data->attrdatalen, 1, REPODATA_ATTRDATA_BLOCK);
3047 if (attriddatastart)
3049 data->attriddatalen -= attriddatastart;
3050 memmove(data->attriddata, data->attriddata + attriddatastart, data->attriddatalen * sizeof(Id));
3051 data->attriddata = solv_extend_resize(data->attriddata, data->attriddatalen, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
3055 /* internalalize some key into incore/vincore data */
3058 repodata_serialize_key(Repodata *data, struct extdata *newincore,
3059 struct extdata *newvincore,
3061 Repokey *key, Id val)
3065 unsigned int oldvincorelen = 0;
3069 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3072 oldvincorelen = xd->len;
3076 case REPOKEY_TYPE_VOID:
3077 case REPOKEY_TYPE_CONSTANT:
3078 case REPOKEY_TYPE_CONSTANTID:
3079 case REPOKEY_TYPE_DELETED:
3081 case REPOKEY_TYPE_STR:
3082 data_addblob(xd, data->attrdata + val, strlen((char *)(data->attrdata + val)) + 1);
3084 case REPOKEY_TYPE_MD5:
3085 data_addblob(xd, data->attrdata + val, SIZEOF_MD5);
3087 case REPOKEY_TYPE_SHA1:
3088 data_addblob(xd, data->attrdata + val, SIZEOF_SHA1);
3090 case REPOKEY_TYPE_SHA224:
3091 data_addblob(xd, data->attrdata + val, SIZEOF_SHA224);
3093 case REPOKEY_TYPE_SHA256:
3094 data_addblob(xd, data->attrdata + val, SIZEOF_SHA256);
3096 case REPOKEY_TYPE_SHA384:
3097 data_addblob(xd, data->attrdata + val, SIZEOF_SHA384);
3099 case REPOKEY_TYPE_SHA512:
3100 data_addblob(xd, data->attrdata + val, SIZEOF_SHA512);
3102 case REPOKEY_TYPE_NUM:
3103 if (val & 0x80000000)
3105 data_addid64(xd, data->attrnum64data[val ^ 0x80000000]);
3109 case REPOKEY_TYPE_ID:
3110 case REPOKEY_TYPE_DIR:
3111 data_addid(xd, val);
3113 case REPOKEY_TYPE_BINARY:
3116 unsigned char *dp = data_read_id(data->attrdata + val, &len);
3117 dp += (unsigned int)len;
3118 data_addblob(xd, data->attrdata + val, dp - (data->attrdata + val));
3121 case REPOKEY_TYPE_IDARRAY:
3122 for (ida = data->attriddata + val; *ida; ida++)
3123 data_addideof(xd, ida[0], ida[1] ? 0 : 1);
3125 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3126 for (ida = data->attriddata + val; *ida; ida += 3)
3128 data_addid(xd, ida[0]);
3129 data_addid(xd, ida[1]);
3130 data_addideof(xd, ida[2], ida[3] ? 0 : 1);
3133 case REPOKEY_TYPE_DIRSTRARRAY:
3134 for (ida = data->attriddata + val; *ida; ida += 2)
3136 data_addideof(xd, ida[0], ida[2] ? 0 : 1);
3137 data_addblob(xd, data->attrdata + ida[1], strlen((char *)(data->attrdata + ida[1])) + 1);
3140 case REPOKEY_TYPE_FIXARRAY:
3144 for (ida = data->attriddata + val; *ida; ida++)
3148 kp = data->xattrs[-*ida];
3150 continue; /* ignore empty elements */
3152 for (; *kp; kp += 2)
3156 schemaid = repodata_schema2id(data, schema, 1);
3157 else if (schemaid != repodata_schema2id(data, schema, 0))
3159 pool_debug(data->repo->pool, SOLV_ERROR, "repodata_serialize_key: fixarray substructs with different schemas\n");
3164 data_addid(xd, num);
3167 data_addid(xd, schemaid);
3168 for (ida = data->attriddata + val; *ida; ida++)
3170 Id *kp = data->xattrs[-*ida];
3173 for (; *kp; kp += 2)
3174 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3178 case REPOKEY_TYPE_FLEXARRAY:
3181 for (ida = data->attriddata + val; *ida; ida++)
3183 data_addid(xd, num);
3184 for (ida = data->attriddata + val; *ida; ida++)
3186 Id *kp = data->xattrs[-*ida];
3189 data_addid(xd, 0); /* XXX */
3196 schemaid = repodata_schema2id(data, schema, 1);
3197 data_addid(xd, schemaid);
3198 kp = data->xattrs[-*ida];
3200 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3205 pool_debug(data->repo->pool, SOLV_FATAL, "repodata_serialize_key: don't know how to handle type %d\n", key->type);
3208 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3210 /* put offset/len in incore */
3211 data_addid(newincore, data->lastverticaloffset + oldvincorelen);
3212 oldvincorelen = xd->len - oldvincorelen;
3213 data_addid(newincore, oldvincorelen);
3217 /* create a circular linked list of all keys that share
3218 * the same keyname */
3220 calculate_keylink(Repodata *data)
3224 Id maxkeyname = 0, *keytable = 0;
3225 link = solv_calloc(data->nkeys, sizeof(Id));
3226 if (data->nkeys <= 2)
3228 for (i = 1; i < data->nkeys; i++)
3230 Id n = data->keys[i].name;
3231 if (n >= maxkeyname)
3233 keytable = solv_realloc2(keytable, n + 128, sizeof(Id));
3234 memset(keytable + maxkeyname, 0, (n + 128 - maxkeyname) * sizeof(Id));
3235 maxkeyname = n + 128;
3245 /* remove links that just point to themselfs */
3246 for (i = 1; i < data->nkeys; i++)
3249 solv_free(keytable);
3254 repodata_internalize(Repodata *data)
3256 Repokey *key, solvkey;
3258 Id schemaid, keyid, *schema, *sp, oldschemaid, *keyp, *seen;
3259 Offset *oldincoreoffs = 0;
3261 unsigned char *dp, *ndp;
3263 struct extdata newincore;
3264 struct extdata newvincore;
3269 if (!data->attrs && !data->xattrs)
3273 printf("repodata_internalize %d\n", data->repodataid);
3274 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3275 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3277 newvincore.buf = data->vincore;
3278 newvincore.len = data->vincorelen;
3280 /* find the solvables key, create if needed */
3281 memset(&solvkey, 0, sizeof(solvkey));
3282 solvkey.name = REPOSITORY_SOLVABLES;
3283 solvkey.type = REPOKEY_TYPE_FLEXARRAY;
3285 solvkey.storage = KEY_STORAGE_INCORE;
3286 solvkeyid = repodata_key2id(data, &solvkey, data->end != data->start ? 1 : 0);
3288 schema = solv_malloc2(data->nkeys, sizeof(Id));
3289 seen = solv_malloc2(data->nkeys, sizeof(Id));
3291 /* Merge the data already existing (in data->schemata, ->incoredata and
3292 friends) with the new attributes in data->attrs[]. */
3293 nentry = data->end - data->start;
3294 memset(&newincore, 0, sizeof(newincore));
3295 data_addid(&newincore, 0); /* start data at offset 1 */
3297 data->mainschema = 0;
3298 data->mainschemaoffsets = solv_free(data->mainschemaoffsets);
3300 keylink = calculate_keylink(data);
3301 /* join entry data */
3302 /* we start with the meta data, entry -1 */
3303 for (entry = -1; entry < nentry; entry++)
3306 dp = data->incoredata;
3309 dp += entry >= 0 ? data->incoreoffset[entry] : 1;
3310 dp = data_read_id(dp, &oldschemaid);
3312 memset(seen, 0, data->nkeys * sizeof(Id));
3314 fprintf(stderr, "oldschemaid %d\n", oldschemaid);
3315 fprintf(stderr, "schemata %d\n", data->schemata[oldschemaid]);
3316 fprintf(stderr, "schemadata %p\n", data->schemadata);
3319 /* seen: -1: old data, 0: skipped, >0: id + 1 */
3323 for (keyp = data->schemadata + data->schemata[oldschemaid]; *keyp; keyp++)
3327 /* oops, should not happen */
3331 seen[*keyp] = -1; /* use old marker */
3334 haveoldkl = 1; /* potential keylink conflict */
3337 /* strip solvables key */
3338 if (entry < 0 && solvkeyid && seen[solvkeyid])
3341 for (sp = keyp = schema; *sp; sp++)
3342 if (*sp != solvkeyid)
3345 seen[solvkeyid] = 0;
3349 /* add new entries */
3351 keyp = data->attrs ? data->attrs[entry] : 0;
3353 keyp = data->xattrs ? data->xattrs[1] : 0;
3355 for (; *keyp; keyp += 2)
3361 if (haveoldkl && keylink[*keyp]) /* this should be pretty rare */
3364 for (kl = keylink[*keyp]; kl != *keyp; kl = keylink[kl])
3367 /* replacing old key kl, remove from schema and seen */
3369 for (osp = schema; osp < sp; osp++)
3372 memmove(osp, osp + 1, (sp - osp) * sizeof(Id));
3380 seen[*keyp] = keyp[1] + 1;
3383 /* add solvables key if needed */
3384 if (entry < 0 && data->end != data->start)
3386 *sp++ = solvkeyid; /* always last in schema */
3393 /* Ideally we'd like to sort the new schema here, to ensure
3394 schema equality independend of the ordering. */
3395 schemaid = repodata_schema2id(data, schema, 1);
3397 schemaid = oldschemaid;
3401 data->mainschemaoffsets = solv_calloc(sp - schema, sizeof(Id));
3402 data->mainschema = schemaid;
3405 /* find offsets in old incore data */
3409 for (sp = data->schemadata + data->schemata[oldschemaid]; *sp; sp++)
3410 if (seen[*sp] == -1)
3411 lastneeded = sp + 1;
3415 oldincoreoffs = solv_malloc2(data->nkeys, 2 * sizeof(Offset));
3416 for (sp = data->schemadata + data->schemata[oldschemaid]; sp != lastneeded; sp++)
3418 /* Skip the data associated with this old key. */
3419 key = data->keys + *sp;
3421 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3423 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3424 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3426 else if (key->storage == KEY_STORAGE_INCORE)
3427 ndp = data_skip_key(data, ndp, key);
3428 oldincoreoffs[*sp * 2] = dp - data->incoredata;
3429 oldincoreoffs[*sp * 2 + 1] = ndp - dp;
3435 /* just copy over the complete old entry (including the schemaid) if there was no new data */
3436 if (entry >= 0 && !neednewschema && oldschemaid && (!data->attrs || !data->attrs[entry]) && dp)
3438 ndp = data->incoredata + data->incoreoffset[entry];
3439 data->incoreoffset[entry] = newincore.len;
3440 data_addblob(&newincore, ndp, dp - ndp);
3444 /* Now create data blob. We walk through the (possibly new) schema
3445 and either copy over old data, or insert the new. */
3447 data->incoreoffset[entry] = newincore.len;
3448 data_addid(&newincore, schemaid);
3450 /* we don't use a pointer to the schemadata here as repodata_serialize_key
3451 * may call repodata_schema2id() which might realloc our schemadata */
3452 for (schemaidx = data->schemata[schemaid]; (keyid = data->schemadata[schemaidx]) != 0; schemaidx++)
3456 data->mainschemaoffsets[schemaidx - data->schemata[schemaid]] = newincore.len;
3457 if (keyid == solvkeyid)
3459 /* add flexarray entry count */
3460 data_addid(&newincore, data->end - data->start);
3461 break; /* always the last entry */
3464 if (seen[keyid] == -1)
3466 if (oldincoreoffs[keyid * 2 + 1])
3467 data_addblob(&newincore, data->incoredata + oldincoreoffs[keyid * 2], oldincoreoffs[keyid * 2 + 1]);
3469 else if (seen[keyid])
3470 repodata_serialize_key(data, &newincore, &newvincore, schema, data->keys + keyid, seen[keyid] - 1);
3475 if (entry >= 0 && data->attrs)
3477 if (data->attrs[entry])
3478 data->attrs[entry] = solv_free(data->attrs[entry]);
3479 if (entry && entry % 4096 == 0 && data->nxattrs <= 2 && entry + 64 < nentry)
3481 compact_attrdata(data, entry + 1, nentry); /* try to free some memory */
3483 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3484 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3485 printf(" incore data: %d K\n", newincore.len / 1024);
3486 printf(" sum: %d K\n", (newincore.len + data->attrdatalen + data->attriddatalen * 4) / 1024);
3487 /* malloc_stats(); */
3492 /* free all xattrs */
3493 for (entry = 0; entry < data->nxattrs; entry++)
3494 if (data->xattrs[entry])
3495 solv_free(data->xattrs[entry]);
3496 data->xattrs = solv_free(data->xattrs);
3499 data->lasthandle = 0;
3501 data->lastdatalen = 0;
3505 solv_free(oldincoreoffs);
3506 repodata_free_schemahash(data);
3508 solv_free(data->incoredata);
3509 data->incoredata = newincore.buf;
3510 data->incoredatalen = newincore.len;
3511 data->incoredatafree = 0;
3513 solv_free(data->vincore);
3514 data->vincore = newvincore.buf;
3515 data->vincorelen = newvincore.len;
3517 data->attrs = solv_free(data->attrs);
3518 data->attrdata = solv_free(data->attrdata);
3519 data->attriddata = solv_free(data->attriddata);
3520 data->attrnum64data = solv_free(data->attrnum64data);
3521 data->attrdatalen = 0;
3522 data->attriddatalen = 0;
3523 data->attrnum64datalen = 0;
3525 printf("repodata_internalize %d done\n", data->repodataid);
3526 printf(" incore data: %d K\n", data->incoredatalen / 1024);
3531 repodata_disable_paging(Repodata *data)
3533 if (maybe_load_repodata(data, 0))
3535 repopagestore_disable_paging(&data->store);
3541 repodata_load_stub(Repodata *data)
3543 Repo *repo = data->repo;
3544 Pool *pool = repo->pool;
3546 struct _Pool_tmpspace oldtmpspace;
3549 if (!pool->loadcallback)
3551 data->state = REPODATA_ERROR;
3554 data->state = REPODATA_LOADING;
3556 /* save tmp space and pos */
3557 oldtmpspace = pool->tmpspace;
3558 memset(&pool->tmpspace, 0, sizeof(pool->tmpspace));
3561 r = pool->loadcallback(pool, data, pool->loadcallbackdata);
3563 /* restore tmp space and pos */
3564 for (i = 0; i < POOL_TMPSPACEBUF; i++)
3565 solv_free(pool->tmpspace.buf[i]);
3566 pool->tmpspace = oldtmpspace;
3567 if (r && oldpos.repo == repo && oldpos.repodataid == data->repodataid)
3568 memset(&oldpos, 0, sizeof(oldpos));
3571 data->state = r ? REPODATA_AVAILABLE : REPODATA_ERROR;
3575 repodata_add_stubkey(Repodata *data, Id keyname, Id keytype)
3579 xkey.name = keyname;
3580 xkey.type = keytype;
3581 xkey.storage = KEY_STORAGE_INCORE;
3583 repodata_key2id(data, &xkey, 1);
3587 repodata_add_stub(Repodata **datap)
3589 Repodata *data = *datap;
3590 Repo *repo = data->repo;
3591 Id repodataid = data - repo->repodata;
3592 Repodata *sdata = repo_add_repodata(repo, 0);
3593 data = repo->repodata + repodataid;
3594 if (data->end > data->start)
3595 repodata_extend_block(sdata, data->start, data->end - data->start);
3596 sdata->state = REPODATA_STUB;
3597 sdata->loadcallback = repodata_load_stub;
3603 repodata_create_stubs(Repodata *data)
3605 Repo *repo = data->repo;
3606 Pool *pool = repo->pool;
3613 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3614 while (dataiterator_step(&di))
3615 if (di.data == data)
3617 dataiterator_free(&di);
3620 stubdataids = solv_calloc(cnt, sizeof(*stubdataids));
3621 for (i = 0; i < cnt; i++)
3623 sdata = repodata_add_stub(&data);
3624 stubdataids[i] = sdata - repo->repodata;
3627 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3629 while (dataiterator_step(&di))
3631 if (di.data != data)
3633 if (di.key->name == REPOSITORY_EXTERNAL && !di.nparents)
3635 dataiterator_entersub(&di);
3636 sdata = repo->repodata + stubdataids[i++];
3640 switch (di.key->type)
3642 case REPOKEY_TYPE_ID:
3643 repodata_set_id(sdata, SOLVID_META, di.key->name, di.kv.id);
3645 case REPOKEY_TYPE_CONSTANTID:
3646 repodata_set_constantid(sdata, SOLVID_META, di.key->name, di.kv.id);
3648 case REPOKEY_TYPE_STR:
3649 repodata_set_str(sdata, SOLVID_META, di.key->name, di.kv.str);
3651 case REPOKEY_TYPE_VOID:
3652 repodata_set_void(sdata, SOLVID_META, di.key->name);
3654 case REPOKEY_TYPE_NUM:
3655 repodata_set_num(sdata, SOLVID_META, di.key->name, SOLV_KV_NUM64(&di.kv));
3658 repodata_set_bin_checksum(sdata, SOLVID_META, di.key->name, di.key->type, (const unsigned char *)di.kv.str);
3660 case REPOKEY_TYPE_IDARRAY:
3661 repodata_add_idarray(sdata, SOLVID_META, di.key->name, di.kv.id);
3662 if (di.key->name == REPOSITORY_KEYS)
3667 xkeyname = di.kv.id;
3671 repodata_add_stubkey(sdata, xkeyname, di.kv.id);
3680 dataiterator_free(&di);
3681 for (i = 0; i < cnt; i++)
3682 repodata_internalize(repo->repodata + stubdataids[i]);
3683 solv_free(stubdataids);
3688 repodata_memused(Repodata *data)
3690 return data->incoredatalen + data->vincorelen;