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"
33 // Wrapper for realloc() that crashes and burns if the call fails.
34 void *reallok(void *ptr, size_t size) {
35 void *p = realloc(ptr, size);
37 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
42 #define realloc reallok
45 // Open a database environment
46 DB_ENV *open_dbenv(char *dirname) {
53 int dbversion_major, dbversion_minor, dbversion_patch;
55 printf( "db: open_dbenv() starting\n"
56 "db: Linked zlib: %s\n"
57 "db: Compiled libdb: %s\n"
58 "db: Linked libdb: %s\n",
61 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
64 // Create synthetic integer version numbers and compare them.
65 // Never run with a libdb older than the one with which it was compiled.
66 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
67 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
68 if (compiled_db_version > linked_db_version) {
69 printf( "db: ctdl3264 is running with a version of libdb older than the one with which it was compiled.\n"
70 "db: This is an invalid configuration. ctdl3264 will now exit to prevent data loss.");
74 printf("db: Setting up DB environment\n");
75 ret = db_env_create(&dbenv, 0);
77 printf("db: db_env_create: %s\n", db_strerror(ret));
78 printf("db: exit code %d\n", ret);
82 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
83 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
85 printf("db: set_cachesize: %s\n", db_strerror(ret));
86 dbenv->close(dbenv, 0);
87 printf("db: exit code %d\n", ret);
91 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
92 printf("db: set_lk_detect: %s\n", db_strerror(ret));
93 dbenv->close(dbenv, 0);
94 printf("db: exit code %d\n", ret);
98 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
99 printf("db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
100 ret = dbenv->open(dbenv, dirname, flags, 0);
102 printf("db: dbenv->open: %s\n", db_strerror(ret));
103 dbenv->close(dbenv, 0);
104 printf("db: exit code %d\n", ret);
112 void close_dbenv(DB_ENV *dbenv) {
113 printf("db: closing dbenv\n");
114 int ret = dbenv->close(dbenv, 0);
116 printf("db: dbenv->close: %s\n", db_strerror(ret));
121 // convert function for a message in msgmain
122 void convert_msgmain(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
125 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
126 out_msgnum = (long)in_msgnum;
128 if (in_key->size != 4) {
129 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
133 // If the msgnum is negative, we are looking at METADATA
135 struct MetaData_32 *meta32 = (struct MetaData_32 *)in_data->data;
137 // 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);
139 out_key->size = sizeof(long);
140 out_key->data = realloc(out_key->data, out_key->size);
141 memcpy(out_key->data, &out_msgnum, sizeof(long));
143 out_data->size = sizeof(struct MetaData);
144 out_data->data = realloc(out_data->data, out_data->size);
145 struct MetaData *meta64 = (struct MetaData *)out_data->data;
146 memset(meta64, 0, sizeof(struct MetaData));
147 meta64->meta_msgnum = (long) meta32->meta_msgnum;
148 meta64->meta_refcount = (int) meta32->meta_refcount;
149 strcpy(meta64->meta_content_type, meta32->meta_content_type);
150 meta64->meta_rfc822_length = (long) meta32->meta_rfc822_length;
153 // If the msgnum is positive, we are looking at a MESSAGE
154 else if (in_msgnum > 0) {
155 out_key->size = sizeof(long);
156 out_key->data = realloc(out_key->data, out_key->size);
157 memcpy(out_key->data, &out_msgnum, sizeof(long));
159 // copy the message verbatim
160 out_data->size = in_data->size;
161 out_data->data = realloc(out_data->data, out_data->size);
162 memcpy(out_data->data, in_data->data, out_data->size);
163 // printf("\033[32m\033[1mMessage: %ld\033[0m\n", out_msgnum);
166 // If the msgnum is 0 it's probably not a valid record.
168 // printf("\033[31mmsgmain: message number 0 is impossible, skipping this record\033[0m\n");
173 // convert function for a user record
174 void convert_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
176 // The key is a string so we can just copy it over
177 out_key->size = in_key->size;
178 out_key->data = realloc(out_key->data, out_key->size);
179 memcpy(out_key->data, in_key->data, in_key->size);
181 struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
183 out_data->size = sizeof(struct ctdluser);
184 out_data->data = realloc(out_data->data, out_data->size);
185 struct ctdluser *user64 = (struct ctdluser *)out_data->data;
187 user64->version = (int) user32->version;
188 user64->uid = (uid_t) user32->uid;
189 strcpy(user64->password, user32->password);
190 user64->flags = (unsigned) user32->flags;
191 user64->axlevel = (cit_uint8_t) user32->axlevel;
192 user64->usernum = (long) user32->usernum;
193 user64->lastcall = (time_t) user32->lastcall;
194 user64->USuserpurge = (int) user32->USuserpurge;
195 strcpy(user64->fullname, user32->fullname);
196 user64->msgnum_bio = (long) user32->msgnum_bio;
197 user64->msgnum_pic = (long) user32->msgnum_pic;
198 strcpy(user64->emailaddrs, user32->emailaddrs);
199 user64->msgnum_inboxrules = (long) user32->msgnum_inboxrules;
200 user64->lastproc_inboxrules = (long) user32->lastproc_inboxrules;
202 // printf("\033[32m\033[1mUser: %s\033[0m\n", user64->fullname);
206 // convert function for a room record
207 void convert_rooms(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
209 // The key is a string so we can just copy it over
210 out_key->size = in_key->size;
211 out_key->data = realloc(out_key->data, out_key->size);
212 memcpy(out_key->data, in_key->data, in_key->size);
215 struct ctdlroom_32 *room32 = (struct ctdlroom_32 *)in_data->data;
216 out_data->size = sizeof(struct ctdlroom);
217 out_data->data = realloc(out_data->data, out_data->size);
218 struct ctdlroom *room64 = (struct ctdlroom *)out_data->data;
220 strcpy(room64->QRname, room32->QRname);
221 strcpy(room64->QRpasswd, room32->QRpasswd);
222 room64->QRroomaide = (long) room32->QRroomaide;
223 room64->QRhighest = (long) room32->QRhighest;
224 room64->QRgen = (time_t) room32->QRgen;
225 room64->QRflags = (unsigned) room32->QRflags;
226 strcpy(room64->QRdirname, room32->QRdirname);
227 room64->msgnum_info = (long) room32->msgnum_info;
228 room64->QRfloor = (char) room32->QRfloor;
229 room64->QRmtime = (time_t) room32->QRmtime;
230 room64->QRep.expire_mode = (int) room32->QRep.expire_mode;
231 room64->QRep.expire_value = (int) room32->QRep.expire_value;
232 room64->QRnumber = (long) room32->QRnumber;
233 room64->QRorder = (char) room32->QRorder;
234 room64->QRflags2 = (unsigned) room32->QRflags2;
235 room64->QRdefaultview = (int) room32->QRdefaultview;
236 room64->msgnum_pic = (long) room32->msgnum_pic;
238 // printf("\033[32m\033[1mRoom: %s\033[0m\n", room64->QRname);
242 // convert function for a floor record
243 void convert_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
245 // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
246 out_key->size = in_key->size;
247 out_key->data = realloc(out_key->data, out_key->size);
248 memcpy(out_key->data, in_key->data, in_key->size);
251 struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
252 out_data->size = sizeof(struct floor);
253 out_data->data = realloc(out_data->data, out_data->size);
254 struct floor *floor64 = (struct floor *)out_data->data;
256 // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
257 floor64->f_flags = (unsigned short) floor32->f_flags;
258 strcpy(floor64->f_name, floor32->f_name);
259 floor64->f_ref_count = (int) floor32->f_ref_count;
260 floor64->f_ep.expire_mode = (int) floor32->f_ep.expire_mode;
261 floor64->f_ep.expire_value = (int) floor32->f_ep.expire_value;
263 // printf("\033[32m\033[1mFloor: %s\033[0m\n", floor64->f_name);
267 // convert function for a msglist or a fulltext index record
268 // (both are indexed by a long and the data is arrays of longs)
269 void convert_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
272 char *table = (which_cdb == CDB_FULLTEXT) ? "FullText" : "Msglist";
274 // records are indexed by a single "long" and contains an array of zero or more "long"s
275 // and remember ... "long" is int32_t on the source system
278 memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
279 out_roomnum = (long) in_roomnum;
281 if (in_key->size != 4) {
282 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
286 int num_msgs = in_data->size / sizeof(int32_t);
287 // printf("\033[32m\033[1m%s: key %ld (%d messages)\033[0m\n", table, out_roomnum, num_msgs);
289 // the key is a "long"
290 out_key->size = sizeof(out_roomnum);
291 out_key->data = realloc(out_key->data, out_key->size);
292 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
294 // the data is another array, but a wider type
295 out_data->size = sizeof(long) * num_msgs;
296 out_data->data = realloc(out_data->data, out_data->size);
300 for (i=0; i<num_msgs; ++i) {
301 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
302 out_msg = (long) in_msg;
303 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
304 // printf("msg#%ld\n", out_msg);
309 // convert function for a visit record
310 void convert_visits(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
313 struct visit_32 *visit32 = (struct visit_32 *)in_data->data;
314 out_data->size = sizeof(struct visit);
315 out_data->data = realloc(out_data->data, out_data->size);
316 struct visit *visit64 = (struct visit *)out_data->data;
318 // the data (zero it out so it will compress well)
319 memset(visit64, 0, sizeof(struct visit));
320 visit64->v_roomnum = (long) visit32->v_roomnum;
321 visit64->v_roomgen = (long) visit32->v_roomgen;
322 visit64->v_usernum = (long) visit32->v_usernum;
323 visit64->v_lastseen = (long) visit32->v_lastseen;
324 visit64->v_flags = (unsigned) visit32->v_flags;
325 strcpy(visit64->v_seen, visit32->v_seen);
326 strcpy(visit64->v_answered, visit32->v_answered);
327 visit64->v_view = (int) visit32->v_view;
329 // printf("\033[32m\033[1mVisit: room %10ld, gen %10ld, user %10ld\033[0m\n", visit64->v_roomnum, visit64->v_roomgen, visit64->v_usernum);
331 // create the key (which is based on the data, so there is no need to convert the old key)
332 out_key->size = sizeof(struct visit_index);
333 out_key->data = realloc(out_key->data, out_key->size);
334 struct visit_index *newvisitindex = (struct visit_index *) out_key->data;
335 newvisitindex->iRoomID = visit64->v_roomnum;
336 newvisitindex->iRoomGen = visit64->v_roomgen;
337 newvisitindex->iUserID = visit64->v_usernum;
341 // convert function for a directory record
342 void convert_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
344 // the key is a string
345 out_key->size = in_key->size;
346 out_key->data = realloc(out_key->data, out_key->size + 1);
347 memcpy(out_key->data, in_key->data, in_key->size);
348 char *k = (char *)out_key->data;
349 k[out_key->size] = 0;
351 // the data is also a string
352 out_data->size = in_data->size;
353 out_data->data = realloc(out_data->data, out_data->size + 1);
354 memcpy(out_data->data, in_data->data, in_data->size);
355 char *d = (char *)out_data->data;
356 d[out_data->size] = 0;
358 // please excuse my friend, he isn't null terminated
359 // printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
363 // convert function for a use table record
364 void convert_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
366 // the key is an int, which is the same size (32 bits) on both 32 and 64 bit systems
367 out_key->size = in_key->size;
368 out_key->data = realloc(out_key->data, out_key->size);
369 memcpy(out_key->data, in_key->data, in_key->size);
371 // the data is a "struct UseTable"
372 struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
373 out_data->size = sizeof(struct UseTable);
374 out_data->data = realloc(out_data->data, out_data->size);
375 memset(out_data->data, 0, out_data->size);
376 struct UseTable *use64 = (struct UseTable *)out_data->data;
379 use64->hash = use32->hash;
380 use64->timestamp = (time_t) use32->timestamp;
382 // printf("\033[32m\033[1muse table: %d , %s\033[0m\n", use64->hash, asctime(localtime(&use64->timestamp)));
386 // convert function for large message texts
387 void convert_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
389 // The key is a packed long
392 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
393 out_msgnum = (long)in_msgnum;
395 if (in_key->size != 4) {
396 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
400 out_key->size = sizeof(long);
401 out_key->data = realloc(out_key->data, out_key->size);
402 memcpy(out_key->data, &out_msgnum, sizeof(long));
404 // the data is binary-ish but has no packed integers
405 out_data->size = in_data->size;
406 out_data->data = realloc(out_data->data, out_data->size);
407 memcpy(out_data->data, in_data->data, in_data->size);
409 // printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
413 // convert function for EUID Index records
414 void convert_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
416 // The structure of an euidindex record *key* is:
417 // |----room_number----|----------EUID-------------|
418 // (sizeof long) (actual length of euid)
420 // The structure of an euidindex record *value* is:
421 // |-----msg_number----|----room_number----|----------EUID-------------|
422 // (sizeof long) (sizeof long) (actual length of euid)
424 int32_t in_msgnum = 0;
425 int32_t in_roomnum = 0;
428 long out_roomnum = 0;
430 memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
431 memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
432 strcpy(euid, in_data->data+(sizeof(int32_t)*2));
434 out_msgnum = (long) in_msgnum;
435 out_roomnum = (long) in_roomnum;
436 // printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
438 out_key->size = sizeof(long) + strlen(euid) + 1;
439 out_key->data = realloc(out_key->data, out_key->size);
440 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
441 strcpy(out_key->data+sizeof(out_roomnum), euid);
443 out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
444 out_data->data = realloc(out_data->data, out_data->size);
445 memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
446 memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
447 strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
453 //printf(" in_key: ");
454 //for (i=0; i<in_key->size; ++i) {
456 //memcpy(&ch, in_key->data+i, 1);
457 //printf("%02X ", (int) ch);
461 //printf(" out_key: ");
462 //for (i=0; i<out_key->size; ++i) {
464 //memcpy(&ch, out_key->data+i, 1);
465 //printf("%02X ", (int) ch);
469 //printf(" in_data: ");
470 //for (i=0; i<in_data->size; ++i) {
472 //memcpy(&ch, in_data->data+i, 1);
473 // printf("%02X ", (int) ch);
477 //printf("out_data: ");
478 //for (i=0; i<out_data->size; ++i) {
480 //memcpy(&ch, out_data->data+i, 1);
481 //printf("%02X ", (int) ch);
489 // convert users-by-number records
490 void convert_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
493 // and remember ... "long" is int32_t on the source system
496 memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
497 out_usernum = (long) in_usernum;
499 if (in_key->size != 4) {
500 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
504 out_key->size = sizeof(out_usernum);
505 out_key->data = realloc(out_key->data, out_key->size);
506 memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
509 out_data->size = in_data->size;
510 out_data->data = realloc(out_data->data, out_data->size);
511 memcpy(out_data->data, in_data->data, in_data->size);
513 // printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
517 // convert function for a config record
518 void convert_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
520 // the key is a string
521 out_key->size = in_key->size;
522 out_key->data = realloc(out_key->data, out_key->size + 1);
523 memcpy(out_key->data, in_key->data, in_key->size);
524 char *k = (char *)out_key->data;
525 k[out_key->size] = 0;
527 // the data is a pair of strings
528 out_data->size = in_data->size;
529 out_data->data = realloc(out_data->data, out_data->size + 1);
530 memcpy(out_data->data, in_data->data, in_data->size);
531 char *d = (char *)out_data->data;
532 d[out_data->size] = 0;
534 // please excuse my friend, he isn't null terminated
535 // printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
539 // For obsolete databases, zero all the output
540 void zero_function(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
546 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
547 convert_msgmain, // CDB_MSGMAIN
548 convert_users, // CDB_USERS
549 convert_rooms, // CDB_ROOMS
550 convert_floors, // CDB_FLOORTAB
551 convert_msglists, // CDB_MSGLISTS
552 convert_visits, // CDB_VISIT
553 convert_dir, // CDB_DIRECTORY
554 convert_usetable, // CDB_USETABLE
555 convert_bigmsgs, // CDB_BIGMSGS
556 convert_msglists, // CDB_FULLTEXT
557 convert_euidindex, // CDB_EUIDINDEX
558 convert_usersbynumber, // CDB_USERSBYNUMBER
559 zero_function, // CDB_UNUSED1 (obsolete)
560 convert_config // CDB_CONFIG
564 void convert_table(int which_cdb, DB_ENV *src_dbenv, DB_ENV *dst_dbenv) {
570 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
571 DB *src_dbp, *dst_dbp;
573 DBT in_key, in_data, out_key, out_data, uncomp_data;
574 int num_good_rows = 0;
575 int num_bad_rows = 0;
577 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
579 // create a database handle for the source table
580 ret = db_create(&src_dbp, src_dbenv, 0);
582 printf("db: db_create: %s\n", db_strerror(ret));
583 printf("db: exit code %d\n", ret);
587 // open the file containing the source table
588 // printf("\033[33m\033[1mdb: opening source %s\033[0m\n", dbfilename);
589 ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
591 printf("db: db_open: %s\n", db_strerror(ret));
592 printf("db: exit code %d\n", ret);
596 // create a database handle for the destination table
597 ret = db_create(&dst_dbp, dst_dbenv, 0);
599 printf("db: db_create: %s\n", db_strerror(ret));
600 printf("db: exit code %d\n", ret);
604 // open the file containing the destination table
605 // printf("\033[33m\033[1mdb: opening destination %s\033[0m\n", dbfilename);
606 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
608 printf("db: db_open: %s\n", db_strerror(ret));
609 printf("db: exit code %d\n", ret);
613 // Acquire a cursor to read the source table
614 // printf("\033[33m\033[1mdb: acquiring cursor\033[0m\n");
615 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
616 printf("db: db_cursor: %s\n", db_strerror(ret));
617 printf("db: exit code %d\n", ret);
621 // Zero out these database keys
622 memset(&in_key, 0, sizeof(DBT)); // input
623 memset(&in_data, 0, sizeof(DBT));
624 memset(&out_key, 0, sizeof(DBT)); // output
625 memset(&out_data, 0, sizeof(DBT));
626 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
628 // Walk through the database, calling convert functions as we go and clearing buffers before each call.
629 while (out_key.size = 0, out_data.size = 0, (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
631 // If either the key or data are zero length, skip this record
632 if ((in_key.size == 0) || (in_data.size == 0)) {
634 // printf("\033[31minput keylen=%d , datalen=%d , skipping record\033[0m\n", in_key.size, in_data.size);
637 else { // Both key and data are >0 length so we're good to go
639 // Do we need to decompress?
640 static int32_t magic = COMPRESS_MAGIC;
642 if ( (in_data.size >= sizeof(struct CtdlCompressHeader_32)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
644 // yes, we need to decompress
646 struct CtdlCompressHeader_32 comp32;
647 memcpy(&comp32, in_data.data, sizeof(struct CtdlCompressHeader_32));
648 uncomp_data.size = comp32.uncompressed_len;
649 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
650 destLen = (uLongf)comp32.uncompressed_len;
652 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader_32), (uLong)comp32.compressed_len);
654 printf("db: uncompress() error %d\n", ret);
657 // 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);
660 // 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);
663 // Call the convert function registered to this table
664 convert_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data), &out_key, &out_data);
666 // write the converted record to the target database
667 if (out_key.size > 0) {
669 // 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);
670 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
673 printf("db: cdb_put(%d): %s", which_cdb, db_strerror(ret));
680 // printf("\033[31moutput keylen=%d , skipping record\033[0m\n", out_key.size);
683 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
684 printf("\033[33m%5d\033[37m \033[32m%9d\033[37m \033[31m%8d\033[0m\r", which_cdb, num_good_rows, num_bad_rows);
689 if (ret != DB_NOTFOUND) {
690 printf("db: db_get: %s\n", db_strerror(ret));
691 printf("db: exit code %d\n", ret);
697 // free any leftover out_data pointers
700 free(uncomp_data.data);
702 // ...and close the database (table)
703 // printf("\033[33m\033[1mdb: closing source %02x\033[0m\n", which_cdb);
704 ret = src_dbp->close(src_dbp, 0);
706 printf("db: db_close: %s\n", db_strerror(ret));
709 // printf("\033[33m\033[1mdb: closing destination %02x\033[0m\n", which_cdb);
710 ret = dst_dbp->close(dst_dbp, 0);
712 printf("db: db_close: %s\n", db_strerror(ret));
720 int main(int argc, char **argv) {
721 char *src_dir = NULL;
722 char *dst_dir = NULL;
724 static DB_ENV *src_dbenv; // Source DB environment (global)
725 static DB_ENV *dst_dbenv; // Source DB environment (global)
727 // Check to make sure we're running on the target 64-bit system
728 if (sizeof(void *) != 8) {
729 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
730 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
734 // Parse command line
736 while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
748 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
754 printf("------------------------------------------------------------------------\n");
755 printf("ctdl3264 converts a Citadel database written on a 32-bit system to one \n");
756 printf("that can be run on a 64-bit system. It is intended to be run OFFLINE. \n");
757 printf("Neither the source nor the target data directories should be mounted by \n");
758 printf("a running Citadel server. We \033[1mguarantee\033[0m data corruption if you do not \n");
759 printf("observe this warning! The source [-s] directory should contain a copy \n");
760 printf("of the database from your 32-bit system. The destination [-d] directory\n");
761 printf("should be empty and will receive your 64-bit database. \n");
762 printf("------------------------------------------------------------------------\n");
763 printf(" Source 32-bit directory: %s\n", src_dir);
764 printf("Destination 64-bit directory: %s\n", dst_dir);
765 printf("------------------------------------------------------------------------\n");
767 if (confirmed == 1) {
768 printf("You have specified the [-y] flag, so processing will continue.\n");
771 printf("Please read [ https://www.citadel.org/ctdl3264.html ] to learn how to proceed.\n");
776 src_dbenv = open_dbenv(src_dir);
777 dst_dbenv = open_dbenv(dst_dir);
778 printf("table good rows bad rows\n");
779 printf("----- --------- --------\n");
780 for (int i = 0; i < MAXCDB; ++i) {
781 convert_table(i, src_dbenv, dst_dbenv);
783 close_dbenv(src_dbenv);
784 close_dbenv(dst_dbenv);