1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
|
#include "system.h"
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "lib/rpmdb_internal.h"
#include <rpm/rpmstring.h>
#include <rpm/rpmlog.h>
#define BDB_HASH 0
#define BDB_BTREE 1
struct dbiCursor_s;
struct bdb_kv {
unsigned char *kv;
unsigned int len;
};
struct bdb_db {
int fd; /* file descriptor of database */
int type; /* BDB_HASH / BDB_BTREE */
unsigned int pagesize;
unsigned int lastpage;
int swapped; /* different endianess? */
/* btree */
unsigned int root; /* root page of the b-tree */
/* hash */
unsigned int maxbucket;
unsigned int highmask;
unsigned int lowmask;
unsigned int spares[32]; /* spare pages for each splitpoint */
};
struct bdb_cur {
struct bdb_db *db;
struct bdb_kv key; /* key and value from the db entry */
struct bdb_kv val;
unsigned char *page; /* the page we're looking at */
unsigned char *ovpage;
struct bdb_kv keyov; /* space to store oversized keys/values */
struct bdb_kv valov;
int state; /* 1: onpage, -1: error */
int idx; /* entry index */
int numidx; /* number of entries on the page */
int islookup; /* we're doing a lookup operation */
/* hash */
unsigned int bucket; /* current bucket */
};
static void swap16(unsigned char *p)
{
int a = p[0];
p[0] = p[1];
p[1] = a;
}
static void swap32(unsigned char *p)
{
int a = p[0];
p[0] = p[3];
p[3] = a;
a = p[1];
p[1] = p[2];
p[2] = a;
}
static void swap32_2(unsigned char *p)
{
swap32(p);
swap32(p + 4);
}
static void bdb_swapmetapage(struct bdb_db *db, unsigned char *page)
{
int i, maxi = db->type == BDB_HASH ? 224 : 92;
for (i = 8; i < maxi; i += 4)
swap32((unsigned char *)(page + i));
swap32((unsigned char *)(page + 24));
}
static void bdb_swappage(struct bdb_db *db, unsigned char *page)
{
unsigned int pagesize = db->pagesize;
int type, i, nent, off;
swap32(page + 8); /* page number */
swap32_2(page + 12); /* prev/next page */
swap16(page + 20); /* nitems */
swap16(page + 22); /* highfree */
type = page[25];
if (type != 2 && type != 13 && type != 3 && type != 5)
return;
nent = *(uint16_t *)(page + 20);
if (nent > (pagesize - 26) / 2)
nent = (pagesize - 26) / 2;
for (i = 0; i < nent; i++) {
int minoff = 26 + nent * 2;
swap16(page + 26 + i * 2); /* offset */
off = *(uint16_t *)(page + 26 + i * 2);
if (off < minoff || off >= pagesize)
continue;
if (type == 2 || type == 13) { /* hash */
if (page[off] == 3 && off + 12 <= pagesize)
swap32_2(page + off + 4); /* page no/length */
} else if (type == 3) { /* btree internal */
if (off + 12 > pagesize)
continue;
swap16(page + off); /* length */
swap32_2(page + off + 4); /* page no/num recs */
if (page[off + 2] == 3 && off + 24 <= pagesize)
swap32_2(page + off + 16); /* with overflow page/length */
} else if (type == 5) { /* btree leaf */
if (off + 3 <= pagesize && page[off + 2] == 1)
swap16(page + off); /* length */
else if (off + 12 <= pagesize && page[off + 2] == 3)
swap32_2(page + off + 4); /* overflow page/length */
}
}
}
static int bdb_getpage(struct bdb_db *db, unsigned char *page, unsigned int pageno)
{
if (!pageno || pageno > db->lastpage)
return -1;
if (pread(db->fd, page, db->pagesize, (off_t)pageno * db->pagesize) != db->pagesize) {
rpmlog(RPMLOG_ERR, "pread: %s\n", strerror(errno));
return -1;
}
if (db->swapped)
bdb_swappage(db, page);
if (pageno != *(uint32_t *)(page + 8))
return -1;
return 0;
}
static void bdb_close(struct bdb_db *db)
{
if (db->fd >= 0)
close(db->fd);
free(db);
}
static struct bdb_db *bdb_open(const char *name)
{
uint32_t meta[512 / 4];
int i, fd;
struct bdb_db *db;
fd = open(name, O_RDONLY);
if (fd == -1) {
return NULL;
}
db = xcalloc(1, sizeof(*db));
db->fd = fd;
if (pread(fd, meta, 512, 0) != 512) {
rpmlog(RPMLOG_ERR, "%s: pread: %s\n", name, strerror(errno));
bdb_close(db);
return NULL;
}
if (meta[3] == 0x00061561 || meta[3] == 0x61150600) {
db->type = BDB_HASH;
db->swapped = meta[3] == 0x61150600;
} else if (meta[3] == 0x00053162 || meta[3] == 0x62310500) {
db->type = BDB_BTREE;
db->swapped = meta[3] == 0x62310500;
} else {
rpmlog(RPMLOG_ERR, "%s: not a berkeley db hash/btree database\n", name);
bdb_close(db);
return NULL;
}
if (db->swapped)
bdb_swapmetapage(db, (unsigned char *)meta);
db->pagesize = meta[5];
db->lastpage = meta[8];
if (db->type == BDB_HASH) {
if (meta[4] < 8 || meta[4] > 10) {
rpmlog(RPMLOG_ERR, "%s: unsupported hash version %d\n", name, meta[4]);
bdb_close(db);
return NULL;
}
db->maxbucket = meta[18];
db->highmask = meta[19];
db->lowmask = meta[20];
for (i = 0; i < 32; i++)
db->spares[i] = meta[24 + i];
}
if (db->type == BDB_BTREE) {
if (meta[4] < 9 || meta[4] > 10) {
rpmlog(RPMLOG_ERR, "%s: unsupported btree version %d\n", name, meta[4]);
bdb_close(db);
return NULL;
}
db->root = meta[22];
}
return db;
}
/****** overflow handling ******/
static int ovfl_get(struct bdb_cur *cur, struct bdb_kv *kv, struct bdb_kv *ov, uint32_t *pagenolen)
{
unsigned int pageno = pagenolen[0];
unsigned int len = pagenolen[1];
unsigned int plen;
unsigned char *p;
if (len == 0)
return -1;
if (len > ov->len) {
if (ov->kv)
ov->kv = xrealloc(ov->kv, len);
else
ov->kv = xmalloc(len);
ov->len = len;
}
if (!cur->ovpage)
cur->ovpage = xmalloc(cur->db->pagesize);
p = ov->kv;
while (len > 0) {
if (bdb_getpage(cur->db, cur->ovpage, pageno))
return -1;
if (cur->ovpage[25] != 7)
return -1;
plen = *(uint16_t *)(cur->ovpage + 22);
if (plen + 26 > cur->db->pagesize || plen > len)
return -1;
memcpy(p, cur->ovpage + 26, plen);
p += plen;
len -= plen;
pageno = *(uint32_t *)(cur->ovpage + 16);
}
if (kv) {
kv->kv = ov->kv;
kv->len = pagenolen[1];
}
return 0;
}
/****** hash implementation ******/
static int hash_bucket_to_page(struct bdb_db *db, unsigned int bucket)
{
unsigned int b;
int i = 0;
for (b = bucket; b; b >>= 1)
i++;
return bucket + db->spares[i];
}
static int hash_lookup(struct bdb_cur *cur, const unsigned char *key, unsigned int keyl)
{
uint32_t bucket;
unsigned int pg, i;
cur->state = -1;
for (bucket = 0, i = 0; i < keyl; i++)
bucket = (bucket * 16777619) ^ key[i];
bucket &= cur->db->highmask;
if (bucket > cur->db->maxbucket)
bucket &= cur->db->lowmask;
cur->bucket = bucket;
pg = hash_bucket_to_page(cur->db, bucket);
if (bdb_getpage(cur->db, cur->page, pg))
return -1;
if (cur->page[25] != 8 && cur->page[25] != 13)
return -1;
cur->idx = (unsigned int)-2;
cur->numidx = *(uint16_t *)(cur->page + 20);
cur->state = 1;
return 0;
}
static int hash_getkv(struct bdb_cur *cur, struct bdb_kv *kv, struct bdb_kv *ov, int off, int len)
{
if (len <= 0 || off + len > cur->db->pagesize)
return -1;
if (cur->page[off] == 1) {
kv->kv = cur->page + off + 1;
kv->len = len - 1;
} else if (cur->page[off] == 3) {
uint32_t ovlpage[2];
if (len != 12)
return -1;
memcpy(ovlpage, cur->page + off + 4, 8); /* off is unaligned */
if (ovfl_get(cur, kv, ov, ovlpage))
return -1;
} else {
return -1;
}
return 0;
}
static int hash_next(struct bdb_cur *cur)
{
int pagesize = cur->db->pagesize;
int koff, klen, voff, vlen;
if (!cur->state && hash_lookup(cur, 0, 0))
return -1;
cur->idx += 2;
for (;;) {
if (cur->idx + 1 >= cur->numidx) {
unsigned int pg;
cur->idx = cur->numidx = 0;
pg = *(uint32_t *)(cur->page + 16);
if (!pg) {
if (cur->islookup || cur->bucket >= cur->db->maxbucket)
return 1;
pg = hash_bucket_to_page(cur->db, ++cur->bucket);
}
if (bdb_getpage(cur->db, cur->page, pg))
return -1;
if (cur->page[25] != 8 && cur->page[25] != 13)
return -1;
cur->numidx = *(uint16_t *)(cur->page + 20);
continue;
}
koff = *(uint16_t *)(cur->page + 26 + 2 * cur->idx);
voff = *(uint16_t *)(cur->page + 28 + 2 * cur->idx);
if (koff >= pagesize || voff >= pagesize)
return -1;
if (cur->idx == 0)
klen = pagesize - koff;
else
klen = *(uint16_t *)(cur->page + 24 + 2 * cur->idx) - koff;
vlen = koff - voff;
if (hash_getkv(cur, &cur->key, &cur->keyov, koff, klen))
return -1;
if (!cur->islookup && hash_getkv(cur, &cur->val, &cur->valov, voff, vlen))
return -1;
return 0;
}
}
static int hash_getval(struct bdb_cur *cur)
{
int koff, voff;
if (cur->state != 1 || cur->idx + 1 >= cur->numidx)
return -1;
koff = *(uint16_t *)(cur->page + 26 + 2 * cur->idx);
voff = *(uint16_t *)(cur->page + 28 + 2 * cur->idx);
return hash_getkv(cur, &cur->val, &cur->valov, voff, koff - voff);
}
/****** btree implementation ******/
static int btree_lookup(struct bdb_cur *cur, const unsigned char *key, unsigned int keylen)
{
int pagesize = cur->db->pagesize;
int off, lastoff, idx, numidx;
unsigned int pg;
unsigned char *ekey;
unsigned int ekeylen;
int cmp;
cur->state = -1;
pg = cur->db->root;
for (;;) {
if (bdb_getpage(cur->db, cur->page, pg))
return -1;
if (cur->page[25] == 5)
break; /* found leaf page */
if (cur->page[25] != 3)
return -1;
numidx = *(uint16_t *)(cur->page + 20);
if (!numidx)
return -1;
for (lastoff = 0, idx = 0; idx < numidx; idx++, lastoff = off) {
off = *(uint16_t *)(cur->page + 26 + 2 * idx);
if ((off & 3) != 0 || off + 3 > pagesize)
return -1;
ekeylen = *(uint16_t *)(cur->page + off);
if (off + 12 + ekeylen > pagesize)
return -1;
if (!keylen) {
lastoff = off;
break;
}
if (idx == 0)
continue;
ekey = cur->page + off + 12;
if ((cur->page[off + 2] & 0x7f) == 3) {
if (ekeylen != 12)
return -1;
if (ovfl_get(cur, 0, &cur->keyov, (uint32_t *)(ekey + 4)))
return -1;
ekeylen = *(uint32_t *)(ekey + 8);
ekey = cur->keyov.kv;
} else if ((cur->page[off + 2] & 0x7f) != 1) {
return -1;
}
cmp = memcmp(ekey, key, keylen < ekeylen ? keylen : ekeylen);
if (cmp > 0 || (cmp == 0 && ekeylen > keylen))
break;
}
pg = *(uint32_t *)(cur->page + lastoff + 4);
}
cur->idx = (unsigned int)-2;
cur->numidx = *(uint16_t *)(cur->page + 20);
cur->state = 1;
return 0;
}
static int btree_getkv(struct bdb_cur *cur, struct bdb_kv *kv, struct bdb_kv *ov, int off)
{
if ((off & 3) != 0)
return -1;
if (cur->page[off + 2] == 1) {
int len = *(uint16_t *)(cur->page + off);
if (off + 3 + len > cur->db->pagesize)
return -1;
kv->kv = cur->page + off + 3;
kv->len = len;
} else if (cur->page[off + 2] == 3) {
if (off + 12 > cur->db->pagesize)
return -1;
if (ovfl_get(cur, kv, ov, (uint32_t *)(cur->page + off + 4)))
return -1;
} else {
return -1;
}
return 0;
}
static int btree_next(struct bdb_cur *cur)
{
int pagesize = cur->db->pagesize;
int koff, voff;
if (!cur->state && btree_lookup(cur, 0, 0))
return -1;
cur->idx += 2;
for (;;) {
if (cur->idx + 1 >= cur->numidx) {
unsigned int pg;
cur->idx = cur->numidx = 0;
pg = *(uint32_t *)(cur->page + 16);
if (cur->islookup || !pg)
return 1;
if (bdb_getpage(cur->db, cur->page, pg))
return -1;
if (cur->page[25] != 5)
return -1;
cur->numidx = *(uint16_t *)(cur->page + 20);
continue;
}
koff = *(uint16_t *)(cur->page + 26 + 2 * cur->idx);
voff = *(uint16_t *)(cur->page + 28 + 2 * cur->idx);
if (koff + 3 > pagesize || voff + 3 > pagesize)
return -1;
if ((cur->page[koff + 2] & 0x80) != 0 || (cur->page[voff + 2] & 0x80) != 0)
continue; /* ignore deleted */
if (btree_getkv(cur, &cur->key, &cur->keyov, koff))
return -1;
if (!cur->islookup && btree_getkv(cur, &cur->val, &cur->valov, voff))
return -1;
return 0;
}
}
static int btree_getval(struct bdb_cur *cur)
{
int voff;
if (cur->state != 1 || cur->idx + 1 >= cur->numidx)
return -1;
voff = *(uint16_t *)(cur->page + 28 + 2 * cur->idx);
return btree_getkv(cur, &cur->val, &cur->valov, voff);
}
/****** cursor functions ******/
static struct bdb_cur *cur_open(struct bdb_db *db)
{
struct bdb_cur *cur = xcalloc(1, sizeof(*cur));
cur->db = db;
cur->page = xmalloc(db->pagesize);
return cur;
}
static void cur_close(struct bdb_cur *cur)
{
if (cur->page)
free(cur->page);
if (cur->ovpage)
free(cur->ovpage);
if (cur->keyov.kv)
free(cur->keyov.kv);
if (cur->valov.kv)
free(cur->valov.kv);
free(cur);
}
static int cur_next(struct bdb_cur *cur)
{
if (cur->state < 0)
return -1;
if (cur->db->type == BDB_HASH)
return hash_next(cur);
if (cur->db->type == BDB_BTREE)
return btree_next(cur);
return -1;
}
static int cur_getval(struct bdb_cur *cur)
{
if (cur->state < 0)
return -1;
if (cur->db->type == BDB_HASH)
return hash_getval(cur);
if (cur->db->type == BDB_BTREE)
return btree_getval(cur);
return -1;
}
static int cur_lookup(struct bdb_cur *cur, const unsigned char *key, unsigned int keyl)
{
int r = -1;
if (cur->db->type == BDB_HASH)
r = hash_lookup(cur, key, keyl);
if (cur->db->type == BDB_BTREE)
r = btree_lookup(cur, key, keyl);
if (r != 0)
return r;
cur->islookup = 1;
while ((r = cur_next(cur)) == 0)
if (keyl == cur->key.len && !memcmp(key, cur->key.kv, keyl))
break;
cur->islookup = 0;
if (r == 0)
r = cur_getval(cur);
return r;
}
static int cur_lookup_ge(struct bdb_cur *cur, const unsigned char *key, unsigned int keyl)
{
int r = -1;
if (cur->db->type == BDB_BTREE)
r = btree_lookup(cur, key, keyl);
if (r != 0)
return r;
cur->islookup = 1;
while ((r = cur_next(cur)) == 0) {
unsigned int ekeyl = cur->key.len;
int cmp = memcmp(cur->key.kv, key, keyl < ekeyl ? keyl : ekeyl);
if (cmp > 0 || (cmp == 0 && ekeyl >= keyl))
break;
}
cur->islookup = 0;
if (r == 0)
r = cur_getval(cur);
else if (r == 1)
r = cur_next(cur);
return r;
}
/****** glue code ******/
static unsigned int getui32(unsigned char *x, int swapped)
{
union _dbswap bs;
memcpy(bs.uc, x, 4);
if (swapped)
_DBSWAP(bs);
return bs.ui;
}
static void setui32(unsigned char *x, uint32_t v, int swapped)
{
union _dbswap bs;
bs.ui = v;
if (swapped)
_DBSWAP(bs);
memcpy(x, bs.uc, 4);
}
static void log_error(dbiIndex dbi)
{
rpmlog(RPMLOG_ERR, "bdb_ro error reading %s database\n", dbi->dbi_file);
}
static int bdbro_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
{
const char *dbhome = rpmdbHome(rdb);
dbiIndex dbi = NULL;
char *path;
if (dbip)
*dbip = NULL;
if ((rdb->db_mode & O_ACCMODE) != O_RDONLY)
return EPERM;
if ((dbi = dbiNew(rdb, rpmtag)) == NULL)
return 1;
path = rstrscat(NULL, dbhome, "/", dbi->dbi_file, NULL);
rpmlog(RPMLOG_DEBUG, "opening db index %s\n", path);
dbi->dbi_db = bdb_open(path);
if (!dbi->dbi_db) {
rpmlog(RPMLOG_ERR, "could not open %s: %s\n", path, strerror(errno));
free(path);
dbiFree(dbi);
return 1;
}
free(path);
dbi->dbi_flags |= DBI_RDONLY;
if (dbip)
*dbip = dbi;
else
(void) dbiClose(dbi, 0);
return 0;
}
static int bdbro_Close(dbiIndex dbi, unsigned int flags)
{
if (dbi->dbi_db)
bdb_close(dbi->dbi_db);
dbiFree(dbi);
return 0;
}
static int bdbro_Verify(dbiIndex dbi, unsigned int flags)
{
return 0;
}
static void bdbro_SetFSync(rpmdb rdb, int enable)
{
}
static int bdbro_Ctrl(rpmdb rdb, dbCtrlOp ctrl)
{
return 0;
}
static dbiCursor bdbro_CursorInit(dbiIndex dbi, unsigned int flags)
{
return dbi ? (void *)cur_open(dbi->dbi_db) : NULL;
}
static dbiCursor bdbro_CursorFree(dbiIndex dbi, dbiCursor dbc)
{
if (dbc)
cur_close((void *)dbc);
return NULL;
}
static void appenddbt(dbiCursor dbc, unsigned char *val, unsigned int vallen, dbiIndexSet *setp)
{
struct bdb_cur *cur = (void *)dbc;
dbiIndexSet set;
unsigned int i;
set = dbiIndexSetNew(vallen / (2 * sizeof(uint32_t)));
set->count = vallen / (2 * sizeof(uint32_t));
for (i = 0; i < set->count; i++, val += 8) {
set->recs[i].hdrNum = getui32(val, cur->db->swapped);
set->recs[i].tagNum = getui32(val + 4, cur->db->swapped);
}
if (*setp == NULL) {
*setp = set;
} else {
dbiIndexSetAppendSet(*setp, set, 0);
dbiIndexSetFree(set);
}
}
static rpmRC bdbro_idxdbPut(dbiIndex dbi, rpmTagVal rpmtag, unsigned int hdrNum, Header h)
{
return RPMRC_FAIL;
}
static rpmRC bdbro_idxdbDel(dbiIndex dbi, rpmTagVal rpmtag, unsigned int hdrNum, Header h)
{
return RPMRC_FAIL;
}
static rpmRC bdbro_idxdbGet(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen,
dbiIndexSet *set, int searchType)
{
struct bdb_cur *cur = (void *)dbc;
int r;
if (!cur)
return RPMRC_FAIL;
if (searchType == DBC_PREFIX_SEARCH) {
rpmRC rc = RPMRC_NOTFOUND;
if (!keyp)
return RPMRC_FAIL;
r = cur_lookup_ge(cur, (const unsigned char *)keyp, keylen);
for (; r == 0; r = cur_next(cur)) {
if (cur->key.len < keylen || memcmp(cur->key.kv, keyp, keylen) != 0)
break;
if (set)
appenddbt(dbc, cur->val.kv, cur->val.len, set);
rc = RPMRC_OK;
}
if (r == -1)
log_error(dbi);
cur->key.kv = 0;
return r == -1 ? RPMRC_FAIL : rc;
}
if (keyp) {
if (keylen == 0) {
keyp = "";
keylen = 1;
}
r = cur_lookup(cur, (const unsigned char *)keyp, keylen);
} else {
r = cur_next(cur);
}
if (r == 0) {
if (set)
appenddbt(dbc, cur->val.kv, cur->val.len, set);
return RPMRC_OK;
}
if (r == -1)
log_error(dbi);
cur->key.kv = 0;
return r == 1 ? RPMRC_NOTFOUND : RPMRC_FAIL;
}
static const void *bdbro_idxdbKey(dbiIndex dbi, dbiCursor dbc, unsigned int *keylen)
{
struct bdb_cur *cur = (void *)dbc;
if (!cur || !cur->key.kv)
return 0;
if (keylen)
*keylen = cur->key.len;
return cur->key.kv;
}
static rpmRC bdbro_pkgdbPut(dbiIndex dbi, dbiCursor dbc, unsigned int *hdrNum,
unsigned char *hdrBlob, unsigned int hdrLen)
{
return RPMRC_FAIL;
}
static rpmRC bdbro_pkgdbDel(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum)
{
return RPMRC_FAIL;
}
static rpmRC bdbro_pkgdbGet(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum,
unsigned char **hdrBlob, unsigned int *hdrLen)
{
struct bdb_cur *cur = (void *)dbc;
int r;
if (hdrNum) {
unsigned char hdrkey[4];
setui32(hdrkey, hdrNum, cur->db->swapped);
r = cur_lookup(cur, hdrkey, 4);
} else {
r = cur_next(cur);
}
if (r == 0) {
if (hdrBlob)
*hdrBlob = cur->val.kv;
if (hdrLen)
*hdrLen = cur->val.len;
return RPMRC_OK;
}
if (r == -1)
log_error(dbi);
cur->key.kv = 0;
return r == 1 ? RPMRC_NOTFOUND : RPMRC_FAIL;
}
static unsigned int bdbro_pkgdbKey(dbiIndex dbi, dbiCursor dbc)
{
struct bdb_cur *cur = (void *)dbc;
if (!cur || !cur->key.kv || cur->key.len != 4)
return 0;
return getui32(cur->key.kv, cur->db->swapped);
}
struct rpmdbOps_s bdbro_dbops = {
.name = "bdb_ro",
.path = "Packages",
.open = bdbro_Open,
.close = bdbro_Close,
.verify = bdbro_Verify,
.setFSync = bdbro_SetFSync,
.ctrl = bdbro_Ctrl,
.cursorInit = bdbro_CursorInit,
.cursorFree = bdbro_CursorFree,
.pkgdbPut = bdbro_pkgdbPut,
.pkgdbDel = bdbro_pkgdbDel,
.pkgdbGet = bdbro_pkgdbGet,
.pkgdbKey = bdbro_pkgdbKey,
.idxdbGet = bdbro_idxdbGet,
.idxdbPut = bdbro_idxdbPut,
.idxdbDel = bdbro_idxdbDel,
.idxdbKey = bdbro_idxdbKey
};
|