1 // Attempt to 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) {
110 // convert function for a message in msgmain
111 void convert_msgmain(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
114 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
115 out_msgnum = (long)in_msgnum;
117 if (in_key->size != 4) {
118 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
122 // If the msgnum is negative, we are looking at METADATA
124 struct MetaData_32 *meta32 = (struct MetaData_32 *)in_data->data;
126 printf("\033[32m\033[1mMetadata: msgnum=%d , refcount=%d , content_type=\"%s\" , rfc822len=%d\033[0m\n", meta32->meta_msgnum, meta32->meta_refcount, meta32->meta_content_type, meta32->meta_rfc822_length);
128 out_key->size = sizeof(long);
129 out_key->data = realloc(out_key->data, out_key->size);
130 memcpy(out_key->data, &out_msgnum, sizeof(long));
132 out_data->size = sizeof(struct MetaData);
133 out_data->data = realloc(out_data->data, out_data->size);
134 struct MetaData *meta64 = (struct MetaData *)out_data->data;
135 memset(meta64, 0, sizeof(struct MetaData));
136 meta64->meta_msgnum = (long) meta32->meta_msgnum;
137 meta64->meta_refcount = (int) meta32->meta_refcount;
138 strcpy(meta64->meta_content_type, meta32->meta_content_type);
139 meta64->meta_rfc822_length = (long) meta32->meta_rfc822_length;
142 // If the msgnum is positive, we are looking at a MESSAGE
143 else if (in_msgnum > 0) {
144 out_key->size = sizeof(long);
145 out_key->data = realloc(out_key->data, out_key->size);
146 memcpy(out_key->data, &out_msgnum, sizeof(long));
148 // copy the message verbatim
149 out_data->size = in_data->size;
150 out_data->data = realloc(out_data->data, out_data->size);
151 memcpy(out_data->data, in_data->data, out_data->size);
152 printf("\033[32m\033[1mMessage: %ld\033[0m\n", out_msgnum);
155 // If the msgnum is 0 it's probably not a valid record.
157 printf("msgmain: record 0 is impossible\n");
162 // convert function for a user record
163 void convert_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
165 // The key is a string so we can just copy it over
166 out_key->size = in_key->size;
167 out_key->data = realloc(out_key->data, out_key->size);
168 memcpy(out_key->data, in_key->data, in_key->size);
170 struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
172 out_data->size = sizeof(struct ctdluser);
173 out_data->data = realloc(out_data->data, out_data->size);
174 struct ctdluser *user64 = (struct ctdluser *)out_data->data;
176 user64->version = (int) user32->version;
177 user64->uid = (uid_t) user32->uid;
178 strcpy(user64->password, user32->password);
179 user64->flags = (unsigned) user32->flags;
180 user64->timescalled = (long) user32->timescalled;
181 user64->posted = (long) user32->posted;
182 user64->axlevel = (cit_uint8_t) user32->axlevel;
183 user64->usernum = (long) user32->usernum;
184 user64->lastcall = (time_t) user32->lastcall;
185 user64->USuserpurge = (int) user32->USuserpurge;
186 strcpy(user64->fullname, user32->fullname);
187 user64->msgnum_bio = (long) user32->msgnum_bio;
188 user64->msgnum_pic = (long) user32->msgnum_pic;
189 strcpy(user64->emailaddrs, user32->emailaddrs);
190 user64->msgnum_inboxrules = (long) user32->msgnum_inboxrules;
191 user64->lastproc_inboxrules = (long) user32->lastproc_inboxrules;
193 printf("\033[32m\033[1mUser: %s\033[0m\n", user64->fullname);
197 // convert function for a room record
198 void convert_rooms(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
200 // The key is a string so we can just copy it over
201 out_key->size = in_key->size;
202 out_key->data = realloc(out_key->data, out_key->size);
203 memcpy(out_key->data, in_key->data, in_key->size);
206 struct ctdlroom_32 *room32 = (struct ctdlroom_32 *)in_data->data;
207 out_data->size = sizeof(struct ctdlroom);
208 out_data->data = realloc(out_data->data, out_data->size);
209 struct ctdlroom *room64 = (struct ctdlroom *)out_data->data;
211 strcpy(room64->QRname, room32->QRname);
212 strcpy(room64->QRpasswd, room32->QRpasswd);
213 room64->QRroomaide = (long) room32->QRroomaide;
214 room64->QRhighest = (long) room32->QRhighest;
215 room64->QRgen = (time_t) room32->QRgen;
216 room64->QRflags = (unsigned) room32->QRflags;
217 strcpy(room64->QRdirname, room32->QRdirname);
218 room64->msgnum_info = (long) room32->msgnum_info;
219 room64->QRfloor = (char) room32->QRfloor;
220 room64->QRmtime = (time_t) room32->QRmtime;
221 room64->QRep.expire_mode = (int) room32->QRep.expire_mode;
222 room64->QRep.expire_value = (int) room32->QRep.expire_value;
223 room64->QRnumber = (long) room32->QRnumber;
224 room64->QRorder = (char) room32->QRorder;
225 room64->QRflags2 = (unsigned) room32->QRflags2;
226 room64->QRdefaultview = (int) room32->QRdefaultview;
227 room64->msgnum_pic = (long) room32->msgnum_pic;
229 printf("\033[32m\033[1mRoom: %s\033[0m\n", room64->QRname);
233 // convert function for a floor record
234 void convert_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
236 // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
237 out_key->size = in_key->size;
238 out_key->data = realloc(out_key->data, out_key->size);
239 memcpy(out_key->data, in_key->data, in_key->size);
242 struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
243 out_data->size = sizeof(struct floor);
244 out_data->data = realloc(out_data->data, out_data->size);
245 struct floor *floor64 = (struct floor *)out_data->data;
247 // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
248 floor64->f_flags = (unsigned short) floor32->f_flags;
249 strcpy(floor64->f_name, floor32->f_name);
250 floor64->f_ref_count = (int) floor32->f_ref_count;
251 floor64->f_ep.expire_mode = (int) floor32->f_ep.expire_mode;
252 floor64->f_ep.expire_value = (int) floor32->f_ep.expire_value;
254 printf("\033[32m\033[1mFloor: %s\033[0m\n", floor64->f_name);
258 // convert function for a msglist or a fulltext index record
259 // (both are indexed by a long and the data is arrays of longs)
260 void convert_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
263 char *table = (which_cdb == CDB_FULLTEXT) ? "FullText" : "Msglist";
265 // records are indexed by a single "long" and contains an array of zero or more "long"s
266 // and remember ... "long" is int32_t on the source system
269 memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
270 out_roomnum = (long) in_roomnum;
272 if (in_key->size != 4) {
273 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
277 int num_msgs = in_data->size / sizeof(int32_t);
278 printf("\033[32m\033[1m%s: key %ld (%d messages)\033[0m\n", table, out_roomnum, num_msgs);
280 // the key is a "long"
281 out_key->size = sizeof(out_roomnum);
282 out_key->data = realloc(out_key->data, out_key->size);
283 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
285 // the data is another array, but a wider type
286 out_data->size = sizeof(long) * num_msgs;
287 out_data->data = realloc(out_data->data, out_data->size);
291 for (i=0; i<num_msgs; ++i) {
292 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
293 out_msg = (long) in_msg;
294 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
295 printf("msg#%ld\n", out_msg);
300 // convert function for a visit record
301 void convert_visits(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
304 struct visit_32 *visit32 = (struct visit_32 *)in_data->data;
305 out_data->size = sizeof(struct visit);
306 out_data->data = realloc(out_data->data, out_data->size);
307 struct visit *visit64 = (struct visit *)out_data->data;
309 // the data (zero it out so it will compress well)
310 memset(visit64, 0, sizeof(struct visit));
311 visit64->v_roomnum = (long) visit32->v_roomnum;
312 visit64->v_roomgen = (long) visit32->v_roomgen;
313 visit64->v_usernum = (long) visit32->v_usernum;
314 visit64->v_lastseen = (long) visit32->v_lastseen;
315 visit64->v_flags = (unsigned) visit32->v_flags;
316 strcpy(visit64->v_seen, visit32->v_seen);
317 strcpy(visit64->v_answered, visit32->v_answered);
318 visit64->v_view = (int) visit32->v_view;
320 printf("\033[32m\033[1mVisit: room %ld, gen %ld, user %ld\033[0m\n", visit64->v_roomnum, visit64->v_roomgen, visit64->v_usernum);
322 // create the key (which is based on the data, so there is no need to the old key)
323 out_key->size = sizeof(struct visit_index);
324 out_key->data = realloc(out_key->data, out_key->size);
325 struct visit_index *newvisitindex = (struct visit_index *) out_key->data;
326 newvisitindex->iRoomID = visit64->v_roomnum;
327 newvisitindex->iRoomGen = visit64->v_roomgen;
328 newvisitindex->iUserID = visit64->v_usernum;
332 // convert function for a directory record
333 void convert_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
335 // the key is a string
336 out_key->size = in_key->size;
337 out_key->data = realloc(out_key->data, out_key->size + 1);
338 memcpy(out_key->data, in_key->data, in_key->size);
339 char *k = (char *)out_key->data;
340 k[out_key->size] = 0;
342 // the data is also a string
343 out_data->size = in_data->size;
344 out_data->data = realloc(out_data->data, out_data->size + 1);
345 memcpy(out_data->data, in_data->data, in_data->size);
346 char *d = (char *)out_data->data;
347 d[out_data->size] = 0;
349 // please excuse my friend, he isn't null terminated
350 printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
354 // convert function for a use table record
355 void convert_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
357 // the key is a string
358 out_key->size = in_key->size;
359 out_key->data = realloc(out_key->data, out_key->size);
360 memcpy(out_key->data, in_key->data, in_key->size);
362 // the data is a "struct UseTable"
363 struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
364 out_data->size = sizeof(struct UseTable);
365 out_data->data = realloc(out_data->data, out_data->size);
366 memset(out_data->data, 0, out_data->size);
367 struct UseTable *use64 = (struct UseTable *)out_data->data;
370 strcpy(use64->ut_msgid, use32->ut_msgid);
371 use64->ut_timestamp = (time_t) use32->ut_timestamp;
375 // convert function for large message texts
376 void convert_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
378 // The key is a packed long
381 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
382 out_msgnum = (long)in_msgnum;
384 if (in_key->size != 4) {
385 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
389 // the data is binary-ish but has no packed integers
390 out_data->size = in_data->size;
391 out_data->data = realloc(out_data->data, out_data->size);
392 memcpy(out_data->data, in_data->data, in_data->size);
394 printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
398 // convert function for EUID Index records
399 void convert_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
401 // The structure of an euidindex record *key* is:
402 // |----room_number----|----------EUID-------------|
403 // (sizeof long) (actual length of euid)
405 // The structure of an euidindex record *value* is:
406 // |-----msg_number----|----room_number----|----------EUID-------------|
407 // (sizeof long) (sizeof long) (actual length of euid)
409 int32_t in_msgnum = 0;
410 int32_t in_roomnum = 0;
413 long out_roomnum = 0;
415 memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
416 memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
417 strcpy(euid, in_data->data+(sizeof(int32_t)*2));
419 out_msgnum = (long) in_msgnum;
420 out_roomnum = (long) in_roomnum;
421 printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
423 out_key->size = sizeof(long) + strlen(euid) + 1;
424 out_key->data = realloc(out_key->data, out_key->size);
425 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
426 strcpy(out_key->data+sizeof(out_roomnum), euid);
428 out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
429 out_data->data = realloc(out_data->data, out_data->size);
430 memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
431 memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
432 strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
439 for (i=0; i<in_key->size; ++i) {
441 memcpy(&ch, in_key->data+i, 1);
442 printf("%02X ", (int) ch);
446 printf(" out_key: ");
447 for (i=0; i<out_key->size; ++i) {
449 memcpy(&ch, out_key->data+i, 1);
450 printf("%02X ", (int) ch);
454 printf(" in_data: ");
455 for (i=0; i<in_data->size; ++i) {
457 memcpy(&ch, in_data->data+i, 1);
458 printf("%02X ", (int) ch);
462 printf("out_data: ");
463 for (i=0; i<out_data->size; ++i) {
465 memcpy(&ch, out_data->data+i, 1);
466 printf("%02X ", (int) ch);
474 // convert users-by-number records
475 void convert_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
478 // and remember ... "long" is int32_t on the source system
481 memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
482 out_usernum = (long) in_usernum;
484 if (in_key->size != 4) {
485 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
489 out_key->size = sizeof(out_usernum);
490 out_key->data = realloc(out_key->data, out_key->size);
491 memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
494 out_data->size = in_data->size;
495 out_data->data = realloc(out_data->data, out_data->size);
496 memcpy(out_data->data, in_data->data, in_data->size);
498 printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
502 // convert function for a config record
503 void convert_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
505 // the key is a string
506 out_key->size = in_key->size;
507 out_key->data = realloc(out_key->data, out_key->size + 1);
508 memcpy(out_key->data, in_key->data, in_key->size);
509 char *k = (char *)out_key->data;
510 k[out_key->size] = 0;
512 // the data is a pair of strings
513 out_data->size = in_data->size;
514 out_data->data = realloc(out_data->data, out_data->size + 1);
515 memcpy(out_data->data, in_data->data, in_data->size);
516 char *d = (char *)out_data->data;
517 d[out_data->size] = 0;
519 // please excuse my friend, he isn't null terminated
520 printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
524 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
525 convert_msgmain, // CDB_MSGMAIN
526 convert_users, // CDB_USERS
527 convert_rooms, // CDB_ROOMS
528 convert_floors, // CDB_FLOORTAB
529 convert_msglists, // CDB_MSGLISTS
530 convert_visits, // CDB_VISIT
531 convert_dir, // CDB_DIRECTORY
532 convert_usetable, // CDB_USETABLE
533 convert_bigmsgs, // CDB_BIGMSGS
534 convert_msglists, // CDB_FULLTEXT
535 convert_euidindex, // CDB_EUIDINDEX
536 convert_usersbynumber, // CDB_USERSBYNUMBER
537 null_function, // CDB_EXTAUTH
538 convert_config // CDB_CONFIG
542 void convert_table(int which_cdb) {
548 printf("\033[43m\033[K\033[0m\n");
549 printf("\033[43m\033[30m Converting table %02x \033[K\033[0m\n", which_cdb);
550 printf("\033[43m\033[K\033[0m\n");
552 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
555 DBT in_key, in_data, out_key, out_data, uncomp_data, recomp_data;
558 // create a database handle
559 ret = db_create(&dbp, dbenv, 0);
561 printf("db: db_create: %s\n", db_strerror(ret));
562 printf("db: exit code %d\n", ret);
567 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
568 printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
569 ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
571 printf("db: db_open: %s\n", db_strerror(ret));
572 printf("db: exit code %d\n", ret);
577 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
578 printf("db: db_cursor: %s\n", db_strerror(ret));
579 printf("db: exit code %d\n", ret);
583 // Zero out these database keys
584 memset(&in_key, 0, sizeof(DBT)); // input
585 memset(&in_data, 0, sizeof(DBT));
586 memset(&out_key, 0, sizeof(DBT)); // output
587 memset(&out_data, 0, sizeof(DBT));
588 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
589 memset(&recomp_data, 0, sizeof(DBT)); // recompressed input (the key doesn't change)
591 // Walk through the database, calling convert functions as we go and clearing buffers before each call.
592 while (out_key.size = 0, out_data.size = 0, (ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
594 // Do we need to decompress?
595 static int32_t magic = COMPRESS_MAGIC;
597 if ( (in_data.size >= sizeof(struct CtdlCompressHeader_32)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
599 // yes, we need to decompress
601 struct CtdlCompressHeader_32 comp32;
602 memcpy(&comp32, in_data.data, sizeof(struct CtdlCompressHeader_32));
603 uncomp_data.size = comp32.uncompressed_len;
604 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
605 destLen = (uLongf)comp32.uncompressed_len;
607 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader_32), (uLong)comp32.compressed_len);
609 printf("db: uncompress() error %d\n", ret);
612 printf("DB: %02x , in_keylen: %-3d , in_datalen: %-10d , dataptr: %012lx \033[31m(decompressed)\033[0m\n", which_cdb, (int)in_key.size, comp32.uncompressed_len, (long unsigned int)uncomp_data.data);
615 printf("DB: %02x , in_keylen: %-3d , in_datalen: %-10d , dataptr: %012lx\n", which_cdb, (int)in_key.size, (int)in_data.size, (long unsigned int)in_data.data);
618 // Call the convert function registered to this table
619 convert_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data), &out_key, &out_data);
621 // The logic here is that if the source data was compressed, we compress the output too
623 struct CtdlCompressHeader zheader;
624 memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
625 zheader.magic = COMPRESS_MAGIC;
626 zheader.uncompressed_len = out_data.size;
627 recomp_data.data = realloc(recomp_data.data, ((out_data.size * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader));
629 if (compress2((Bytef *)(recomp_data.data + sizeof(struct CtdlCompressHeader)), &destLen, (Bytef *)out_data.data, (uLongf) out_data.size, 1) != Z_OK) {
630 printf("db: compress() error\n");
633 recomp_data.size = destLen;
636 // write the ed record to the new database
637 if (out_key.size > 0) {
641 // NOTE TO SELF: if we compressed the output, write recomp_data instead of out_data
644 printf("DB: %02x , out_keylen: %-3d , out_datalen: %-10d , dataptr: %012lx \033[31m(compressed)\033[0m\n", which_cdb, (int)out_key.size, (int)recomp_data.size, (long unsigned int)recomp_data.data);
647 printf("DB: %02x , out_keylen: %-3d , out_datalen: %-10d , dataptr: %012lx\n", which_cdb, (int)out_key.size, (int)out_data.size, (long unsigned int)out_data.data);
650 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
655 if (ret != DB_NOTFOUND) {
656 printf("db: db_get: %s\n", db_strerror(ret));
657 printf("db: exit code %d\n", ret);
661 printf("%d rows\n", num_rows);
663 // free any leftover out_data pointers
666 free(uncomp_data.data);
667 free(recomp_data.data);
670 //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
671 //if ((ret = dbenv->log_flush(dbenv, NULL))) {
672 //printf("db: log_flush: %s\n", db_strerror(ret));
675 // ...and close the database (table)
676 printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
677 ret = dbp->close(dbp, 0);
679 printf("db: db_close: %s\n", db_strerror(ret));
685 int main(int argc, char **argv) {
686 char *src_dir = NULL;
689 // Check to make sure we're running on the target 64-bit system
690 if (sizeof(void *) != 8) {
691 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
692 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
696 // Parse command line
698 while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
707 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
713 printf("------------------------------------------------------------------------\n");
714 printf("ctdl3264 s a Citadel database written on a 32-bit system to one \n");
715 printf("that can be run on a 64-bit system. It is intended to be run OFFLINE. \n");
716 printf("Neither the source nor the target data directories should be mounted by \n");
717 printf("a running Citadel server. We \033[1mguarantee\033[0m data corruption if you do not \n");
718 printf("observe this warning! The source [-s] directory should contain a copy \n");
719 printf("of the database from your 32-bit system. The destination [-d] directory\n");
720 printf("should be empty and will receive your 64-bit database. \n");
721 printf("------------------------------------------------------------------------\n");
722 printf(" Source 32-bit directory: %s\n", src_dir);
723 printf("Destination 64-bit directory: %s\n", "FIXME");
724 printf("------------------------------------------------------------------------\n");
726 if (confirmed == 1) {
727 printf("You have specified the [-y] flag, so processing will continue.\n");
730 printf("Please read [ https://www.citadel.org/ctdl3264.html ] to learn how to proceed.\n");
735 for (int i = 0; i < MAXCDB; ++i) {