-/*
- * This is a data store backend for the Citadel server which uses Berkeley DB.
- *
- * Copyright (c) 1987-2021 by the citadel.org team
- *
- * This program is open source software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License version 3.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
+// This is a data store backend for the Citadel server which uses Berkeley DB.
+//
+// Copyright (c) 1987-2021 by the citadel.org team
+//
+// This program is open source software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License version 3.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
/*****************************************************************************
Tunable configuration parameters for the Berkeley DB back end
#include <stdio.h>
#include <dirent.h>
#include <zlib.h>
-
#include <db.h>
#if DB_VERSION_MAJOR < 5
#endif
#include <libcitadel.h>
-
#include "ctdl_module.h"
#include "control.h"
#include "citserver.h"
* using the cdb_free() routine.
*/
struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen) {
+
+ if (keylen == 0) { // key length zero is impossible
+ return(NULL);
+ }
+
struct cdbdata *tempcdb;
DBT dkey, dret;
int ret;
if (TSD->tid != NULL) {
memset(&dret, 0, sizeof(DBT));
dret.flags = DB_DBT_MALLOC;
- ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0); // crashing here
- } else {
+ ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
+ }
+ else {
DBC *curs;
do {
curs = localcursor(cdb);
ret = curs->c_get(curs, &dkey, &dret, DB_SET);
cclose(curs);
- }
- while (ret == DB_LOCK_DEADLOCK);
+ } while (ret == DB_LOCK_DEADLOCK);
}
if ((ret != 0) && (ret != DB_NOTFOUND)) {
}
tempcdb = (struct cdbdata *) malloc(sizeof(struct cdbdata));
-
if (tempcdb == NULL) {
- syslog(LOG_ERR, "db: cdb_fetch: Cannot allocate memory for tempcdb");
+ syslog(LOG_ERR, "db: cdb_fetch() cannot allocate memory for tempcdb: %m");
cdb_abort();
return NULL; /* make it easier for static analysis... */
- } else {
+ }
+ else {
tempcdb->len = dret.size;
tempcdb->ptr = dret.data;
cdb_decompress_if_necessary(tempcdb);
void cdb_end_transaction(void) {
int i;
- for (i = 0; i < MAXCDB; i++)
+ for (i = 0; i < MAXCDB; i++) {
if (TSD->cursors[i] != NULL) {
syslog(LOG_WARNING, "db: cdb_end_transaction: WARNING: cursor %d still open at transaction end", i);
cclose(TSD->cursors[i]);
TSD->cursors[i] = NULL;
}
+ }
if (TSD->tid == NULL) {
syslog(LOG_ERR, "db: cdb_end_transaction: ERROR: txcommit(NULL) !!");
cdb_abort();
- } else {
+ }
+ else {
txcommit(TSD->tid);
}
if (TSD->tid != NULL) {
syslog(LOG_ERR, "db: cdb_trunc must not be called in a transaction.");
cdb_abort();
- } else {
+ }
+ else {
bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
retry:
}
exit(CTDLEXIT_DB);
}
- } else {
+ }
+ else {
/* txcommit(tid); */
}
}
}
-/*
- * compact (defragment) the database , possibly returning space back to the underlying filesystem
- */
+// compact (defragment) the database, possibly returning space back to the underlying filesystem
void cdb_compact(void) {
int ret;
int i;