summaryrefslogtreecommitdiff
path: root/rpmdb/tdbi.c
blob: 300817c6b45cf062da230f4e9287076be5242829 (plain)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <db.h>

static char * dbfile = "/var/lib/rpm/Dirnames";

int
main(int argc, char *argv[])
{
    DB * db = NULL;
    DB_TXN * txnid = NULL;
    DBC * dbcursor = NULL;
    DBT key;
    DBT data;
    unsigned int flags;
    int rc, ec = 0;

    if (argc > 1)
	dbfile = argv[1];

    if ((rc = db_create(&db, NULL, 0)) != 0) {
	fprintf(stderr, "db_create: %s\n", db_strerror(rc));
	exit (1);
    }

#if (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 1)
    rc = db->open(db, txnid, dbfile, NULL, DB_UNKNOWN, DB_RDONLY, 0664);
#else
    rc = db->open(db, dbfile, NULL, DB_UNKNOWN, DB_RDONLY, 0664);
#endif
    if (rc != 0) {
	db->err(db, rc, "db->open");
	if (!ec) ec = rc;
	goto err;
    }

    if ((rc = db->cursor(db, txnid, &dbcursor, 0)) != 0) {
	db->err(db, rc, "db->cursor");
	if (!ec) ec = rc;
	goto err;
    }

    flags = DB_NEXT;
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    while ((rc = dbcursor->c_get(dbcursor, &key, &data, flags)) == 0) {
	const char * kval;
	size_t klen;
	db_recno_t nrecs;

	nrecs = 0;
	if ((rc = dbcursor->c_count(dbcursor, &nrecs, 0)) != 0) {
	    db->err(db, rc, "dbcursor->c_count");
	    if (!ec) ec = rc;
	}
	kval = key.data;
	klen = key.size;
	printf("%3d %.*s\t%p[%d]\n", nrecs, klen, kval, data.data, data.size);

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));
    }

    if (rc != DB_NOTFOUND) {
	db->err(db, rc, "db->c_get");
	if (!ec) ec = rc;
	goto err;
    }

err:
    if (dbcursor && (rc = dbcursor->c_close(dbcursor)) != 0) {
	db->err(db, rc, "dbcursor->c_close");
	if (!ec) ec = rc;
    }
    if ((rc = db->close(db, 0)) != 0) {
	db->err(db, rc, "db->close");
	if (!ec) ec = rc;
    }
    return ec;
}