1 // Attempt to convert your database from 32-bit to 64-bit.
2 // Don't run this. It doesn't work and if you try to run it you will immediately die.
4 // Copyright (c) 2023 by Art Cancro citadel.org
6 // This program is open source software. Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
14 #include <sys/types.h>
15 #include <sys/socket.h>
23 #include <libcitadel.h>
26 #include "../server/sysdep.h"
27 #include "../server/citadel_defs.h"
28 #include "../server/server.h"
29 #include "../server/citadel_dirs.h"
30 #include "ctdl3264_structs.h"
32 static DB_ENV *dbenv; // The DB environment (global)
34 // Open the database environment
35 void open_dbenv(char *src_dir) {
39 int dbversion_major, dbversion_minor, dbversion_patch;
41 printf( "db: open_dbenv() starting\n"
42 "db: Linked zlib: %s\n"
43 "db: Compiled libdb: %s\n"
44 "db: Linked libdb: %s\n",
47 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
50 // Create synthetic integer version numbers and compare them.
51 // Never allow citserver to run with a libdb older then the one with which it was compiled.
52 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
53 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
54 if (compiled_db_version > linked_db_version) {
55 printf( "db: ctdl3264 is running with a version of libdb older than the one with which it was compiled.\n"
56 "db: This is an invalid configuration. ctdl3264 will now exit to prevent data loss.");
60 printf("db: Setting up DB environment\n");
61 ret = db_env_create(&dbenv, 0);
63 printf("db: db_env_create: %s\n", db_strerror(ret));
64 printf("db: exit code %d\n", ret);
68 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
69 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
71 printf("db: set_cachesize: %s\n", db_strerror(ret));
72 dbenv->close(dbenv, 0);
73 printf("db: exit code %d\n", ret);
77 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
78 printf("db: set_lk_detect: %s\n", db_strerror(ret));
79 dbenv->close(dbenv, 0);
80 printf("db: exit code %d\n", ret);
84 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
85 printf("db: dbenv open(dir=%s, flags=%d)\n", src_dir, flags);
86 ret = dbenv->open(dbenv, src_dir, flags, 0);
88 printf("db: dbenv->open: %s\n", db_strerror(ret));
89 dbenv->close(dbenv, 0);
90 printf("db: exit code %d\n", ret);
96 void close_dbenv(void) {
97 printf("db: closing dbenv\n");
98 int ret = dbenv->close(dbenv, 0);
100 printf("db: DBENV->close: %s\n", db_strerror(ret));
105 // placeholder convert function for the data types not yet implemented
106 void null_function(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
107 printf("DB: %02x , keylen: %3d , datalen: %d , dataptr: %lx\n", which_cdb, (int)in_key->size, (int)in_data->size, (long unsigned int)in_data->data);
111 // convert function for a message in msgmain
112 void convert_msgmain(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
114 memcpy(&msgnum, in_key->data, sizeof(msgnum));
115 printf("msgmain: len is %d , key is %d\n", in_key->size, msgnum);
117 if (in_key->size != 4) {
118 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
123 // If the msgnum is negative, we are looking at METADATA
125 struct MetaData_32 meta32;
126 if (in_data->size != sizeof meta32) {
127 printf("\033[31mmetadata: db says %d bytes , struct says %ld bytes\033[0m\n", in_data->size, sizeof meta32);
130 memset(&meta32, 0, sizeof meta32);
131 memcpy(&meta32, in_data->data, in_data->size);
133 printf("metadata: msgnum=%d , refcount=%d , content_type=\"%s\" , rfc822len=%d\n",
134 meta32.meta_msgnum, meta32.meta_refcount, meta32.meta_content_type, meta32.meta_rfc822_length);
136 //struct MetaData *meta64 = malloc(sizeof(struct MetaData));
137 //memset(meta64, 0, sizeof(struct MetaData));
138 //meta64->meta_msgnum = (long) meta32.meta_msgnum;
139 //meta64->meta_refcount = (int) meta32.meta_refcount;
140 //strcpy(meta64->meta_content_type, meta32.meta_content_type);
141 //meta64->meta_rfc822_length = (long) meta32.meta_rfc822_length;
145 // If the msgnum is positive, we are looking at a MESSAGE
146 else if (msgnum > 0) {
149 // If the msgnum is 0 it's probably not a valid record.
153 // convert function for a message in msgmain
154 void convert_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
156 memcpy(userkey, in_key->data, in_key->size);
157 userkey[in_key->size] = 0;
158 printf("users: len is %d , key is %s\n", in_key->size, userkey);
162 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
163 convert_msgmain, // CDB_MSGMAIN
164 convert_users, // CDB_USERS
165 null_function, // CDB_ROOMS
166 null_function, // CDB_FLOORTAB
167 null_function, // CDB_MSGLISTS
168 null_function, // CDB_VISIT
169 null_function, // CDB_DIRECTORY
170 null_function, // CDB_USETABLE
171 null_function, // CDB_BIGMSGS
172 null_function, // CDB_FULLTEXT
173 null_function, // CDB_EUIDINDEX
174 null_function, // CDB_USERSBYNUMBER
175 null_function, // CDB_EXTAUTH
176 null_function // CDB_CONFIG
180 void convert_table(int which_cdb) {
184 printf("\033[32m\033[1mConverting table %d\033[0m\n", which_cdb);
186 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
189 DBT in_key, in_data, out_key, out_data;
192 // create a database handle
193 ret = db_create(&dbp, dbenv, 0);
195 printf("db: db_create: %s\n", db_strerror(ret));
196 printf("db: exit code %d\n", ret);
201 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
202 printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
203 ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
205 printf("db: db_open: %s\n", db_strerror(ret));
206 printf("db: exit code %d\n", ret);
211 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
212 printf("db: db_cursor: %s\n", db_strerror(ret));
213 printf("db: exit code %d\n", ret);
217 // Zero out these database keys
218 memset(&in_key, 0, sizeof(in_key));
219 memset(&in_data, 0, sizeof(in_data));
220 memset(&out_key, 0, sizeof(out_key));
221 memset(&out_data, 0, sizeof(out_data));
223 // Walk through the database, calling convert functions as we go and clearing buffers before each call.
224 while ((ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
226 // FIXME handle compressed records here
228 // Call the convert function registered to this table
229 convert_functions[which_cdb](which_cdb, &in_key, &in_data, &out_key, &out_data);
231 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
235 if (ret != DB_NOTFOUND) {
236 printf("db: db_get: %s\n", db_strerror(ret));
237 printf("db: exit code %d\n", ret);
241 printf("%d rows\n", num_rows);
244 //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
245 //if ((ret = dbenv->log_flush(dbenv, NULL))) {
246 //printf("db: log_flush: %s\n", db_strerror(ret));
249 // ...and close the database (table)
250 printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
251 ret = dbp->close(dbp, 0);
253 printf("db: db_close: %s\n", db_strerror(ret));
259 int main(int argc, char **argv) {
260 char *src_dir = NULL;
263 // Check to make sure we're running on the target 64-bit system
264 if (sizeof(void *) != 8) {
265 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
266 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
270 // Parse command line
272 while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
281 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
287 printf("------------------------------------------------------------------------\n");
288 printf("ctdl3264 converts a Citadel database written on a 32-bit system to one \n");
289 printf("that can be run on a 64-bit system. It is intended to be run OFFLINE. \n");
290 printf("Neither the source nor the target data directories should be mounted by \n");
291 printf("a running Citadel server. We guarantee data corruption if you do not \n");
292 printf("observe this warning! The source [-s] directory should contain a copy \n");
293 printf("of the database from your 32-bit system. The destination [-d] directory\n");
294 printf("should be empty and will receive your 64-bit database. \n");
295 printf("------------------------------------------------------------------------\n");
296 printf(" Source 32-bit directory: %s\n", src_dir);
297 printf("Destination 64-bit directory: %s\n", "FIXME");
298 printf("------------------------------------------------------------------------\n");
300 if (confirmed == 1) {
301 printf("You have specified the [-y] flag, so processing will continue.\n");
304 printf("Please consult the documentation to learn how to proceed.\n");
309 for (int i = 0; i < MAXCDB; ++i) {