zlib is now mandatory.
[citadel.git] / citadel / database.c
index d0aab07300d56305ba0c4203c74445b03c1317ad..4eb34cdd7a593fef68419b46bef176a93878d361 100644 (file)
@@ -1,22 +1,16 @@
 /*
  * This is a data store backend for the Citadel server which uses Berkeley DB.
  *
- * Copyright (c) 1987-2011 by the citadel.org team
+ * Copyright (c) 1987-2012 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 as published
- * by the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
+ * 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.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
  */
 
 /*****************************************************************************
@@ -43,6 +37,7 @@
 #include <sys/stat.h>
 #include <dirent.h>
 #include <syslog.h>
+#include <zlib.h>
 
 #ifdef HAVE_DB_H
 #include <db.h>
@@ -76,16 +71,12 @@ static DB *dbp[MAXCDB];             /* One DB handle for each Citadel database */
 static DB_ENV *dbenv;          /* The DB environment (global) */
 
 
-#ifdef HAVE_ZLIB
-#include <zlib.h>
-#endif
-
 
 /* Verbose logging callback */
 void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
 {
        if (!IsEmptyStr(msg)) {
-               syslog(LOG_DEBUG, "DB: %s\n", msg);
+               syslog(LOG_DEBUG, "DB: %s", msg);
        }
 }
 
@@ -93,7 +84,7 @@ void cdb_verbose_log(const DB_ENV *dbenv, const char *msg)
 /* Verbose logging callback */
 void cdb_verbose_err(const DB_ENV *dbenv, const char *errpfx, const char *msg)
 {
-       syslog(LOG_ALERT, "DB: %s\n", msg);
+       syslog(LOG_ALERT, "DB: %s", msg);
 }
 
 
@@ -105,7 +96,7 @@ static void txabort(DB_TXN * tid)
        ret = tid->abort(tid);
 
        if (ret) {
-               syslog(LOG_EMERG, "bdb(): txn_abort: %s\n", db_strerror(ret));
+               syslog(LOG_EMERG, "bdb(): txn_abort: %s", db_strerror(ret));
                abort();
        }
 }
@@ -118,7 +109,7 @@ static void txcommit(DB_TXN * tid)
        ret = tid->commit(tid, 0);
 
        if (ret) {
-               syslog(LOG_EMERG, "bdb(): txn_commit: %s\n", db_strerror(ret));
+               syslog(LOG_EMERG, "bdb(): txn_commit: %s", db_strerror(ret));
                abort();
        }
 }
@@ -131,14 +122,14 @@ static void txbegin(DB_TXN ** tid)
        ret = dbenv->txn_begin(dbenv, NULL, tid, 0);
 
        if (ret) {
-               syslog(LOG_EMERG, "bdb(): txn_begin: %s\n", db_strerror(ret));
+               syslog(LOG_EMERG, "bdb(): txn_begin: %s", db_strerror(ret));
                abort();
        }
 }
 
 static void dbpanic(DB_ENV * env, int errval)
 {
-       syslog(LOG_EMERG, "bdb(): PANIC: %s\n", db_strerror(errval));
+       syslog(LOG_EMERG, "bdb(): PANIC: %s", db_strerror(errval));
 }
 
 static void cclose(DBC * cursor)
@@ -146,7 +137,7 @@ static void cclose(DBC * cursor)
        int ret;
 
        if ((ret = cursor->c_close(cursor))) {
-               syslog(LOG_EMERG, "bdb(): c_close: %s\n", db_strerror(ret));
+               syslog(LOG_EMERG, "bdb(): c_close: %s", db_strerror(ret));
                abort();
        }
 }
@@ -157,29 +148,20 @@ static void bailIfCursor(DBC ** cursors, const char *msg)
 
        for (i = 0; i < MAXCDB; i++)
                if (cursors[i] != NULL) {
-                       syslog(LOG_EMERG,
-                               "bdb(): cursor still in progress on cdb %02x: %s\n", i, msg);
+                       syslog(LOG_EMERG, "bdb(): cursor still in progress on cdb %02x: %s", i, msg);
                        abort();
                }
 }
 
-void check_handles(void *arg)
-{
-       if (arg != NULL) {
-               ThreadTSD *tsd = (ThreadTSD *) arg;
-
-               bailIfCursor(tsd->cursors, "in check_handles");
-
-               if (tsd->tid != NULL) {
-                       syslog(LOG_EMERG, "bdb(): transaction still in progress!");
-                       abort();
-               }
-       }
-}
 
 void cdb_check_handles(void)
 {
-       check_handles(pthread_getspecific(ThreadKey));
+       bailIfCursor(TSD->cursors, "in check_handles");
+
+       if (TSD->tid != NULL) {
+               syslog(LOG_EMERG, "bdb(): transaction still in progress!");
+               abort();
+       }
 }
 
 
@@ -197,14 +179,14 @@ static void cdb_cull_logs(void)
 
        /* Get the list of names. */
        if ((ret = dbenv->log_archive(dbenv, &list, flags)) != 0) {
-               syslog(LOG_ERR, "cdb_cull_logs: %s\n", db_strerror(ret));
+               syslog(LOG_ERR, "cdb_cull_logs: %s", db_strerror(ret));
                return;
        }
 
        /* Print the list of names. */
        if (list != NULL) {
                for (file = list; *file != NULL; ++file) {
-                       syslog(LOG_DEBUG, "Deleting log: %s\n", *file);
+                       syslog(LOG_DEBUG, "Deleting log: %s", *file);
                        ret = unlink(*file);
                        if (ret != 0) {
                                snprintf(errmsg, sizeof(errmsg),
@@ -228,7 +210,7 @@ static void cdb_cull_logs(void)
 void cmd_cull(char *argbuf) {
        if (CtdlAccessCheck(ac_internal)) return;
        cdb_cull_logs();
-       cprintf("%d Database log file cull completed.\n", CIT_OK);
+       cprintf("%d Database log file cull completed.", CIT_OK);
 }
 
 
@@ -239,11 +221,11 @@ void cdb_checkpoint(void)
 {
        int ret;
 
-       syslog(LOG_DEBUG, "-- db checkpoint --\n");
+       syslog(LOG_DEBUG, "-- db checkpoint --");
        ret = dbenv->txn_checkpoint(dbenv, MAX_CHECKPOINT_KBYTES, MAX_CHECKPOINT_MINUTES, 0);
 
        if (ret != 0) {
-               syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s\n", db_strerror(ret));
+               syslog(LOG_EMERG, "cdb_checkpoint: txn_checkpoint: %s", db_strerror(ret));
                abort();
        }
 
@@ -270,31 +252,29 @@ void open_databases(void)
        int dbversion_major, dbversion_minor, dbversion_patch;
        int current_dbversion = 0;
 
-       syslog(LOG_DEBUG, "bdb(): open_databases() starting\n");
-       syslog(LOG_DEBUG, "Compiled db: %s\n", DB_VERSION_STRING);
-       syslog(LOG_INFO, "  Linked db: %s\n",
+       syslog(LOG_DEBUG, "bdb(): open_databases() starting");
+       syslog(LOG_DEBUG, "Compiled db: %s", DB_VERSION_STRING);
+       syslog(LOG_INFO, "  Linked db: %s",
                db_version(&dbversion_major, &dbversion_minor, &dbversion_patch));
 
        current_dbversion = (dbversion_major * 1000000) + (dbversion_minor * 1000) + dbversion_patch;
 
-       syslog(LOG_DEBUG, "Calculated dbversion: %d\n", current_dbversion);
-       syslog(LOG_DEBUG, "  Previous dbversion: %d\n", CitControl.MMdbversion);
+       syslog(LOG_DEBUG, "Calculated dbversion: %d", current_dbversion);
+       syslog(LOG_DEBUG, "  Previous dbversion: %d", CitControl.MMdbversion);
 
        if ( (getenv("SUPPRESS_DBVERSION_CHECK") == NULL)
           && (CitControl.MMdbversion > current_dbversion) ) {
-               syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version\n"
-                                       "of Berkeley DB that is older than that which last created or\n"
-                                       "updated the database.  Because this would probably cause data\n"
-                                       "corruption or loss, the server is aborting execution now.\n");
+               syslog(LOG_EMERG, "You are attempting to run the Citadel server using a version");
+               syslog(LOG_EMERG, "of Berkeley DB that is older than that which last created or");
+               syslog(LOG_EMERG, "updated the database.  Because this would probably cause data");
+               syslog(LOG_EMERG, "corruption or loss, the server is aborting execution now.");
                exit(CTDLEXIT_DB);
        }
 
        CitControl.MMdbversion = current_dbversion;
        put_control();
 
-#ifdef HAVE_ZLIB
        syslog(LOG_INFO, "Linked zlib: %s\n", zlibVersion());
-#endif
 
        /*
         * Silently try to create the database subdirectory.  If it's
@@ -316,7 +296,7 @@ void open_databases(void)
                              ctdl_data_dir, strerror(errno));
        }
        syslog(LOG_DEBUG, "bdb(): Setting up DB environment\n");
-       db_env_set_func_yield((int (*)(u_long,  u_long))sched_yield);
+       /* db_env_set_func_yield((int (*)(u_long,  u_long))sched_yield); */
        ret = db_env_create(&dbenv, 0);
        if (ret) {
                syslog(LOG_EMERG, "bdb(): db_env_create: %s\n", db_strerror(ret));
@@ -453,8 +433,6 @@ void close_databases(void)
        int a;
        int ret;
 
-       ctdl_thread_internal_free_tsd();
-       
        if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
                syslog(LOG_EMERG,
                        "txn_checkpoint: %s\n", db_strerror(ret));
@@ -484,7 +462,7 @@ void close_databases(void)
 
 
 /*
- * Compression functions only used if we have zlib
+ * Decompress a database item if it was compressed on disk
  */
 void cdb_decompress_if_necessary(struct cdbdata *cdb)
 {
@@ -496,7 +474,6 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
            (memcmp(cdb->ptr, &magic, sizeof(magic))))
            return;
 
-#ifdef HAVE_ZLIB
        /* At this point we know we're looking at a compressed item. */
 
        struct CtdlCompressHeader zheader;
@@ -529,10 +506,6 @@ void cdb_decompress_if_necessary(struct cdbdata *cdb)
        free(cdb->ptr);
        cdb->len = (size_t) destLen;
        cdb->ptr = uncompressed_data;
-#else                          /* HAVE_ZLIB */
-       syslog(LOG_EMERG, "Database contains compressed data, but this citserver was built without compression support.\n");
-       abort();
-#endif                         /* HAVE_ZLIB */
 }
 
 
@@ -548,13 +521,11 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
        DB_TXN *tid;
        int ret = 0;
 
-#ifdef HAVE_ZLIB
        struct CtdlCompressHeader zheader;
        char *compressed_data = NULL;
        int compressing = 0;
        size_t buffer_len = 0;
        uLongf destLen = 0;
-#endif
 
        memset(&dkey, 0, sizeof(DBT));
        memset(&ddata, 0, sizeof(DBT));
@@ -563,7 +534,6 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
        ddata.size = cdatalen;
        ddata.data = cdata;
 
-#ifdef HAVE_ZLIB
        /* Only compress Visit records.  Everything else is uncompressed. */
        if (cdb == CDB_VISIT) {
                compressing = 1;
@@ -584,26 +554,23 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                ddata.size = (size_t) (sizeof(struct CtdlCompressHeader) + zheader.compressed_len);
                ddata.data = compressed_data;
        }
-#endif
 
-       if (MYTID != NULL) {
+       if (TSD->tid != NULL) {
                ret = dbp[cdb]->put(dbp[cdb],   /* db */
-                                   MYTID,      /* transaction ID */
+                                   TSD->tid,   /* transaction ID */
                                    &dkey,      /* key */
                                    &ddata,     /* data */
                                    0); /* flags */
                if (ret) {
-                       syslog(LOG_EMERG, "cdb_store(%d): %s\n", cdb, db_strerror(ret));
+                       syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
                        abort();
                }
-#ifdef HAVE_ZLIB
                if (compressing)
                        free(compressed_data);
-#endif
                return ret;
 
        } else {
-               bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
+               bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
 
              retry:
                txbegin(&tid);
@@ -617,17 +584,14 @@ int cdb_store(int cdb, const void *ckey, int ckeylen, void *cdata, int cdatalen)
                                txabort(tid);
                                goto retry;
                        } else {
-                               syslog(LOG_EMERG, "cdb_store(%d): %s\n",
-                                       cdb, db_strerror(ret));
+                               syslog(LOG_EMERG, "cdb_store(%d): %s", cdb, db_strerror(ret));
                                abort();
                        }
                } else {
                        txcommit(tid);
-#ifdef HAVE_ZLIB
                        if (compressing) {
                                free(compressed_data);
                        }
-#endif
                        return ret;
                }
        }
@@ -648,8 +612,8 @@ int cdb_delete(int cdb, void *key, int keylen)
        dkey.size = keylen;
        dkey.data = key;
 
-       if (MYTID != NULL) {
-               ret = dbp[cdb]->del(dbp[cdb], MYTID, &dkey, 0);
+       if (TSD->tid != NULL) {
+               ret = dbp[cdb]->del(dbp[cdb], TSD->tid, &dkey, 0);
                if (ret) {
                        syslog(LOG_EMERG, "cdb_delete(%d): %s\n", cdb, db_strerror(ret));
                        if (ret != DB_NOTFOUND) {
@@ -657,7 +621,7 @@ int cdb_delete(int cdb, void *key, int keylen)
                        }
                }
        } else {
-               bailIfCursor(MYCURSORS, "attempt to delete during r/o cursor");
+               bailIfCursor(TSD->cursors, "attempt to delete during r/o cursor");
 
              retry:
                txbegin(&tid);
@@ -684,12 +648,10 @@ static DBC *localcursor(int cdb)
        int ret;
        DBC *curs;
 
-       if (MYCURSORS[cdb] == NULL)
-               ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &curs, 0);
+       if (TSD->cursors[cdb] == NULL)
+               ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &curs, 0);
        else
-               ret =
-                   MYCURSORS[cdb]->c_dup(MYCURSORS[cdb], &curs,
-                                         DB_POSITION);
+               ret = TSD->cursors[cdb]->c_dup(TSD->cursors[cdb], &curs, DB_POSITION);
 
        if (ret) {
                syslog(LOG_EMERG, "localcursor: %s\n", db_strerror(ret));
@@ -716,10 +678,10 @@ struct cdbdata *cdb_fetch(int cdb, const void *key, int keylen)
        dkey.size = keylen;
        dkey.data = key;
 
-       if (MYTID != NULL) {
+       if (TSD->tid != NULL) {
                memset(&dret, 0, sizeof(DBT));
                dret.flags = DB_DBT_MALLOC;
-               ret = dbp[cdb]->get(dbp[cdb], MYTID, &dkey, &dret, 0);
+               ret = dbp[cdb]->get(dbp[cdb], TSD->tid, &dkey, &dret, 0);
        } else {
                DBC *curs;
 
@@ -775,11 +737,11 @@ void cdb_free(struct cdbdata *cdb)
 
 void cdb_close_cursor(int cdb)
 {
-       if (MYCURSORS[cdb] != NULL) {
-               cclose(MYCURSORS[cdb]);
+       if (TSD->cursors[cdb] != NULL) {
+               cclose(TSD->cursors[cdb]);
        }
 
-       MYCURSORS[cdb] = NULL;
+       TSD->cursors[cdb] = NULL;
 }
 
 /* 
@@ -791,17 +753,17 @@ void cdb_rewind(int cdb)
 {
        int ret = 0;
 
-       if (MYCURSORS[cdb] != NULL) {
+       if (TSD->cursors[cdb] != NULL) {
                syslog(LOG_EMERG,
                        "cdb_rewind: must close cursor on database %d before reopening.\n", cdb);
                abort();
-               /* cclose(MYCURSORS[cdb]); */
+               /* cclose(TSD->cursors[cdb]); */
        }
 
        /*
         * Now initialize the cursor
         */
-       ret = dbp[cdb]->cursor(dbp[cdb], MYTID, &MYCURSORS[cdb], 0);
+       ret = dbp[cdb]->cursor(dbp[cdb], TSD->tid, &TSD->cursors[cdb], 0);
        if (ret) {
                syslog(LOG_EMERG, "cdb_rewind: db_cursor: %s\n", db_strerror(ret));
                abort();
@@ -824,15 +786,14 @@ struct cdbdata *cdb_next_item(int cdb)
        memset(&data, 0, sizeof(data));
        data.flags = DB_DBT_MALLOC;
 
-       ret = MYCURSORS[cdb]->c_get(MYCURSORS[cdb], &key, &data, DB_NEXT);
+       ret = TSD->cursors[cdb]->c_get(TSD->cursors[cdb], &key, &data, DB_NEXT);
 
        if (ret) {
                if (ret != DB_NOTFOUND) {
                        syslog(LOG_EMERG, "cdb_next_item(%d): %s\n", cdb, db_strerror(ret));
                        abort();
                }
-               cclose(MYCURSORS[cdb]);
-               MYCURSORS[cdb] = NULL;
+               cdb_close_cursor(cdb);
                return NULL;    /* presumably, end of file */
        }
 
@@ -853,14 +814,14 @@ struct cdbdata *cdb_next_item(int cdb)
 void cdb_begin_transaction(void)
 {
 
-       bailIfCursor(MYCURSORS, "can't begin transaction during r/o cursor");
+       bailIfCursor(TSD->cursors, "can't begin transaction during r/o cursor");
 
-       if (MYTID != NULL) {
+       if (TSD->tid != NULL) {
                syslog(LOG_EMERG, "cdb_begin_transaction: ERROR: nested transaction\n");
                abort();
        }
 
-       txbegin(&MYTID);
+       txbegin(&TSD->tid);
 }
 
 void cdb_end_transaction(void)
@@ -868,23 +829,23 @@ void cdb_end_transaction(void)
        int i;
 
        for (i = 0; i < MAXCDB; i++)
-               if (MYCURSORS[i] != NULL) {
+               if (TSD->cursors[i] != NULL) {
                        syslog(LOG_WARNING,
                                "cdb_end_transaction: WARNING: cursor %d still open at transaction end\n",
                                i);
-                       cclose(MYCURSORS[i]);
-                       MYCURSORS[i] = NULL;
+                       cclose(TSD->cursors[i]);
+                       TSD->cursors[i] = NULL;
                }
 
-       if (MYTID == NULL) {
+       if (TSD->tid == NULL) {
                syslog(LOG_EMERG,
                        "cdb_end_transaction: ERROR: txcommit(NULL) !!\n");
                abort();
        } else {
-               txcommit(MYTID);
+               txcommit(TSD->tid);
        }
 
-       MYTID = NULL;
+       TSD->tid = NULL;
 }
 
 /*
@@ -896,12 +857,11 @@ void cdb_trunc(int cdb)
        int ret;
        u_int32_t count;
 
-       if (MYTID != NULL) {
-               syslog(LOG_EMERG,
-                       "cdb_trunc must not be called in a transaction.\n");
+       if (TSD->tid != NULL) {
+               syslog(LOG_EMERG, "cdb_trunc must not be called in a transaction.");
                abort();
        } else {
-               bailIfCursor(MYCURSORS, "attempt to write during r/o cursor");
+               bailIfCursor(TSD->cursors, "attempt to write during r/o cursor");
 
              retry:
                /* txbegin(&tid); */
@@ -916,9 +876,9 @@ void cdb_trunc(int cdb)
                        } else {
                                syslog(LOG_EMERG, "cdb_truncate(%d): %s\n", cdb, db_strerror(ret));
                                if (ret == ENOMEM) {
-                                       syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information.\n");
+                                       syslog(LOG_EMERG, "You may need to tune your database; please read http://www.citadel.org/doku.php/faq:troubleshooting:out_of_lock_entries for more information.");
                                }
-                               abort();
+                               exit(CTDLEXIT_DB);
                        }
                } else {
                        /* txcommit(tid); */