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
|
#include "system.h"
static int _debug = 0;
#include <rpmlib.h>
#include <rpmurl.h>
#include "dbindex.h"
/*@access dbiIndex@*/
/*@access dbiIndexSet@*/
/*@access dbiIndexRecord@*/
#if USE_DB0
extern struct _dbiVec db0vec;
#define DB0vec &db0vec
#else
#define DB0vec NULL
#endif
#if USE_DB1
extern struct _dbiVec db1vec;
#define DB1vec &db1vec
#else
#define DB1vec NULL
#endif
#if USE_DB2
extern struct _dbiVec db2vec;
#define DB2vec &db2vec
#else
#define DB2vec NULL
#endif
#if USE_DB3
extern struct _dbiVec db3vec;
#define DB3vec &db3vec
#else
#define DB3vec NULL
#endif
int __do_dbenv_remove = -1; /* XXX in dbindex.c, shared with rebuilddb.c */
unsigned int dbiIndexSetCount(dbiIndexSet set) {
return set->count;
}
dbiIndexRecord dbiReturnIndexRecordInstance(unsigned int recOffset, unsigned int fileNumber) {
dbiIndexRecord rec = xmalloc(sizeof(*rec));
rec->recOffset = recOffset;
rec->fileNumber = fileNumber;
return rec;
}
void dbiFreeIndexRecordInstance(dbiIndexRecord rec) {
if (rec) free(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;
}
void dbiIndexRecordOffsetSave(dbiIndexSet set, int recno, unsigned int recoff) {
set->recs[recno].recOffset = recoff;
}
static dbiIndex newDBI(const dbiIndex dbiTemplate) {
dbiIndex dbi = xcalloc(1, sizeof(*dbi));
*dbi = *dbiTemplate; /* structure assignment */
if (dbiTemplate->dbi_basename)
dbi->dbi_basename = xstrdup(dbiTemplate->dbi_basename);
return dbi;
}
static void freeDBI( /*@only@*/ /*@null@*/ dbiIndex dbi) {
if (dbi) {
if (dbi->dbi_dbenv) free(dbi->dbi_dbenv);
if (dbi->dbi_dbinfo) free(dbi->dbi_dbinfo);
if (dbi->dbi_file) xfree(dbi->dbi_file);
if (dbi->dbi_basename) xfree(dbi->dbi_basename);
xfree(dbi);
}
}
int _useDbiMajor = 3; /* XXX shared with rebuilddb.c/rpmdb.c */
static struct _dbiVec *mydbvecs[] = {
DB0vec, DB1vec, DB2vec, DB3vec, NULL
};
dbiIndex dbiOpenIndex(const char * urlfn, int flags, const dbiIndex dbiTemplate)
{
dbiIndex dbi;
const char * filename;
int rc = 0;
(void) urlPath(urlfn, &filename);
if (*filename == '\0') {
rpmError(RPMERR_DBOPEN, _("bad db file %s"), urlfn);
return NULL;
}
dbi = newDBI(dbiTemplate);
dbi->dbi_file = xstrdup(filename);
dbi->dbi_flags = flags;
dbi->dbi_major = _useDbiMajor;
switch (dbi->dbi_major) {
case 3:
case 2:
case 1:
case 0:
if (mydbvecs[dbi->dbi_major] != NULL) {
errno = 0;
rc = (*mydbvecs[dbi->dbi_major]->open) (dbi);
if (rc == 0) {
dbi->dbi_vec = mydbvecs[dbi->dbi_major];
break;
}
}
/*@fallthrough@*/
case -1:
dbi->dbi_major = 4;
while (dbi->dbi_major-- > 0) {
if (_debug)
fprintf(stderr, "*** loop db%d mydbvecs %p\n", dbi->dbi_major, mydbvecs[dbi->dbi_major]);
if (mydbvecs[dbi->dbi_major] == NULL)
continue;
errno = 0;
rc = (*mydbvecs[dbi->dbi_major]->open) (dbi);
if (_debug)
fprintf(stderr, "*** loop db%d rc %d errno %d %s\n", dbi->dbi_major, rc, errno, strerror(errno));
if (rc == 0) {
dbi->dbi_vec = mydbvecs[dbi->dbi_major];
break;
}
if (rc == 1 && dbi->dbi_major == 2) {
fprintf(stderr, "*** FIXME: <message about how to convert db>\n");
fprintf(stderr, _("\n\
--> Please run \"rpm --rebuilddb\" as root to convert your database from\n\
db1 to db2 on-disk format.\n\
\n\
"));
dbi->dbi_major--; /* XXX don't bother with db_185 */
}
}
_useDbiMajor = dbi->dbi_major;
break;
}
if (rc) {
rpmError(RPMERR_DBOPEN, _("cannot open file %s: %s"), urlfn, strerror(errno));
freeDBI(dbi);
dbi = NULL;
}
return dbi;
}
int dbiCloseIndex(dbiIndex dbi) {
int rc;
rc = (*dbi->dbi_vec->close) (dbi, 0);
freeDBI(dbi);
return rc;
}
int dbiSyncIndex(dbiIndex dbi) {
int rc;
rc = (*dbi->dbi_vec->sync) (dbi, 0);
return rc;
}
int dbiGetNextKey(dbiIndex dbi, void ** keyp, size_t * keylenp) {
int rc;
if (dbi == NULL)
return 1;
rc = (*dbi->dbi_vec->cget) (dbi, keyp, keylenp, NULL, NULL);
return rc;
}
int dbiFreeCursor(dbiIndex dbi) {
int rc;
if (dbi == NULL)
return 1;
rc = (*dbi->dbi_vec->cclose) (dbi);
return rc;
}
int dbiSearchIndex(dbiIndex dbi, const char * str, size_t len,
dbiIndexSet * set)
{
int rc;
rc = (*dbi->dbi_vec->SearchIndex) (dbi, str, len, set);
switch (rc) {
case -1:
rpmError(RPMERR_DBGETINDEX, _("error getting record %s from %s"),
str, dbi->dbi_file);
break;
}
return rc;
}
int dbiUpdateIndex(dbiIndex dbi, const char * str, dbiIndexSet set) {
int rc;
rc = (*dbi->dbi_vec->UpdateIndex) (dbi, str, set);
if (set->count) {
if (rc) {
rpmError(RPMERR_DBPUTINDEX, _("error storing record %s into %s"),
str, dbi->dbi_file);
}
} else {
if (rc) {
rpmError(RPMERR_DBPUTINDEX, _("error removing record %s into %s"),
str, dbi->dbi_file);
}
}
return rc;
}
int dbiAppendIndexRecord(dbiIndexSet set, dbiIndexRecord rec)
{
set->count++;
if (set->count == 1) {
set->recs = xmalloc(set->count * sizeof(*(set->recs)));
} else {
set->recs = xrealloc(set->recs, set->count * sizeof(*(set->recs)));
}
set->recs[set->count - 1].recOffset = rec->recOffset;
set->recs[set->count - 1].fileNumber = rec->fileNumber;
return 0;
}
dbiIndexSet dbiCreateIndexSet(void) {
dbiIndexSet set = xmalloc(sizeof(*set));
set->recs = NULL;
set->count = 0;
return set;
}
void dbiFreeIndexSet(dbiIndexSet set) {
if (set) {
if (set->recs) free(set->recs);
free(set);
}
}
/* 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) {
/* structure assignment */
if (from != to) set->recs[to] = set->recs[from];
to++;
numCopied++;
} else {
set->count--;
}
}
return (numCopied == num);
}
|