summaryrefslogtreecommitdiff
path: root/lib/backend/ndb/glue.c
blob: 144ada0e40430ee27807d6ea67b452712e095198 (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
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
#include "system.h"

#include <errno.h>
#include <stdlib.h>

#include "lib/rpmdb_internal.h"
#include <rpm/rpmstring.h>
#include <rpm/rpmlog.h>

#include "lib/backend/ndb/rpmpkg.h"
#include "lib/backend/ndb/rpmxdb.h"
#include "lib/backend/ndb/rpmidx.h"

#include "debug.h"

struct dbiCursor_s {
    dbiIndex dbi; 
    const void *key;
    unsigned int keylen;
    unsigned int hdrNum;
    int flags;

    unsigned int *list;
    unsigned int nlist;
    unsigned int ilist;
    unsigned char *listdata;
};

struct ndbEnv_s {
    rpmpkgdb pkgdb;
    rpmxdb xdb;
    int refs;

    unsigned int hdrNum;
    void *data;
    unsigned int datalen;
};

static void closeEnv(rpmdb rdb)
{
    struct ndbEnv_s *ndbenv = rdb->db_dbenv;
    if (--ndbenv->refs == 0) {
	if (ndbenv->xdb) {
	    rpmxdbClose(ndbenv->xdb);
	    rpmlog(RPMLOG_DEBUG, "closed   db index       %s/Index.db\n", rpmdbHome(rdb));
	}
	if (ndbenv->pkgdb) {
	    rpmpkgClose(ndbenv->pkgdb);
	    rpmlog(RPMLOG_DEBUG, "closed   db index       %s/Packages.db\n", rpmdbHome(rdb));
	}
	if (ndbenv->data)
	    free(ndbenv->data);
	free(ndbenv);
	rdb->db_dbenv = 0;
    }
}

static struct ndbEnv_s *openEnv(rpmdb rdb)
{
    struct ndbEnv_s *ndbenv = rdb->db_dbenv;
    if (!ndbenv)
	rdb->db_dbenv = ndbenv = xcalloc(1, sizeof(struct ndbEnv_s));
    ndbenv->refs++;
    return ndbenv;
}

static int ndb_Close(dbiIndex dbi, unsigned int flags)
{
    rpmdb rdb = dbi->dbi_rpmdb;
    if (dbi->dbi_type != DBI_PRIMARY && dbi->dbi_db) {
	rpmidxClose(dbi->dbi_db);
	rpmlog(RPMLOG_DEBUG, "closed   db index       %s\n", dbi->dbi_file);
    }
    if (rdb->db_dbenv)
	closeEnv(rdb);
    dbi->dbi_db = 0;
    return 0;
}

static int ndb_Open(rpmdb rdb, rpmDbiTagVal rpmtag, dbiIndex * dbip, int flags)
{
    const char *dbhome = rpmdbHome(rdb);
    struct ndbEnv_s *ndbenv;
    dbiIndex dbi;
    int rc, oflags, ioflags;

    if (dbip)
	*dbip = NULL;

    if ((dbi = dbiNew(rdb, rpmtag)) == NULL)
	return 1;

    ndbenv = openEnv(rdb);

    oflags = O_RDWR;
    if ((rdb->db_mode & O_ACCMODE) == O_RDONLY)
	oflags = O_RDONLY;
    
    if (dbi->dbi_type == DBI_PRIMARY) {
	rpmpkgdb pkgdb = 0;
	char *path = rstrscat(NULL, dbhome, "/Packages.db", NULL);
	rpmlog(RPMLOG_DEBUG, "opening  db index       %s mode=0x%x\n", path, rdb->db_mode);
	rc = rpmpkgOpen(&pkgdb, path, oflags, 0666);
 	if (rc && errno == ENOENT) {
	    oflags = O_RDWR|O_CREAT;
	    dbi->dbi_flags |= DBI_CREATED;
	    rc = rpmpkgOpen(&pkgdb, path, oflags, 0666);
	}
	if (rc) {
	    perror("rpmpkgOpen");
	    free(path);
	    ndb_Close(dbi, 0);
	    return 1;
	}
	free(path);
	dbi->dbi_db = ndbenv->pkgdb = pkgdb;

	if ((oflags & (O_RDWR | O_RDONLY)) == O_RDONLY)
	    dbi->dbi_flags |= DBI_RDONLY;
    } else {
	unsigned int id;
	rpmidxdb idxdb = 0;
	if (!ndbenv->pkgdb) {
	    ndb_Close(dbi, 0);
	    return 1;	/* please open primary first */
	}
	if (!ndbenv->xdb) {
	    char *path = rstrscat(NULL, dbhome, "/Index.db", NULL);
	    rpmlog(RPMLOG_DEBUG, "opening  db index       %s mode=0x%x\n", path, rdb->db_mode);

	    /* Open indexes readwrite if possible */
	    ioflags = O_RDWR;
	    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
	    if (rc && errno == EACCES) {
		/* If it is not asked for rw explicitly, try to open ro */
		if (!(oflags & O_RDWR)) {
		    ioflags = O_RDONLY;
		    rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
		}
	    } else if (rc && errno == ENOENT) {
		ioflags = O_CREAT|O_RDWR;
		rc = rpmxdbOpen(&ndbenv->xdb, rdb->db_pkgs->dbi_db, path, ioflags, 0666);
	    }
	    if (rc) {
		perror("rpmxdbOpen");
		free(path);
		ndb_Close(dbi, 0);
		return 1;
	    }
	    free(path);
	}
	if (rpmxdbLookupBlob(ndbenv->xdb, &id, rpmtag, 0, 0) == RPMRC_NOTFOUND) {
	    dbi->dbi_flags |= DBI_CREATED;
	}
	rpmlog(RPMLOG_DEBUG, "opening  db index       %s tag=%d\n", dbiName(dbi), rpmtag);
	if (rpmidxOpenXdb(&idxdb, rdb->db_pkgs->dbi_db, ndbenv->xdb, rpmtag)) {
	    perror("rpmidxOpenXdb");
	    ndb_Close(dbi, 0);
	    return 1;
	}
	dbi->dbi_db = idxdb;

	if (rpmxdbIsRdonly(ndbenv->xdb))
	    dbi->dbi_flags |= DBI_RDONLY;
    }


    if (dbip != NULL)
	*dbip = dbi;
    else
	ndb_Close(dbi, 0);
    return 0;
}

static int ndb_Verify(dbiIndex dbi, unsigned int flags)
{
    return 1;
}

static void ndb_SetFSync(rpmdb rdb, int enable)
{
}

static int indexSync(rpmpkgdb pkgdb, rpmxdb xdb)
{
    unsigned int generation;
    int rc;
    if (!pkgdb || !xdb)
        return 1;
    if (rpmpkgLock(pkgdb, 1))
        return 1;
    if (rpmpkgGeneration(pkgdb, &generation)) {
        rpmpkgUnlock(pkgdb, 1);
        return 1;
    }
    rc = rpmxdbSetUserGeneration(xdb, generation);
    rpmpkgUnlock(pkgdb, 1);
    return rc;
}

static int ndb_Ctrl(rpmdb rdb, dbCtrlOp ctrl)
{
    struct ndbEnv_s *ndbenv = rdb->db_dbenv;

    switch (ctrl) {
    case DB_CTRL_LOCK_RO:
	if (!rdb->db_pkgs)
	    return 1;
	return rpmpkgLock(rdb->db_pkgs->dbi_db, 0);
    case DB_CTRL_LOCK_RW:
	if (!rdb->db_pkgs)
	    return 1;
	return rpmpkgLock(rdb->db_pkgs->dbi_db, 1);
    case DB_CTRL_UNLOCK_RO:
	if (!rdb->db_pkgs)
	    return 1;
	return rpmpkgUnlock(rdb->db_pkgs->dbi_db, 0);
    case DB_CTRL_UNLOCK_RW:
	if (!rdb->db_pkgs)
	    return 1;
	return rpmpkgUnlock(rdb->db_pkgs->dbi_db, 1);
    case DB_CTRL_INDEXSYNC:
	if (!ndbenv)
	    return 1;
	return indexSync(ndbenv->pkgdb, ndbenv->xdb);
    default:
	break;
    }
    return 0;
}

static dbiCursor ndb_CursorInit(dbiIndex dbi, unsigned int flags)
{
    dbiCursor dbc = xcalloc(1, sizeof(*dbc));
    dbc->dbi = dbi;
    dbc->flags = flags;
    return dbc;
}

static dbiCursor ndb_CursorFree(dbiIndex dbi, dbiCursor dbc)
{
    if (dbc) {
	if (dbc->list)
	    free(dbc->list);
	if (dbc->listdata)
	    free(dbc->listdata);
	free(dbc);
    }
    return NULL;
}


static void setdata(dbiCursor dbc,  unsigned int hdrNum, unsigned char *hdrBlob, unsigned int hdrLen)
{
    struct ndbEnv_s *ndbenv = dbc->dbi->dbi_rpmdb->db_dbenv;
    if (ndbenv->data)
	free(ndbenv->data);
    ndbenv->hdrNum  = hdrNum;
    ndbenv->data    = hdrBlob;
    ndbenv->datalen = hdrLen;
}

static rpmRC ndb_pkgdbNew(dbiIndex dbi, dbiCursor dbc,  unsigned int *hdrNum)
{
    int rc = rpmpkgNextPkgIdx(dbc->dbi->dbi_db, hdrNum);
    if (!rc)
	setdata(dbc, *hdrNum, 0, 0);
    return rc;
}

static rpmRC ndb_pkgdbPut(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum, unsigned char *hdrBlob, unsigned int hdrLen)
{
    int rc = rpmpkgPut(dbc->dbi->dbi_db, hdrNum, hdrBlob, hdrLen);
    if (!rc) {
	dbc->hdrNum = hdrNum;
	setdata(dbc, hdrNum, 0, 0);
    }
    return rc;
}

static rpmRC ndb_pkgdbDel(dbiIndex dbi, dbiCursor dbc,  unsigned int hdrNum)
{
    dbc->hdrNum = 0;
    setdata(dbc, 0, 0, 0);
    return rpmpkgDel(dbc->dbi->dbi_db, hdrNum);
}

/* iterate over all packages */
static rpmRC ndb_pkgdbIter(dbiIndex dbi, dbiCursor dbc, unsigned char **hdrBlob, unsigned int *hdrLen)
{
    int rc;
    unsigned int hdrNum;

    if (!dbc->list) {
	rc = rpmpkgList(dbc->dbi->dbi_db, &dbc->list, &dbc->nlist);
	if (rc)
	    return rc;
	dbc->ilist = 0;
    }
    for (;;) {
	if (dbc->ilist >= dbc->nlist) {
	    rc = RPMRC_NOTFOUND;
	    break;
	}
	*hdrBlob = 0;
	hdrNum = dbc->list[dbc->ilist];
	rc = rpmpkgGet(dbc->dbi->dbi_db, hdrNum, hdrBlob, hdrLen);
	if (rc && rc != RPMRC_NOTFOUND)
	    break;
	dbc->ilist++;
	if (!rc) {
	    dbc->hdrNum = hdrNum;
	    setdata(dbc, hdrNum, *hdrBlob, *hdrLen);
	    break;
	}
    }
    return rc;
}

static rpmRC ndb_pkgdbGet(dbiIndex dbi, dbiCursor dbc, unsigned int hdrNum, unsigned char **hdrBlob, unsigned int *hdrLen)
{
    int rc;
    struct ndbEnv_s *ndbenv = dbc->dbi->dbi_rpmdb->db_dbenv;

    if (!hdrNum)
	return ndb_pkgdbIter(dbi, dbc, hdrBlob, hdrLen);
    if (hdrNum == ndbenv->hdrNum && ndbenv->data) {
	*hdrBlob = ndbenv->data;
	*hdrLen = ndbenv->datalen;
	return RPMRC_OK;
    }
    rc = rpmpkgGet(dbc->dbi->dbi_db, hdrNum, hdrBlob, hdrLen);
    if (!rc) {
	dbc->hdrNum = hdrNum;
	setdata(dbc, hdrNum, *hdrBlob, *hdrLen);
    }
    return rc;
}

static unsigned int ndb_pkgdbKey(dbiIndex dbi, dbiCursor dbc)
{
    return dbc->hdrNum;
}


static void addtoset(dbiIndexSet *set, unsigned int *pkglist, unsigned int pkglistn)
{
    unsigned int i, j;
    dbiIndexSet newset = dbiIndexSetNew(pkglistn / 2);
    for (i = j = 0; i < pkglistn; i += 2) {
	newset->recs[j].hdrNum = pkglist[i];
	newset->recs[j].tagNum = pkglist[i + 1];
	j++;
    }
    newset->count = j;
    if (pkglist)
	free(pkglist);
    if (*set) {
	dbiIndexSetAppendSet(*set, newset, 0);
	dbiIndexSetFree(newset);
    } else
	*set = newset;
}

/* Iterate over all index entries */
static rpmRC ndb_idxdbIter(dbiIndex dbi, dbiCursor dbc, dbiIndexSet *set)
{
    int rc;
    if (!dbc->list) {
	/* setup iteration list on first call */
	rc = rpmidxList(dbc->dbi->dbi_db, &dbc->list, &dbc->nlist, &dbc->listdata);
	if (rc)
	    return rc;
	dbc->ilist = 0;
    }
    for (;;) {
	unsigned char *k;
	unsigned int kl;
	unsigned int *pkglist, pkglistn;
	if (dbc->ilist >= dbc->nlist) {
	    rc = RPMRC_NOTFOUND;
	    break;
	}
	k = dbc->listdata + dbc->list[dbc->ilist];
	kl = dbc->list[dbc->ilist + 1];
#if 0
	if (searchType == DBC_KEY_SEARCH) {
	    dbc->ilist += 2;
	    dbc->key = k;
	    dbc->keylen = kl;
	    rc = RPMRC_OK;
	    break;
	}
#endif
	pkglist = 0;
	pkglistn = 0;
	rc = rpmidxGet(dbc->dbi->dbi_db, k, kl, &pkglist, &pkglistn);
	if (rc && rc != RPMRC_NOTFOUND)
	    break;
	dbc->ilist += 2;
	if (!rc && pkglistn) {
	    addtoset(set, pkglist, pkglistn);
	    dbc->key = k;
	    dbc->keylen = kl;
	    break;
	}
	if (pkglist)
	    free(pkglist);
    }
    return rc;
}

static rpmRC ndb_idxdbGet(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexSet *set, int searchType)
{
    int rc;
    unsigned int *pkglist = 0, pkglistn = 0;

    if (!keyp)
	return ndb_idxdbIter(dbi, dbc, set);

    if (searchType == DBC_PREFIX_SEARCH) {
	unsigned int *list = 0, nlist = 0, i = 0;
	unsigned char *listdata = 0;
	int rrc = RPMRC_NOTFOUND;
	rc = rpmidxList(dbc->dbi->dbi_db, &list, &nlist, &listdata);
	if (rc)
	    return rc;
	for (i = 0; i < nlist && !rc; i += 2) {
	    unsigned char *k = listdata + list[i];
	    unsigned int kl = list[i + 1];
	    if (kl < keylen || memcmp(k, keyp, keylen) != 0)
		continue;
	    rc = ndb_idxdbGet(dbi, dbc, (char *)k, kl, set, DBC_NORMAL_SEARCH);
	    if (rc == RPMRC_NOTFOUND)
		rc = 0;
	    else
		rrc = rc;
	}
	if (list)
	    free(list);
	if (listdata)
	    free(listdata);
	return rc ? rc : rrc;
    }

    rc = rpmidxGet(dbc->dbi->dbi_db, (const unsigned char *)keyp, keylen, &pkglist, &pkglistn);
    if (!rc)
	addtoset(set, pkglist, pkglistn);
    return rc;
}

static rpmRC ndb_idxdbPut(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec)
{
    return rpmidxPut(dbc->dbi->dbi_db, (const unsigned char *)keyp, keylen, rec->hdrNum, rec->tagNum);
}

static rpmRC ndb_idxdbDel(dbiIndex dbi, dbiCursor dbc, const char *keyp, size_t keylen, dbiIndexItem rec)
{
    return rpmidxDel(dbc->dbi->dbi_db, (const unsigned char *)keyp, keylen, rec->hdrNum, rec->tagNum);
}

static const void * ndb_idxdbKey(dbiIndex dbi, dbiCursor dbc, unsigned int *keylen)
{
    if (dbc->key && keylen)
	*keylen = dbc->keylen;
    return dbc->key;
}



struct rpmdbOps_s ndb_dbops = {
    .open	= ndb_Open,
    .close	= ndb_Close,
    .verify	= ndb_Verify,
    .setFSync	= ndb_SetFSync,
    .ctrl	= ndb_Ctrl,

    .cursorInit	= ndb_CursorInit,
    .cursorFree	= ndb_CursorFree,

    .pkgdbNew	= ndb_pkgdbNew,
    .pkgdbPut	= ndb_pkgdbPut,
    .pkgdbDel	= ndb_pkgdbDel,
    .pkgdbGet	= ndb_pkgdbGet,
    .pkgdbKey	= ndb_pkgdbKey,

    .idxdbGet	= ndb_idxdbGet,
    .idxdbPut	= ndb_idxdbPut,
    .idxdbDel	= ndb_idxdbDel,
    .idxdbKey	= ndb_idxdbKey
};