-// Don't run this. It doesn't work and if you try to run it you will immediately die.
+// Dump the Citadel database to a flat file that can be restored by ctdlload on any architecture
//
-// Copyright (c) 2023 by Art Cancro citadel.org
+// Copyright (c) 2023-2024 by Art Cancro citadel.org
//
// This program is open source software. Use, duplication, or disclosure
// is subject to the terms of the GNU General Public License, version 3.
#include <errno.h>
#include <stdarg.h>
#include <limits.h>
+#include <syslog.h>
#include <libcitadel.h>
#include <zlib.h>
-#include <db.h>
#include "../server/sysdep.h"
#include "../server/citadel_defs.h"
#include "../server/server.h"
#include "../server/citadel_dirs.h"
+#include "../server/database.h"
+uid_t ctdluid = 0;
// Wrapper for realloc() that crashes and burns if the call fails.
void *reallok(void *ptr, size_t size) {
void *p = realloc(ptr, size);
if (!p) {
fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
- exit(1);
+ abort();
}
return p;
}
-#define realloc reallok
-// convert a binary blob to hex (non-reentrant!!!)
-char *hexout(void *data, size_t len) {
+// convert a binary blob to base64 (non-reentrant!)
+char *b64out(void *data, size_t len) {
static char *outbuf = NULL;
static size_t outlen = 0;
- int i;
- char ch;
if ((outbuf == NULL) || (outlen < (len * 2))) {
outbuf = reallok(outbuf, (len * 2));
outlen = len * 2;
}
- for (i=0; i<len; ++i) {
- ch = 0;
- memcpy(&ch, data+i, 1);
- sprintf((outbuf + (i * 2)), "%02X", (int) ch);
- }
-
+ CtdlEncodeBase64(outbuf, data, len, 0);
return(outbuf);
}
-// Open a database environment
-DB_ENV *open_dbenv(char *dirname) {
-
- DB_ENV *dbenv = NULL;
-
- int ret;
- int i;
- u_int32_t flags = 0;
- int dbversion_major, dbversion_minor, dbversion_patch;
-
- fprintf(stderr,
- "db: open_dbenv() starting\n"
- "db: Linked zlib: %s\n"
- "db: Compiled libdb: %s\n"
- "db: Linked libdb: %s\n",
- zlibVersion(),
- DB_VERSION_STRING,
- db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
- );
-
- // Create synthetic integer version numbers and compare them.
- // Never run with a libdb older than the one with which it was compiled.
- int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
- int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
- if (compiled_db_version > linked_db_version) {
- fprintf(stderr, "db: ctdldump is running with a version of libdb older than the one with which it was compiled.\n"
- "db: This is an invalid configuration. ctdldump will now exit to prevent data loss.");
- exit(CTDLEXIT_DB);
- }
-
- fprintf(stderr, "db: Setting up DB environment\n");
- ret = db_env_create(&dbenv, 0);
- if (ret) {
- fprintf(stderr, "db: db_env_create: %s\n", db_strerror(ret));
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
- ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
- if (ret) {
- fprintf(stderr, "db: set_cachesize: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
- fprintf(stderr, "db: set_lk_detect: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
- fprintf(stderr, "db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
- ret = dbenv->open(dbenv, dirname, flags, 0);
- if (ret) {
- fprintf(stderr, "db: dbenv->open: %s\n", db_strerror(ret));
- dbenv->close(dbenv, 0);
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- return(dbenv);
-}
-
-
-void close_dbenv(DB_ENV *dbenv) {
- int ret = dbenv->close(dbenv, 0);
- if (ret) {
- fprintf(stderr, "db: dbenv->close: %s\n", db_strerror(ret));
- }
-}
-
-
// export function for a message in msgmain
-void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
+void export_msgmain(int which_cdb, struct cdbkeyval kv) {
long in_msgnum;
- if (in_key->size != sizeof(long)) {
- fprintf(stderr, "\033[31m\033[1m *** BAD DATA *** ABORTING *** \033[0m\n");
- abort();
- }
-
- memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
+ memcpy(&in_msgnum, kv.key.ptr, sizeof(in_msgnum));
// If the msgnum is negative, we are looking at METADATA
if (in_msgnum < 0) {
- struct MetaData *meta = (struct MetaData *)in_data->data;
- printf("msgmeta|%ld|%d|%s|%ld\n", meta->meta_msgnum, meta->meta_refcount, meta->meta_content_type, meta->meta_rfc822_length);
+ struct MetaData *meta = (struct MetaData *)kv.val.ptr;
+ printf("msgmeta|%ld|%d|%s|%ld|\n",
+ meta->meta_msgnum,
+ meta->meta_refcount,
+ meta->meta_content_type,
+ meta->meta_rfc822_length
+ );
}
// If the msgnum is positive, we are looking at a MESSAGE
else if (in_msgnum > 0) {
- printf("msgtext|%s\n", hexout(in_data->data, in_data->size));
+ printf("msgtext|%ld|%s|\n", in_msgnum, b64out(kv.val.ptr, kv.val.len));
}
// If the msgnum is 0 it's probably not a valid record.
// export function for a user record
-void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
-
- struct ctdluser *user = (struct ctdluser *)in_data->data;
-
- printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld\n",
- user->version, // Citadel version which created this record
- user->uid, // Associate with a unix account?
- user->password, // password
- user->flags, // See US_ flags
- user->axlevel, // Access level
- user->usernum, // User number (never recycled)
- user->lastcall, // Date/time of most recent login
- user->USuserpurge, // Purge time (in days) for user
- user->fullname, // Display name (primary identifier)
- user->msgnum_bio, // msgnum of user's profile (bio)
- user->msgnum_pic, // msgnum of user's avatar (photo)
- hexout(user->emailaddrs, strlen(user->emailaddrs)), // Internet email addresses
- user->msgnum_inboxrules, // msgnum of user's inbox filtering rules
- user->lastproc_inboxrules // msgnum of last message filtered
+void export_user(int which_cdb, struct cdbkeyval kv) {
+
+ struct ctdluser *user = (struct ctdluser *)kv.val.ptr;
+
+ printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
+ user->version,
+ user->uid,
+ user->password,
+ user->flags,
+ user->axlevel,
+ user->usernum,
+ user->lastcall,
+ user->USuserpurge,
+ user->fullname,
+ user->msgnum_bio,
+ user->msgnum_pic,
+ b64out(user->emailaddrs, strlen(user->emailaddrs)),
+ user->msgnum_inboxrules,
+ user->lastproc_inboxrules
);
}
// export function for a room record
-void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
+void export_room(int which_cdb, struct cdbkeyval kv) {
- struct ctdlroom *room = (struct ctdlroom *)in_data->data;
+ struct ctdlroom *room = (struct ctdlroom *)kv.val.ptr;
- printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld\n",
+ printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
room->QRname,
room->QRpasswd,
room->QRroomaide,
// export function for a floor record
-void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
+void export_floor(int which_cdb, struct cdbkeyval kv) {
int floor_num;
- memcpy(&floor_num, in_key->data, sizeof(int));
+ memcpy(&floor_num, kv.key.ptr, sizeof(int));
- struct floor *floor = (struct floor *)in_data->data;
+ struct floor *floor = (struct floor *)kv.val.ptr;
- printf("floor|%d|%u|%s|%d|%d|%d\n",
+ printf("floor|%d|%u|%s|%d|%d|%d|\n",
floor_num,
floor->f_flags,
floor->f_name,
}
-// export function for a msglist or a fulltext index record
-// (both are indexed by a long and the data is arrays of longs)
-void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
+// export function for a msglist
+// (indexed by a long and the data is arrays of longs)
+void export_msglist(int which_cdb, struct cdbkeyval kv) {
int i;
int num_msgs;
long msg;
// records are indexed by a single "long" and contains an array of zero or more "long"s
long roomnum;
- memcpy(&roomnum, in_key->data, sizeof(long));
+ memcpy(&roomnum, kv.key.ptr, sizeof(long));
printf("msglist|%ld|", roomnum);
- if (in_data->size > 0) {
- num_msgs = in_data->size / sizeof(long);
+ if (kv.val.len > 0) {
+ num_msgs = kv.val.len / sizeof(long);
+ for (i=0; i<num_msgs; ++i) {
+ memcpy(&msg, (kv.val.ptr + (i * sizeof(long))), sizeof(long));
+ if (i != 0) {
+ printf(",");
+ }
+ printf("%ld", msg);
+ }
+ }
+ printf("|\n");
+}
+
+
+// export function for a full text search index record
+// (indexed by an int and the data is arrays of longs)
+void export_fulltext(int which_cdb, struct cdbkeyval kv) {
+ int i;
+ int num_msgs;
+ long msg;
+
+ // records are indexed by a single "int" and contains an array of zero or more "long"s
+ int indexnum;
+ memcpy(&indexnum, kv.key.ptr, sizeof(int));
+
+ printf("fulltext|%d|", indexnum);
+
+ if (kv.val.len > 0) {
+ num_msgs = kv.val.len / sizeof(long);
for (i=0; i<num_msgs; ++i) {
- memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
+ memcpy(&msg, (kv.val.ptr + (i * sizeof(long))), sizeof(long));
if (i != 0) {
printf(",");
}
printf("%ld", msg);
}
}
- printf("\n");
+ printf("|\n");
}
// export function for a visit record
-void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
+void export_visit(int which_cdb, struct cdbkeyval kv) {
+ struct visit *visit = (struct visit *)kv.val.ptr;
+ int i, len;
+
+ // If there is corrupt data in the "seen" array, cut that out before exporting
+ len = strlen(visit->v_seen);
+ for (i=0; i<len; ++i) {
+ if (!isprint(visit->v_seen[i])) {
+ visit->v_seen[i] = 0;
+ }
+ }
- struct visit *visit = (struct visit *)in_data->data;
+ // If there is corrupt data in the "answered" array, cut that out before exporting
+ len = strlen(visit->v_answered);
+ for (i=0; i<len; ++i) {
+ if (!isprint(visit->v_answered[i])) {
+ visit->v_answered[i] = 0;
+ }
+ }
- printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d\n",
+ // output the record
+ printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
visit->v_roomnum,
visit->v_roomgen,
visit->v_usernum,
}
-#if 0
// export function for a directory record
-void export_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
-
- // the key is a string
- out_key->size = in_key->size;
- out_key->data = realloc(out_key->data, out_key->size + 1);
- memcpy(out_key->data, in_key->data, in_key->size);
- char *k = (char *)out_key->data;
- k[out_key->size] = 0;
-
- // the data is also a string
- out_data->size = in_data->size;
- out_data->data = realloc(out_data->data, out_data->size + 1);
- memcpy(out_data->data, in_data->data, in_data->size);
- char *d = (char *)out_data->data;
- d[out_data->size] = 0;
-
- // please excuse my friend, he isn't null terminated
- // printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
+void export_dir(int which_cdb, struct cdbkeyval kv) {
+ printf("dir|");
+ fwrite(kv.key.ptr, kv.key.len, 1, stdout);
+ printf("|%s|\n", (char *)kv.val.ptr);
}
// export function for a use table record
-void export_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
-
- // the key is an int, which is the same size (32 bits) on both 32 and 64 bit systems
- out_key->size = in_key->size;
- out_key->data = realloc(out_key->data, out_key->size);
- memcpy(out_key->data, in_key->data, in_key->size);
-
- // the data is a "struct UseTable"
- struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
- out_data->size = sizeof(struct UseTable);
- out_data->data = realloc(out_data->data, out_data->size);
- memset(out_data->data, 0, out_data->size);
- struct UseTable *use64 = (struct UseTable *)out_data->data;
-
- // the data
- use64->hash = use32->hash;
- use64->timestamp = (time_t) use32->timestamp;
-
- // printf("\033[32m\033[1muse table: %d , %s\033[0m\n", use64->hash, asctime(localtime(&use64->timestamp)));
+void export_usetable(int which_cdb, struct cdbkeyval kv) {
+ struct UseTable *u = (struct UseTable *)kv.val.ptr;
+ printf("use|%d|%ld|\n", u->hash, u->timestamp);
}
// export function for large message texts
-void export_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
-
- // The key is a packed long
- int32_t in_msgnum;
- long out_msgnum;
- memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
- out_msgnum = (long)in_msgnum;
-
- if (in_key->size != 4) {
- fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
- abort();
- }
-
- out_key->size = sizeof(long);
- out_key->data = realloc(out_key->data, out_key->size);
- memcpy(out_key->data, &out_msgnum, sizeof(long));
-
- // the data is binary-ish but has no packed integers
- out_data->size = in_data->size;
- out_data->data = realloc(out_data->data, out_data->size);
- memcpy(out_data->data, in_data->data, in_data->size);
+void export_bigmsg(int which_cdb, struct cdbkeyval kv) {
+ long msgnum;
- // printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
+ memcpy(&msgnum, kv.key.ptr, sizeof(msgnum));
+ printf("bigmsg|%ld|%s|\n", msgnum, b64out(kv.val.ptr, kv.val.len));
}
// export function for EUID Index records
-void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
+void export_euidindex(int which_cdb, struct cdbkeyval kv) {
// The structure of an euidindex record *key* is:
// |----room_number----|----------EUID-------------|
// |-----msg_number----|----room_number----|----------EUID-------------|
// (sizeof long) (sizeof long) (actual length of euid)
- int32_t in_msgnum = 0;
- int32_t in_roomnum = 0;
- char euid[SIZ];
- long out_msgnum = 0;
- long out_roomnum = 0;
-
- memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
- memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
- strcpy(euid, in_data->data+(sizeof(int32_t)*2));
-
- out_msgnum = (long) in_msgnum;
- out_roomnum = (long) in_roomnum;
- // printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
-
- out_key->size = sizeof(long) + strlen(euid) + 1;
- out_key->data = realloc(out_key->data, out_key->size);
- memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
- strcpy(out_key->data+sizeof(out_roomnum), euid);
-
- out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
- out_data->data = realloc(out_data->data, out_data->size);
- memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
- memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
- strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
+ long msgnum, roomnum;
+ char *euid;
+
+ memcpy(&msgnum, kv.val.ptr, sizeof(long));
+ memcpy(&roomnum, kv.val.ptr+sizeof(long), sizeof(msgnum));
+ euid = kv.val.ptr+(sizeof(long)*2);
+
+ printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
}
-// convert users-by-number records
-void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
+// export users-by-number records
+// (This is a secondary index -- should we just regenerate the data after import?)
+void export_usersbynumber(int which_cdb, struct cdbkeyval kv) {
// key is a long
- // and remember ... "long" is int32_t on the source system
- int32_t in_usernum;
- long out_usernum;
- memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
- out_usernum = (long) in_usernum;
-
- if (in_key->size != 4) {
- fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
- abort();
- }
-
- out_key->size = sizeof(out_usernum);
- out_key->data = realloc(out_key->data, out_key->size);
- memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
+ long usernum;
+ memcpy(&usernum, kv.key.ptr, sizeof(usernum));
// value is a string
- out_data->size = in_data->size;
- out_data->data = realloc(out_data->data, out_data->size);
- memcpy(out_data->data, in_data->data, in_data->size);
-
- // printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
+ printf("usersbynumber|%ld|%s|\n", usernum, (char *)kv.val.ptr);
}
// export function for a config record
-void export_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
-
- // the key is a string
- out_key->size = in_key->size;
- out_key->data = realloc(out_key->data, out_key->size + 1);
- memcpy(out_key->data, in_key->data, in_key->size);
- char *k = (char *)out_key->data;
- k[out_key->size] = 0;
-
- // the data is a pair of strings
- out_data->size = in_data->size;
- out_data->data = realloc(out_data->data, out_data->size + 1);
- memcpy(out_data->data, in_data->data, in_data->size);
- char *d = (char *)out_data->data;
- d[out_data->size] = 0;
-
- // please excuse my friend, he isn't null terminated
- // printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
+void export_config(int which_cdb, struct cdbkeyval kv) {
+
+ printf("config|%s|%s|\n",
+ (char *)kv.val.ptr,
+ (char *)kv.val.ptr + strlen(kv.val.ptr) + 1
+ );
+
}
-#endif
// For obsolete databases, zero all the output
-void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
- //printf("Table %02x, keylen=%d, datalen=%d\n", which_cdb, in_key->size, in_data->size);
+void zero_function(int which_cdb, struct cdbkeyval kv) {
+ // do nothing
}
-void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
- export_msgmain, // CDB_MSGMAIN
- export_user, // CDB_USERS
- export_room, // CDB_ROOMS
- export_floor, // CDB_FLOORTAB
- export_msglist, // CDB_MSGLISTS
- export_visit, // CDB_VISIT
- zero_function, // CDB_DIRECTORY
- zero_function, // CDB_USETABLE
- zero_function, // CDB_BIGMSGS
- zero_function, // CDB_FULLTEXT
- zero_function, // CDB_EUIDINDEX
- zero_function, // CDB_USERSBYNUMBER
- zero_function, // CDB_UNUSED1 (obsolete)
- zero_function // CDB_CONFIG
+void (*export_functions[])(int which_cdb, struct cdbkeyval kv) = {
+ export_msgmain, // 00 CDB_MSGMAIN
+ export_user, // 01 CDB_USERS
+ export_room, // 02 CDB_ROOMS
+ export_floor, // 03 CDB_FLOORTAB
+ export_msglist, // 04 CDB_MSGLISTS
+ export_visit, // 05 CDB_VISIT
+ export_dir, // 06 CDB_DIRECTORY
+ export_usetable, // 07 CDB_USETABLE
+ export_bigmsg, // 08 CDB_BIGMSGS
+ export_fulltext, // 09 CDB_FULLTEXT
+ export_euidindex, // 0a CDB_EUIDINDEX
+ export_usersbynumber, // 0b CDB_USERSBYNUMBER
+ zero_function, // 0c CDB_UNUSED1 (obsolete)
+ export_config // 0d CDB_CONFIG
};
-void export_table(int which_cdb, DB_ENV *src_dbenv) {
+void export_table(int which_cdb) {
int ret;
- int compressed;
- char dbfilename[32];
- uLongf destLen = 0;
-
- // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
- DB *src_dbp;
- DBC *src_dbcp;
- DBT in_key, in_data, uncomp_data;
+ struct cdbkeyval ckv;
+
int num_good_rows = 0;
int num_bad_rows = 0;
- snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
-
- // create a database handle for the source table
- ret = db_create(&src_dbp, src_dbenv, 0);
- if (ret) {
- fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
+ cdb_rewind(which_cdb);
+ while (ckv = cdb_next_item(which_cdb), ckv.val.ptr!=NULL) { // always read through to the end
+ // Call the export function registered to this table
+ export_functions[which_cdb](which_cdb, ckv);
}
- // open the file containing the source table
- ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
- if (ret) {
- fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- // Acquire a cursor to read the source table
- if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
- fprintf(stderr, "db: db_cursor: %s\n", db_strerror(ret));
- fprintf(stderr, "db: exit code %d\n", ret);
- exit(CTDLEXIT_DB);
- }
-
- // Zero out these database keys
- memset(&in_key, 0, sizeof(DBT)); // input
- memset(&in_data, 0, sizeof(DBT));
- memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
-
- // Walk through the database, calling export functions as we go and clearing buffers before each call.
- while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
-
- // If either the key or data are zero length, skip this record
- if ((in_key.size == 0) || (in_data.size == 0)) {
- ++num_bad_rows;
- }
-
- else { // Both key and data are >0 length so we're good to go
-
- // Do we need to decompress?
- static int32_t magic = COMPRESS_MAGIC;
- compressed = 0;
- if ( (in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
-
- // yes, we need to decompress
- compressed = 1;
- struct CtdlCompressHeader comp;
- memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
- uncomp_data.size = comp.uncompressed_len;
- uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
- destLen = (uLongf)comp.uncompressed_len;
-
- ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader), (uLong)comp.compressed_len);
- if (ret != Z_OK) {
- fprintf(stderr, "db: uncompress() error %d\n", ret);
- exit(CTDLEXIT_DB);
- }
- }
-
- // Call the export function registered to this table
- export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
-
- // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
- fflush(stdout);
- }
- }
-
- // free any leftover out_data pointers
- free(uncomp_data.data);
-
- // ...and close the database (table)
- ret = src_dbp->close(src_dbp, 0);
- if (ret) {
- fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
- }
-
-
+ // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
+ fflush(stdout);
}
int main(int argc, char **argv) {
int i = 0;
- char *src_dir = NULL;
- char *dst_dir = NULL;
int confirmed = 0;
- static DB_ENV *src_dbenv; // Source DB environment (global)
+ char *ctdldir = CTDLDIR;
+
+ // display the greeting
+ fprintf(stderr,
+ "\033[44m\033[1m╔════════════════════════════════════════════════════════════════════════╗\033[0m\n"
+ "\033[44m\033[1m║ DB Dump utility for Citadel version %-4d ║\033[0m\n"
+ "\033[44m\033[1m║ Copyright (c) 2023-2024 by citadel.org et al. ║\033[0m\n"
+ "\033[44m\033[1m║ This program is open source software. Use, duplication, or disclosure ║\033[0m\n"
+ "\033[44m\033[1m║ is subject to the terms of the GNU General Public license v3. ║\033[0m\n"
+ "\033[44m\033[1m╚════════════════════════════════════════════════════════════════════════╝\033[0m\n",
+ REV_LEVEL
+ );
// Parse command line
int a;
- while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
+ while ((a = getopt(argc, argv, "h:y")) != EOF) {
switch (a) {
case 'h':
- src_dir = optarg;
- break;
- case 'd':
- dst_dir = optarg;
+ ctdldir = optarg;
break;
case 'y':
confirmed = 1;
break;
default:
- fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
+ fprintf(stderr, "%s: usage: %s -s citadel_dir [>dumpfile]\n", argv[0], argv[0]);
exit(2);
}
}
- // Warn the user
- fprintf(stderr, "------------------------------------------------------------------------\n");
- fprintf(stderr, "This utility must be run while the server is OFFLINE. \n");
- fprintf(stderr, "We \033[1mguarantee\033[0m data corruption if you do not \n");
- fprintf(stderr, "observe this warning! The source [-s] directory should contain a copy \n");
- fprintf(stderr, "of the database from your source system. The dump [-d] directory \n");
- fprintf(stderr, "should be empty and will receive your dump file. \n");
- fprintf(stderr, "------------------------------------------------------------------------\n");
- fprintf(stderr, " Source (database) directory: %s\n", src_dir);
- fprintf(stderr, "------------------------------------------------------------------------\n");
-
if (confirmed == 1) {
- fprintf(stderr, "You have specified the [-y] flag, so processing will continue.\n");
+ fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
}
else {
- fprintf(stderr, "Please read [ https://www.citadel.org/ctdldump.html ] to learn how to proceed.\n");
- exit(0);
+ fprintf(stderr, "ctdldump: usage: ctdldump -y -h[citadel_dir] >[dump_file]\n");
+ fprintf(stderr, " -y : yes, I know this program can do damage and I want to run it anyway.\n");
+ fprintf(stderr, " -h : [citadel_dir] is your server directory, usually /usr/local/citadel\n");
+ fprintf(stderr, " Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
+ exit(1);
+ }
+
+ if (chdir(ctdldir) != 0) {
+ fprintf(stderr, "ctdlload: unable to change directory to [%s]: %m", ctdldir);
+ exit(2);
}
- src_dbenv = open_dbenv(src_dir);
+ // backend modules use syslog -- redirect to stderr
+ openlog("ctdldump", LOG_PERROR , LOG_DAEMON);
+
+ // initialize the database backend
+ cdb_init_backends();
+ cdb_open_databases();
+
+ printf("begin|\n");
for (i = 0; i < MAXCDB; ++i) {
- export_table(i, src_dbenv);
+ export_table(i);
}
- close_dbenv(src_dbenv);
+ printf("end|\n");
+ fflush(stdout);
+
+ // close databases
+ cdb_close_databases();
+ fprintf(stderr, "ctdldump: \033[32m\033[1mfinished\033[0m\n");
exit(0);
}