* the specified number of kilobytes has been written, or if the specified
* number of minutes has passed, since the last checkpoint.
*/
-#define MAX_CHECKPOINT_KBYTES 0
+#define MAX_CHECKPOINT_KBYTES 256
#define MAX_CHECKPOINT_MINUTES 15
/*****************************************************************************/
+#ifdef DLL_EXPORT
+#define IN_LIBCIT
+#endif
+
#include "sysdep.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
+
+#ifdef HAVE_DB_H
#include <db.h>
+#elif defined(HAVE_DB4_DB_H)
+#include <db4/db.h>
+#elif defined(HAVE_DB3_DB_H)
+#include <db3/db.h>
+#else
+#error Neither <db.h> nor <db3/db.h> was found by configure. Install db3-devel.
+#endif
+
#include <pthread.h>
#include "citadel.h"
#include "server.h"
+#include "serv_extensions.h"
#include "citserver.h"
#include "database.h"
+#include "msgbase.h"
#include "sysdep_decls.h"
-#include "dynloader.h"
static DB *dbp[MAXCDB]; /* One DB handle for each Citadel database */
static DB_ENV *dbenv; /* The DB environment (global) */
DBC *cursors[MAXCDB]; /* Cursors, for traversals... */
};
+#ifdef HAVE_ZLIB
+#include <zlib.h>
+#endif
+
static pthread_key_t tsdkey;
#define MYCURSORS (((struct cdbtsd*)pthread_getspecific(tsdkey))->cursors)
if (pthread_getspecific(tsdkey) != NULL)
return;
- tsd = mallok(sizeof *tsd);
+ tsd = mallok(sizeof(struct cdbtsd));
tsd->tid = NULL;
* Cull the database logs
*/
static void cdb_cull_logs(void) {
- DIR *dp;
- struct dirent *d;
- char filename[SIZ];
- struct stat statbuf;
+ u_int32_t flags;
+ int ret;
+ char **file, **list;
+ char errmsg[SIZ];
lprintf(5, "Database log file cull started.\n");
- dp = opendir("data");
- if (dp == NULL) return;
+ flags = DB_ARCH_ABS;
+
+ /* Get the list of names. */
+#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
+ if ((ret = log_archive(dbenv, &list, flags, NULL)) != 0) {
+#elif DB_VERSION_MAJOR >= 4
+ if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
+#else
+ if ((ret = log_archive(dbenv, &list, flags)) != 0) {
+#endif
+ lprintf(1, "cdb_cull_logs: %s\n", db_strerror(ret));
+ return;
+ }
- while (d = readdir(dp), d != NULL) {
- if (!strncasecmp(d->d_name, "log.", 4)) {
- sprintf(filename, "./data/%s", d->d_name);
- stat(filename, &statbuf);
- if ((time(NULL) - statbuf.st_mtime) > 432000L) {
- lprintf(5, "%s ... deleted\n", filename);
- unlink(filename);
- }
- else {
- lprintf(5, "%s ... kept\n", filename);
+ /* Print the list of names. */
+ if (list != NULL) {
+ for (file = list; *file != NULL; ++file) {
+ lprintf(9, "Deleting log: %s\n", *file);
+ ret = unlink(*file);
+ if (ret != 0) {
+ snprintf(errmsg, sizeof(errmsg),
+ " ** ERROR **\n \n \n "
+ "Citadel was unable to delete the "
+ "database log file '%s' because of the "
+ "following error:\n \n %s\n \n"
+ " This log file is no longer in use "
+ "and may be safely deleted.\n",
+ *file,
+ strerror(errno));
+ aide_message(errmsg);
}
}
+ free(list);
}
- closedir(dp);
-
lprintf(5, "Database log file cull ended.\n");
}
int ret;
static time_t last_cull = 0L;
+#if DB_VERSION_MAJOR >= 4
+ ret = dbenv->txn_checkpoint(dbenv,
+#else
ret = txn_checkpoint(dbenv,
+#endif
MAX_CHECKPOINT_KBYTES,
MAX_CHECKPOINT_MINUTES,
0);
- if (ret) {
- lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
+
+/* The DB_INCOMPLETE error is no longer possible (or even defined) as of
+ * Berkeley DB v4.1. When we get to the point where v4.0 and earlier are no
+ * longer supported, we can remove this ifdef.
+ */
+#ifdef DB_INCOMPLETE
+ if ( (ret != 0) && (ret != DB_INCOMPLETE) ) {
+ lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
+ db_strerror(ret));
abort();
}
-
+ if (ret == DB_INCOMPLETE) {
+ lprintf(3, "WARNING: txn_checkpoint: %s\n", db_strerror(ret));
+ }
+#else /* DB_INCOMPLETE */
+ if (ret != 0) {
+ lprintf(1, "cdb_checkpoint: txn_checkpoint: %s\n",
+ db_strerror(ret));
+ abort();
+ }
+#endif /* DB_INCOMPLETE */
/* Cull the logs if we haven't done so for 24 hours */
if ((time(NULL) - last_cull) > 86400L) {
u_int32_t flags = 0;
lprintf(9, "cdb_*: open_databases() starting\n");
+#ifdef HAVE_ZLIB
+ lprintf(5, "zlib compression version %s\n", zlibVersion());
+#endif
+
/*
* Silently try to create the database subdirectory. If it's
* already there, no problem.
/* Arbitrary names for our tables -- we reference them by
* number, so we don't have string names for them.
*/
- sprintf(dbfilename, "cdb.%02x", i);
+ snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i);
ret = dbp[i]->open(dbp[i],
+#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
+ NULL, /* new parameter */
+#endif
dbfilename,
NULL,
DB_BTREE,
- DB_CREATE|DB_THREAD,
+ DB_CREATE|DB_THREAD
+#if DB_VERSION_MAJOR >= 4 && DB_VERSION_MINOR >= 1
+ |DB_AUTO_COMMIT
+#endif
+ ,
0600);
if (ret) {
lprintf(1, "cdb_*: db_open[%d]: %s\n", i, db_strerror(ret));
cdb_free_tsd();
+#if DB_VERSION_MAJOR >= 4
+ if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
+#else
if ((ret = txn_checkpoint(dbenv, 0, 0, 0))) {
+#endif
lprintf(1, "cdb_*: txn_checkpoint: %s\n", db_strerror(ret));
abort();
}
}
}
+
+/*
+ * Compression functions only used if we have zlib
+ */
+#ifdef HAVE_ZLIB
+
+void cdb_decompress_if_necessary(struct cdbdata *cdb) {
+ static int magic = COMPRESS_MAGIC;
+ struct CtdlCompressHeader zheader;
+ char *uncompressed_data;
+ char *compressed_data;
+ uLongf destLen, sourceLen;
+
+ if (cdb == NULL) return;
+ if (cdb->ptr == NULL) return;
+ if (memcmp(cdb->ptr, &magic, sizeof(magic))) return;
+
+ /* At this point we know we're looking at a compressed item. */
+ memcpy(&zheader, cdb->ptr, sizeof(struct CtdlCompressHeader));
+
+ compressed_data = cdb->ptr;
+ compressed_data += sizeof(struct CtdlCompressHeader);
+
+ sourceLen = (uLongf) zheader.compressed_len;
+ destLen = (uLongf) zheader.uncompressed_len;
+ uncompressed_data = mallok(zheader.uncompressed_len);
+
+ if (uncompress( (Bytef *) uncompressed_data,
+ &destLen,
+ compressed_data,
+ sourceLen
+ ) != Z_OK) {
+ lprintf(1, "uncompress() error\n");
+ abort();
+ }
+
+ phree(cdb->ptr);
+ cdb->len = (size_t) destLen;
+ cdb->ptr = uncompressed_data;
+}
+
+#endif /* HAVE_ZLIB */
+
+
/*
* Store a piece of data. Returns 0 if the operation was successful. If a
* key already exists it should be overwritten.
DBT dkey, ddata;
DB_TXN *tid;
int ret;
+
+#ifdef HAVE_ZLIB
+ struct CtdlCompressHeader zheader;
+ char *compressed_data = NULL;
+ int compressing = 0;
+ size_t buffer_len;
+ uLongf destLen;
+#endif
memset(&dkey, 0, sizeof(DBT));
memset(&ddata, 0, sizeof(DBT));
dkey.data = ckey;
ddata.size = cdatalen;
ddata.data = cdata;
+
+#ifdef HAVE_ZLIB
+ /* Only compress Visit records. Everything else is uncompressed. */
+ if (cdb == CDB_VISIT) {
+ compressing = 1;
+ zheader.magic = COMPRESS_MAGIC;
+ zheader.uncompressed_len = cdatalen;
+ buffer_len = ( (cdatalen * 101) / 100 ) + 100
+ + sizeof(struct CtdlCompressHeader) ;
+ destLen = (uLongf) buffer_len;
+ compressed_data = mallok(buffer_len);
+ if (compress2(
+ (Bytef *) (compressed_data +
+ sizeof(struct CtdlCompressHeader)),
+ &destLen,
+ (Bytef *) cdata,
+ (uLongf) cdatalen,
+ 1
+ ) != Z_OK) {
+ lprintf(1, "compress2() error\n");
+ abort();
+ }
+ zheader.compressed_len = (size_t) destLen;
+ memcpy(compressed_data, &zheader,
+ sizeof(struct CtdlCompressHeader));
+ ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) +
+ zheader.compressed_len);
+ ddata.data = compressed_data;
+ }
+#endif
if (MYTID != NULL)
{
db_strerror(ret));
abort();
}
+#ifdef HAVE_ZLIB
+ if (compressing) phree(compressed_data);
+#endif
return ret;
}
else
{
txcommit(tid);
+#ifdef HAVE_ZLIB
+ if (compressing) phree(compressed_data);
+#endif
return ret;
}
}
tempcdb->len = dret.size;
tempcdb->ptr = dret.data;
+#ifdef HAVE_ZLIB
+ cdb_decompress_if_necessary(tempcdb);
+#endif
return (tempcdb);
}
phree(cdb);
}
+void cdb_close_cursor(int cdb)
+{
+ if (MYCURSORS[cdb] != NULL)
+ cclose(MYCURSORS[cdb]);
+
+ MYCURSORS[cdb] = NULL;
+}
/*
* Prepare for a sequential search of an entire database.
cdbret = (struct cdbdata *) mallok(sizeof(struct cdbdata));
cdbret->len = data.size;
cdbret->ptr = data.data;
+#ifdef HAVE_ZLIB
+ cdb_decompress_if_necessary(cdbret);
+#endif
return (cdbret);
}
+
/*
* Transaction-based stuff. I'm writing this as I bake cookies...
*/
MYTID = NULL;
}
+/*
+ * Truncate (delete every record)
+ */
+void cdb_trunc(int cdb)
+{
+ DB_TXN *tid;
+ int ret;
+#if DB_VERSION_MAJOR > 3 || DB_VERSION_MINOR > 2
+ u_int32_t count;
+#endif
+
+ if (MYTID != NULL)
+ {
+ lprintf(1, "cdb_trunc must not be called in a transaction.\n");
+ abort();
+ }
+ else
+ {
+ bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
+
+#if DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR < 3
+ for (;;)
+ {
+ DBT key, data;
+
+ /* Initialize the key/data pair so the flags aren't set. */
+ memset(&key, 0, sizeof(key));
+ memset(&data, 0, sizeof(data));
+
+ txbegin(&tid);
+
+ ret = dbp[cdb]->cursor(dbp[cdb], tid, &MYCURSORS[cdb], 0);
+ if (ret)
+ {
+ lprintf(1, "cdb_trunc: db_cursor: %s\n", db_strerror(ret));
+ abort();
+ }
+
+ ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb],
+ &key, &data, DB_NEXT);
+ if (ret)
+ {
+ cclose(MYCURSORS[cdb]);
+ txabort(tid);
+ if (ret == DB_LOCK_DEADLOCK)
+ continue;
+
+ if (ret == DB_NOTFOUND)
+ break;
+
+ lprintf(1, "cdb_trunc: c_get: %s\n", db_strerror(ret));
+ abort();
+ }
+
+ ret = MYCURSORS[cdb]->c_del(MYCURSORS[cdb], 0);
+ if (ret)
+ {
+ cclose(MYCURSORS[cdb]);
+ txabort(tid);
+ if (ret == DB_LOCK_DEADLOCK)
+ continue;
+
+ lprintf(1, "cdb_trunc: c_del: %s\n", db_strerror(ret));
+ abort();
+ }
+
+ cclose(MYCURSORS[cdb]);
+ txcommit(tid);
+ }
+#else
+ retry:
+ txbegin(&tid);
+
+ if ((ret = dbp[cdb]->truncate(dbp[cdb], /* db */
+ tid, /* transaction ID */
+ &count, /* #rows deleted */
+ 0))) /* flags */
+ {
+ if (ret == DB_LOCK_DEADLOCK)
+ {
+ txabort(tid);
+ goto retry;
+ }
+ else
+ {
+ lprintf(1, "cdb_truncate(%d): %s\n", cdb,
+ db_strerror(ret));
+ abort();
+ }
+ }
+ else
+ {
+ txcommit(tid);
+ }
+#endif
+ }
+}