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
|
#include "system.h"
#include <rpmlib.h>
int dbiIndexSetCount(dbiIndexSet set) {
return set.count;
}
/* structure return */
dbiIndexRecord dbiReturnIndexRecordInstance(unsigned int recOffset, unsigned int fileNumber) {
dbiIndexRecord rec;
rec.recOffset = recOffset;
rec.fileNumber = fileNumber;
return rec;
}
unsigned int dbiIndexRecordOffset(dbiIndexSet set, int recno) {
return set.recs[recno].recOffset;
}
unsigned int dbiIndexRecordFileNumber(dbiIndexSet set, int recno) {
return set.recs[recno].fileNumber;
}
dbiIndex * dbiOpenIndex(const char * filename, int flags, int perms, DBTYPE type) {
dbiIndex * dbi;
dbi = malloc(sizeof(*dbi));
dbi->db = dbopen(filename, flags, perms, type, NULL);
if (!dbi->db) {
free(dbi);
rpmError(RPMERR_DBOPEN, _("cannot open file %s: %s"), filename,
strerror(errno));
return NULL;
}
dbi->indexname = strdup(filename);
return dbi;
}
void dbiCloseIndex(dbiIndex * dbi) {
dbi->db->close(dbi->db);
free(dbi->indexname);
free(dbi);
}
void dbiSyncIndex(dbiIndex * dbi) {
dbi->db->sync(dbi->db, 0);
}
int dbiGetFirstKey(dbiIndex * dbi, const char ** keyp) {
DBT key, data;
int rc;
rc = dbi->db->seq(dbi->db, &key, &data, R_FIRST);
if (rc) {
return 1;
}
{ char *k = malloc(key.size + 1);
memcpy(k, key.data, key.size);
k[key.size] = '\0';
*keyp = k;
}
return 0;
}
int dbiSearchIndex(dbiIndex * dbi, const char * str, dbiIndexSet * set) {
DBT key, data;
int rc;
key.data = (void *)str;
key.size = strlen(str);
rc = dbi->db->get(dbi->db, &key, &data, 0);
if (rc == -1) {
rpmError(RPMERR_DBGETINDEX, _("error getting record %s from %s"),
str, dbi->indexname);
return -1;
} else if (rc == 1) {
return 1;
}
set->recs = malloc(data.size);
memcpy(set->recs, data.data, data.size);
set->count = data.size / sizeof(dbiIndexRecord);
return 0;
}
int dbiUpdateIndex(dbiIndex * dbi, const char * str, dbiIndexSet * set) {
/* 0 on success */
DBT key, data;
int rc;
key.data = (void *)str;
key.size = strlen(str);
if (set->count) {
data.data = set->recs;
data.size = set->count * sizeof(dbiIndexRecord);
rc = dbi->db->put(dbi->db, &key, &data, 0);
if (rc) {
rpmError(RPMERR_DBPUTINDEX, _("error storing record %s into %s"),
str, dbi->indexname);
return 1;
}
} else {
rc = dbi->db->del(dbi->db, &key, 0);
if (rc) {
rpmError(RPMERR_DBPUTINDEX, _("error removing record %s into %s"),
str, dbi->indexname);
return 1;
}
}
return 0;
}
int dbiAppendIndexRecord(dbiIndexSet * set, dbiIndexRecord rec) {
set->count++;
if (set->count == 1) {
set->recs = malloc(set->count * sizeof(dbiIndexRecord));
} else {
set->recs = realloc(set->recs, set->count * sizeof(dbiIndexRecord));
}
set->recs[set->count - 1] = rec;
return 0;
}
/* structure return */
dbiIndexSet dbiCreateIndexRecord(void) {
dbiIndexSet set;
set.recs = NULL;
set.count = 0;
return set;
}
void dbiFreeIndexRecord(dbiIndexSet set) {
if (set.recs) free(set.recs);
}
/* returns 1 on failure */
int dbiRemoveIndexRecord(dbiIndexSet * set, dbiIndexRecord rec) {
int from;
int to = 0;
int num = set->count;
int numCopied = 0;
for (from = 0; from < num; from++) {
if (rec.recOffset != set->recs[from].recOffset ||
rec.fileNumber != set->recs[from].fileNumber) {
if (from != to) set->recs[to] = set->recs[from];
to++;
numCopied++;
} else {
set->count--;
}
}
return (numCopied == num);
}
|