1 /*
2 * Copyright (c) 2007-2018, SUSE Inc.
3 *
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
6 */
7
8 /*
9 * repo_rpmdb
10 *
11 * convert rpm db to repo
12 *
13 */
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <limits.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <assert.h>
24 #include <stdint.h>
25 #include <errno.h>
26
27 #ifdef ENABLE_RPMDB
28
29 #include <rpm/rpmio.h>
30 #include <rpm/rpmpgp.h>
31 #ifndef RPM5
32 #include <rpm/header.h>
33 #endif
34 #include <rpm/rpmdb.h>
35
36 #endif
37
38 #include "pool.h"
39 #include "repo.h"
40 #include "hash.h"
41 #include "util.h"
42 #include "queue.h"
43 #include "chksum.h"
44 #include "repo_rpmdb.h"
45 #include "repo_solv.h"
46 #ifdef ENABLE_COMPLEX_DEPS
47 #include "pool_parserpmrichdep.h"
48 #endif
49
50 /* 3: added triggers */
51 /* 4: fixed triggers */
52 /* 5: fixed checksum copying */
53 /* 6: add SOLVABLE_PREREQ_IGNOREINST support */
54 /* 7: fix bug in ignoreinst logic */
55 #define RPMDB_COOKIE_VERSION 7
56
57 #define TAG_NAME 1000
58 #define TAG_VERSION 1001
59 #define TAG_RELEASE 1002
60 #define TAG_EPOCH 1003
61 #define TAG_SUMMARY 1004
62 #define TAG_DESCRIPTION 1005
63 #define TAG_BUILDTIME 1006
64 #define TAG_BUILDHOST 1007
65 #define TAG_INSTALLTIME 1008
66 #define TAG_SIZE 1009
67 #define TAG_DISTRIBUTION 1010
68 #define TAG_VENDOR 1011
69 #define TAG_LICENSE 1014
70 #define TAG_PACKAGER 1015
71 #define TAG_GROUP 1016
72 #define TAG_URL 1020
73 #define TAG_ARCH 1022
74 #define TAG_FILESIZES 1028
75 #define TAG_FILEMODES 1030
76 #define TAG_FILEMD5S 1035
77 #define TAG_FILELINKTOS 1036
78 #define TAG_FILEFLAGS 1037
79 #define TAG_SOURCERPM 1044
80 #define TAG_PROVIDENAME 1047
81 #define TAG_REQUIREFLAGS 1048
82 #define TAG_REQUIRENAME 1049
83 #define TAG_REQUIREVERSION 1050
84 #define TAG_NOSOURCE 1051
85 #define TAG_NOPATCH 1052
86 #define TAG_CONFLICTFLAGS 1053
87 #define TAG_CONFLICTNAME 1054
88 #define TAG_CONFLICTVERSION 1055
89 #define TAG_TRIGGERNAME 1066
90 #define TAG_TRIGGERVERSION 1067
91 #define TAG_TRIGGERFLAGS 1068
92 #define TAG_CHANGELOGTIME 1080
93 #define TAG_CHANGELOGNAME 1081
94 #define TAG_CHANGELOGTEXT 1082
95 #define TAG_OBSOLETENAME 1090
96 #define TAG_FILEDEVICES 1095
97 #define TAG_FILEINODES 1096
98 #define TAG_SOURCEPACKAGE 1106
99 #define TAG_PROVIDEFLAGS 1112
100 #define TAG_PROVIDEVERSION 1113
101 #define TAG_OBSOLETEFLAGS 1114
102 #define TAG_OBSOLETEVERSION 1115
103 #define TAG_DIRINDEXES 1116
104 #define TAG_BASENAMES 1117
105 #define TAG_DIRNAMES 1118
106 #define TAG_PAYLOADFORMAT 1124
107 #define TAG_PATCHESNAME 1133
108 #define TAG_FILECOLORS 1140
109 #define TAG_OLDSUGGESTSNAME 1156
110 #define TAG_OLDSUGGESTSVERSION 1157
111 #define TAG_OLDSUGGESTSFLAGS 1158
112 #define TAG_OLDENHANCESNAME 1159
113 #define TAG_OLDENHANCESVERSION 1160
114 #define TAG_OLDENHANCESFLAGS 1161
115
116 /* rpm5 tags */
117 #define TAG_DISTEPOCH 1218
118
119 /* rpm4 tags */
120 #define TAG_LONGFILESIZES 5008
121 #define TAG_LONGSIZE 5009
122 #define TAG_RECOMMENDNAME 5046
123 #define TAG_RECOMMENDVERSION 5047
124 #define TAG_RECOMMENDFLAGS 5048
125 #define TAG_SUGGESTNAME 5049
126 #define TAG_SUGGESTVERSION 5050
127 #define TAG_SUGGESTFLAGS 5051
128 #define TAG_SUPPLEMENTNAME 5052
129 #define TAG_SUPPLEMENTVERSION 5053
130 #define TAG_SUPPLEMENTFLAGS 5054
131 #define TAG_ENHANCENAME 5055
132 #define TAG_ENHANCEVERSION 5056
133 #define TAG_ENHANCEFLAGS 5057
134
135 /* signature tags */
136 #define TAG_SIGBASE 256
137 #define TAG_SIGMD5 (TAG_SIGBASE + 5)
138 #define TAG_SHA1HEADER (TAG_SIGBASE + 13)
139 #define TAG_SHA256HEADER (TAG_SIGBASE + 17)
140
141 #define SIGTAG_SIZE 1000
142 #define SIGTAG_PGP 1002 /* RSA signature */
143 #define SIGTAG_MD5 1004 /* header+payload md5 checksum */
144 #define SIGTAG_GPG 1005 /* DSA signature */
145
146 #define DEP_LESS (1 << 1)
147 #define DEP_GREATER (1 << 2)
148 #define DEP_EQUAL (1 << 3)
149 #define DEP_STRONG (1 << 27)
150 #define DEP_PRE_IN ((1 << 6) | (1 << 9) | (1 << 10))
151 #define DEP_PRE_UN ((1 << 6) | (1 << 11) | (1 << 12))
152
153 #define FILEFLAG_GHOST (1 << 6)
154
155
156 /* some limits to guard against corrupt rpms */
157 /* dsize limits taken from rpm's lib/header.c */
158 #define MAX_SIG_CNT 0x10000
159 #define MAX_SIG_DSIZE 0x4000000
160
161 #define MAX_HDR_CNT 0x10000
162 #define MAX_HDR_DSIZE 0x10000000
163
164
165 #ifndef ENABLE_RPMPKG_LIBRPM
166
167 typedef struct rpmhead {
168 int cnt;
169 unsigned int dcnt;
170 unsigned char *dp;
171 unsigned char data[1];
172 } RpmHead;
173
174
175 static inline void
headinit(RpmHead * h,unsigned int cnt,unsigned int dcnt)176 headinit(RpmHead *h, unsigned int cnt, unsigned int dcnt)
177 {
178 h->cnt = (int)cnt;
179 h->dcnt = dcnt;
180 h->dp = h->data + 16 * cnt;
181 h->dp[dcnt] = 0;
182 }
183
184 static inline unsigned char *
headfindtag(RpmHead * h,int tag)185 headfindtag(RpmHead *h, int tag)
186 {
187 unsigned int i;
188 unsigned char *d, taga[4];
189 d = h->dp - 16;
190 taga[0] = tag >> 24;
191 taga[1] = tag >> 16;
192 taga[2] = tag >> 8;
193 taga[3] = tag;
194 for (i = 0; i < h->cnt; i++, d -= 16)
195 if (d[3] == taga[3] && d[2] == taga[2] && d[1] == taga[1] && d[0] == taga[0])
196 return d;
197 return 0;
198 }
199
200 static int
headexists(RpmHead * h,int tag)201 headexists(RpmHead *h, int tag)
202 {
203 return headfindtag(h, tag) ? 1 : 0;
204 }
205
206 static uint32_t *
headint32array(RpmHead * h,int tag,int * cnt)207 headint32array(RpmHead *h, int tag, int *cnt)
208 {
209 uint32_t *r;
210 unsigned int i, o;
211 unsigned char *d = headfindtag(h, tag);
212
213 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
214 return 0;
215 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
216 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
217 if (o > h->dcnt || i > h->dcnt || o + 4 * i > h->dcnt)
218 return 0;
219 d = h->dp + o;
220 r = solv_calloc(i ? i : 1, sizeof(uint32_t));
221 if (cnt)
222 *cnt = i;
223 for (o = 0; o < i; o++, d += 4)
224 r[o] = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
225 return r;
226 }
227
228 /* returns the first entry of an integer array */
229 static uint32_t
headint32(RpmHead * h,int tag)230 headint32(RpmHead *h, int tag)
231 {
232 unsigned int i, o;
233 unsigned char *d = headfindtag(h, tag);
234
235 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 4)
236 return 0;
237 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
238 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
239 if (i == 0 || o > h->dcnt || i > h->dcnt || o + 4 * i > h->dcnt)
240 return 0;
241 d = h->dp + o;
242 return d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
243 }
244
245 static uint64_t *
headint64array(RpmHead * h,int tag,int * cnt)246 headint64array(RpmHead *h, int tag, int *cnt)
247 {
248 uint64_t *r;
249 unsigned int i, o;
250 unsigned char *d = headfindtag(h, tag);
251
252 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
253 return 0;
254 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
255 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
256 if (o > h->dcnt || i > h->dcnt || o + 8 * i > h->dcnt)
257 return 0;
258 d = h->dp + o;
259 r = solv_calloc(i ? i : 1, sizeof(uint64_t));
260 if (cnt)
261 *cnt = i;
262 for (o = 0; o < i; o++, d += 8)
263 {
264 uint32_t x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
265 r[o] = (uint64_t)x << 32 | (uint32_t)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
266 }
267 return r;
268 }
269
270 /* returns the first entry of an 64bit integer array */
271 static uint64_t
headint64(RpmHead * h,int tag)272 headint64(RpmHead *h, int tag)
273 {
274 uint32_t x;
275 unsigned int i, o;
276 unsigned char *d = headfindtag(h, tag);
277
278 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 5)
279 return 0;
280 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
281 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
282 if (i == 0 || o > h->dcnt || i > h->dcnt || o + 8 * i > h->dcnt)
283 return 0;
284 d = h->dp + o;
285 x = d[0] << 24 | d[1] << 16 | d[2] << 8 | d[3];
286 return (uint64_t)x << 32 | (uint32_t)(d[4] << 24 | d[5] << 16 | d[6] << 8 | d[7]);
287 }
288
289 static uint16_t *
headint16array(RpmHead * h,int tag,int * cnt)290 headint16array(RpmHead *h, int tag, int *cnt)
291 {
292 uint16_t *r;
293 unsigned int i, o;
294 unsigned char *d = headfindtag(h, tag);
295
296 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 3)
297 return 0;
298 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
299 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
300 if (o > h->dcnt || i > h->dcnt || o + 2 * i > h->dcnt)
301 return 0;
302 d = h->dp + o;
303 r = solv_calloc(i ? i : 1, sizeof(uint16_t));
304 if (cnt)
305 *cnt = i;
306 for (o = 0; o < i; o++, d += 2)
307 r[o] = d[0] << 8 | d[1];
308 return r;
309 }
310
311 static char *
headstring(RpmHead * h,int tag)312 headstring(RpmHead *h, int tag)
313 {
314 unsigned int o;
315 unsigned char *d = headfindtag(h, tag);
316 /* 6: STRING, 9: I18NSTRING */
317 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || (d[7] != 6 && d[7] != 9))
318 return 0;
319 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
320 if (o >= h->dcnt)
321 return 0;
322 return (char *)h->dp + o;
323 }
324
325 static char **
headstringarray(RpmHead * h,int tag,int * cnt)326 headstringarray(RpmHead *h, int tag, int *cnt)
327 {
328 unsigned int i, o;
329 unsigned char *d = headfindtag(h, tag);
330 char **r;
331
332 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
333 return 0;
334 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
335 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
336 if (o > h->dcnt || i > h->dcnt)
337 return 0;
338 r = solv_calloc(i ? i : 1, sizeof(char *));
339 if (cnt)
340 *cnt = i;
341 d = h->dp + o;
342 for (o = 0; o < i; o++)
343 {
344 r[o] = (char *)d;
345 if (o + 1 < i)
346 d += strlen((char *)d) + 1;
347 if (d >= h->dp + h->dcnt)
348 {
349 solv_free(r);
350 return 0;
351 }
352 }
353 return r;
354 }
355
356 static unsigned char *
headbinary(RpmHead * h,int tag,unsigned int * sizep)357 headbinary(RpmHead *h, int tag, unsigned int *sizep)
358 {
359 unsigned int i, o;
360 unsigned char *d = headfindtag(h, tag);
361 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 7)
362 return 0;
363 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
364 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
365 if (o > h->dcnt || i > h->dcnt || o + i > h->dcnt)
366 return 0;
367 if (sizep)
368 *sizep = i;
369 return h->dp + o;
370 }
371
372 static int
headissourceheuristic(RpmHead * h)373 headissourceheuristic(RpmHead *h)
374 {
375 unsigned int i, o;
376 unsigned char *d = headfindtag(h, TAG_DIRNAMES);
377 if (!d || d[4] != 0 || d[5] != 0 || d[6] != 0 || d[7] != 8)
378 return 0;
379 o = d[8] << 24 | d[9] << 16 | d[10] << 8 | d[11];
380 i = d[12] << 24 | d[13] << 16 | d[14] << 8 | d[15];
381 return i == 1 && o < h->dcnt && !h->dp[o] ? 1 : 0;
382 }
383
384 static inline void
headfree(RpmHead * h)385 headfree(RpmHead *h)
386 {
387 solv_free(h);
388 }
389
390 #else
391
392 typedef struct headerToken_s RpmHead;
393
394 static int
headexists(RpmHead * h,int tag)395 headexists(RpmHead *h, int tag)
396 {
397 return headerIsEntry(h, tag);
398 }
399
headget(RpmHead * h,int tag,int * cnt,int alloc)400 static void *headget(RpmHead *h, int tag, int *cnt, int alloc)
401 {
402 struct rpmtd_s td;
403 if (!headerGet(h, tag, &td, alloc ? HEADERGET_ALLOC : HEADERGET_MINMEM))
404 return 0;
405 if (cnt)
406 *cnt = td.count;
407 return td.data;
408 }
409
410 static uint32_t *
headint32array(RpmHead * h,int tag,int * cnt)411 headint32array(RpmHead *h, int tag, int *cnt)
412 {
413 return headget(h, tag, cnt, 1);
414 }
415
416 static uint32_t
headint32(RpmHead * h,int tag)417 headint32(RpmHead *h, int tag)
418 {
419 uint32_t *arr = headget(h, tag, 0, 0);
420 return arr ? arr[0] : 0;
421 }
422
423 static uint64_t *
headint64array(RpmHead * h,int tag,int * cnt)424 headint64array(RpmHead *h, int tag, int *cnt)
425 {
426 return headget(h, tag, cnt, 1);
427 }
428
429 /* returns the first entry of an 64bit integer array */
430 static uint64_t
headint64(RpmHead * h,int tag)431 headint64(RpmHead *h, int tag)
432 {
433 uint64_t *arr = headget(h, tag, 0, 0);
434 return arr ? arr[0] : 0;
435 }
436
437 static uint16_t *
headint16array(RpmHead * h,int tag,int * cnt)438 headint16array(RpmHead *h, int tag, int *cnt)
439 {
440 return headget(h, tag, cnt, 1);
441 }
442
443 static char *
headstring(RpmHead * h,int tag)444 headstring(RpmHead *h, int tag)
445 {
446 return headget(h, tag, 0, 0);
447 }
448
449 static char **
headstringarray(RpmHead * h,int tag,int * cnt)450 headstringarray(RpmHead *h, int tag, int *cnt)
451 {
452 return headget(h, tag, cnt, 1);
453 }
454
455 static unsigned char *
headbinary(RpmHead * h,int tag,unsigned int * sizep)456 headbinary(RpmHead *h, int tag, unsigned int *sizep)
457 {
458 unsigned char *b = headget(h, tag, (int *)sizep, 0);
459 if (b && sizep && (tag == TAG_SIGMD5 || tag == SIGTAG_MD5) && *sizep > 16) {
460 /* due to a bug in rpm the count may be bigger if HEADERIMPORT_FAST is used */
461 *sizep = 16;
462 }
463 return b;
464 }
465
466 static int
headissourceheuristic(RpmHead * h)467 headissourceheuristic(RpmHead *h)
468 {
469 struct rpmtd_s td;
470 int issource;
471 if (!headerGet(h, TAG_DIRNAMES, &td, HEADERGET_MINMEM))
472 return 0;
473 issource = td.count == 1 && td.data && ((char **)td.data)[0] && !((char **)td.data)[0][0];
474 rpmtdFreeData(&td);
475 return issource;
476 }
477
478 static inline void
headfree(RpmHead * h)479 headfree(RpmHead *h)
480 {
481 headerFree(h);
482 }
483
484 #endif
485
headtoevr(RpmHead * h)486 static char *headtoevr(RpmHead *h)
487 {
488 unsigned int epoch;
489 char *version, *v;
490 char *release;
491 char *evr;
492 char *distepoch;
493
494 version = headstring(h, TAG_VERSION);
495 release = headstring(h, TAG_RELEASE);
496 epoch = headint32(h, TAG_EPOCH);
497 if (!version || !release)
498 return 0;
499 for (v = version; *v >= '0' && *v <= '9'; v++)
500 ;
501 if (epoch || (v != version && *v == ':'))
502 {
503 char epochbuf[11]; /* 32bit decimal will fit in */
504 sprintf(epochbuf, "%u", epoch);
505 evr = solv_malloc(strlen(epochbuf) + 1 + strlen(version) + 1 + strlen(release) + 1);
506 sprintf(evr, "%s:%s-%s", epochbuf, version, release);
507 }
508 else
509 {
510 evr = solv_malloc(strlen(version) + 1 + strlen(release) + 1);
511 sprintf(evr, "%s-%s", version, release);
512 }
513 distepoch = headstring(h, TAG_DISTEPOCH);
514 if (distepoch && *distepoch)
515 {
516 int l = strlen(evr);
517 evr = solv_realloc(evr, l + strlen(distepoch) + 2);
518 evr[l++] = ':';
519 strcpy(evr + l, distepoch);
520 }
521 return evr;
522 }
523
524
525 static void
setutf8string(Repodata * repodata,Id handle,Id tag,const char * str)526 setutf8string(Repodata *repodata, Id handle, Id tag, const char *str)
527 {
528 if (str[solv_validutf8(str)])
529 {
530 char *ustr = solv_latin1toutf8(str); /* not utf8, assume latin1 */
531 repodata_set_str(repodata, handle, tag, ustr);
532 solv_free(ustr);
533 }
534 else
535 repodata_set_str(repodata, handle, tag, str);
536 }
537
538 static int
ignq_sortcmp(const void * va,const void * vb,void * dp)539 ignq_sortcmp(const void *va, const void *vb, void *dp)
540 {
541 int r = *(Id *)va - *(Id *)vb;
542 if (!r)
543 r = ((Id *)va)[1] - ((Id *)vb)[1];
544 return r;
545 }
546
547 /*
548 * strong: 0: ignore strongness
549 * 1: filter to strong
550 * 2: filter to weak
551 */
552 static unsigned int
makedeps(Pool * pool,Repo * repo,RpmHead * rpmhead,int tagn,int tagv,int tagf,int flags,Queue * ignq)553 makedeps(Pool *pool, Repo *repo, RpmHead *rpmhead, int tagn, int tagv, int tagf, int flags, Queue *ignq)
554 {
555 char **n, **v;
556 uint32_t *f;
557 int i, cc, nc, vc, fc;
558 int haspre, premask, has_ign;
559 unsigned int olddeps;
560 Id *ida;
561 int strong = 0;
562
563 n = headstringarray(rpmhead, tagn, &nc);
564 if (!n)
565 {
566 switch (tagn)
567 {
568 case TAG_SUGGESTNAME:
569 tagn = TAG_OLDSUGGESTSNAME;
570 tagv = TAG_OLDSUGGESTSVERSION;
571 tagf = TAG_OLDSUGGESTSFLAGS;
572 strong = -1;
573 break;
574 case TAG_ENHANCENAME:
575 tagn = TAG_OLDENHANCESNAME;
576 tagv = TAG_OLDENHANCESVERSION;
577 tagf = TAG_OLDENHANCESFLAGS;
578 strong = -1;
579 break;
580 case TAG_RECOMMENDNAME:
581 tagn = TAG_OLDSUGGESTSNAME;
582 tagv = TAG_OLDSUGGESTSVERSION;
583 tagf = TAG_OLDSUGGESTSFLAGS;
584 strong = 1;
585 break;
586 case TAG_SUPPLEMENTNAME:
587 tagn = TAG_OLDENHANCESNAME;
588 tagv = TAG_OLDENHANCESVERSION;
589 tagf = TAG_OLDENHANCESFLAGS;
590 strong = 1;
591 break;
592 default:
593 return 0;
594 }
595 n = headstringarray(rpmhead, tagn, &nc);
596 }
597 if (!n || !nc)
598 return 0;
599 vc = fc = 0;
600 v = headstringarray(rpmhead, tagv, &vc);
601 f = headint32array(rpmhead, tagf, &fc);
602 if (!v || !f || nc != vc || nc != fc)
603 {
604 char *pkgname = rpm_query(rpmhead, 0);
605 pool_error(pool, 0, "bad dependency entries for %s: %d %d %d", pkgname ? pkgname : "<NULL>", nc, vc, fc);
606 solv_free(pkgname);
607 solv_free(n);
608 solv_free(v);
609 solv_free(f);
610 return 0;
611 }
612
613 cc = nc;
614 haspre = 0; /* add no prereq marker */
615 premask = tagn == TAG_REQUIRENAME ? DEP_PRE_IN | DEP_PRE_UN : 0;
616 if ((flags & RPM_ADD_NO_RPMLIBREQS) || strong)
617 {
618 /* we do filtering */
619 cc = 0;
620 for (i = 0; i < nc; i++)
621 {
622 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
623 continue;
624 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
625 if (!strncmp(n[i], "rpmlib(", 7))
626 continue;
627 if ((f[i] & premask) != 0)
628 haspre = 1;
629 cc++;
630 }
631 }
632 else if (premask)
633 {
634 /* no filtering, just look for the first prereq */
635 for (i = 0; i < nc; i++)
636 if ((f[i] & premask) != 0)
637 {
638 haspre = 1;
639 break;
640 }
641 }
642 if (cc == 0)
643 {
644 solv_free(n);
645 solv_free(v);
646 solv_free(f);
647 return 0;
648 }
649 cc += haspre; /* add slot for the prereq marker */
650 olddeps = repo_reserve_ids(repo, 0, cc);
651 ida = repo->idarraydata + olddeps;
652
653 has_ign = 0;
654 for (i = 0; ; i++)
655 {
656 Id id;
657 if (i == nc)
658 {
659 if (haspre != 1)
660 break;
661 haspre = 2; /* pass two: prereqs */
662 i = 0;
663 *ida++ = SOLVABLE_PREREQMARKER;
664 }
665 if (strong && (f[i] & DEP_STRONG) != (strong < 0 ? 0 : DEP_STRONG))
666 continue;
667 if (haspre)
668 {
669 if (haspre == 1 && (f[i] & premask) != 0)
670 continue;
671 if (haspre == 2 && (f[i] & premask) == 0)
672 continue;
673 }
674 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
675 if (!strncmp(n[i], "rpmlib(", 7))
676 continue;
677 #ifdef ENABLE_COMPLEX_DEPS
678 if ((f[i] & (DEP_LESS|DEP_EQUAL|DEP_GREATER)) == 0 && n[i][0] == '(')
679 {
680 id = pool_parserpmrichdep(pool, n[i]);
681 if (id)
682 *ida++ = id;
683 else
684 cc--;
685 continue;
686 }
687 #endif
688 id = pool_str2id(pool, n[i], 1);
689 if (f[i] & (DEP_LESS|DEP_GREATER|DEP_EQUAL))
690 {
691 Id evr;
692 int fl = 0;
693 if ((f[i] & DEP_LESS) != 0)
694 fl |= REL_LT;
695 if ((f[i] & DEP_EQUAL) != 0)
696 fl |= REL_EQ;
697 if ((f[i] & DEP_GREATER) != 0)
698 fl |= REL_GT;
699 if (v[i][0] == '0' && v[i][1] == ':' && v[i][2])
700 evr = pool_str2id(pool, v[i] + 2, 1);
701 else
702 evr = pool_str2id(pool, v[i], 1);
703 id = pool_rel2id(pool, id, evr, fl, 1);
704 }
705 *ida++ = id;
706 if (haspre == 2 && ignq)
707 {
708 int is_ign = (f[i] & DEP_PRE_IN) != 0 && (f[i] & DEP_PRE_UN) == 0 ? 1 : 0;
709 has_ign |= is_ign;
710 queue_push2(ignq, id, is_ign);
711 }
712 }
713 *ida++ = 0;
714 repo->idarraysize += cc + 1;
715 solv_free(n);
716 solv_free(v);
717 solv_free(f);
718 if (ignq && ignq->count)
719 {
720 int j = 0;
721 if (has_ign && ignq->count == 2)
722 j = 1;
723 else if (has_ign)
724 {
725 Id id, lastid = 0;
726
727 solv_sort(ignq->elements, ignq->count / 2, sizeof(Id) * 2, ignq_sortcmp, 0);
728 for (i = j = 0; i < ignq->count; i += 2)
729 {
730 id = ignq->elements[i];
731 if (id != lastid && ignq->elements[i + 1] > 0)
732 ignq->elements[j++] = id;
733 lastid = id;
734 }
735 }
736 queue_truncate(ignq, j);
737 }
738 return olddeps;
739 }
740
741 static Id
repodata_str2dir_rooted(Repodata * data,char * str,int create)742 repodata_str2dir_rooted(Repodata *data, char *str, int create)
743 {
744 char buf[256], *bp;
745 int l = strlen(str);
746 Id id;
747
748 if (l + 2 <= sizeof(buf))
749 bp = buf;
750 else
751 bp = solv_malloc(l + 2);
752 bp[0] = '/';
753 strcpy(bp + 1, str);
754 id = repodata_str2dir(data, bp, create);
755 if (bp != buf)
756 solv_free(bp);
757 return id;
758 }
759
760 static void
adddudata(Repodata * data,Id handle,RpmHead * rpmhead,char ** dn,uint32_t * di,int fc,int dc)761 adddudata(Repodata *data, Id handle, RpmHead *rpmhead, char **dn, uint32_t *di, int fc, int dc)
762 {
763 Id did;
764 int i, fszc;
765 unsigned int *fkb, *fn;
766 uint64_t *fsz64;
767 uint32_t *fsz, *fino;
768 uint16_t *fm;
769 unsigned int inotest[256], inotestok;
770
771 if (!fc)
772 return;
773 if ((fsz64 = headint64array(rpmhead, TAG_LONGFILESIZES, &fszc)) != 0)
774 {
775 /* convert to kbyte */
776 fsz = solv_malloc2(fszc, sizeof(*fsz));
777 for (i = 0; i < fszc; i++)
778 fsz[i] = fsz64[i] ? fsz64[i] / 1024 + 1 : 0;
779 solv_free(fsz64);
780 }
781 else if ((fsz = headint32array(rpmhead, TAG_FILESIZES, &fszc)) != 0)
782 {
783 /* convert to kbyte */
784 for (i = 0; i < fszc; i++)
785 if (fsz[i])
786 fsz[i] = fsz[i] / 1024 + 1;
787 }
788 else
789 return;
790 if (fc != fszc)
791 {
792 solv_free(fsz);
793 return;
794 }
795
796 /* stupid rpm records sizes of directories, so we have to check the mode */
797 fm = headint16array(rpmhead, TAG_FILEMODES, &fszc);
798 if (!fm || fc != fszc)
799 {
800 solv_free(fsz);
801 solv_free(fm);
802 return;
803 }
804 fino = headint32array(rpmhead, TAG_FILEINODES, &fszc);
805 if (!fino || fc != fszc)
806 {
807 solv_free(fsz);
808 solv_free(fm);
809 solv_free(fino);
810 return;
811 }
812
813 /* kill hardlinked entries */
814 inotestok = 0;
815 if (fc < sizeof(inotest))
816 {
817 /* quick test just hashing the inode numbers */
818 memset(inotest, 0, sizeof(inotest));
819 for (i = 0; i < fc; i++)
820 {
821 int off, bit;
822 if (fsz[i] == 0 || !S_ISREG(fm[i]))
823 continue; /* does not matter */
824 off = (fino[i] >> 5) & (sizeof(inotest)/sizeof(*inotest) - 1);
825 bit = 1 << (fino[i] & 31);
826 if ((inotest[off] & bit) != 0)
827 break;
828 inotest[off] |= bit;
829 }
830 if (i == fc)
831 inotestok = 1; /* no conflict found */
832 }
833 if (!inotestok)
834 {
835 /* hardlinked files are possible, check ino/dev pairs */
836 unsigned int *fdev = headint32array(rpmhead, TAG_FILEDEVICES, &fszc);
837 unsigned int *fx, j;
838 unsigned int mask, hash, hh;
839 if (!fdev || fc != fszc)
840 {
841 solv_free(fsz);
842 solv_free(fm);
843 solv_free(fdev);
844 solv_free(fino);
845 return;
846 }
847 mask = fc;
848 while ((mask & (mask - 1)) != 0)
849 mask = mask & (mask - 1);
850 mask <<= 2;
851 if (mask > sizeof(inotest)/sizeof(*inotest))
852 fx = solv_calloc(mask, sizeof(unsigned int));
853 else
854 {
855 fx = inotest;
856 memset(fx, 0, mask * sizeof(unsigned int));
857 }
858 mask--;
859 for (i = 0; i < fc; i++)
860 {
861 if (fsz[i] == 0 || !S_ISREG(fm[i]))
862 continue;
863 hash = (fino[i] + fdev[i] * 31) & mask;
864 hh = 7;
865 while ((j = fx[hash]) != 0)
866 {
867 if (fino[j - 1] == fino[i] && fdev[j - 1] == fdev[i])
868 {
869 fsz[i] = 0; /* kill entry */
870 break;
871 }
872 hash = (hash + hh++) & mask;
873 }
874 if (!j)
875 fx[hash] = i + 1;
876 }
877 if (fx != inotest)
878 solv_free(fx);
879 solv_free(fdev);
880 }
881 solv_free(fino);
882
883 /* sum up inode count and kbytes for each directory */
884 fn = solv_calloc(dc, sizeof(unsigned int));
885 fkb = solv_calloc(dc, sizeof(unsigned int));
886 for (i = 0; i < fc; i++)
887 {
888 if (di[i] >= dc)
889 continue; /* corrupt entry */
890 fn[di[i]]++;
891 if (fsz[i] == 0 || !S_ISREG(fm[i]))
892 continue;
893 fkb[di[i]] += fsz[i];
894 }
895 solv_free(fsz);
896 solv_free(fm);
897 /* commit */
898 for (i = 0; i < dc; i++)
899 {
900 if (!fn[i])
901 continue;
902 if (dn[i][0] != '/')
903 {
904 Solvable *s = data->repo->pool->solvables + handle;
905 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
906 did = repodata_str2dir(data, "/usr/src", 1);
907 else
908 did = repodata_str2dir_rooted(data, dn[i], 1);
909 }
910 else
911 did = repodata_str2dir(data, dn[i], 1);
912 repodata_add_dirnumnum(data, handle, SOLVABLE_DISKUSAGE, did, fkb[i], fn[i]);
913 }
914 solv_free(fn);
915 solv_free(fkb);
916 }
917
918 static int
is_filtered(const char * dir)919 is_filtered(const char *dir)
920 {
921 if (!dir)
922 return 1;
923 /* the dirs always have a trailing / in rpm */
924 if (strstr(dir, "bin/"))
925 return 0;
926 if (!strncmp(dir, "/etc/", 5))
927 return 0;
928 if (!strcmp(dir, "/usr/lib/"))
929 return 2;
930 return 1;
931 }
932
933 static void
addfilelist(Repodata * data,Id handle,RpmHead * rpmhead,int flags)934 addfilelist(Repodata *data, Id handle, RpmHead *rpmhead, int flags)
935 {
936 char **bn;
937 char **dn;
938 uint32_t *di;
939 int bnc, dnc, dic;
940 int i;
941 Id did;
942 uint32_t lastdii = -1;
943 int lastfiltered = 0;
944
945 if (!data)
946 return;
947 bn = headstringarray(rpmhead, TAG_BASENAMES, &bnc);
948 if (!bn)
949 return;
950 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dnc);
951 if (!dn)
952 {
953 solv_free(bn);
954 return;
955 }
956 di = headint32array(rpmhead, TAG_DIRINDEXES, &dic);
957 if (!di)
958 {
959 solv_free(bn);
960 solv_free(dn);
961 return;
962 }
963 if (bnc != dic)
964 {
965 pool_error(data->repo->pool, 0, "bad filelist");
966 return;
967 }
968
969 adddudata(data, handle, rpmhead, dn, di, bnc, dnc);
970
971 did = -1;
972 for (i = 0; i < bnc; i++)
973 {
974 char *b = bn[i];
975
976 if (did < 0 || di[i] != lastdii)
977 {
978 if (di[i] >= dnc)
979 continue; /* corrupt entry */
980 did = 0;
981 lastdii = di[i];
982 if ((flags & RPM_ADD_FILTERED_FILELIST) != 0)
983 {
984 lastfiltered = is_filtered(dn[di[i]]);
985 if (lastfiltered == 1)
986 continue;
987 }
988 if (dn[lastdii][0] != '/')
989 did = repodata_str2dir_rooted(data, dn[lastdii], 1);
990 else
991 did = repodata_str2dir(data, dn[lastdii], 1);
992 }
993 if (!b)
994 continue;
995 if (*b == '/') /* work around rpm bug */
996 b++;
997 if (lastfiltered && (lastfiltered != 2 || strcmp(b, "sendmail")))
998 continue;
999 repodata_add_dirstr(data, handle, SOLVABLE_FILELIST, did, b);
1000 }
1001 solv_free(bn);
1002 solv_free(dn);
1003 solv_free(di);
1004 }
1005
1006 static void
addchangelog(Repodata * data,Id handle,RpmHead * rpmhead)1007 addchangelog(Repodata *data, Id handle, RpmHead *rpmhead)
1008 {
1009 char **cn;
1010 char **cx;
1011 uint32_t *ct;
1012 int i, cnc, cxc, ctc = 0;
1013 Queue hq;
1014
1015 ct = headint32array(rpmhead, TAG_CHANGELOGTIME, &ctc);
1016 cx = headstringarray(rpmhead, TAG_CHANGELOGTEXT, &cxc);
1017 cn = headstringarray(rpmhead, TAG_CHANGELOGNAME, &cnc);
1018 if (!ct || !cx || !cn || !ctc || ctc != cxc || ctc != cnc)
1019 {
1020 solv_free(ct);
1021 solv_free(cx);
1022 solv_free(cn);
1023 return;
1024 }
1025 queue_init(&hq);
1026 for (i = 0; i < ctc; i++)
1027 {
1028 Id h = repodata_new_handle(data);
1029 if (ct[i])
1030 repodata_set_num(data, h, SOLVABLE_CHANGELOG_TIME, ct[i]);
1031 if (cn[i])
1032 setutf8string(data, h, SOLVABLE_CHANGELOG_AUTHOR, cn[i]);
1033 if (cx[i])
1034 setutf8string(data, h, SOLVABLE_CHANGELOG_TEXT, cx[i]);
1035 queue_push(&hq, h);
1036 }
1037 for (i = 0; i < hq.count; i++)
1038 repodata_add_flexarray(data, handle, SOLVABLE_CHANGELOG, hq.elements[i]);
1039 queue_free(&hq);
1040 solv_free(ct);
1041 solv_free(cx);
1042 solv_free(cn);
1043 }
1044
1045 static void
set_description_author(Repodata * data,Id handle,char * str)1046 set_description_author(Repodata *data, Id handle, char *str)
1047 {
1048 char *aut, *p;
1049 for (aut = str; (aut = strchr(aut, '\n')) != 0; aut++)
1050 if (!strncmp(aut, "\nAuthors:\n--------\n", 19))
1051 break;
1052 if (aut)
1053 {
1054 /* oh my, found SUSE special author section */
1055 int l = aut - str;
1056 str = solv_strdup(str);
1057 aut = str + l;
1058 str[l] = 0;
1059 while (l > 0 && str[l - 1] == '\n')
1060 str[--l] = 0;
1061 if (l)
1062 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
1063 p = aut + 19;
1064 aut = str; /* copy over */
1065 while (*p == ' ' || *p == '\n')
1066 p++;
1067 while (*p)
1068 {
1069 if (*p == '\n')
1070 {
1071 *aut++ = *p++;
1072 while (*p == ' ')
1073 p++;
1074 continue;
1075 }
1076 *aut++ = *p++;
1077 }
1078 while (aut != str && aut[-1] == '\n')
1079 aut--;
1080 *aut = 0;
1081 if (*str)
1082 setutf8string(data, handle, SOLVABLE_AUTHORS, str);
1083 free(str);
1084 }
1085 else if (*str)
1086 setutf8string(data, handle, SOLVABLE_DESCRIPTION, str);
1087 }
1088
1089 static int
rpmhead2solv(Pool * pool,Repo * repo,Repodata * data,Solvable * s,RpmHead * rpmhead,int flags)1090 rpmhead2solv(Pool *pool, Repo *repo, Repodata *data, Solvable *s, RpmHead *rpmhead, int flags)
1091 {
1092 char *name;
1093 char *evr;
1094 char *sourcerpm;
1095 Queue ignq;
1096 Id ignqbuf[64];
1097
1098 name = headstring(rpmhead, TAG_NAME);
1099 if (!name)
1100 {
1101 pool_error(pool, 0, "package has no name");
1102 return 0;
1103 }
1104 if (!(flags & RPMDB_KEEP_GPG_PUBKEY) && !strcmp(name, "gpg-pubkey"))
1105 return 0;
1106 s->name = pool_str2id(pool, name, 1);
1107 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
1108 if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
1109 s->arch = pool_str2id(pool, headstring(rpmhead, TAG_ARCH), 1);
1110 else
1111 {
1112 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
1113 s->arch = ARCH_NOSRC;
1114 else
1115 s->arch = ARCH_SRC;
1116 }
1117 if (!s->arch)
1118 s->arch = ARCH_NOARCH;
1119 evr = headtoevr(rpmhead);
1120 s->evr = pool_str2id(pool, evr, 1);
1121 solv_free(evr);
1122 s->vendor = pool_str2id(pool, headstring(rpmhead, TAG_VENDOR), 1);
1123
1124 queue_init_buffer(&ignq, ignqbuf, sizeof(ignqbuf)/sizeof(*ignqbuf));
1125
1126 s->provides = makedeps(pool, repo, rpmhead, TAG_PROVIDENAME, TAG_PROVIDEVERSION, TAG_PROVIDEFLAGS, 0, 0);
1127 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
1128 s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
1129 s->requires = makedeps(pool, repo, rpmhead, TAG_REQUIRENAME, TAG_REQUIREVERSION, TAG_REQUIREFLAGS, flags, &ignq);
1130 s->conflicts = makedeps(pool, repo, rpmhead, TAG_CONFLICTNAME, TAG_CONFLICTVERSION, TAG_CONFLICTFLAGS, 0, 0);
1131 s->obsoletes = makedeps(pool, repo, rpmhead, TAG_OBSOLETENAME, TAG_OBSOLETEVERSION, TAG_OBSOLETEFLAGS, 0, 0);
1132
1133 s->recommends = makedeps(pool, repo, rpmhead, TAG_RECOMMENDNAME, TAG_RECOMMENDVERSION, TAG_RECOMMENDFLAGS, 0, 0);
1134 s->suggests = makedeps(pool, repo, rpmhead, TAG_SUGGESTNAME, TAG_SUGGESTVERSION, TAG_SUGGESTFLAGS, 0, 0);
1135 s->supplements = makedeps(pool, repo, rpmhead, TAG_SUPPLEMENTNAME, TAG_SUPPLEMENTVERSION, TAG_SUPPLEMENTFLAGS, 0, 0);
1136 s->enhances = makedeps(pool, repo, rpmhead, TAG_ENHANCENAME, TAG_ENHANCEVERSION, TAG_ENHANCEFLAGS, 0, 0);
1137
1138 repo_rewrite_suse_deps(s, 0);
1139
1140 if (data && ignq.count)
1141 repodata_set_idarray(data, s - pool->solvables, SOLVABLE_PREREQ_IGNOREINST, &ignq);
1142 queue_free(&ignq);
1143
1144 if (data)
1145 {
1146 Id handle;
1147 char *str;
1148 unsigned int u32;
1149 unsigned long long u64;
1150
1151 handle = s - pool->solvables;
1152 str = headstring(rpmhead, TAG_SUMMARY);
1153 if (str)
1154 setutf8string(data, handle, SOLVABLE_SUMMARY, str);
1155 str = headstring(rpmhead, TAG_DESCRIPTION);
1156 if (str)
1157 set_description_author(data, handle, str);
1158 str = headstring(rpmhead, TAG_GROUP);
1159 if (str)
1160 repodata_set_poolstr(data, handle, SOLVABLE_GROUP, str);
1161 str = headstring(rpmhead, TAG_LICENSE);
1162 if (str)
1163 repodata_set_poolstr(data, handle, SOLVABLE_LICENSE, str);
1164 str = headstring(rpmhead, TAG_URL);
1165 if (str)
1166 repodata_set_str(data, handle, SOLVABLE_URL, str);
1167 str = headstring(rpmhead, TAG_DISTRIBUTION);
1168 if (str)
1169 repodata_set_poolstr(data, handle, SOLVABLE_DISTRIBUTION, str);
1170 str = headstring(rpmhead, TAG_PACKAGER);
1171 if (str)
1172 repodata_set_poolstr(data, handle, SOLVABLE_PACKAGER, str);
1173 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1174 {
1175 unsigned char *chksum;
1176 unsigned int chksumsize;
1177 chksum = headbinary(rpmhead, TAG_SIGMD5, &chksumsize);
1178 if (chksum && chksumsize == 16)
1179 repodata_set_bin_checksum(data, handle, SOLVABLE_PKGID, REPOKEY_TYPE_MD5, chksum);
1180 }
1181 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1182 {
1183 str = headstring(rpmhead, TAG_SHA1HEADER);
1184 if (str && strlen(str) == 40)
1185 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA1, str);
1186 else if (str && strlen(str) == 64)
1187 repodata_set_checksum(data, handle, SOLVABLE_HDRID, REPOKEY_TYPE_SHA256, str);
1188 }
1189 u32 = headint32(rpmhead, TAG_BUILDTIME);
1190 if (u32)
1191 repodata_set_num(data, handle, SOLVABLE_BUILDTIME, u32);
1192 str = headstring(rpmhead, TAG_BUILDHOST);
1193 if (str)
1194 repodata_set_str(data, handle, SOLVABLE_BUILDHOST, str);
1195 u32 = headint32(rpmhead, TAG_INSTALLTIME);
1196 if (u32)
1197 repodata_set_num(data, handle, SOLVABLE_INSTALLTIME, u32);
1198 u64 = headint64(rpmhead, TAG_LONGSIZE);
1199 if (u64)
1200 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u64);
1201 else
1202 {
1203 u32 = headint32(rpmhead, TAG_SIZE);
1204 if (u32)
1205 repodata_set_num(data, handle, SOLVABLE_INSTALLSIZE, u32);
1206 }
1207 if (sourcerpm)
1208 repodata_set_sourcepkg(data, handle, sourcerpm);
1209 if ((flags & RPM_ADD_TRIGGERS) != 0)
1210 {
1211 unsigned int ida = makedeps(pool, repo, rpmhead, TAG_TRIGGERNAME, TAG_TRIGGERVERSION, TAG_TRIGGERFLAGS, 0, 0);
1212 Id id, lastid = 0;
1213 for (lastid = 0; (id = repo->idarraydata[ida]) != 0; ida++, lastid = id)
1214 if (id != lastid)
1215 repodata_add_idarray(data, handle, SOLVABLE_TRIGGERS, id);
1216 }
1217 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1218 addfilelist(data, handle, rpmhead, flags);
1219 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1220 addchangelog(data, handle, rpmhead);
1221 }
1222 return 1;
1223 }
1224
1225 static inline unsigned int
getu32(const unsigned char * dp)1226 getu32(const unsigned char *dp)
1227 {
1228 return dp[0] << 24 | dp[1] << 16 | dp[2] << 8 | dp[3];
1229 }
1230
1231 #ifdef ENABLE_RPMDB
1232
1233 struct rpmdbentry {
1234 Id rpmdbid;
1235 Id nameoff;
1236 };
1237
1238 #define ENTRIES_BLOCK 255
1239 #define NAMEDATA_BLOCK 1023
1240
1241 # ifdef ENABLE_RPMDB_LIBRPM
1242 # include "repo_rpmdb_librpm.h"
1243 # else
1244 # include "repo_rpmdb_bdb.h"
1245 # endif
1246
1247 #else
1248
1249 /* dummy state just to store pool/rootdir and header data */
1250 struct rpmdbstate {
1251 Pool *pool;
1252 char *rootdir;
1253
1254 RpmHead *rpmhead; /* header storage space */
1255 unsigned int rpmheadsize;
1256 };
1257
1258 #endif
1259
1260
1261 #ifndef ENABLE_RPMPKG_LIBRPM
1262
1263 static inline RpmHead *
realloc_head(struct rpmdbstate * state,unsigned int len)1264 realloc_head(struct rpmdbstate *state, unsigned int len)
1265 {
1266 if (len > state->rpmheadsize)
1267 {
1268 state->rpmheadsize = len + 128;
1269 state->rpmhead = solv_realloc(state->rpmhead, sizeof(*state->rpmhead) + state->rpmheadsize);
1270 }
1271 return state->rpmhead;
1272 }
1273
1274 static int
headfromfp(struct rpmdbstate * state,const char * name,FILE * fp,unsigned char * lead,unsigned int cnt,unsigned int dsize,unsigned int pad,Chksum * chk1,Chksum * chk2)1275 headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
1276 {
1277 unsigned int len = 16 * cnt + dsize + pad;
1278 RpmHead *rpmhead = realloc_head(state, len + 1);
1279 if (fread(rpmhead->data, len, 1, fp) != 1)
1280 return pool_error(state->pool, 0, "%s: unexpected EOF", name);
1281 if (chk1)
1282 solv_chksum_add(chk1, rpmhead->data, len);
1283 if (chk2)
1284 solv_chksum_add(chk2, rpmhead->data, len);
1285 headinit(rpmhead, cnt, dsize);
1286 return 1;
1287 }
1288
1289 # if defined(ENABLE_RPMDB) && (!defined(ENABLE_RPMDB_LIBRPM) || defined(HAVE_RPMDBNEXTITERATORHEADERBLOB))
1290
1291 static int
headfromhdrblob(struct rpmdbstate * state,const unsigned char * data,unsigned int size)1292 headfromhdrblob(struct rpmdbstate *state, const unsigned char *data, unsigned int size)
1293 {
1294 unsigned int dsize, cnt, len;
1295 RpmHead *rpmhead;
1296 if (size < 8)
1297 return pool_error(state->pool, 0, "corrupt rpm database (size)");
1298 cnt = getu32(data);
1299 dsize = getu32(data + 4);
1300 if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
1301 return pool_error(state->pool, 0, "corrupt rpm database (cnt/dcnt)");
1302 if (8 + cnt * 16 + dsize > size)
1303 return pool_error(state->pool, 0, "corrupt rpm database (data size)");
1304 len = 16 * cnt + dsize;
1305 rpmhead = realloc_head(state, len + 1);
1306 memcpy(rpmhead->data, data + 8, len);
1307 headinit(rpmhead, cnt, dsize);
1308 return 1;
1309 }
1310
1311 # endif
1312
1313 #else
1314
1315 static int
headfromfp(struct rpmdbstate * state,const char * name,FILE * fp,unsigned char * lead,unsigned int cnt,unsigned int dsize,unsigned int pad,Chksum * chk1,Chksum * chk2)1316 headfromfp(struct rpmdbstate *state, const char *name, FILE *fp, unsigned char *lead, unsigned int cnt, unsigned int dsize, unsigned int pad, Chksum *chk1, Chksum *chk2)
1317 {
1318 unsigned int len = 16 * cnt + dsize + pad;
1319 char *buf = solv_malloc(8 + len);
1320 Header h;
1321 memcpy(buf, lead + 8, 8);
1322 if (fread(buf + 8, len, 1, fp) != 1)
1323 {
1324 solv_free(buf);
1325 return pool_error(state->pool, 0, "%s: unexpected EOF", name);
1326 }
1327 if (chk1)
1328 solv_chksum_add(chk1, buf + 8, len);
1329 if (chk2)
1330 solv_chksum_add(chk2, buf + 8, len);
1331 h = headerImport(buf, 8 + len - pad, HEADERIMPORT_FAST);
1332 if (!h)
1333 {
1334 solv_free(buf);
1335 return pool_error(state->pool, 0, "%s: headerImport error", name);
1336 }
1337 if (state->rpmhead)
1338 headfree(state->rpmhead);
1339 state->rpmhead = h;
1340 return 1;
1341 }
1342
1343 #endif
1344
1345 static void
freestate(struct rpmdbstate * state)1346 freestate(struct rpmdbstate *state)
1347 {
1348 /* close down */
1349 #ifdef ENABLE_RPMDB
1350 if (state->dbenvopened)
1351 closedbenv(state);
1352 if (state->dbpath_allocated)
1353 solv_free((char *)state->dbpath);
1354 #endif
1355 if (state->rootdir)
1356 solv_free(state->rootdir);
1357 headfree(state->rpmhead);
1358 }
1359
1360 void *
rpm_state_create(Pool * pool,const char * rootdir)1361 rpm_state_create(Pool *pool, const char *rootdir)
1362 {
1363 struct rpmdbstate *state;
1364 state = solv_calloc(1, sizeof(*state));
1365 state->pool = pool;
1366 if (rootdir)
1367 state->rootdir = solv_strdup(rootdir);
1368 return state;
1369 }
1370
1371 void *
rpm_state_free(void * state)1372 rpm_state_free(void *state)
1373 {
1374 if (state)
1375 freestate(state);
1376 return solv_free(state);
1377 }
1378
1379
1380 #ifdef ENABLE_RPMDB
1381
1382
1383 /******************************************************************/
1384
1385 static Offset
copydeps(Pool * pool,Repo * repo,Offset fromoff,Repo * fromrepo)1386 copydeps(Pool *pool, Repo *repo, Offset fromoff, Repo *fromrepo)
1387 {
1388 int cc;
1389 Id *ida, *from;
1390 Offset ido;
1391
1392 if (!fromoff)
1393 return 0;
1394 from = fromrepo->idarraydata + fromoff;
1395 for (ida = from, cc = 0; *ida; ida++, cc++)
1396 ;
1397 if (cc == 0)
1398 return 0;
1399 ido = repo_reserve_ids(repo, 0, cc);
1400 ida = repo->idarraydata + ido;
1401 memcpy(ida, from, (cc + 1) * sizeof(Id));
1402 repo->idarraysize += cc + 1;
1403 return ido;
1404 }
1405
1406 struct solvable_copy_cbdata {
1407 Repodata *data;
1408 Id handle;
1409 Id subhandle;
1410 Id *dircache;
1411 int bad;
1412 };
1413
1414 static int
solvable_copy_cb(void * vcbdata,Solvable * r,Repodata * fromdata,Repokey * key,KeyValue * kv)1415 solvable_copy_cb(void *vcbdata, Solvable *r, Repodata *fromdata, Repokey *key, KeyValue *kv)
1416 {
1417 struct solvable_copy_cbdata *cbdata = vcbdata;
1418 Repodata *data = cbdata->data;
1419 Id handle = cbdata->handle;
1420
1421 switch (key->type)
1422 {
1423 case REPOKEY_TYPE_ID:
1424 case REPOKEY_TYPE_CONSTANTID:
1425 case REPOKEY_TYPE_IDARRAY: /* used for triggers */
1426 if (data->localpool || fromdata->localpool)
1427 kv->id = repodata_translate_id(data, fromdata, kv->id, 1);
1428 break;
1429 case REPOKEY_TYPE_DIRNUMNUMARRAY:
1430 case REPOKEY_TYPE_DIRSTRARRAY:
1431 kv->id = repodata_translate_dir(data, fromdata, kv->id, 1, fromdata->repodataid == 1 ? cbdata->dircache : 0);
1432 if (!kv->id)
1433 {
1434 cbdata->bad = 1; /* oops, cannot copy this */
1435 return 0;
1436 }
1437 break;
1438 case REPOKEY_TYPE_FIXARRAY:
1439 cbdata->handle = repodata_new_handle(data);
1440 repodata_add_fixarray(data, handle, key->name, cbdata->handle);
1441 repodata_search_arrayelement(fromdata, 0, 0, 0, kv, &solvable_copy_cb, cbdata);
1442 cbdata->handle = handle;
1443 return 0;
1444 case REPOKEY_TYPE_FLEXARRAY:
1445 cbdata->handle = repodata_new_handle(data);
1446 repodata_add_flexarray(data, handle, key->name, cbdata->handle);
1447 repodata_search_arrayelement(fromdata, 0, 0, 0, kv, &solvable_copy_cb, cbdata);
1448 cbdata->handle = handle;
1449 return 0;
1450 default:
1451 break;
1452 }
1453 repodata_set_kv(data, handle, key->name, key->type, kv);
1454 return 0;
1455 }
1456
1457 static int
solvable_copy(Solvable * s,Solvable * r,Repodata * data,Id * dircache,Id ** oldkeyskip)1458 solvable_copy(Solvable *s, Solvable *r, Repodata *data, Id *dircache, Id **oldkeyskip)
1459 {
1460 int p, i;
1461 Repo *repo = s->repo;
1462 Pool *pool = repo->pool;
1463 Repo *fromrepo = r->repo;
1464 struct solvable_copy_cbdata cbdata;
1465 Id *keyskip;
1466
1467 /* copy solvable data */
1468 s->name = r->name;
1469 s->evr = r->evr;
1470 s->arch = r->arch;
1471 s->vendor = r->vendor;
1472 s->provides = copydeps(pool, repo, r->provides, fromrepo);
1473 s->requires = copydeps(pool, repo, r->requires, fromrepo);
1474 s->conflicts = copydeps(pool, repo, r->conflicts, fromrepo);
1475 s->obsoletes = copydeps(pool, repo, r->obsoletes, fromrepo);
1476 s->recommends = copydeps(pool, repo, r->recommends, fromrepo);
1477 s->suggests = copydeps(pool, repo, r->suggests, fromrepo);
1478 s->supplements = copydeps(pool, repo, r->supplements, fromrepo);
1479 s->enhances = copydeps(pool, repo, r->enhances, fromrepo);
1480
1481 /* copy all attributes */
1482 if (!data || fromrepo->nrepodata < 2)
1483 return 1;
1484 cbdata.data = data;
1485 cbdata.handle = s - pool->solvables;
1486 cbdata.subhandle = 0;
1487 cbdata.dircache = dircache;
1488 cbdata.bad = 0;
1489 p = r - fromrepo->pool->solvables;
1490 if (fromrepo->nrepodata == 2)
1491 {
1492 Repodata *fromdata = repo_id2repodata(fromrepo, 1);
1493 if (p >= fromdata->start && p < fromdata->end)
1494 repodata_search(fromdata, p, 0, 0, solvable_copy_cb, &cbdata);
1495 }
1496 else
1497 {
1498 keyskip = repo_create_keyskip(repo, p, oldkeyskip);
1499 FOR_REPODATAS(fromrepo, i, data)
1500 {
1501 if (p >= data->start && p < data->end)
1502 repodata_search_keyskip(data, p, 0, 0, keyskip, solvable_copy_cb, &cbdata);
1503 }
1504 }
1505 if (cbdata.bad)
1506 {
1507 repodata_unset_uninternalized(data, cbdata.handle, 0);
1508 memset(s, 0, sizeof(*s));
1509 s->repo = repo;
1510 return 0;
1511 }
1512 return 1;
1513 }
1514
1515 /* used to sort entries by package name that got returned in some database order */
1516 static int
rpmids_sort_cmp(const void * va,const void * vb,void * dp)1517 rpmids_sort_cmp(const void *va, const void *vb, void *dp)
1518 {
1519 struct rpmdbentry const *a = va, *b = vb;
1520 char *namedata = dp;
1521 int r;
1522 r = strcmp(namedata + a->nameoff, namedata + b->nameoff);
1523 if (r)
1524 return r;
1525 return a->rpmdbid - b->rpmdbid;
1526 }
1527
1528 static int
pkgids_sort_cmp(const void * va,const void * vb,void * dp)1529 pkgids_sort_cmp(const void *va, const void *vb, void *dp)
1530 {
1531 Repo *repo = dp;
1532 Pool *pool = repo->pool;
1533 Solvable *a = pool->solvables + *(Id *)va;
1534 Solvable *b = pool->solvables + *(Id *)vb;
1535 Id *rpmdbid;
1536
1537 if (a->name != b->name)
1538 return strcmp(pool_id2str(pool, a->name), pool_id2str(pool, b->name));
1539 rpmdbid = repo->rpmdbid;
1540 return rpmdbid[(a - pool->solvables) - repo->start] - rpmdbid[(b - pool->solvables) - repo->start];
1541 }
1542
1543 static void
swap_solvables(Repo * repo,Repodata * data,Id pa,Id pb)1544 swap_solvables(Repo *repo, Repodata *data, Id pa, Id pb)
1545 {
1546 Pool *pool = repo->pool;
1547 Solvable tmp;
1548
1549 tmp = pool->solvables[pa];
1550 pool->solvables[pa] = pool->solvables[pb];
1551 pool->solvables[pb] = tmp;
1552 if (repo->rpmdbid)
1553 {
1554 Id tmpid = repo->rpmdbid[pa - repo->start];
1555 repo->rpmdbid[pa - repo->start] = repo->rpmdbid[pb - repo->start];
1556 repo->rpmdbid[pb - repo->start] = tmpid;
1557 }
1558 /* only works if nothing is already internalized! */
1559 if (data)
1560 repodata_swap_attrs(data, pa, pb);
1561 }
1562
1563 static void
mkrpmdbcookie(struct stat * st,unsigned char * cookie,int flags)1564 mkrpmdbcookie(struct stat *st, unsigned char *cookie, int flags)
1565 {
1566 int f = 0;
1567 memset(cookie, 0, 32);
1568 cookie[3] = RPMDB_COOKIE_VERSION;
1569 memcpy(cookie + 16, &st->st_ino, sizeof(st->st_ino));
1570 memcpy(cookie + 24, &st->st_dev, sizeof(st->st_dev));
1571 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1572 f |= 1;
1573 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1574 f |= 2;
1575 if ((flags & RPM_ADD_WITH_CHANGELOG) != 0)
1576 f |= 4;
1577 if ((flags & RPM_ADD_NO_FILELIST) == 0)
1578 f |= 8;
1579 if ((flags & RPM_ADD_NO_RPMLIBREQS) != 0)
1580 cookie[1] = 1;
1581 cookie[0] = f;
1582 }
1583
1584 /*
1585 * read rpm db as repo
1586 *
1587 */
1588
1589 int
repo_add_rpmdb(Repo * repo,Repo * ref,int flags)1590 repo_add_rpmdb(Repo *repo, Repo *ref, int flags)
1591 {
1592 Pool *pool = repo->pool;
1593 struct stat packagesstat;
1594 unsigned char newcookie[32];
1595 const unsigned char *oldcookie = 0;
1596 Id oldcookietype = 0;
1597 Repodata *data;
1598 int count = 0, done = 0;
1599 struct rpmdbstate state;
1600 int i;
1601 Solvable *s;
1602 unsigned int now;
1603
1604 now = solv_timems(0);
1605 memset(&state, 0, sizeof(state));
1606 state.pool = pool;
1607 if (flags & REPO_USE_ROOTDIR)
1608 state.rootdir = solv_strdup(pool_get_rootdir(pool));
1609
1610 data = repo_add_repodata(repo, flags);
1611
1612 if (ref && !(ref->nsolvables && ref->rpmdbid && ref->pool == repo->pool))
1613 {
1614 if ((flags & RPMDB_EMPTY_REFREPO) != 0)
1615 repo_empty(ref, 1);
1616 ref = 0;
1617 }
1618
1619 if (!opendbenv(&state))
1620 {
1621 solv_free(state.rootdir);
1622 return -1;
1623 }
1624
1625 /* XXX: should get ro lock of Packages database! */
1626 if (stat_database(&state, &packagesstat))
1627 {
1628 freestate(&state);
1629 return -1;
1630 }
1631 mkrpmdbcookie(&packagesstat, newcookie, flags);
1632 repodata_set_bin_checksum(data, SOLVID_META, REPOSITORY_RPMDBCOOKIE, REPOKEY_TYPE_SHA256, newcookie);
1633
1634 if (ref)
1635 oldcookie = repo_lookup_bin_checksum(ref, SOLVID_META, REPOSITORY_RPMDBCOOKIE, &oldcookietype);
1636 if (!ref || !oldcookie || oldcookietype != REPOKEY_TYPE_SHA256 || memcmp(oldcookie, newcookie, 32) != 0)
1637 {
1638 int solvstart = 0, solvend = 0;
1639 Id dbid;
1640
1641 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1642 repo_empty(ref, 1); /* get it out of the way */
1643 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1644 count = count_headers(&state);
1645 if (pkgdb_cursor_open(&state))
1646 {
1647 freestate(&state);
1648 return -1;
1649 }
1650 i = 0;
1651 s = 0;
1652 while ((dbid = pkgdb_cursor_getrpm(&state)) != 0)
1653 {
1654 if (dbid == -1)
1655 {
1656 pkgdb_cursor_close(&state);
1657 freestate(&state);
1658 return -1;
1659 }
1660 if (!s)
1661 {
1662 s = pool_id2solvable(pool, repo_add_solvable(repo));
1663 if (!solvstart)
1664 solvstart = s - pool->solvables;
1665 solvend = s - pool->solvables + 1;
1666 }
1667 if (!repo->rpmdbid)
1668 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1669 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1670 if (rpmhead2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS))
1671 {
1672 i++;
1673 s = 0;
1674 }
1675 else
1676 {
1677 /* We can reuse this solvable, but make sure it's still
1678 associated with this repo. */
1679 memset(s, 0, sizeof(*s));
1680 s->repo = repo;
1681 }
1682 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1683 {
1684 if (done < count)
1685 done++;
1686 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1687 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1688 }
1689 }
1690 pkgdb_cursor_close(&state);
1691 if (s)
1692 {
1693 /* oops, could not reuse. free it instead */
1694 s = solvable_free(s, 1);
1695 solvend--;
1696 }
1697 /* now sort all solvables in the new solvstart..solvend block */
1698 if (solvend - solvstart > 1)
1699 {
1700 Id *pkgids = solv_malloc2(solvend - solvstart, sizeof(Id));
1701 for (i = solvstart; i < solvend; i++)
1702 pkgids[i - solvstart] = i;
1703 solv_sort(pkgids, solvend - solvstart, sizeof(Id), pkgids_sort_cmp, repo);
1704 /* adapt order */
1705 for (i = solvstart; i < solvend; i++)
1706 {
1707 int j = pkgids[i - solvstart];
1708 while (j < i)
1709 j = pkgids[i - solvstart] = pkgids[j - solvstart];
1710 if (j != i)
1711 swap_solvables(repo, data, i, j);
1712 }
1713 solv_free(pkgids);
1714 }
1715 }
1716 else
1717 {
1718 Id *dircache;
1719 Id *oldkeyskip = 0;
1720 struct rpmdbentry *entries = 0, *rp;
1721 int nentries = 0;
1722 char *namedata = 0;
1723 unsigned int refmask, h;
1724 Id id, *refhash;
1725 int res;
1726
1727 /* get ids of installed rpms */
1728 entries = getinstalledrpmdbids(&state, "Name", 0, &nentries, &namedata, flags & RPMDB_KEEP_GPG_PUBKEY);
1729 if (!entries)
1730 {
1731 freestate(&state);
1732 return -1;
1733 }
1734
1735 /* sort by name */
1736 if (nentries > 1)
1737 solv_sort(entries, nentries, sizeof(*entries), rpmids_sort_cmp, namedata);
1738
1739 /* create hash from dbid to ref */
1740 refmask = mkmask(ref->nsolvables);
1741 refhash = solv_calloc(refmask + 1, sizeof(Id));
1742 for (i = 0; i < ref->end - ref->start; i++)
1743 {
1744 if (!ref->rpmdbid[i])
1745 continue;
1746 h = ref->rpmdbid[i] & refmask;
1747 while (refhash[h])
1748 h = (h + 317) & refmask;
1749 refhash[h] = i + 1; /* make it non-zero */
1750 }
1751
1752 /* count the misses, they will cost us time */
1753 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1754 {
1755 for (i = 0, rp = entries; i < nentries; i++, rp++)
1756 {
1757 if (refhash)
1758 {
1759 Id dbid = rp->rpmdbid;
1760 h = dbid & refmask;
1761 while ((id = refhash[h]))
1762 {
1763 if (ref->rpmdbid[id - 1] == dbid)
1764 break;
1765 h = (h + 317) & refmask;
1766 }
1767 if (id)
1768 continue;
1769 }
1770 count++;
1771 }
1772 }
1773
1774 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1775 s = pool_id2solvable(pool, repo_add_solvable_block_before(repo, nentries, ref));
1776 else
1777 s = pool_id2solvable(pool, repo_add_solvable_block(repo, nentries));
1778 if (!repo->rpmdbid)
1779 repo->rpmdbid = repo_sidedata_create(repo, sizeof(Id));
1780
1781 dircache = repodata_create_dirtranscache(data);
1782 for (i = 0, rp = entries; i < nentries; i++, rp++, s++)
1783 {
1784 Id dbid = rp->rpmdbid;
1785 repo->rpmdbid[(s - pool->solvables) - repo->start] = dbid;
1786 if (refhash)
1787 {
1788 h = dbid & refmask;
1789 while ((id = refhash[h]))
1790 {
1791 if (ref->rpmdbid[id - 1] == dbid)
1792 break;
1793 h = (h + 317) & refmask;
1794 }
1795 if (id)
1796 {
1797 Solvable *r = ref->pool->solvables + ref->start + (id - 1);
1798 if (r->repo == ref && solvable_copy(s, r, data, dircache, &oldkeyskip))
1799 continue;
1800 }
1801 }
1802 res = getrpm_dbid(&state, dbid);
1803 if (res <= 0)
1804 {
1805 if (!res)
1806 pool_error(pool, -1, "inconsistent rpm database, key %d not found. run 'rpm --rebuilddb' to fix.", dbid);
1807 freestate(&state);
1808 solv_free(entries);
1809 solv_free(namedata);
1810 solv_free(refhash);
1811 dircache = repodata_free_dirtranscache(dircache);
1812 return -1;
1813 }
1814 rpmhead2solv(pool, repo, data, s, state.rpmhead, flags | RPM_ADD_TRIGGERS);
1815 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1816 {
1817 if (done < count)
1818 done++;
1819 if (done < count && (done - 1) * 100 / count != done * 100 / count)
1820 pool_debug(pool, SOLV_ERROR, "%%%% %d\n", done * 100 / count);
1821 }
1822 }
1823 dircache = repodata_free_dirtranscache(dircache);
1824
1825 solv_free(oldkeyskip);
1826 solv_free(entries);
1827 solv_free(namedata);
1828 solv_free(refhash);
1829 if (ref && (flags & RPMDB_EMPTY_REFREPO) != 0)
1830 repo_empty(ref, 1);
1831 }
1832
1833 freestate(&state);
1834 if (!(flags & REPO_NO_INTERNALIZE))
1835 repodata_internalize(data);
1836 if ((flags & RPMDB_REPORT_PROGRESS) != 0)
1837 pool_debug(pool, SOLV_ERROR, "%%%% 100\n");
1838 POOL_DEBUG(SOLV_DEBUG_STATS, "repo_add_rpmdb took %d ms\n", solv_timems(now));
1839 POOL_DEBUG(SOLV_DEBUG_STATS, "repo size: %d solvables\n", repo->nsolvables);
1840 POOL_DEBUG(SOLV_DEBUG_STATS, "repo memory used: %d K incore, %d K idarray\n", repodata_memused(data)/1024, repo->idarraysize / (int)(1024/sizeof(Id)));
1841 return 0;
1842 }
1843
1844 int
repo_add_rpmdb_reffp(Repo * repo,FILE * fp,int flags)1845 repo_add_rpmdb_reffp(Repo *repo, FILE *fp, int flags)
1846 {
1847 int res;
1848 Repo *ref = 0;
1849
1850 if (!fp)
1851 return repo_add_rpmdb(repo, 0, flags);
1852 ref = repo_create(repo->pool, "add_rpmdb_reffp");
1853 if (repo_add_solv(ref, fp, 0) != 0)
1854 {
1855 repo_free(ref, 1);
1856 ref = 0;
1857 }
1858 if (ref && ref->start == ref->end)
1859 {
1860 repo_free(ref, 1);
1861 ref = 0;
1862 }
1863 if (ref)
1864 repo_disable_paging(ref);
1865 res = repo_add_rpmdb(repo, ref, flags | RPMDB_EMPTY_REFREPO);
1866 if (ref)
1867 repo_free(ref, 1);
1868 return res;
1869 }
1870
1871 #endif /* ENABLE_RPMDB */
1872
1873 Id
repo_add_rpm(Repo * repo,const char * rpm,int flags)1874 repo_add_rpm(Repo *repo, const char *rpm, int flags)
1875 {
1876 unsigned int sigdsize, sigcnt, sigpad, l;
1877 Pool *pool = repo->pool;
1878 Solvable *s;
1879 struct rpmdbstate state;
1880 char *payloadformat;
1881 FILE *fp;
1882 unsigned char lead[4096];
1883 int headerstart, headerend;
1884 struct stat stb;
1885 Repodata *data;
1886 unsigned char pkgid[16];
1887 unsigned char leadsigid[16];
1888 unsigned char hdrid[32];
1889 int pkgidtype, leadsigidtype, hdridtype;
1890 Id chksumtype = 0;
1891 Chksum *chksumh = 0;
1892 Chksum *leadsigchksumh = 0;
1893
1894 data = repo_add_repodata(repo, flags);
1895
1896 if ((flags & RPM_ADD_WITH_SHA256SUM) != 0)
1897 chksumtype = REPOKEY_TYPE_SHA256;
1898 else if ((flags & RPM_ADD_WITH_SHA1SUM) != 0)
1899 chksumtype = REPOKEY_TYPE_SHA1;
1900
1901 /* open rpm */
1902 if ((fp = fopen(flags & REPO_USE_ROOTDIR ? pool_prepend_rootdir_tmp(pool, rpm) : rpm, "r")) == 0)
1903 {
1904 pool_error(pool, -1, "%s: %s", rpm, strerror(errno));
1905 return 0;
1906 }
1907 if (fstat(fileno(fp), &stb))
1908 {
1909 pool_error(pool, -1, "fstat: %s", strerror(errno));
1910 fclose(fp);
1911 return 0;
1912 }
1913
1914 /* setup state */
1915 memset(&state, 0, sizeof(state));
1916 state.pool = pool;
1917
1918 /* process lead */
1919 if (chksumtype)
1920 chksumh = solv_chksum_create(chksumtype);
1921 if ((flags & RPM_ADD_WITH_LEADSIGID) != 0)
1922 leadsigchksumh = solv_chksum_create(REPOKEY_TYPE_MD5);
1923 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
1924 {
1925 pool_error(pool, -1, "%s: not a rpm", rpm);
1926 solv_chksum_free(leadsigchksumh, 0);
1927 solv_chksum_free(chksumh, 0);
1928 fclose(fp);
1929 return 0;
1930 }
1931 if (chksumh)
1932 solv_chksum_add(chksumh, lead, 96 + 16);
1933 if (leadsigchksumh)
1934 solv_chksum_add(leadsigchksumh, lead, 96 + 16);
1935
1936 /* process signature header */
1937 if (lead[78] != 0 || lead[79] != 5)
1938 {
1939 pool_error(pool, -1, "%s: not a rpm v5 header", rpm);
1940 solv_chksum_free(leadsigchksumh, 0);
1941 solv_chksum_free(chksumh, 0);
1942 fclose(fp);
1943 return 0;
1944 }
1945 if (getu32(lead + 96) != 0x8eade801)
1946 {
1947 pool_error(pool, -1, "%s: bad signature header", rpm);
1948 solv_chksum_free(leadsigchksumh, 0);
1949 solv_chksum_free(chksumh, 0);
1950 fclose(fp);
1951 return 0;
1952 }
1953 sigcnt = getu32(lead + 96 + 8);
1954 sigdsize = getu32(lead + 96 + 12);
1955 if (sigcnt >= MAX_SIG_CNT || sigdsize >= MAX_SIG_DSIZE)
1956 {
1957 pool_error(pool, -1, "%s: bad signature header", rpm);
1958 solv_chksum_free(leadsigchksumh, 0);
1959 solv_chksum_free(chksumh, 0);
1960 fclose(fp);
1961 return 0;
1962 }
1963 sigpad = sigdsize & 7 ? 8 - (sigdsize & 7) : 0;
1964 headerstart = 96 + 16 + sigcnt * 16 + sigdsize + sigpad;
1965 pkgidtype = leadsigidtype = hdridtype = 0;
1966 if ((flags & (RPM_ADD_WITH_PKGID | RPM_ADD_WITH_HDRID)) != 0)
1967 {
1968 if (!headfromfp(&state, rpm, fp, lead + 96, sigcnt, sigdsize, sigpad, chksumh, leadsigchksumh))
1969 {
1970 solv_chksum_free(leadsigchksumh, 0);
1971 solv_chksum_free(chksumh, 0);
1972 fclose(fp);
1973 return 0;
1974 }
1975 if ((flags & RPM_ADD_WITH_PKGID) != 0)
1976 {
1977 unsigned char *chksum;
1978 unsigned int chksumsize;
1979 chksum = headbinary(state.rpmhead, SIGTAG_MD5, &chksumsize);
1980 if (chksum && chksumsize == 16)
1981 {
1982 pkgidtype = REPOKEY_TYPE_MD5;
1983 memcpy(pkgid, chksum, 16);
1984 }
1985 }
1986 if ((flags & RPM_ADD_WITH_HDRID) != 0)
1987 {
1988 const char *str = headstring(state.rpmhead, TAG_SHA1HEADER);
1989 if (str && strlen(str) == 40)
1990 {
1991 if (solv_hex2bin(&str, hdrid, 20) == 20)
1992 hdridtype = REPOKEY_TYPE_SHA1;
1993 }
1994 else if (str && strlen(str) == 64)
1995 {
1996 if (solv_hex2bin(&str, hdrid, 32) == 32)
1997 hdridtype = REPOKEY_TYPE_SHA256;
1998 }
1999 }
2000 }
2001 else
2002 {
2003 /* just skip the signature header */
2004 unsigned int len = sigcnt * 16 + sigdsize + sigpad;
2005 while (len)
2006 {
2007 l = len > 4096 ? 4096 : len;
2008 if (fread(lead, l, 1, fp) != 1)
2009 {
2010 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2011 solv_chksum_free(leadsigchksumh, 0);
2012 solv_chksum_free(chksumh, 0);
2013 fclose(fp);
2014 return 0;
2015 }
2016 if (chksumh)
2017 solv_chksum_add(chksumh, lead, l);
2018 if (leadsigchksumh)
2019 solv_chksum_add(leadsigchksumh, lead, l);
2020 len -= l;
2021 }
2022 }
2023 if (leadsigchksumh)
2024 {
2025 leadsigchksumh = solv_chksum_free(leadsigchksumh, leadsigid);
2026 leadsigidtype = REPOKEY_TYPE_MD5;
2027 }
2028
2029 /* process main header */
2030 if (fread(lead, 16, 1, fp) != 1)
2031 {
2032 pool_error(pool, -1, "%s: unexpected EOF", rpm);
2033 solv_chksum_free(chksumh, 0);
2034 fclose(fp);
2035 return 0;
2036 }
2037 if (chksumh)
2038 solv_chksum_add(chksumh, lead, 16);
2039 if (getu32(lead) != 0x8eade801)
2040 {
2041 pool_error(pool, -1, "%s: bad header", rpm);
2042 solv_chksum_free(chksumh, 0);
2043 fclose(fp);
2044 return 0;
2045 }
2046 sigcnt = getu32(lead + 8);
2047 sigdsize = getu32(lead + 12);
2048 if (sigcnt >= MAX_HDR_CNT || sigdsize >= MAX_HDR_DSIZE)
2049 {
2050 pool_error(pool, -1, "%s: bad header", rpm);
2051 solv_chksum_free(chksumh, 0);
2052 fclose(fp);
2053 return 0;
2054 }
2055 headerend = headerstart + 16 + sigdsize + sigcnt * 16;
2056
2057 if (!headfromfp(&state, rpm, fp, lead, sigcnt, sigdsize, 0, chksumh, 0))
2058 {
2059 solv_chksum_free(chksumh, 0);
2060 fclose(fp);
2061 return 0;
2062 }
2063 if (headexists(state.rpmhead, TAG_PATCHESNAME))
2064 {
2065 /* this is a patch rpm, ignore */
2066 pool_error(pool, -1, "%s: is patch rpm", rpm);
2067 fclose(fp);
2068 solv_chksum_free(chksumh, 0);
2069 headfree(state.rpmhead);
2070 return 0;
2071 }
2072 payloadformat = headstring(state.rpmhead, TAG_PAYLOADFORMAT);
2073 if (payloadformat && !strcmp(payloadformat, "drpm"))
2074 {
2075 /* this is a delta rpm */
2076 pool_error(pool, -1, "%s: is delta rpm", rpm);
2077 fclose(fp);
2078 solv_chksum_free(chksumh, 0);
2079 headfree(state.rpmhead);
2080 return 0;
2081 }
2082 if (chksumh)
2083 while ((l = fread(lead, 1, sizeof(lead), fp)) > 0)
2084 solv_chksum_add(chksumh, lead, l);
2085 fclose(fp);
2086 s = pool_id2solvable(pool, repo_add_solvable(repo));
2087 if (!rpmhead2solv(pool, repo, data, s, state.rpmhead, flags & ~(RPM_ADD_WITH_HDRID | RPM_ADD_WITH_PKGID)))
2088 {
2089 s = solvable_free(s, 1);
2090 solv_chksum_free(chksumh, 0);
2091 headfree(state.rpmhead);
2092 return 0;
2093 }
2094 if (!(flags & REPO_NO_LOCATION))
2095 repodata_set_location(data, s - pool->solvables, 0, 0, rpm);
2096 if (S_ISREG(stb.st_mode))
2097 repodata_set_num(data, s - pool->solvables, SOLVABLE_DOWNLOADSIZE, (unsigned long long)stb.st_size);
2098 repodata_set_num(data, s - pool->solvables, SOLVABLE_HEADEREND, headerend);
2099 if (pkgidtype)
2100 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_PKGID, pkgidtype, pkgid);
2101 if (hdridtype)
2102 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_HDRID, hdridtype, hdrid);
2103 if (leadsigidtype)
2104 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_LEADSIGID, leadsigidtype, leadsigid);
2105 if (chksumh)
2106 {
2107 repodata_set_bin_checksum(data, s - pool->solvables, SOLVABLE_CHECKSUM, chksumtype, solv_chksum_get(chksumh, 0));
2108 chksumh = solv_chksum_free(chksumh, 0);
2109 }
2110 headfree(state.rpmhead);
2111 if (!(flags & REPO_NO_INTERNALIZE))
2112 repodata_internalize(data);
2113 return s - pool->solvables;
2114 }
2115
2116 Id
repo_add_rpm_handle(Repo * repo,void * rpmhandle,int flags)2117 repo_add_rpm_handle(Repo *repo, void *rpmhandle, int flags)
2118 {
2119 Pool *pool = repo->pool;
2120 Repodata *data;
2121 RpmHead *rpmhead = rpmhandle;
2122 Solvable *s;
2123 char *payloadformat;
2124
2125 data = repo_add_repodata(repo, flags);
2126 if (headexists(rpmhead, TAG_PATCHESNAME))
2127 {
2128 pool_error(pool, -1, "is a patch rpm");
2129 return 0;
2130 }
2131 payloadformat = headstring(rpmhead, TAG_PAYLOADFORMAT);
2132 if (payloadformat && !strcmp(payloadformat, "drpm"))
2133 {
2134 /* this is a delta rpm */
2135 pool_error(pool, -1, "is a delta rpm");
2136 return 0;
2137 }
2138 s = pool_id2solvable(pool, repo_add_solvable(repo));
2139 if (!rpmhead2solv(pool, repo, data, s, rpmhead, flags))
2140 {
2141 s = solvable_free(s, 1);
2142 return 0;
2143 }
2144 if (!(flags & REPO_NO_INTERNALIZE))
2145 repodata_internalize(data);
2146 return s - pool->solvables;
2147 }
2148
2149 static inline void
linkhash(const char * lt,char * hash)2150 linkhash(const char *lt, char *hash)
2151 {
2152 unsigned int r = 0;
2153 const unsigned char *str = (const unsigned char *)lt;
2154 int l, c;
2155
2156 l = strlen(lt);
2157 while ((c = *str++) != 0)
2158 r += (r << 3) + c;
2159 sprintf(hash, "%08x%08x%08x%08x", r, l, 0, 0);
2160 }
2161
2162 void
rpm_iterate_filelist(void * rpmhandle,int flags,void (* cb)(void *,const char *,struct filelistinfo *),void * cbdata)2163 rpm_iterate_filelist(void *rpmhandle, int flags, void (*cb)(void *, const char *, struct filelistinfo *), void *cbdata)
2164 {
2165 RpmHead *rpmhead = rpmhandle;
2166 char **bn;
2167 char **dn;
2168 char **md = 0;
2169 char **lt = 0;
2170 uint32_t *di, diidx;
2171 uint32_t *co = 0;
2172 uint32_t *ff = 0;
2173 uint16_t *fm;
2174 unsigned int lastdir;
2175 int lastdirl;
2176 int cnt, dcnt, cnt2;
2177 int i, l1, l;
2178 char *space = 0;
2179 int spacen = 0;
2180 char md5[33];
2181 struct filelistinfo info;
2182
2183 dn = headstringarray(rpmhead, TAG_DIRNAMES, &dcnt);
2184 if (!dn)
2185 return;
2186 if ((flags & RPM_ITERATE_FILELIST_ONLYDIRS) != 0)
2187 {
2188 for (i = 0; i < dcnt; i++)
2189 (*cb)(cbdata, dn[i], 0);
2190 solv_free(dn);
2191 return;
2192 }
2193 bn = headstringarray(rpmhead, TAG_BASENAMES, &cnt);
2194 if (!bn)
2195 {
2196 solv_free(dn);
2197 return;
2198 }
2199 di = headint32array(rpmhead, TAG_DIRINDEXES, &cnt2);
2200 if (!di || cnt != cnt2)
2201 {
2202 solv_free(di);
2203 solv_free(bn);
2204 solv_free(dn);
2205 return;
2206 }
2207 fm = headint16array(rpmhead, TAG_FILEMODES, &cnt2);
2208 if (!fm || cnt != cnt2)
2209 {
2210 solv_free(fm);
2211 solv_free(di);
2212 solv_free(bn);
2213 solv_free(dn);
2214 return;
2215 }
2216 if ((flags & RPM_ITERATE_FILELIST_WITHMD5) != 0)
2217 {
2218 md = headstringarray(rpmhead, TAG_FILEMD5S, &cnt2);
2219 if (!md || cnt != cnt2)
2220 {
2221 solv_free(md);
2222 solv_free(fm);
2223 solv_free(di);
2224 solv_free(bn);
2225 solv_free(dn);
2226 return;
2227 }
2228 }
2229 if ((flags & RPM_ITERATE_FILELIST_WITHCOL) != 0)
2230 {
2231 co = headint32array(rpmhead, TAG_FILECOLORS, &cnt2);
2232 if (co && cnt != cnt2)
2233 {
2234 solv_free(co);
2235 solv_free(md);
2236 solv_free(fm);
2237 solv_free(di);
2238 solv_free(bn);
2239 solv_free(dn);
2240 return;
2241 }
2242 }
2243 if ((flags & RPM_ITERATE_FILELIST_NOGHOSTS) != 0)
2244 {
2245 ff = headint32array(rpmhead, TAG_FILEFLAGS, &cnt2);
2246 if (!ff || cnt != cnt2)
2247 {
2248 solv_free(ff);
2249 solv_free(co);
2250 solv_free(md);
2251 solv_free(fm);
2252 solv_free(di);
2253 solv_free(bn);
2254 solv_free(dn);
2255 return;
2256 }
2257 }
2258 lastdir = dcnt;
2259 lastdirl = 0;
2260 memset(&info, 0, sizeof(info));
2261 for (i = 0; i < cnt; i++)
2262 {
2263 if (ff && (ff[i] & FILEFLAG_GHOST) != 0)
2264 continue;
2265 diidx = di[i];
2266 if (diidx >= dcnt)
2267 continue;
2268 l1 = lastdir == diidx ? lastdirl : strlen(dn[diidx]);
2269 l = l1 + strlen(bn[i]) + 1;
2270 if (l > spacen)
2271 {
2272 spacen = l + 16;
2273 space = solv_realloc(space, spacen);
2274 }
2275 if (lastdir != diidx)
2276 {
2277 strcpy(space, dn[diidx]);
2278 lastdir = diidx;
2279 lastdirl = l1;
2280 }
2281 strcpy(space + l1, bn[i]);
2282 info.diridx = diidx;
2283 info.dirlen = l1;
2284 if (fm)
2285 info.mode = fm[i];
2286 if (md)
2287 {
2288 info.digest = md[i];
2289 if (fm && S_ISLNK(fm[i]))
2290 {
2291 info.digest = 0;
2292 if (!lt)
2293 {
2294 lt = headstringarray(rpmhead, TAG_FILELINKTOS, &cnt2);
2295 if (cnt != cnt2)
2296 lt = solv_free(lt);
2297 }
2298 if (lt)
2299 {
2300 linkhash(lt[i], md5);
2301 info.digest = md5;
2302 }
2303 }
2304 if (!info.digest)
2305 {
2306 sprintf(md5, "%08x%08x%08x%08x", (fm[i] >> 12) & 65535, 0, 0, 0);
2307 info.digest = md5;
2308 }
2309 }
2310 info.color = co ? co[i] : 0;
2311 (*cb)(cbdata, space, &info);
2312 }
2313 solv_free(space);
2314 solv_free(lt);
2315 solv_free(md);
2316 solv_free(fm);
2317 solv_free(di);
2318 solv_free(bn);
2319 solv_free(dn);
2320 solv_free(co);
2321 solv_free(ff);
2322 }
2323
2324 char *
rpm_query(void * rpmhandle,Id what)2325 rpm_query(void *rpmhandle, Id what)
2326 {
2327 const char *name, *arch, *sourcerpm;
2328 char *evr, *r;
2329 int l;
2330
2331 RpmHead *rpmhead = rpmhandle;
2332 r = 0;
2333 switch (what)
2334 {
2335 case 0: /* return canonical name of rpm */
2336 name = headstring(rpmhead, TAG_NAME);
2337 if (!name)
2338 name = "";
2339 sourcerpm = headstring(rpmhead, TAG_SOURCERPM);
2340 if (sourcerpm || !(headexists(rpmhead, TAG_SOURCEPACKAGE) || headissourceheuristic(rpmhead)))
2341 arch = headstring(rpmhead, TAG_ARCH);
2342 else
2343 {
2344 if (headexists(rpmhead, TAG_NOSOURCE) || headexists(rpmhead, TAG_NOPATCH))
2345 arch = "nosrc";
2346 else
2347 arch = "src";
2348 }
2349 if (!arch)
2350 arch = "noarch";
2351 evr = headtoevr(rpmhead);
2352 l = strlen(name) + 1 + strlen(evr ? evr : "") + 1 + strlen(arch) + 1;
2353 r = solv_malloc(l);
2354 sprintf(r, "%s-%s.%s", name, evr ? evr : "", arch);
2355 solv_free(evr);
2356 break;
2357 case SOLVABLE_NAME:
2358 name = headstring(rpmhead, TAG_NAME);
2359 r = solv_strdup(name);
2360 break;
2361 case SOLVABLE_SUMMARY:
2362 name = headstring(rpmhead, TAG_SUMMARY);
2363 r = solv_strdup(name);
2364 break;
2365 case SOLVABLE_DESCRIPTION:
2366 name = headstring(rpmhead, TAG_DESCRIPTION);
2367 r = solv_strdup(name);
2368 break;
2369 case SOLVABLE_EVR:
2370 r = headtoevr(rpmhead);
2371 break;
2372 }
2373 return r;
2374 }
2375
2376 unsigned long long
rpm_query_num(void * rpmhandle,Id what,unsigned long long notfound)2377 rpm_query_num(void *rpmhandle, Id what, unsigned long long notfound)
2378 {
2379 RpmHead *rpmhead = rpmhandle;
2380 unsigned int u32;
2381
2382 switch (what)
2383 {
2384 case SOLVABLE_INSTALLTIME:
2385 u32 = headint32(rpmhead, TAG_INSTALLTIME);
2386 return u32 ? u32 : notfound;
2387 }
2388 return notfound;
2389 }
2390
2391 #ifdef ENABLE_RPMDB
2392
2393 int
rpm_installedrpmdbids(void * rpmstate,const char * index,const char * match,Queue * rpmdbidq)2394 rpm_installedrpmdbids(void *rpmstate, const char *index, const char *match, Queue *rpmdbidq)
2395 {
2396 struct rpmdbentry *entries;
2397 int nentries, i;
2398
2399 entries = getinstalledrpmdbids(rpmstate, index ? index : "Name", match, &nentries, 0, 0);
2400 if (rpmdbidq)
2401 {
2402 queue_empty(rpmdbidq);
2403 for (i = 0; i < nentries; i++)
2404 queue_push(rpmdbidq, entries[i].rpmdbid);
2405 }
2406 solv_free(entries);
2407 return nentries;
2408 }
2409
2410 int
rpm_hash_database_state(void * rpmstate,Chksum * chk)2411 rpm_hash_database_state(void *rpmstate, Chksum *chk)
2412 {
2413 struct rpmdbstate *state = rpmstate;
2414 struct stat stb;
2415 if (stat_database(state, &stb))
2416 return -1;
2417 if (state->dbenvopened != 1 && !opendbenv(state))
2418 return -1;
2419 solv_chksum_add(chk, &stb.st_mtime, sizeof(stb.st_mtime));
2420 solv_chksum_add(chk, &stb.st_size, sizeof(stb.st_size));
2421 solv_chksum_add(chk, &stb.st_ino, sizeof(stb.st_ino));
2422 hash_name_index(rpmstate, chk);
2423 return 0;
2424 }
2425
2426 int
rpm_stat_database(void * rpmstate,void * stb)2427 rpm_stat_database(void *rpmstate, void *stb)
2428 {
2429 return stat_database((struct rpmdbstate *)rpmstate, (struct stat *)stb) ? -1 : 0;
2430 }
2431
2432 void *
rpm_byrpmdbid(void * rpmstate,Id rpmdbid)2433 rpm_byrpmdbid(void *rpmstate, Id rpmdbid)
2434 {
2435 struct rpmdbstate *state = rpmstate;
2436 int r;
2437
2438 r = getrpm_dbid(state, rpmdbid);
2439 if (!r)
2440 pool_error(state->pool, 0, "header #%d not in database", rpmdbid);
2441 return r <= 0 ? 0 : state->rpmhead;
2442 }
2443
2444 #endif /* ENABLE_RPMDB */
2445
2446 void *
rpm_byfp(void * rpmstate,FILE * fp,const char * name)2447 rpm_byfp(void *rpmstate, FILE *fp, const char *name)
2448 {
2449 struct rpmdbstate *state = rpmstate;
2450 unsigned int sigdsize, sigcnt, l;
2451 unsigned char lead[4096];
2452
2453 if (fread(lead, 96 + 16, 1, fp) != 1 || getu32(lead) != 0xedabeedb)
2454 {
2455 pool_error(state->pool, 0, "%s: not a rpm", name);
2456 return 0;
2457 }
2458 if (lead[78] != 0 || lead[79] != 5)
2459 {
2460 pool_error(state->pool, 0, "%s: not a V5 header", name);
2461 return 0;
2462 }
2463
2464 /* skip signature header */
2465 if (getu32(lead + 96) != 0x8eade801)
2466 {
2467 pool_error(state->pool, 0, "%s: bad signature header", name);
2468 return 0;
2469 }
2470 sigcnt = getu32(lead + 96 + 8);
2471 sigdsize = getu32(lead + 96 + 12);
2472 if (sigcnt >= MAX_SIG_CNT || sigdsize >= MAX_SIG_DSIZE)
2473 {
2474 pool_error(state->pool, 0, "%s: bad signature header", name);
2475 return 0;
2476 }
2477 sigdsize += sigcnt * 16;
2478 sigdsize = (sigdsize + 7) & ~7;
2479 while (sigdsize)
2480 {
2481 l = sigdsize > 4096 ? 4096 : sigdsize;
2482 if (fread(lead, l, 1, fp) != 1)
2483 {
2484 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2485 return 0;
2486 }
2487 sigdsize -= l;
2488 }
2489
2490 if (fread(lead, 16, 1, fp) != 1)
2491 {
2492 pool_error(state->pool, 0, "%s: unexpected EOF", name);
2493 return 0;
2494 }
2495 if (getu32(lead) != 0x8eade801)
2496 {
2497 pool_error(state->pool, 0, "%s: bad header", name);
2498 return 0;
2499 }
2500 sigcnt = getu32(lead + 8);
2501 sigdsize = getu32(lead + 12);
2502 if (sigcnt >= MAX_HDR_CNT || sigdsize >= MAX_HDR_DSIZE)
2503 {
2504 pool_error(state->pool, 0, "%s: bad header", name);
2505 return 0;
2506 }
2507 if (!headfromfp(state, name, fp, lead, sigcnt, sigdsize, 0, 0, 0))
2508 return 0;
2509 return state->rpmhead;
2510 }
2511
2512 #if defined(ENABLE_RPMDB_BYRPMHEADER) || defined(ENABLE_RPMDB_LIBRPM)
2513
2514 void *
rpm_byrpmh(void * rpmstate,Header h)2515 rpm_byrpmh(void *rpmstate, Header h)
2516 {
2517 struct rpmdbstate *state = rpmstate;
2518 #ifndef ENABLE_RPMPKG_LIBRPM
2519 const unsigned char *uh;
2520 unsigned int dsize, cnt, len;
2521 RpmHead *rpmhead;
2522
2523 if (!h)
2524 return 0;
2525 #ifndef RPM5
2526 uh = headerUnload(h);
2527 #else
2528 uh = headerUnload(h, NULL);
2529 #endif
2530 if (!uh)
2531 return 0;
2532 cnt = getu32(uh);
2533 dsize = getu32(uh + 4);
2534 if (cnt >= MAX_HDR_CNT || dsize >= MAX_HDR_DSIZE)
2535 {
2536 free((void *)uh);
2537 return 0;
2538 }
2539 len = 16 * cnt + dsize;
2540 rpmhead = realloc_head(state, len + 1);;
2541 memcpy(rpmhead->data, uh + 8, len);
2542 headinit(rpmhead, cnt, dsize);
2543 free((void *)uh);
2544 #else
2545 if (!h)
2546 return 0;
2547 if (state->rpmhead)
2548 headfree(state->rpmhead);
2549 state->rpmhead = headerLink(h);
2550 #endif
2551 return state->rpmhead;
2552 }
2553
2554 #endif /* defined(ENABLE_RPMDB_BYRPMHEADER) || defined(ENABLE_RPMDB_LIBRPM) */
2555
2556