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 : "";
354 if (did == 1 && !suf)
359 comp = dirpool_compid(&data->dirpool, parent);
360 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
362 parent = dirpool_parent(&data->dirpool, parent);
367 l += strlen(suf) + 1;
368 p = pool_alloctmpspace(pool, l + 1) + l;
379 comp = dirpool_compid(&data->dirpool, parent);
380 comps = stringpool_id2str(data->localpool ? &data->spool : &pool->ss, comp);
383 strncpy(p, comps, l);
384 parent = dirpool_parent(&data->dirpool, parent);
392 /***************************************************************
396 static inline unsigned char *
397 data_skip_schema(Repodata *data, unsigned char *dp, Id schema)
399 Id *keyp = data->schemadata + data->schemata[schema];
400 for (; *keyp; keyp++)
401 dp = data_skip_key(data, dp, data->keys + *keyp);
405 static unsigned char *
406 data_skip_key(Repodata *data, unsigned char *dp, Repokey *key)
408 int nentries, schema;
411 case REPOKEY_TYPE_FIXARRAY:
412 dp = data_read_id(dp, &nentries);
415 dp = data_read_id(dp, &schema);
417 dp = data_skip_schema(data, dp, schema);
419 case REPOKEY_TYPE_FLEXARRAY:
420 dp = data_read_id(dp, &nentries);
423 dp = data_read_id(dp, &schema);
424 dp = data_skip_schema(data, dp, schema);
428 if (key->storage == KEY_STORAGE_INCORE)
429 dp = data_skip(dp, key->type);
430 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
432 dp = data_skip(dp, REPOKEY_TYPE_ID);
433 dp = data_skip(dp, REPOKEY_TYPE_ID);
439 static unsigned char *
440 forward_to_key(Repodata *data, Id keyid, Id *keyp, unsigned char *dp)
446 if (data->mainschemaoffsets && dp == data->incoredata + data->mainschemaoffsets[0] && keyp == data->schemadata + data->schemata[data->mainschema])
449 for (i = 0; (k = *keyp++) != 0; i++)
451 return data->incoredata + data->mainschemaoffsets[i];
454 while ((k = *keyp++) != 0)
458 if (data->keys[k].storage == KEY_STORAGE_VERTICAL_OFFSET)
460 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip offset */
461 dp = data_skip(dp, REPOKEY_TYPE_ID); /* skip length */
464 if (data->keys[k].storage != KEY_STORAGE_INCORE)
466 dp = data_skip_key(data, dp, data->keys + k);
471 static unsigned char *
472 get_vertical_data(Repodata *data, Repokey *key, Id off, Id len)
477 if (off >= data->lastverticaloffset)
479 off -= data->lastverticaloffset;
480 if ((unsigned int)off + len > data->vincorelen)
482 return data->vincore + off;
484 if ((unsigned int)off + len > key->size)
486 /* we now have the offset, go into vertical */
487 off += data->verticaloffset[key - data->keys];
488 /* fprintf(stderr, "key %d page %d\n", key->name, off / REPOPAGE_BLOBSIZE); */
489 dp = repopagestore_load_page_range(&data->store, off / REPOPAGE_BLOBSIZE, (off + len - 1) / REPOPAGE_BLOBSIZE);
492 dp += off % REPOPAGE_BLOBSIZE;
496 static inline unsigned char *
497 get_data(Repodata *data, Repokey *key, unsigned char **dpp, int advance)
499 unsigned char *dp = *dpp;
503 if (key->storage == KEY_STORAGE_INCORE)
506 *dpp = data_skip_key(data, dp, key);
509 else if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
512 dp = data_read_id(dp, &off);
513 dp = data_read_id(dp, &len);
516 return get_vertical_data(data, key, off, len);
522 load_repodata(Repodata *data)
524 if (data->loadcallback)
526 data->loadcallback(data);
527 if (data->state == REPODATA_AVAILABLE)
530 data->state = REPODATA_ERROR;
535 maybe_load_repodata(Repodata *data, Id keyname)
537 if (keyname && !repodata_precheck_keyname(data, keyname))
538 return 0; /* do not bother... */
545 for (i = 1; i < data->nkeys; i++)
546 if (keyname == data->keys[i].name)
548 if (i == data->nkeys)
551 return load_repodata(data);
554 case REPODATA_AVAILABLE:
555 case REPODATA_LOADING:
558 data->state = REPODATA_ERROR;
563 static inline unsigned char *
564 solvid2data(Repodata *data, Id solvid, Id *schemap)
566 unsigned char *dp = data->incoredata;
569 if (solvid == SOLVID_META)
570 dp += 1; /* offset of "meta" solvable */
571 else if (solvid == SOLVID_POS)
573 Pool *pool = data->repo->pool;
574 if (data->repo != pool->pos.repo)
576 if (data != data->repo->repodata + pool->pos.repodataid)
579 if (pool->pos.dp != 1)
581 *schemap = pool->pos.schema;
587 if (solvid < data->start || solvid >= data->end)
589 dp += data->incoreoffset[solvid - data->start];
591 return data_read_id(dp, schemap);
594 /************************************************************************
598 static unsigned char *
599 find_key_data(Repodata *data, Id solvid, Id keyname, Repokey **keypp)
602 Id schema, *keyp, *kp;
605 if (!maybe_load_repodata(data, keyname))
607 dp = solvid2data(data, solvid, &schema);
610 keyp = data->schemadata + data->schemata[schema];
611 for (kp = keyp; *kp; kp++)
612 if (data->keys[*kp].name == keyname)
616 *keypp = key = data->keys + *kp;
617 if (key->type == REPOKEY_TYPE_DELETED)
619 if (key->type == REPOKEY_TYPE_VOID || key->type == REPOKEY_TYPE_CONSTANT || key->type == REPOKEY_TYPE_CONSTANTID)
620 return dp; /* no need to forward... */
621 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
622 return 0; /* get_data will not work, no need to forward */
623 dp = forward_to_key(data, *kp, keyp, dp);
626 return get_data(data, key, &dp, 0);
630 repodata_lookup_type(Repodata *data, Id solvid, Id keyname)
632 Id schema, *keyp, *kp;
633 if (!maybe_load_repodata(data, keyname))
635 if (!solvid2data(data, solvid, &schema))
637 keyp = data->schemadata + data->schemata[schema];
638 for (kp = keyp; *kp; kp++)
639 if (data->keys[*kp].name == keyname)
640 return data->keys[*kp].type;
645 repodata_lookup_id(Repodata *data, Id solvid, Id keyname)
651 dp = find_key_data(data, solvid, keyname, &key);
654 if (key->type == REPOKEY_TYPE_CONSTANTID)
656 if (key->type != REPOKEY_TYPE_ID)
658 dp = data_read_id(dp, &id);
663 repodata_lookup_str(Repodata *data, Id solvid, Id keyname)
669 dp = find_key_data(data, solvid, keyname, &key);
672 if (key->type == REPOKEY_TYPE_STR)
673 return (const char *)dp;
674 if (key->type == REPOKEY_TYPE_CONSTANTID)
676 else if (key->type == REPOKEY_TYPE_ID)
677 dp = data_read_id(dp, &id);
681 return stringpool_id2str(&data->spool, id);
682 return pool_id2str(data->repo->pool, id);
686 repodata_lookup_num(Repodata *data, Id solvid, Id keyname, unsigned long long *value)
690 unsigned int high, low;
693 dp = find_key_data(data, solvid, keyname, &key);
698 case REPOKEY_TYPE_NUM:
699 data_read_num64(dp, &low, &high);
700 *value = (unsigned long long)high << 32 | low;
702 case REPOKEY_TYPE_U32:
703 data_read_u32(dp, &low);
706 case REPOKEY_TYPE_CONSTANT:
715 repodata_lookup_void(Repodata *data, Id solvid, Id keyname)
721 if (!maybe_load_repodata(data, keyname))
723 dp = solvid2data(data, solvid, &schema);
726 /* can't use find_key_data as we need to test the type */
727 for (keyp = data->schemadata + data->schemata[schema]; *keyp; keyp++)
728 if (data->keys[*keyp].name == keyname && data->keys[*keyp].type == REPOKEY_TYPE_VOID)
733 const unsigned char *
734 repodata_lookup_bin_checksum(Repodata *data, Id solvid, Id keyname, Id *typep)
739 dp = find_key_data(data, solvid, keyname, &key);
754 repodata_lookup_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
762 dp = find_key_data(data, solvid, keyname, &key);
765 if (key->type != REPOKEY_TYPE_IDARRAY)
769 dp = data_read_ideof(dp, &id, &eof);
778 repodata_lookup_binary(Repodata *data, Id solvid, Id keyname, int *lenp)
784 dp = find_key_data(data, solvid, keyname, &key);
785 if (!dp || key->type != REPOKEY_TYPE_BINARY)
790 dp = data_read_id(dp, &len);
796 repodata_globalize_id(Repodata *data, Id id, int create)
798 if (!id || !data || !data->localpool)
800 return pool_str2id(data->repo->pool, stringpool_id2str(&data->spool, id), create);
804 repodata_localize_id(Repodata *data, Id id, int create)
806 if (!id || !data || !data->localpool)
808 return stringpool_str2id(&data->spool, pool_id2str(data->repo->pool, id), create);
812 repodata_translate_id(Repodata *data, Repodata *fromdata, Id id, int create)
814 if (!id || !data || !fromdata)
816 if (!data->localpool || !fromdata->localpool)
818 if (fromdata->localpool)
819 id = repodata_globalize_id(fromdata, id, create);
821 id = repodata_localize_id(data, id, create);
824 /* localpool is set in both data and fromdata */
825 return stringpool_str2id(&data->spool, stringpool_id2str(&fromdata->spool, id), create);
829 repodata_lookup_id_uninternalized(Repodata *data, Id solvid, Id keyname, Id voidid)
834 ap = data->attrs[solvid - data->start];
839 if (data->keys[*ap].name != keyname)
841 if (data->keys[*ap].type == REPOKEY_TYPE_VOID)
843 if (data->keys[*ap].type == REPOKEY_TYPE_ID)
851 repodata_lookup_dirstrarray_uninternalized(Repodata *data, Id solvid, Id keyname, Id *didp, Id *iterp)
855 if (iter == 0) /* find key data */
859 ap = data->attrs[solvid - data->start];
863 if (data->keys[*ap].name == keyname && data->keys[*ap].type == REPOKEY_TYPE_DIRSTRARRAY)
870 for (ap = data->attriddata + iter; *ap; ap += 2)
872 if (did && ap[0] != did)
875 *iterp = ap - data->attriddata + 2;
876 return (const char *)data->attrdata + ap[1];
882 const unsigned char *
883 repodata_lookup_bin_checksum_uninternalized(Repodata *data, Id solvid, Id keyname, Id *typep)
888 ap = data->attrs[solvid - data->start];
893 if (data->keys[*ap].name != keyname)
895 switch (data->keys[*ap].type)
898 *typep = data->keys[*ap].type;
899 return (const unsigned char *)data->attrdata + ap[1];
907 /************************************************************************
913 repodata_stringify(Pool *pool, Repodata *data, Repokey *key, KeyValue *kv, int flags)
917 case REPOKEY_TYPE_ID:
918 case REPOKEY_TYPE_CONSTANTID:
919 case REPOKEY_TYPE_IDARRAY:
920 if (data && data->localpool)
921 kv->str = stringpool_id2str(&data->spool, kv->id);
923 kv->str = pool_id2str(pool, kv->id);
924 if ((flags & SEARCH_SKIP_KIND) != 0 && key->storage == KEY_STORAGE_SOLVABLE)
927 for (s = kv->str; *s >= 'a' && *s <= 'z'; s++)
929 if (*s == ':' && s > kv->str)
933 case REPOKEY_TYPE_STR:
935 case REPOKEY_TYPE_DIRSTRARRAY:
936 if (!(flags & SEARCH_FILES))
937 return kv->str; /* match just the basename */
939 return kv->str; /* already stringified */
940 /* Put the full filename into kv->str. */
941 kv->str = repodata_dir2str(data, kv->id, kv->str);
942 kv->num = 1; /* mark stringification */
945 if (!(flags & SEARCH_CHECKSUMS))
946 return 0; /* skip em */
948 return kv->str; /* already stringified */
949 kv->str = repodata_chk2str(data, key->type, (const unsigned char *)kv->str);
950 kv->num = 1; /* mark stringification */
958 struct subschema_data {
964 /* search a specific repodata */
966 repodata_search(Repodata *data, Id solvid, Id keyname, int flags, int (*callback)(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv), void *cbdata)
970 Id keyid, *kp, *keyp;
971 unsigned char *dp, *ddp;
977 if (!maybe_load_repodata(data, keyname))
979 if (solvid == SOLVID_SUBSCHEMA)
981 struct subschema_data *subd = cbdata;
982 cbdata = subd->cbdata;
984 schema = subd->parent->id;
985 dp = (unsigned char *)subd->parent->str;
986 kv.parent = subd->parent;
991 dp = solvid2data(data, solvid, &schema);
994 s = data->repo->pool->solvables + solvid;
997 keyp = data->schemadata + data->schemata[schema];
1000 /* search for a specific key */
1001 for (kp = keyp; *kp; kp++)
1002 if (data->keys[*kp].name == keyname)
1006 dp = forward_to_key(data, *kp, keyp, dp);
1012 while ((keyid = *keyp++) != 0)
1015 key = data->keys + keyid;
1016 ddp = get_data(data, key, &dp, *keyp ? 1 : 0);
1018 if (key->type == REPOKEY_TYPE_DELETED)
1020 if (key->type == REPOKEY_TYPE_FLEXARRAY || key->type == REPOKEY_TYPE_FIXARRAY)
1022 struct subschema_data subd;
1026 subd.cbdata = cbdata;
1029 ddp = data_read_id(ddp, &nentries);
1033 while (ddp && nentries > 0)
1037 if (key->type == REPOKEY_TYPE_FLEXARRAY || !kv.entry)
1038 ddp = data_read_id(ddp, &schema);
1040 kv.str = (char *)ddp;
1041 stop = callback(cbdata, s, data, key, &kv);
1042 if (stop > SEARCH_NEXT_KEY)
1044 if (stop && stop != SEARCH_ENTERSUB)
1046 if ((flags & SEARCH_SUB) != 0 || stop == SEARCH_ENTERSUB)
1047 repodata_search(data, SOLVID_SUBSCHEMA, 0, flags, callback, &subd);
1048 ddp = data_skip_schema(data, ddp, schema);
1051 if (!nentries && (flags & SEARCH_ARRAYSENTINEL) != 0)
1055 kv.str = (char *)ddp;
1056 stop = callback(cbdata, s, data, key, &kv);
1057 if (stop > SEARCH_NEXT_KEY)
1067 ddp = data_fetch(ddp, &kv, key);
1070 stop = callback(cbdata, s, data, key, &kv);
1073 while (!kv.eof && !stop);
1074 if (onekey || stop > SEARCH_NEXT_KEY)
1080 repodata_setpos_kv(Repodata *data, KeyValue *kv)
1082 Pool *pool = data->repo->pool;
1084 pool_clear_pos(pool);
1087 pool->pos.repo = data->repo;
1088 pool->pos.repodataid = data - data->repo->repodata;
1089 pool->pos.dp = (unsigned char *)kv->str - data->incoredata;
1090 pool->pos.schema = kv->id;
1094 /************************************************************************
1095 * data iterator functions
1099 solvabledata_fetch(Solvable *s, KeyValue *kv, Id keyname)
1113 case SOLVABLE_VENDOR:
1116 case SOLVABLE_PROVIDES:
1118 return s->provides ? s->repo->idarraydata + s->provides : 0;
1119 case SOLVABLE_OBSOLETES:
1121 return s->obsoletes ? s->repo->idarraydata + s->obsoletes : 0;
1122 case SOLVABLE_CONFLICTS:
1124 return s->conflicts ? s->repo->idarraydata + s->conflicts : 0;
1125 case SOLVABLE_REQUIRES:
1127 return s->requires ? s->repo->idarraydata + s->requires : 0;
1128 case SOLVABLE_RECOMMENDS:
1130 return s->recommends ? s->repo->idarraydata + s->recommends : 0;
1131 case SOLVABLE_SUPPLEMENTS:
1133 return s->supplements ? s->repo->idarraydata + s->supplements : 0;
1134 case SOLVABLE_SUGGESTS:
1136 return s->suggests ? s->repo->idarraydata + s->suggests : 0;
1137 case SOLVABLE_ENHANCES:
1139 return s->enhances ? s->repo->idarraydata + s->enhances : 0;
1142 return s->repo->rpmdbid ? s->repo->rpmdbid + (s - s->repo->pool->solvables - s->repo->start) : 0;
1149 datamatcher_init(Datamatcher *ma, const char *match, int flags)
1151 match = match ? solv_strdup(match) : 0;
1156 if ((flags & SEARCH_STRINGMASK) == SEARCH_REGEX)
1158 ma->matchdata = solv_calloc(1, sizeof(regex_t));
1159 ma->error = regcomp((regex_t *)ma->matchdata, match, REG_EXTENDED | REG_NOSUB | REG_NEWLINE | ((flags & SEARCH_NOCASE) ? REG_ICASE : 0));
1162 solv_free(ma->matchdata);
1163 ma->flags = (flags & ~SEARCH_STRINGMASK) | SEARCH_ERROR;
1166 if ((flags & SEARCH_FILES) != 0 && match)
1168 /* prepare basename check */
1169 if ((flags & SEARCH_STRINGMASK) == SEARCH_STRING || (flags & SEARCH_STRINGMASK) == SEARCH_STRINGEND)
1171 const char *p = strrchr(match, '/');
1172 ma->matchdata = (void *)(p ? p + 1 : match);
1174 else if ((flags & SEARCH_STRINGMASK) == SEARCH_GLOB)
1177 for (p = match + strlen(match) - 1; p >= match; p--)
1178 if (*p == '[' || *p == ']' || *p == '*' || *p == '?' || *p == '/')
1180 ma->matchdata = (void *)(p + 1);
1187 datamatcher_free(Datamatcher *ma)
1190 ma->match = solv_free((char *)ma->match);
1191 if ((ma->flags & SEARCH_STRINGMASK) == SEARCH_REGEX && ma->matchdata)
1193 regfree(ma->matchdata);
1194 solv_free(ma->matchdata);
1200 datamatcher_match(Datamatcher *ma, const char *str)
1203 switch ((ma->flags & SEARCH_STRINGMASK))
1205 case SEARCH_SUBSTRING:
1206 if (ma->flags & SEARCH_NOCASE)
1207 return strcasestr(str, ma->match) != 0;
1209 return strstr(str, ma->match) != 0;
1211 if (ma->flags & SEARCH_NOCASE)
1212 return !strcasecmp(ma->match, str);
1214 return !strcmp(ma->match, str);
1215 case SEARCH_STRINGSTART:
1216 if (ma->flags & SEARCH_NOCASE)
1217 return !strncasecmp(ma->match, str, strlen(ma->match));
1219 return !strncmp(ma->match, str, strlen(ma->match));
1220 case SEARCH_STRINGEND:
1221 l = strlen(str) - strlen(ma->match);
1224 if (ma->flags & SEARCH_NOCASE)
1225 return !strcasecmp(ma->match, str + l);
1227 return !strcmp(ma->match, str + l);
1229 return !fnmatch(ma->match, str, (ma->flags & SEARCH_NOCASE) ? FNM_CASEFOLD : 0);
1231 return !regexec((const regex_t *)ma->matchdata, str, 0, NULL, 0);
1237 /* check if the matcher can match the provides basename */
1240 datamatcher_checkbasename(Datamatcher *ma, const char *basename)
1243 const char *match = ma->matchdata;
1246 switch (ma->flags & SEARCH_STRINGMASK)
1250 case SEARCH_STRINGEND:
1251 if (match != ma->match)
1252 break; /* had slash, do exact match on basename */
1255 /* check if the basename ends with match */
1256 l = strlen(basename) - strlen(match);
1262 return 1; /* maybe matches */
1264 if ((ma->flags & SEARCH_NOCASE) != 0)
1265 return !strcasecmp(match, basename);
1267 return !strcmp(match, basename);
1271 repodata_filelistfilter_matches(Repodata *data, const char *str)
1273 /* '.*bin\/.*', '^\/etc\/.*', '^\/usr\/lib\/sendmail$' */
1274 /* for now hardcoded */
1275 if (strstr(str, "bin/"))
1277 if (!strncmp(str, "/etc/", 5))
1279 if (!strcmp(str, "/usr/lib/sendmail"))
1301 di_nextarrayelement,
1307 di_entersolvablekey,
1311 /* see dataiterator.h for documentation */
1313 dataiterator_init(Dataiterator *di, Pool *pool, Repo *repo, Id p, Id keyname, const char *match, int flags)
1315 memset(di, 0, sizeof(*di));
1317 di->flags = flags & ~SEARCH_THISSOLVID;
1318 if (!pool || (repo && repo->pool != pool))
1326 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1332 di->keyname = keyname;
1333 di->keynames[0] = keyname;
1334 dataiterator_set_search(di, repo, p);
1339 dataiterator_init_clone(Dataiterator *di, Dataiterator *from)
1344 if (di->dupstr == di->kv.str)
1345 di->dupstr = solv_memdup(di->dupstr, di->dupstrn);
1352 memset(&di->matcher, 0, sizeof(di->matcher));
1353 if (from->matcher.match)
1354 datamatcher_init(&di->matcher, from->matcher.match, from->matcher.flags);
1359 for (i = 1; i < di->nparents; i++)
1360 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1361 di->kv.parent = &di->parents[di->nparents - 1].kv;
1366 dataiterator_set_match(Dataiterator *di, const char *match, int flags)
1368 di->flags = (flags & ~SEARCH_THISSOLVID) | (di->flags & SEARCH_THISSOLVID);
1369 datamatcher_free(&di->matcher);
1370 memset(&di->matcher, 0, sizeof(di->matcher));
1374 if ((error = datamatcher_init(&di->matcher, match, flags)) != 0)
1384 dataiterator_set_search(Dataiterator *di, Repo *repo, Id p)
1388 di->flags &= ~SEARCH_THISSOLVID;
1392 if (!di->pool->urepos)
1400 di->repo = di->pool->repos[di->repoid];
1402 di->state = di_enterrepo;
1404 dataiterator_jump_to_solvid(di, p);
1408 dataiterator_set_keyname(Dataiterator *di, Id keyname)
1411 di->keyname = keyname;
1412 di->keynames[0] = keyname;
1416 dataiterator_prepend_keyname(Dataiterator *di, Id keyname)
1420 if (di->nkeynames >= sizeof(di->keynames)/sizeof(*di->keynames) - 2)
1422 di->state = di_bye; /* sorry */
1425 for (i = di->nkeynames + 1; i > 0; i--)
1426 di->keynames[i] = di->keynames[i - 1];
1427 di->keynames[0] = di->keyname = keyname;
1432 dataiterator_free(Dataiterator *di)
1434 if (di->matcher.match)
1435 datamatcher_free(&di->matcher);
1437 solv_free(di->dupstr);
1440 static unsigned char *
1441 dataiterator_find_keyname(Dataiterator *di, Id keyname)
1444 Repokey *keys = di->data->keys, *key;
1447 for (keyp = di->keyp; *keyp; keyp++)
1448 if (keys[*keyp].name == keyname)
1453 if (key->type == REPOKEY_TYPE_DELETED)
1455 if (key->storage != KEY_STORAGE_INCORE && key->storage != KEY_STORAGE_VERTICAL_OFFSET)
1456 return 0; /* get_data will not work, no need to forward */
1457 dp = forward_to_key(di->data, *keyp, di->keyp, di->dp);
1465 is_filelist_extension(Repodata *data)
1468 if (!repodata_precheck_keyname(data, SOLVABLE_FILELIST))
1470 for (j = 1; j < data->nkeys; j++)
1471 if (data->keys[j].name == SOLVABLE_FILELIST)
1473 if (j == data->nkeys)
1475 if (data->state != REPODATA_AVAILABLE)
1477 for (j = 1; j < data->nkeys; j++)
1478 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1484 dataiterator_filelistcheck(Dataiterator *di)
1487 int needcomplete = 0;
1488 Repodata *data = di->data;
1490 if ((di->flags & SEARCH_COMPLETE_FILELIST) != 0)
1491 if (!di->matcher.match
1492 || ((di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_STRING
1493 && (di->matcher.flags & (SEARCH_STRINGMASK|SEARCH_NOCASE)) != SEARCH_GLOB)
1494 || !repodata_filelistfilter_matches(data, di->matcher.match))
1496 if (data->state != REPODATA_AVAILABLE)
1497 return needcomplete ? 1 : 0;
1500 /* we don't need the complete filelist, so ignore all stubs */
1501 if (data->repo->nrepodata == 2)
1503 for (j = 1; j < data->nkeys; j++)
1504 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1510 /* we need the complete filelist. check if we habe a filtered filelist and there's
1511 * a extension with the complete filelist later on */
1512 for (j = 1; j < data->nkeys; j++)
1513 if (data->keys[j].name == SOLVABLE_FILELIST)
1515 if (j == data->nkeys)
1516 return 0; /* does not have filelist */
1517 for (j = 1; j < data->nkeys; j++)
1518 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1520 if (j == data->nkeys)
1521 return 1; /* this is the externsion */
1522 while (data - data->repo->repodata + 1 < data->repo->nrepodata)
1525 if (is_filelist_extension(data))
1533 dataiterator_step(Dataiterator *di)
1537 if (di->state == di_nextattr && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET && di->vert_ddp && di->vert_storestate != di->data->storestate) {
1538 unsigned int ddpoff = di->ddp - di->vert_ddp;
1539 di->vert_off += ddpoff;
1540 di->vert_len -= ddpoff;
1541 di->ddp = di->vert_ddp = get_vertical_data(di->data, di->key, di->vert_off, di->vert_len);
1542 di->vert_storestate = di->data->storestate;
1544 di->state = di_nextkey;
1550 case di_enterrepo: di_enterrepo:
1551 if (!di->repo || (di->repo->disabled && !(di->flags & SEARCH_DISABLED_REPOS)))
1553 if (!(di->flags & SEARCH_THISSOLVID))
1555 di->solvid = di->repo->start - 1; /* reset solvid iterator */
1556 goto di_nextsolvable;
1560 case di_entersolvable: di_entersolvable:
1563 di->repodataid = 1; /* reset repodata iterator */
1564 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)
1566 extern Repokey repo_solvablekeys[RPM_RPMDBID - SOLVABLE_NAME + 1];
1568 di->key = repo_solvablekeys + (di->keyname ? di->keyname - SOLVABLE_NAME : 0);
1570 goto di_entersolvablekey;
1575 case di_enterrepodata: di_enterrepodata:
1578 if (di->repodataid >= di->repo->nrepodata)
1579 goto di_nextsolvable;
1580 di->data = di->repo->repodata + di->repodataid;
1582 if (di->repodataid && di->keyname == SOLVABLE_FILELIST && !dataiterator_filelistcheck(di))
1583 goto di_nextrepodata;
1584 if (!maybe_load_repodata(di->data, di->keyname))
1585 goto di_nextrepodata;
1586 di->dp = solvid2data(di->data, di->solvid, &schema);
1588 goto di_nextrepodata;
1589 if (di->solvid == SOLVID_POS)
1590 di->solvid = di->pool->pos.solvid;
1591 /* reset key iterator */
1592 di->keyp = di->data->schemadata + di->data->schemata[schema];
1595 case di_enterschema: di_enterschema:
1597 di->dp = dataiterator_find_keyname(di, di->keyname);
1598 if (!di->dp || !*di->keyp)
1602 goto di_nextrepodata;
1606 case di_enterkey: di_enterkey:
1608 di->key = di->data->keys + *di->keyp;
1611 /* this is get_data() modified to store vert_ data */
1612 if (di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
1615 di->dp = data_read_id(di->dp, &off);
1616 di->dp = data_read_id(di->dp, &len);
1617 di->vert_ddp = di->ddp = get_vertical_data(di->data, di->key, off, len);
1620 di->vert_storestate = di->data->storestate;
1622 else if (di->key->storage == KEY_STORAGE_INCORE)
1625 if (di->keyp[1] && (!di->keyname || (di->flags & SEARCH_SUB) != 0))
1626 di->dp = data_skip_key(di->data, di->dp, di->key);
1632 if (di->key->type == REPOKEY_TYPE_DELETED)
1634 if (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY)
1636 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1642 di->ddp = data_fetch(di->ddp, &di->kv, di->key);
1644 di->state = di_nextkey;
1646 di->state = di_nextattr;
1649 case di_nextkey: di_nextkey:
1650 if (!di->keyname && *++di->keyp)
1656 case di_nextrepodata: di_nextrepodata:
1657 if (di->repodataid && ++di->repodataid < di->repo->nrepodata)
1658 goto di_enterrepodata;
1661 case di_nextsolvable: di_nextsolvable:
1662 if (!(di->flags & SEARCH_THISSOLVID))
1665 di->solvid = di->repo->start;
1668 for (; di->solvid < di->repo->end; di->solvid++)
1670 if (di->pool->solvables[di->solvid].repo == di->repo)
1671 goto di_entersolvable;
1676 case di_nextrepo: di_nextrepo:
1681 if (di->repoid < di->pool->nrepos)
1683 di->repo = di->pool->repos[di->repoid];
1689 case di_bye: di_bye:
1693 case di_enterarray: di_enterarray:
1694 if (di->key->name == REPOSITORY_SOLVABLES)
1696 di->ddp = data_read_id(di->ddp, (Id *)&di->kv.num);
1701 case di_nextarrayelement: di_nextarrayelement:
1704 di->ddp = data_skip_schema(di->data, di->ddp, di->kv.id);
1705 if (di->kv.entry == di->kv.num)
1707 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1709 if (!(di->flags & SEARCH_ARRAYSENTINEL))
1711 di->kv.str = (char *)di->ddp;
1713 di->state = di_nextkey;
1716 if (di->kv.entry == di->kv.num - 1)
1718 if (di->key->type == REPOKEY_TYPE_FLEXARRAY || !di->kv.entry)
1719 di->ddp = data_read_id(di->ddp, &di->kv.id);
1720 di->kv.str = (char *)di->ddp;
1721 if (di->nkeynames && di->nparents - di->rootlevel < di->nkeynames)
1723 if ((di->flags & SEARCH_SUB) != 0)
1724 di->state = di_entersub;
1726 di->state = di_nextarrayelement;
1729 case di_entersub: di_entersub:
1730 if (di->nparents == sizeof(di->parents)/sizeof(*di->parents) - 1)
1731 goto di_nextarrayelement; /* sorry, full */
1732 di->parents[di->nparents].kv = di->kv;
1733 di->parents[di->nparents].dp = di->dp;
1734 di->parents[di->nparents].keyp = di->keyp;
1735 di->dp = (unsigned char *)di->kv.str;
1736 di->keyp = di->data->schemadata + di->data->schemata[di->kv.id];
1737 memset(&di->kv, 0, sizeof(di->kv));
1738 di->kv.parent = &di->parents[di->nparents].kv;
1740 di->keyname = di->keynames[di->nparents - di->rootlevel];
1741 goto di_enterschema;
1743 case di_leavesub: di_leavesub:
1744 if (di->nparents - 1 < di->rootlevel)
1747 di->dp = di->parents[di->nparents].dp;
1748 di->kv = di->parents[di->nparents].kv;
1749 di->keyp = di->parents[di->nparents].keyp;
1750 di->key = di->data->keys + *di->keyp;
1751 di->ddp = (unsigned char *)di->kv.str;
1752 di->keyname = di->keynames[di->nparents - di->rootlevel];
1753 goto di_nextarrayelement;
1755 /* special solvable attr handling follows */
1757 case di_nextsolvablekey: di_nextsolvablekey:
1758 if (di->keyname || di->key->name == RPM_RPMDBID)
1759 goto di_enterrepodata;
1763 case di_entersolvablekey: di_entersolvablekey:
1764 di->idp = solvabledata_fetch(di->pool->solvables + di->solvid, &di->kv, di->key->name);
1765 if (!di->idp || !*di->idp)
1766 goto di_nextsolvablekey;
1770 di->kv.id = *di->idp;
1771 di->kv.num = *di->idp; /* for rpmdbid */
1772 di->kv.num2 = 0; /* for rpmdbid */
1774 di->state = di_nextsolvablekey;
1780 case di_nextsolvableattr:
1781 di->state = di_nextsolvableattr;
1782 di->kv.id = *di->idp++;
1787 di->state = di_nextsolvablekey;
1793 if (di->matcher.match)
1796 /* simple pre-check so that we don't need to stringify */
1797 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->matcher.flags & SEARCH_FILES) != 0)
1798 if (!datamatcher_checkbasename(&di->matcher, di->kv.str))
1800 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
1802 if (di->keyname && (di->key->type == REPOKEY_TYPE_FIXARRAY || di->key->type == REPOKEY_TYPE_FLEXARRAY))
1806 if (!datamatcher_match(&di->matcher, str))
1811 if (di->keyname == SOLVABLE_FILELIST && di->key->type == REPOKEY_TYPE_DIRSTRARRAY && (di->flags & SEARCH_FILES) != 0)
1812 repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags);
1814 /* found something! */
1820 dataiterator_entersub(Dataiterator *di)
1822 if (di->state == di_nextarrayelement)
1823 di->state = di_entersub;
1827 dataiterator_setpos(Dataiterator *di)
1829 if (di->kv.eof == 2)
1831 pool_clear_pos(di->pool);
1834 di->pool->pos.solvid = di->solvid;
1835 di->pool->pos.repo = di->repo;
1836 di->pool->pos.repodataid = di->data - di->repo->repodata;
1837 di->pool->pos.schema = di->kv.id;
1838 di->pool->pos.dp = (unsigned char *)di->kv.str - di->data->incoredata;
1842 dataiterator_setpos_parent(Dataiterator *di)
1844 if (!di->kv.parent || di->kv.parent->eof == 2)
1846 pool_clear_pos(di->pool);
1849 di->pool->pos.solvid = di->solvid;
1850 di->pool->pos.repo = di->repo;
1851 di->pool->pos.repodataid = di->data - di->repo->repodata;
1852 di->pool->pos.schema = di->kv.parent->id;
1853 di->pool->pos.dp = (unsigned char *)di->kv.parent->str - di->data->incoredata;
1856 /* clones just the position, not the search keys/matcher */
1858 dataiterator_clonepos(Dataiterator *di, Dataiterator *from)
1860 di->state = from->state;
1861 di->flags &= ~SEARCH_THISSOLVID;
1862 di->flags |= (from->flags & SEARCH_THISSOLVID);
1863 di->repo = from->repo;
1864 di->data = from->data;
1866 di->ddp = from->ddp;
1867 di->idp = from->idp;
1868 di->keyp = from->keyp;
1869 di->key = from->key;
1871 di->repodataid = from->repodataid;
1872 di->solvid = from->solvid;
1873 di->repoid = from->repoid;
1874 di->rootlevel = from->rootlevel;
1875 memcpy(di->parents, from->parents, sizeof(from->parents));
1876 di->nparents = from->nparents;
1880 for (i = 1; i < di->nparents; i++)
1881 di->parents[i].kv.parent = &di->parents[i - 1].kv;
1882 di->kv.parent = &di->parents[di->nparents - 1].kv;
1886 if (from->dupstr && from->dupstr == from->kv.str)
1888 di->dupstrn = from->dupstrn;
1889 di->dupstr = solv_memdup(from->dupstr, from->dupstrn);
1894 dataiterator_seek(Dataiterator *di, int whence)
1896 if ((whence & DI_SEEK_STAY) != 0)
1897 di->rootlevel = di->nparents;
1898 switch (whence & ~DI_SEEK_STAY)
1901 if (di->state != di_nextarrayelement)
1903 if ((whence & DI_SEEK_STAY) != 0)
1904 di->rootlevel = di->nparents + 1; /* XXX: dangerous! */
1905 di->state = di_entersub;
1907 case DI_SEEK_PARENT:
1914 if (di->rootlevel > di->nparents)
1915 di->rootlevel = di->nparents;
1916 di->dp = di->parents[di->nparents].dp;
1917 di->kv = di->parents[di->nparents].kv;
1918 di->keyp = di->parents[di->nparents].keyp;
1919 di->key = di->data->keys + *di->keyp;
1920 di->ddp = (unsigned char *)di->kv.str;
1921 di->keyname = di->keynames[di->nparents - di->rootlevel];
1922 di->state = di_nextarrayelement;
1924 case DI_SEEK_REWIND:
1930 di->dp = (unsigned char *)di->kv.parent->str;
1931 di->keyp = di->data->schemadata + di->data->schemata[di->kv.parent->id];
1932 di->state = di_enterschema;
1940 dataiterator_skip_attribute(Dataiterator *di)
1942 if (di->state == di_nextsolvableattr)
1943 di->state = di_nextsolvablekey;
1945 di->state = di_nextkey;
1949 dataiterator_skip_solvable(Dataiterator *di)
1954 di->keyname = di->keynames[0];
1955 di->state = di_nextsolvable;
1959 dataiterator_skip_repo(Dataiterator *di)
1964 di->keyname = di->keynames[0];
1965 di->state = di_nextrepo;
1969 dataiterator_jump_to_solvid(Dataiterator *di, Id solvid)
1974 di->keyname = di->keynames[0];
1975 if (solvid == SOLVID_POS)
1977 di->repo = di->pool->pos.repo;
1984 if (!di->pool->pos.repodataid && di->pool->pos.solvid == SOLVID_META) {
1985 solvid = SOLVID_META; /* META pos hack */
1987 di->data = di->repo->repodata + di->pool->pos.repodataid;
1991 else if (solvid > 0)
1993 di->repo = di->pool->solvables[solvid].repo;
1998 if (!di->pool->urepos)
2004 di->repo = di->pool->repos[di->repoid];
2006 if (solvid != SOLVID_POS)
2008 di->solvid = solvid;
2010 di->flags |= SEARCH_THISSOLVID;
2011 di->state = di_enterrepo;
2015 dataiterator_jump_to_repo(Dataiterator *di, Repo *repo)
2021 di->repoid = 0; /* 0 means stay at repo */
2024 di->flags &= ~SEARCH_THISSOLVID;
2025 di->state = di_enterrepo;
2029 dataiterator_match(Dataiterator *di, Datamatcher *ma)
2032 if (!(str = repodata_stringify(di->pool, di->data, di->key, &di->kv, di->flags)))
2034 return ma ? datamatcher_match(ma, str) : 1;
2038 dataiterator_strdup(Dataiterator *di)
2042 if (!di->kv.str || di->kv.str == di->dupstr)
2044 switch (di->key->type)
2047 case REPOKEY_TYPE_DIRSTRARRAY:
2048 if (di->kv.num) /* was it stringified into tmp space? */
2049 l = strlen(di->kv.str) + 1;
2054 if (l < 0 && di->key->storage == KEY_STORAGE_VERTICAL_OFFSET)
2056 switch (di->key->type)
2058 case REPOKEY_TYPE_STR:
2059 case REPOKEY_TYPE_DIRSTRARRAY:
2060 l = strlen(di->kv.str) + 1;
2063 l = solv_chksum_len(di->key->type);
2065 case REPOKEY_TYPE_BINARY:
2072 if (!di->dupstrn || di->dupstrn < l)
2074 di->dupstrn = l + 16;
2075 di->dupstr = solv_realloc(di->dupstr, di->dupstrn);
2078 memcpy(di->dupstr, di->kv.str, l);
2079 di->kv.str = di->dupstr;
2083 /************************************************************************
2084 * data modify functions
2087 /* extend repodata so that it includes solvables p */
2089 repodata_extend(Repodata *data, Id p)
2091 if (data->start == data->end)
2092 data->start = data->end = p;
2095 int old = data->end - data->start;
2096 int new = p - data->end + 1;
2099 data->attrs = solv_extend(data->attrs, old, new, sizeof(Id *), REPODATA_BLOCK);
2100 memset(data->attrs + old, 0, new * sizeof(Id *));
2102 data->incoreoffset = solv_extend(data->incoreoffset, old, new, sizeof(Id), REPODATA_BLOCK);
2103 memset(data->incoreoffset + old, 0, new * sizeof(Id));
2106 if (p < data->start)
2108 int old = data->end - data->start;
2109 int new = data->start - p;
2112 data->attrs = solv_extend_resize(data->attrs, old + new, sizeof(Id *), REPODATA_BLOCK);
2113 memmove(data->attrs + new, data->attrs, old * sizeof(Id *));
2114 memset(data->attrs, 0, new * sizeof(Id *));
2116 data->incoreoffset = solv_extend_resize(data->incoreoffset, old + new, sizeof(Id), REPODATA_BLOCK);
2117 memmove(data->incoreoffset + new, data->incoreoffset, old * sizeof(Id));
2118 memset(data->incoreoffset, 0, new * sizeof(Id));
2123 /* shrink end of repodata */
2125 repodata_shrink(Repodata *data, int end)
2129 if (data->end <= end)
2131 if (data->start >= end)
2135 for (i = 0; i < data->end - data->start; i++)
2136 solv_free(data->attrs[i]);
2137 data->attrs = solv_free(data->attrs);
2139 data->incoreoffset = solv_free(data->incoreoffset);
2140 data->start = data->end = 0;
2145 for (i = end; i < data->end; i++)
2146 solv_free(data->attrs[i - data->start]);
2147 data->attrs = solv_extend_resize(data->attrs, end - data->start, sizeof(Id *), REPODATA_BLOCK);
2149 if (data->incoreoffset)
2150 data->incoreoffset = solv_extend_resize(data->incoreoffset, end - data->start, sizeof(Id), REPODATA_BLOCK);
2154 /* extend repodata so that it includes solvables from start to start + num - 1 */
2156 repodata_extend_block(Repodata *data, Id start, Id num)
2160 if (!data->incoreoffset)
2162 /* this also means that data->attrs is NULL */
2163 data->incoreoffset = solv_calloc_block(num, sizeof(Id), REPODATA_BLOCK);
2164 data->start = start;
2165 data->end = start + num;
2168 repodata_extend(data, start);
2170 repodata_extend(data, start + num - 1);
2173 /**********************************************************************/
2176 #define REPODATA_ATTRS_BLOCK 31
2177 #define REPODATA_ATTRDATA_BLOCK 1023
2178 #define REPODATA_ATTRIDDATA_BLOCK 63
2179 #define REPODATA_ATTRNUM64DATA_BLOCK 15
2183 repodata_new_handle(Repodata *data)
2187 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2188 data->nxattrs = 2; /* -1: SOLVID_META */
2190 data->xattrs = solv_extend(data->xattrs, data->nxattrs, 1, sizeof(Id *), REPODATA_BLOCK);
2191 data->xattrs[data->nxattrs] = 0;
2192 return -(data->nxattrs++);
2196 repodata_get_attrp(Repodata *data, Id handle)
2200 if (handle == SOLVID_META && !data->xattrs)
2202 data->xattrs = solv_calloc_block(1, sizeof(Id *), REPODATA_BLOCK);
2205 return data->xattrs - handle;
2207 if (handle < data->start || handle >= data->end)
2208 repodata_extend(data, handle);
2210 data->attrs = solv_calloc_block(data->end - data->start, sizeof(Id *), REPODATA_BLOCK);
2211 return data->attrs + (handle - data->start);
2215 repodata_insert_keyid(Repodata *data, Id handle, Id keyid, Id val, int overwrite)
2221 app = repodata_get_attrp(data, handle);
2226 /* Determine equality based on the name only, allows us to change
2227 type (when overwrite is set), and makes TYPE_CONSTANT work. */
2228 for (pp = ap; *pp; pp += 2)
2229 if (data->keys[*pp].name == data->keys[keyid].name)
2233 if (overwrite || data->keys[*pp].type == REPOKEY_TYPE_DELETED)
2242 ap = solv_extend(ap, i, 3, sizeof(Id), REPODATA_ATTRS_BLOCK);
2252 repodata_set(Repodata *data, Id solvid, Repokey *key, Id val)
2256 keyid = repodata_key2id(data, key, 1);
2257 repodata_insert_keyid(data, solvid, keyid, val, 1);
2261 repodata_set_id(Repodata *data, Id solvid, Id keyname, Id id)
2265 key.type = REPOKEY_TYPE_ID;
2267 key.storage = KEY_STORAGE_INCORE;
2268 repodata_set(data, solvid, &key, id);
2272 repodata_set_num(Repodata *data, Id solvid, Id keyname, unsigned long long num)
2276 key.type = REPOKEY_TYPE_NUM;
2278 key.storage = KEY_STORAGE_INCORE;
2279 if (num >= 0x80000000)
2281 data->attrnum64data = solv_extend(data->attrnum64data, data->attrnum64datalen, 1, sizeof(unsigned long long), REPODATA_ATTRNUM64DATA_BLOCK);
2282 data->attrnum64data[data->attrnum64datalen] = num;
2283 num = 0x80000000 | data->attrnum64datalen++;
2285 repodata_set(data, solvid, &key, (Id)num);
2289 repodata_set_poolstr(Repodata *data, Id solvid, Id keyname, const char *str)
2293 if (data->localpool)
2294 id = stringpool_str2id(&data->spool, str, 1);
2296 id = pool_str2id(data->repo->pool, str, 1);
2298 key.type = REPOKEY_TYPE_ID;
2300 key.storage = KEY_STORAGE_INCORE;
2301 repodata_set(data, solvid, &key, id);
2305 repodata_set_constant(Repodata *data, Id solvid, Id keyname, unsigned int constant)
2309 key.type = REPOKEY_TYPE_CONSTANT;
2310 key.size = constant;
2311 key.storage = KEY_STORAGE_INCORE;
2312 repodata_set(data, solvid, &key, 0);
2316 repodata_set_constantid(Repodata *data, Id solvid, Id keyname, Id id)
2320 key.type = REPOKEY_TYPE_CONSTANTID;
2322 key.storage = KEY_STORAGE_INCORE;
2323 repodata_set(data, solvid, &key, 0);
2327 repodata_set_void(Repodata *data, Id solvid, Id keyname)
2331 key.type = REPOKEY_TYPE_VOID;
2333 key.storage = KEY_STORAGE_INCORE;
2334 repodata_set(data, solvid, &key, 0);
2338 repodata_set_str(Repodata *data, Id solvid, Id keyname, const char *str)
2343 l = strlen(str) + 1;
2345 key.type = REPOKEY_TYPE_STR;
2347 key.storage = KEY_STORAGE_INCORE;
2348 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2349 memcpy(data->attrdata + data->attrdatalen, str, l);
2350 repodata_set(data, solvid, &key, data->attrdatalen);
2351 data->attrdatalen += l;
2355 repodata_set_binary(Repodata *data, Id solvid, Id keyname, void *buf, int len)
2363 key.type = REPOKEY_TYPE_BINARY;
2365 key.storage = KEY_STORAGE_INCORE;
2366 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, len + 5, 1, REPODATA_ATTRDATA_BLOCK);
2367 dp = data->attrdata + data->attrdatalen;
2368 if (len >= (1 << 14))
2370 if (len >= (1 << 28))
2371 *dp++ = (len >> 28) | 128;
2372 if (len >= (1 << 21))
2373 *dp++ = (len >> 21) | 128;
2374 *dp++ = (len >> 14) | 128;
2376 if (len >= (1 << 7))
2377 *dp++ = (len >> 7) | 128;
2380 memcpy(dp, buf, len);
2381 repodata_set(data, solvid, &key, data->attrdatalen);
2382 data->attrdatalen = dp + len - data->attrdata;
2385 /* add an array element consisting of entrysize Ids to the repodata. modifies attriddata
2386 * so that the caller can append entrysize new elements plus the termination zero there */
2388 repodata_add_array(Repodata *data, Id handle, Id keyname, Id keytype, int entrysize)
2391 Id *ida, *pp, **ppp;
2393 /* check if it is the same as last time, this speeds things up a lot */
2394 if (handle == data->lasthandle && data->keys[data->lastkey].name == keyname && data->keys[data->lastkey].type == keytype && data->attriddatalen == data->lastdatalen)
2396 /* great! just append the new data */
2397 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2398 data->attriddatalen--; /* overwrite terminating 0 */
2399 data->lastdatalen += entrysize;
2403 ppp = repodata_get_attrp(data, handle);
2407 for (; *pp; pp += 2)
2408 if (data->keys[*pp].name == keyname)
2411 if (!pp || !*pp || data->keys[*pp].type != keytype)
2413 /* not found. allocate new key */
2419 key.storage = KEY_STORAGE_INCORE;
2420 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2421 keyid = repodata_key2id(data, &key, 1);
2422 repodata_insert_keyid(data, handle, keyid, data->attriddatalen, 1);
2423 data->lasthandle = handle;
2424 data->lastkey = keyid;
2425 data->lastdatalen = data->attriddatalen + entrysize + 1;
2429 for (ida = data->attriddata + pp[1]; *ida; ida += entrysize)
2430 oldsize += entrysize;
2431 if (ida + 1 == data->attriddata + data->attriddatalen)
2433 /* this was the last entry, just append it */
2434 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, entrysize, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2435 data->attriddatalen--; /* overwrite terminating 0 */
2439 /* too bad. move to back. */
2440 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, oldsize + entrysize + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2441 memcpy(data->attriddata + data->attriddatalen, data->attriddata + pp[1], oldsize * sizeof(Id));
2442 pp[1] = data->attriddatalen;
2443 data->attriddatalen += oldsize;
2445 data->lasthandle = handle;
2446 data->lastkey = *pp;
2447 data->lastdatalen = data->attriddatalen + entrysize + 1;
2451 repodata_set_bin_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2452 const unsigned char *str)
2457 if (!(l = solv_chksum_len(type)))
2462 key.storage = KEY_STORAGE_INCORE;
2463 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2464 memcpy(data->attrdata + data->attrdatalen, str, l);
2465 repodata_set(data, solvid, &key, data->attrdatalen);
2466 data->attrdatalen += l;
2470 repodata_set_checksum(Repodata *data, Id solvid, Id keyname, Id type,
2473 unsigned char buf[64];
2476 if (!(l = solv_chksum_len(type)))
2478 if (l > sizeof(buf) || solv_hex2bin(&str, buf, l) != l)
2480 repodata_set_bin_checksum(data, solvid, keyname, type, buf);
2484 repodata_chk2str(Repodata *data, Id type, const unsigned char *buf)
2488 if (!(l = solv_chksum_len(type)))
2490 return pool_bin2hex(data->repo->pool, buf, l);
2493 /* rpm filenames don't contain the epoch, so strip it */
2494 static inline const char *
2495 evrid2vrstr(Pool *pool, Id evrid)
2497 const char *p, *evr = pool_id2str(pool, evrid);
2500 for (p = evr; *p >= '0' && *p <= '9'; p++)
2502 return p != evr && *p == ':' && p[1] ? p + 1 : evr;
2506 repodata_set_poolstrn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2509 if (data->localpool)
2510 id = stringpool_strn2id(&data->spool, str, l, 1);
2512 id = pool_strn2id(data->repo->pool, str, l, 1);
2513 repodata_set_id(data, solvid, keyname, id);
2517 repodata_set_strn(Repodata *data, Id solvid, Id keyname, const char *str, int l)
2520 repodata_set_str(data, solvid, keyname, str);
2523 char *s = solv_strdup(str);
2525 repodata_set_str(data, solvid, keyname, s);
2531 repodata_set_location(Repodata *data, Id solvid, int medianr, const char *dir, const char *file)
2533 Pool *pool = data->repo->pool;
2535 const char *str, *fp;
2539 repodata_set_constant(data, solvid, SOLVABLE_MEDIANR, medianr);
2542 if ((dir = strrchr(file, '/')) != 0)
2553 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2558 if (l == 1 && dir[0] == '.')
2560 s = pool->solvables + solvid;
2563 str = pool_id2str(pool, s->arch);
2564 if (!strncmp(dir, str, l) && !str[l])
2565 repodata_set_void(data, solvid, SOLVABLE_MEDIADIR);
2567 repodata_set_strn(data, solvid, SOLVABLE_MEDIADIR, dir, l);
2570 str = pool_id2str(pool, s->name);
2572 if ((!l || !strncmp(fp, str, l)) && fp[l] == '-')
2575 str = evrid2vrstr(pool, s->evr);
2577 if ((!l || !strncmp(fp, str, l)) && fp[l] == '.')
2580 str = pool_id2str(pool, s->arch);
2582 if ((!l || !strncmp(fp, str, l)) && !strcmp(fp + l, ".rpm"))
2584 repodata_set_void(data, solvid, SOLVABLE_MEDIAFILE);
2589 repodata_set_str(data, solvid, SOLVABLE_MEDIAFILE, file);
2592 /* XXX: medianr is currently not stored */
2594 repodata_set_deltalocation(Repodata *data, Id handle, int medianr, const char *dir, const char *file)
2597 const char *evr, *suf, *s;
2601 if ((dir = strrchr(file, '/')) != 0)
2612 if (l >= 2 && dir[0] == '.' && dir[1] == '/' && (l == 2 || dir[2] != '/'))
2617 if (l == 1 && dir[0] == '.')
2620 repodata_set_poolstrn(data, handle, DELTA_LOCATION_DIR, dir, l);
2621 evr = strchr(file, '-');
2624 for (s = evr - 1; s > file; s--)
2631 suf = strrchr(file, '.');
2634 for (s = suf - 1; s > file; s--)
2640 if (!strcmp(suf, ".delta.rpm") || !strcmp(suf, ".patch.rpm"))
2642 /* We accept one more item as suffix. */
2643 for (s = suf - 1; s > file; s--)
2653 if (suf && evr && suf < evr)
2655 repodata_set_poolstrn(data, handle, DELTA_LOCATION_NAME, file, evr ? evr - file : strlen(file));
2657 repodata_set_poolstrn(data, handle, DELTA_LOCATION_EVR, evr + 1, suf ? suf - evr - 1: strlen(evr + 1));
2659 repodata_set_poolstr(data, handle, DELTA_LOCATION_SUFFIX, suf + 1);
2663 repodata_set_sourcepkg(Repodata *data, Id solvid, const char *sourcepkg)
2665 Pool *pool = data->repo->pool;
2666 Solvable *s = pool->solvables + solvid;
2667 const char *p, *sevr, *sarch, *name, *evr;
2669 p = strrchr(sourcepkg, '.');
2670 if (!p || strcmp(p, ".rpm") != 0)
2673 repodata_set_str(data, solvid, SOLVABLE_SOURCENAME, sourcepkg);
2677 while (p > sourcepkg && *p != '.')
2679 if (*p != '.' || p == sourcepkg)
2682 while (p > sourcepkg && *p != '-')
2684 if (*p != '-' || p == sourcepkg)
2687 while (p > sourcepkg && *p != '-')
2689 if (*p != '-' || p == sourcepkg)
2692 pool = s->repo->pool;
2694 name = pool_id2str(pool, s->name);
2695 if (name && !strncmp(sourcepkg, name, sevr - sourcepkg - 1) && name[sevr - sourcepkg - 1] == 0)
2696 repodata_set_void(data, solvid, SOLVABLE_SOURCENAME);
2698 repodata_set_id(data, solvid, SOLVABLE_SOURCENAME, pool_strn2id(pool, sourcepkg, sevr - sourcepkg - 1, 1));
2700 evr = evrid2vrstr(pool, s->evr);
2701 if (evr && !strncmp(sevr, evr, sarch - sevr - 1) && evr[sarch - sevr - 1] == 0)
2702 repodata_set_void(data, solvid, SOLVABLE_SOURCEEVR);
2704 repodata_set_id(data, solvid, SOLVABLE_SOURCEEVR, pool_strn2id(pool, sevr, sarch - sevr - 1, 1));
2706 if (!strcmp(sarch, "src.rpm"))
2707 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_SRC);
2708 else if (!strcmp(sarch, "nosrc.rpm"))
2709 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, ARCH_NOSRC);
2711 repodata_set_constantid(data, solvid, SOLVABLE_SOURCEARCH, pool_strn2id(pool, sarch, strlen(sarch) - 4, 1));
2715 repodata_set_idarray(Repodata *data, Id solvid, Id keyname, Queue *q)
2721 key.type = REPOKEY_TYPE_IDARRAY;
2723 key.storage = KEY_STORAGE_INCORE;
2724 repodata_set(data, solvid, &key, data->attriddatalen);
2725 data->attriddata = solv_extend(data->attriddata, data->attriddatalen, q->count + 1, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
2726 for (i = 0; i < q->count; i++)
2727 data->attriddata[data->attriddatalen++] = q->elements[i];
2728 data->attriddata[data->attriddatalen++] = 0;
2732 repodata_add_dirnumnum(Repodata *data, Id solvid, Id keyname, Id dir, Id num, Id num2)
2736 fprintf(stderr, "repodata_add_dirnumnum %d %d %d %d (%d)\n", solvid, dir, num, num2, data->attriddatalen);
2738 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRNUMNUMARRAY, 3);
2739 data->attriddata[data->attriddatalen++] = dir;
2740 data->attriddata[data->attriddatalen++] = num;
2741 data->attriddata[data->attriddatalen++] = num2;
2742 data->attriddata[data->attriddatalen++] = 0;
2746 repodata_add_dirstr(Repodata *data, Id solvid, Id keyname, Id dir, const char *str)
2752 l = strlen(str) + 1;
2753 data->attrdata = solv_extend(data->attrdata, data->attrdatalen, l, 1, REPODATA_ATTRDATA_BLOCK);
2754 memcpy(data->attrdata + data->attrdatalen, str, l);
2755 stroff = data->attrdatalen;
2756 data->attrdatalen += l;
2759 fprintf(stderr, "repodata_add_dirstr %d %d %s (%d)\n", solvid, dir, str, data->attriddatalen);
2761 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_DIRSTRARRAY, 2);
2762 data->attriddata[data->attriddatalen++] = dir;
2763 data->attriddata[data->attriddatalen++] = stroff;
2764 data->attriddata[data->attriddatalen++] = 0;
2768 repodata_add_idarray(Repodata *data, Id solvid, Id keyname, Id id)
2771 fprintf(stderr, "repodata_add_idarray %d %d (%d)\n", solvid, id, data->attriddatalen);
2773 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_IDARRAY, 1);
2774 data->attriddata[data->attriddatalen++] = id;
2775 data->attriddata[data->attriddatalen++] = 0;
2779 repodata_add_poolstr_array(Repodata *data, Id solvid, Id keyname,
2783 if (data->localpool)
2784 id = stringpool_str2id(&data->spool, str, 1);
2786 id = pool_str2id(data->repo->pool, str, 1);
2787 repodata_add_idarray(data, solvid, keyname, id);
2791 repodata_add_fixarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2793 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FIXARRAY, 1);
2794 data->attriddata[data->attriddatalen++] = ghandle;
2795 data->attriddata[data->attriddatalen++] = 0;
2799 repodata_add_flexarray(Repodata *data, Id solvid, Id keyname, Id ghandle)
2801 repodata_add_array(data, solvid, keyname, REPOKEY_TYPE_FLEXARRAY, 1);
2802 data->attriddata[data->attriddatalen++] = ghandle;
2803 data->attriddata[data->attriddatalen++] = 0;
2807 repodata_unset_uninternalized(Repodata *data, Id solvid, Id keyname)
2810 app = repodata_get_attrp(data, solvid);
2814 for (; *ap; ap += 2)
2815 if (data->keys[*ap].name == keyname)
2821 for (; *ap; ap += 2)
2823 if (data->keys[*ap].name == keyname)
2831 /* XXX: does not work correctly, needs fix in iterators! */
2833 repodata_unset(Repodata *data, Id solvid, Id keyname)
2837 key.type = REPOKEY_TYPE_DELETED;
2839 key.storage = KEY_STORAGE_INCORE;
2840 repodata_set(data, solvid, &key, 0);
2843 /* add all (uninternalized) attrs from src to dest */
2845 repodata_merge_attrs(Repodata *data, Id dest, Id src)
2848 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2850 for (; *keyp; keyp += 2)
2851 repodata_insert_keyid(data, dest, keyp[0], keyp[1], 0);
2854 /* add some (uninternalized) attrs from src to dest */
2856 repodata_merge_some_attrs(Repodata *data, Id dest, Id src, Map *keyidmap, int overwrite)
2859 if (dest == src || !data->attrs || !(keyp = data->attrs[src - data->start]))
2861 for (; *keyp; keyp += 2)
2862 if (!keyidmap || MAPTST(keyidmap, keyp[0]))
2863 repodata_insert_keyid(data, dest, keyp[0], keyp[1], overwrite);
2866 /* swap (uninternalized) attrs from src and dest */
2868 repodata_swap_attrs(Repodata *data, Id dest, Id src)
2871 if (!data->attrs || dest == src)
2873 if (dest < data->start || dest >= data->end)
2874 repodata_extend(data, dest);
2875 if (src < data->start || src >= data->end)
2876 repodata_extend(data, src);
2877 tmpattrs = data->attrs[dest - data->start];
2878 data->attrs[dest - data->start] = data->attrs[src - data->start];
2879 data->attrs[src - data->start] = tmpattrs;
2883 /**********************************************************************/
2885 /* TODO: unify with repo_write and repo_solv! */
2887 #define EXTDATA_BLOCK 1023
2895 data_addid(struct extdata *xd, Id sx)
2897 unsigned int x = (unsigned int)sx;
2900 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2901 dp = xd->buf + xd->len;
2906 *dp++ = (x >> 28) | 128;
2908 *dp++ = (x >> 21) | 128;
2909 *dp++ = (x >> 14) | 128;
2912 *dp++ = (x >> 7) | 128;
2914 xd->len = dp - xd->buf;
2918 data_addid64(struct extdata *xd, unsigned long long x)
2920 if (x >= 0x100000000)
2924 data_addid(xd, (Id)(x >> 35));
2925 xd->buf[xd->len - 1] |= 128;
2927 data_addid(xd, (Id)((unsigned int)x | 0x80000000));
2928 xd->buf[xd->len - 5] = (x >> 28) | 128;
2931 data_addid(xd, (Id)x);
2935 data_addideof(struct extdata *xd, Id sx, int eof)
2937 unsigned int x = (unsigned int)sx;
2940 xd->buf = solv_extend(xd->buf, xd->len, 5, 1, EXTDATA_BLOCK);
2941 dp = xd->buf + xd->len;
2946 *dp++ = (x >> 27) | 128;
2948 *dp++ = (x >> 20) | 128;
2949 *dp++ = (x >> 13) | 128;
2952 *dp++ = (x >> 6) | 128;
2953 *dp++ = eof ? (x & 63) : (x & 63) | 64;
2954 xd->len = dp - xd->buf;
2958 data_addblob(struct extdata *xd, unsigned char *blob, int len)
2960 xd->buf = solv_extend(xd->buf, xd->len, len, 1, EXTDATA_BLOCK);
2961 memcpy(xd->buf + xd->len, blob, len);
2965 /*********************************/
2967 /* this is to reduct memory usage when internalizing oversized repos */
2969 compact_attrdata(Repodata *data, int entry, int nentry)
2972 unsigned int attrdatastart = data->attrdatalen;
2973 unsigned int attriddatastart = data->attriddatalen;
2974 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
2976 for (i = entry; i < nentry; i++)
2978 Id v, *attrs = data->attrs[i];
2981 for (; *attrs; attrs += 2)
2983 switch (data->keys[*attrs].type)
2985 case REPOKEY_TYPE_STR:
2986 case REPOKEY_TYPE_BINARY:
2988 if ((unsigned int)attrs[1] < attrdatastart)
2989 attrdatastart = attrs[1];
2991 case REPOKEY_TYPE_DIRSTRARRAY:
2992 for (v = attrs[1]; data->attriddata[v] ; v += 2)
2993 if (data->attriddata[v + 1] < attrdatastart)
2994 attrdatastart = data->attriddata[v + 1];
2996 case REPOKEY_TYPE_IDARRAY:
2997 case REPOKEY_TYPE_DIRNUMNUMARRAY:
2998 if ((unsigned int)attrs[1] < attriddatastart)
2999 attriddatastart = attrs[1];
3001 case REPOKEY_TYPE_FIXARRAY:
3002 case REPOKEY_TYPE_FLEXARRAY:
3010 printf("compact_attrdata %d %d\n", entry, nentry);
3011 printf("attrdatastart: %d\n", attrdatastart);
3012 printf("attriddatastart: %d\n", attriddatastart);
3014 if (attrdatastart < 1024 * 1024 * 4 && attriddatastart < 1024 * 1024)
3016 for (i = entry; i < nentry; i++)
3018 Id v, *attrs = data->attrs[i];
3021 for (; *attrs; attrs += 2)
3023 switch (data->keys[*attrs].type)
3025 case REPOKEY_TYPE_STR:
3026 case REPOKEY_TYPE_BINARY:
3028 attrs[1] -= attrdatastart;
3030 case REPOKEY_TYPE_DIRSTRARRAY:
3031 for (v = attrs[1]; data->attriddata[v] ; v += 2)
3032 data->attriddata[v + 1] -= attrdatastart;
3034 case REPOKEY_TYPE_IDARRAY:
3035 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3036 attrs[1] -= attriddatastart;
3045 data->attrdatalen -= attrdatastart;
3046 memmove(data->attrdata, data->attrdata + attrdatastart, data->attrdatalen);
3047 data->attrdata = solv_extend_resize(data->attrdata, data->attrdatalen, 1, REPODATA_ATTRDATA_BLOCK);
3049 if (attriddatastart)
3051 data->attriddatalen -= attriddatastart;
3052 memmove(data->attriddata, data->attriddata + attriddatastart, data->attriddatalen * sizeof(Id));
3053 data->attriddata = solv_extend_resize(data->attriddata, data->attriddatalen, sizeof(Id), REPODATA_ATTRIDDATA_BLOCK);
3057 /* internalalize some key into incore/vincore data */
3060 repodata_serialize_key(Repodata *data, struct extdata *newincore,
3061 struct extdata *newvincore,
3063 Repokey *key, Id val)
3067 unsigned int oldvincorelen = 0;
3071 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3074 oldvincorelen = xd->len;
3078 case REPOKEY_TYPE_VOID:
3079 case REPOKEY_TYPE_CONSTANT:
3080 case REPOKEY_TYPE_CONSTANTID:
3081 case REPOKEY_TYPE_DELETED:
3083 case REPOKEY_TYPE_STR:
3084 data_addblob(xd, data->attrdata + val, strlen((char *)(data->attrdata + val)) + 1);
3086 case REPOKEY_TYPE_MD5:
3087 data_addblob(xd, data->attrdata + val, SIZEOF_MD5);
3089 case REPOKEY_TYPE_SHA1:
3090 data_addblob(xd, data->attrdata + val, SIZEOF_SHA1);
3092 case REPOKEY_TYPE_SHA224:
3093 data_addblob(xd, data->attrdata + val, SIZEOF_SHA224);
3095 case REPOKEY_TYPE_SHA256:
3096 data_addblob(xd, data->attrdata + val, SIZEOF_SHA256);
3098 case REPOKEY_TYPE_SHA384:
3099 data_addblob(xd, data->attrdata + val, SIZEOF_SHA384);
3101 case REPOKEY_TYPE_SHA512:
3102 data_addblob(xd, data->attrdata + val, SIZEOF_SHA512);
3104 case REPOKEY_TYPE_NUM:
3105 if (val & 0x80000000)
3107 data_addid64(xd, data->attrnum64data[val ^ 0x80000000]);
3111 case REPOKEY_TYPE_ID:
3112 case REPOKEY_TYPE_DIR:
3113 data_addid(xd, val);
3115 case REPOKEY_TYPE_BINARY:
3118 unsigned char *dp = data_read_id(data->attrdata + val, &len);
3119 dp += (unsigned int)len;
3120 data_addblob(xd, data->attrdata + val, dp - (data->attrdata + val));
3123 case REPOKEY_TYPE_IDARRAY:
3124 for (ida = data->attriddata + val; *ida; ida++)
3125 data_addideof(xd, ida[0], ida[1] ? 0 : 1);
3127 case REPOKEY_TYPE_DIRNUMNUMARRAY:
3128 for (ida = data->attriddata + val; *ida; ida += 3)
3130 data_addid(xd, ida[0]);
3131 data_addid(xd, ida[1]);
3132 data_addideof(xd, ida[2], ida[3] ? 0 : 1);
3135 case REPOKEY_TYPE_DIRSTRARRAY:
3136 for (ida = data->attriddata + val; *ida; ida += 2)
3138 data_addideof(xd, ida[0], ida[2] ? 0 : 1);
3139 data_addblob(xd, data->attrdata + ida[1], strlen((char *)(data->attrdata + ida[1])) + 1);
3142 case REPOKEY_TYPE_FIXARRAY:
3146 for (ida = data->attriddata + val; *ida; ida++)
3150 kp = data->xattrs[-*ida];
3152 continue; /* ignore empty elements */
3154 for (; *kp; kp += 2)
3158 schemaid = repodata_schema2id(data, schema, 1);
3159 else if (schemaid != repodata_schema2id(data, schema, 0))
3161 pool_debug(data->repo->pool, SOLV_ERROR, "repodata_serialize_key: fixarray substructs with different schemas\n");
3166 data_addid(xd, num);
3169 data_addid(xd, schemaid);
3170 for (ida = data->attriddata + val; *ida; ida++)
3172 Id *kp = data->xattrs[-*ida];
3175 for (; *kp; kp += 2)
3176 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3180 case REPOKEY_TYPE_FLEXARRAY:
3183 for (ida = data->attriddata + val; *ida; ida++)
3185 data_addid(xd, num);
3186 for (ida = data->attriddata + val; *ida; ida++)
3188 Id *kp = data->xattrs[-*ida];
3191 data_addid(xd, 0); /* XXX */
3198 schemaid = repodata_schema2id(data, schema, 1);
3199 data_addid(xd, schemaid);
3200 kp = data->xattrs[-*ida];
3202 repodata_serialize_key(data, newincore, newvincore, schema, data->keys + *kp, kp[1]);
3207 pool_debug(data->repo->pool, SOLV_FATAL, "repodata_serialize_key: don't know how to handle type %d\n", key->type);
3210 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3212 /* put offset/len in incore */
3213 data_addid(newincore, data->lastverticaloffset + oldvincorelen);
3214 oldvincorelen = xd->len - oldvincorelen;
3215 data_addid(newincore, oldvincorelen);
3219 /* create a circular linked list of all keys that share
3220 * the same keyname */
3222 calculate_keylink(Repodata *data)
3226 Id maxkeyname = 0, *keytable = 0;
3227 link = solv_calloc(data->nkeys, sizeof(Id));
3228 if (data->nkeys <= 2)
3230 for (i = 1; i < data->nkeys; i++)
3232 Id n = data->keys[i].name;
3233 if (n >= maxkeyname)
3235 keytable = solv_realloc2(keytable, n + 128, sizeof(Id));
3236 memset(keytable + maxkeyname, 0, (n + 128 - maxkeyname) * sizeof(Id));
3237 maxkeyname = n + 128;
3247 /* remove links that just point to themselfs */
3248 for (i = 1; i < data->nkeys; i++)
3251 solv_free(keytable);
3256 repodata_internalize(Repodata *data)
3258 Repokey *key, solvkey;
3260 Id schemaid, keyid, *schema, *sp, oldschemaid, *keyp, *seen;
3261 Offset *oldincoreoffs = 0;
3263 unsigned char *dp, *ndp;
3265 struct extdata newincore;
3266 struct extdata newvincore;
3271 if (!data->attrs && !data->xattrs)
3275 printf("repodata_internalize %d\n", data->repodataid);
3276 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3277 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3279 newvincore.buf = data->vincore;
3280 newvincore.len = data->vincorelen;
3282 /* find the solvables key, create if needed */
3283 memset(&solvkey, 0, sizeof(solvkey));
3284 solvkey.name = REPOSITORY_SOLVABLES;
3285 solvkey.type = REPOKEY_TYPE_FLEXARRAY;
3287 solvkey.storage = KEY_STORAGE_INCORE;
3288 solvkeyid = repodata_key2id(data, &solvkey, data->end != data->start ? 1 : 0);
3290 schema = solv_malloc2(data->nkeys, sizeof(Id));
3291 seen = solv_malloc2(data->nkeys, sizeof(Id));
3293 /* Merge the data already existing (in data->schemata, ->incoredata and
3294 friends) with the new attributes in data->attrs[]. */
3295 nentry = data->end - data->start;
3296 memset(&newincore, 0, sizeof(newincore));
3297 data_addid(&newincore, 0); /* start data at offset 1 */
3299 data->mainschema = 0;
3300 data->mainschemaoffsets = solv_free(data->mainschemaoffsets);
3302 keylink = calculate_keylink(data);
3303 /* join entry data */
3304 /* we start with the meta data, entry -1 */
3305 for (entry = -1; entry < nentry; entry++)
3308 dp = data->incoredata;
3311 dp += entry >= 0 ? data->incoreoffset[entry] : 1;
3312 dp = data_read_id(dp, &oldschemaid);
3314 memset(seen, 0, data->nkeys * sizeof(Id));
3316 fprintf(stderr, "oldschemaid %d\n", oldschemaid);
3317 fprintf(stderr, "schemata %d\n", data->schemata[oldschemaid]);
3318 fprintf(stderr, "schemadata %p\n", data->schemadata);
3321 /* seen: -1: old data, 0: skipped, >0: id + 1 */
3325 for (keyp = data->schemadata + data->schemata[oldschemaid]; *keyp; keyp++)
3329 /* oops, should not happen */
3333 seen[*keyp] = -1; /* use old marker */
3336 haveoldkl = 1; /* potential keylink conflict */
3339 /* strip solvables key */
3340 if (entry < 0 && solvkeyid && seen[solvkeyid])
3343 for (sp = keyp = schema; *sp; sp++)
3344 if (*sp != solvkeyid)
3347 seen[solvkeyid] = 0;
3351 /* add new entries */
3353 keyp = data->attrs ? data->attrs[entry] : 0;
3355 keyp = data->xattrs ? data->xattrs[1] : 0;
3357 for (; *keyp; keyp += 2)
3363 if (haveoldkl && keylink[*keyp]) /* this should be pretty rare */
3366 for (kl = keylink[*keyp]; kl != *keyp; kl = keylink[kl])
3369 /* replacing old key kl, remove from schema and seen */
3371 for (osp = schema; osp < sp; osp++)
3374 memmove(osp, osp + 1, (sp - osp) * sizeof(Id));
3382 seen[*keyp] = keyp[1] + 1;
3385 /* add solvables key if needed */
3386 if (entry < 0 && data->end != data->start)
3388 *sp++ = solvkeyid; /* always last in schema */
3395 /* Ideally we'd like to sort the new schema here, to ensure
3396 schema equality independend of the ordering. */
3397 schemaid = repodata_schema2id(data, schema, 1);
3399 schemaid = oldschemaid;
3403 data->mainschemaoffsets = solv_calloc(sp - schema, sizeof(Id));
3404 data->mainschema = schemaid;
3407 /* find offsets in old incore data */
3411 for (sp = data->schemadata + data->schemata[oldschemaid]; *sp; sp++)
3412 if (seen[*sp] == -1)
3413 lastneeded = sp + 1;
3417 oldincoreoffs = solv_malloc2(data->nkeys, 2 * sizeof(Offset));
3418 for (sp = data->schemadata + data->schemata[oldschemaid]; sp != lastneeded; sp++)
3420 /* Skip the data associated with this old key. */
3421 key = data->keys + *sp;
3423 if (key->storage == KEY_STORAGE_VERTICAL_OFFSET)
3425 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3426 ndp = data_skip(ndp, REPOKEY_TYPE_ID);
3428 else if (key->storage == KEY_STORAGE_INCORE)
3429 ndp = data_skip_key(data, ndp, key);
3430 oldincoreoffs[*sp * 2] = dp - data->incoredata;
3431 oldincoreoffs[*sp * 2 + 1] = ndp - dp;
3437 /* just copy over the complete old entry (including the schemaid) if there was no new data */
3438 if (entry >= 0 && !neednewschema && oldschemaid && (!data->attrs || !data->attrs[entry]) && dp)
3440 ndp = data->incoredata + data->incoreoffset[entry];
3441 data->incoreoffset[entry] = newincore.len;
3442 data_addblob(&newincore, ndp, dp - ndp);
3446 /* Now create data blob. We walk through the (possibly new) schema
3447 and either copy over old data, or insert the new. */
3449 data->incoreoffset[entry] = newincore.len;
3450 data_addid(&newincore, schemaid);
3452 /* we don't use a pointer to the schemadata here as repodata_serialize_key
3453 * may call repodata_schema2id() which might realloc our schemadata */
3454 for (schemaidx = data->schemata[schemaid]; (keyid = data->schemadata[schemaidx]) != 0; schemaidx++)
3458 data->mainschemaoffsets[schemaidx - data->schemata[schemaid]] = newincore.len;
3459 if (keyid == solvkeyid)
3461 /* add flexarray entry count */
3462 data_addid(&newincore, data->end - data->start);
3463 break; /* always the last entry */
3466 if (seen[keyid] == -1)
3468 if (oldincoreoffs[keyid * 2 + 1])
3469 data_addblob(&newincore, data->incoredata + oldincoreoffs[keyid * 2], oldincoreoffs[keyid * 2 + 1]);
3471 else if (seen[keyid])
3472 repodata_serialize_key(data, &newincore, &newvincore, schema, data->keys + keyid, seen[keyid] - 1);
3477 if (entry >= 0 && data->attrs)
3479 if (data->attrs[entry])
3480 data->attrs[entry] = solv_free(data->attrs[entry]);
3481 if (entry && entry % 4096 == 0 && data->nxattrs <= 2 && entry + 64 < nentry)
3483 compact_attrdata(data, entry + 1, nentry); /* try to free some memory */
3485 printf(" attr data: %d K\n", data->attrdatalen / 1024);
3486 printf(" attrid data: %d K\n", data->attriddatalen / (1024 / 4));
3487 printf(" incore data: %d K\n", newincore.len / 1024);
3488 printf(" sum: %d K\n", (newincore.len + data->attrdatalen + data->attriddatalen * 4) / 1024);
3489 /* malloc_stats(); */
3494 /* free all xattrs */
3495 for (entry = 0; entry < data->nxattrs; entry++)
3496 if (data->xattrs[entry])
3497 solv_free(data->xattrs[entry]);
3498 data->xattrs = solv_free(data->xattrs);
3501 data->lasthandle = 0;
3503 data->lastdatalen = 0;
3507 solv_free(oldincoreoffs);
3508 repodata_free_schemahash(data);
3510 solv_free(data->incoredata);
3511 data->incoredata = newincore.buf;
3512 data->incoredatalen = newincore.len;
3513 data->incoredatafree = 0;
3515 solv_free(data->vincore);
3516 data->vincore = newvincore.buf;
3517 data->vincorelen = newvincore.len;
3519 data->attrs = solv_free(data->attrs);
3520 data->attrdata = solv_free(data->attrdata);
3521 data->attriddata = solv_free(data->attriddata);
3522 data->attrnum64data = solv_free(data->attrnum64data);
3523 data->attrdatalen = 0;
3524 data->attriddatalen = 0;
3525 data->attrnum64datalen = 0;
3527 printf("repodata_internalize %d done\n", data->repodataid);
3528 printf(" incore data: %d K\n", data->incoredatalen / 1024);
3533 repodata_disable_paging(Repodata *data)
3535 if (maybe_load_repodata(data, 0))
3537 repopagestore_disable_paging(&data->store);
3543 repodata_load_stub(Repodata *data)
3545 Repo *repo = data->repo;
3546 Pool *pool = repo->pool;
3548 struct _Pool_tmpspace oldtmpspace;
3551 if (!pool->loadcallback)
3553 data->state = REPODATA_ERROR;
3556 data->state = REPODATA_LOADING;
3558 /* save tmp space and pos */
3559 oldtmpspace = pool->tmpspace;
3560 memset(&pool->tmpspace, 0, sizeof(pool->tmpspace));
3563 r = pool->loadcallback(pool, data, pool->loadcallbackdata);
3565 /* restore tmp space and pos */
3566 for (i = 0; i < POOL_TMPSPACEBUF; i++)
3567 solv_free(pool->tmpspace.buf[i]);
3568 pool->tmpspace = oldtmpspace;
3569 if (r && oldpos.repo == repo && oldpos.repodataid == data->repodataid)
3570 memset(&oldpos, 0, sizeof(oldpos));
3573 data->state = r ? REPODATA_AVAILABLE : REPODATA_ERROR;
3577 repodata_add_stubkey(Repodata *data, Id keyname, Id keytype)
3581 xkey.name = keyname;
3582 xkey.type = keytype;
3583 xkey.storage = KEY_STORAGE_INCORE;
3585 repodata_key2id(data, &xkey, 1);
3589 repodata_add_stub(Repodata **datap)
3591 Repodata *data = *datap;
3592 Repo *repo = data->repo;
3593 Id repodataid = data - repo->repodata;
3594 Repodata *sdata = repo_add_repodata(repo, 0);
3595 data = repo->repodata + repodataid;
3596 if (data->end > data->start)
3597 repodata_extend_block(sdata, data->start, data->end - data->start);
3598 sdata->state = REPODATA_STUB;
3599 sdata->loadcallback = repodata_load_stub;
3605 repodata_create_stubs(Repodata *data)
3607 Repo *repo = data->repo;
3608 Pool *pool = repo->pool;
3615 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3616 while (dataiterator_step(&di))
3617 if (di.data == data)
3619 dataiterator_free(&di);
3622 stubdataids = solv_calloc(cnt, sizeof(*stubdataids));
3623 for (i = 0; i < cnt; i++)
3625 sdata = repodata_add_stub(&data);
3626 stubdataids[i] = sdata - repo->repodata;
3629 dataiterator_init(&di, pool, repo, SOLVID_META, REPOSITORY_EXTERNAL, 0, 0);
3631 while (dataiterator_step(&di))
3633 if (di.data != data)
3635 if (di.key->name == REPOSITORY_EXTERNAL && !di.nparents)
3637 dataiterator_entersub(&di);
3638 sdata = repo->repodata + stubdataids[i++];
3642 switch (di.key->type)
3644 case REPOKEY_TYPE_ID:
3645 repodata_set_id(sdata, SOLVID_META, di.key->name, di.kv.id);
3647 case REPOKEY_TYPE_CONSTANTID:
3648 repodata_set_constantid(sdata, SOLVID_META, di.key->name, di.kv.id);
3650 case REPOKEY_TYPE_STR:
3651 repodata_set_str(sdata, SOLVID_META, di.key->name, di.kv.str);
3653 case REPOKEY_TYPE_VOID:
3654 repodata_set_void(sdata, SOLVID_META, di.key->name);
3656 case REPOKEY_TYPE_NUM:
3657 repodata_set_num(sdata, SOLVID_META, di.key->name, SOLV_KV_NUM64(&di.kv));
3660 repodata_set_bin_checksum(sdata, SOLVID_META, di.key->name, di.key->type, (const unsigned char *)di.kv.str);
3662 case REPOKEY_TYPE_IDARRAY:
3663 repodata_add_idarray(sdata, SOLVID_META, di.key->name, di.kv.id);
3664 if (di.key->name == REPOSITORY_KEYS)
3669 xkeyname = di.kv.id;
3673 repodata_add_stubkey(sdata, xkeyname, di.kv.id);
3682 dataiterator_free(&di);
3683 for (i = 0; i < cnt; i++)
3684 repodata_internalize(repo->repodata + stubdataids[i]);
3685 solv_free(stubdataids);
3690 repodata_memused(Repodata *data)
3692 return data->incoredatalen + data->vincorelen;