ctdl3264: straightened out the compression stuff
[citadel.git] / citadel / utils / ctdl3264.c
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.
3 //
4 // Copyright (c) 2023 by Art Cancro citadel.org
5 //
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.
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <limits.h>
23 #include <libcitadel.h>
24 #include <zlib.h>
25 #include <db.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"
31
32 static DB_ENV *dbenv;           // The DB environment (global)
33
34 // Open the database environment
35 void open_dbenv(char *src_dir) {
36         int ret;
37         int i;
38         u_int32_t flags = 0;
39         int dbversion_major, dbversion_minor, dbversion_patch;
40
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",
45                 zlibVersion(),
46                 DB_VERSION_STRING,
47                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
48         );
49
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.");
57                 exit(CTDLEXIT_DB);
58         }
59
60         printf("db: Setting up DB environment\n");
61         ret = db_env_create(&dbenv, 0);
62         if (ret) {
63                 printf("db: db_env_create: %s\n", db_strerror(ret));
64                 printf("db: exit code %d\n", ret);
65                 exit(CTDLEXIT_DB);
66         }
67
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);
70         if (ret) {
71                 printf("db: set_cachesize: %s\n", db_strerror(ret));
72                 dbenv->close(dbenv, 0);
73                 printf("db: exit code %d\n", ret);
74                 exit(CTDLEXIT_DB);
75         }
76
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);
81                 exit(CTDLEXIT_DB);
82         }
83
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);
87         if (ret) {
88                 printf("db: dbenv->open: %s\n", db_strerror(ret));
89                 dbenv->close(dbenv, 0);
90                 printf("db: exit code %d\n", ret);
91                 exit(CTDLEXIT_DB);
92         }
93 }
94
95
96 void close_dbenv(void) {
97         printf("db: closing dbenv\n");
98         int ret = dbenv->close(dbenv, 0);
99         if (ret) {
100                 printf("db: DBENV->close: %s\n", db_strerror(ret));
101         }
102 }
103
104
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 }
108
109
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) {
112         int32_t in_msgnum;
113         long out_msgnum;
114         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
115         out_msgnum = (long)in_msgnum;
116
117         if (in_key->size != 4) {
118                 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
119                 abort();
120         }
121
122         // If the msgnum is negative, we are looking at METADATA
123         if (in_msgnum < 0) {
124                 struct MetaData_32 *meta32 = (struct MetaData_32 *)in_data->data;
125
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);
127
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));
131
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;
140         }
141
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));
147
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);
153         }
154
155         // If the msgnum is 0 it's probably not a valid record.
156         else {
157                 printf("msgmain: record 0 is impossible\n");
158         }
159 }
160
161
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) {
164
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);
169
170         struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
171
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;
175
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;
192
193         printf("\033[32m\033[1mUser: %s\033[0m\n", user64->fullname);
194 }
195
196
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) {
199
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);
204
205         // data
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;
210
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;
228
229         printf("\033[32m\033[1mRoom: %s\033[0m\n", room64->QRname);
230 }
231
232
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) {
235
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);
240
241         // data
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;
246
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;
253
254         printf("\033[32m\033[1mFloor: %s\033[0m\n", floor64->f_name);
255 }
256
257
258 // convert function for a msglist
259 void convert_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
260         int i;
261
262         // msglist records are indexed by a single "long" and contains an array of zero or more "long"s
263         // and remember ... "long" is int32_t on the source system
264         int32_t in_roomnum;
265         long out_roomnum;
266         memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
267         out_roomnum = (long) in_roomnum;
268
269         if (in_key->size != 4) {
270                 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
271                 abort();
272         }
273
274         int num_msgs = in_data->size / sizeof(int32_t);
275         printf("\033[32m\033[1mMsglist: for room %ld (%d messages)\033[0m\n", out_roomnum, num_msgs);
276
277         // the key is a "long"
278         out_key->size = sizeof(out_roomnum);
279         out_key->data = realloc(out_key->data, out_key->size);
280         memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
281
282         // the data is another array, but a wider type
283         out_data->size = sizeof(long) * num_msgs;
284         out_data->data = realloc(out_data->data, out_data->size);
285
286         int32_t in_msg = 0;
287         long out_msg = 0;
288         for (i=0; i<num_msgs; ++i) {
289                 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
290                 out_msg = (long) in_msg;
291                 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
292                 printf("#%ld\n", out_msg);
293         }
294 }
295
296
297 // convert function for a visit record
298 void convert_visits(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
299
300         // data
301         struct visit_32 *visit32 = (struct visit_32 *)in_data->data;
302         out_data->size = sizeof(struct visit);
303         out_data->data = realloc(out_data->data, out_data->size);
304         struct visit *visit64 = (struct visit *)out_data->data;
305
306         // convert the data (zero it out so it will compress well)
307         memset(visit64, 0, sizeof(struct visit));
308         visit64->v_roomnum              = (long)        visit32->v_roomnum;
309         visit64->v_roomgen              = (long)        visit32->v_roomgen;
310         visit64->v_usernum              = (long)        visit32->v_usernum;
311         visit64->v_lastseen             = (long)        visit32->v_lastseen;
312         visit64->v_flags                = (unsigned)    visit32->v_flags;
313         strcpy(visit64->v_seen,                         visit32->v_seen);
314         strcpy(visit64->v_answered,                     visit32->v_answered);
315         visit64->v_view                 = (int)         visit32->v_view;
316
317         printf("\033[32m\033[1mVisit: room %ld, gen %ld, user %ld\033[0m\n", visit64->v_roomnum, visit64->v_roomgen, visit64->v_usernum);
318
319         // create the key (which is based on the data, so there is no need to convert the old key)
320         out_key->size = sizeof(struct visit_index);
321         out_key->data = realloc(out_key->data, out_key->size);
322         struct visit_index *newvisitindex = (struct visit_index *) out_key->data;
323         newvisitindex->iRoomID          =               visit64->v_roomnum;
324         newvisitindex->iRoomGen         =               visit64->v_roomgen;
325         newvisitindex->iUserID          =               visit64->v_usernum;
326 }
327
328
329 // convert function for a directory record
330 void convert_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
331
332         // the key is a string
333         out_key->size = in_key->size;
334         out_key->data = realloc(out_key->data, out_key->size);
335         memcpy(out_key->data, in_key->data, in_key->size);
336
337         // the data is also a string
338         out_data->size = in_data->size;
339         out_data->data = realloc(out_data->data, out_data->size);
340         memcpy(out_data->data, in_data->data, in_data->size);
341
342         // please excuse my friend, he isn't null terminated
343         printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
344 }
345
346
347 // convert function for a use table record
348 void convert_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
349
350         // the key is a string
351         out_key->size = in_key->size;
352         out_key->data = realloc(out_key->data, out_key->size);
353         memcpy(out_key->data, in_key->data, in_key->size);
354
355         // the data is a "struct UseTable"
356         struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
357         out_data->size = sizeof(struct UseTable);
358         out_data->data = realloc(out_data->data, out_data->size);
359         memset(out_data->data, 0, out_data->size);
360         struct UseTable *use64 = (struct UseTable *)out_data->data;
361
362         // convert the data
363         strcpy(use64->ut_msgid,                         use32->ut_msgid);
364         use64->ut_timestamp             = (time_t)      use32->ut_timestamp;
365 }
366
367
368 // convert function for large message texts
369 void convert_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
370
371         // The key is a packed long
372         int32_t in_msgnum;
373         long out_msgnum;
374         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
375         out_msgnum = (long)in_msgnum;
376
377         if (in_key->size != 4) {
378                 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
379                 abort();
380         }
381
382         // the data is binary-ish but has no packed integers
383         out_data->size = in_data->size;
384         out_data->data = realloc(out_data->data, out_data->size);
385         memcpy(out_data->data, in_data->data, in_data->size);
386
387         printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
388 }
389
390
391 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
392         convert_msgmain,        // CDB_MSGMAIN
393         convert_users,          // CDB_USERS
394         convert_rooms,          // CDB_ROOMS
395         convert_floors,         // CDB_FLOORTAB
396         convert_msglists,       // CDB_MSGLISTS
397         convert_visits,         // CDB_VISIT
398         convert_dir,            // CDB_DIRECTORY
399         convert_usetable,       // CDB_USETABLE
400         convert_bigmsgs,        // CDB_BIGMSGS
401         null_function,          // CDB_FULLTEXT
402         null_function,          // CDB_EUIDINDEX
403         null_function,          // CDB_USERSBYNUMBER
404         null_function,          // CDB_EXTAUTH
405         null_function           // CDB_CONFIG
406 };
407
408
409 void convert_table(int which_cdb) {
410         int ret;
411         int compressed;
412         char dbfilename[32];
413         uLongf destLen = 0;
414
415         printf("\033[32m\033[1mConverting table %d\033[0m\n", which_cdb);
416
417         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
418         DB *dbp;
419         DBC *dbcp;
420         DBT in_key, in_data, out_key, out_data, uncomp_data, recomp_data;
421         int num_rows = 0;
422
423         // create a database handle
424         ret = db_create(&dbp, dbenv, 0);
425         if (ret) {
426                 printf("db: db_create: %s\n", db_strerror(ret));
427                 printf("db: exit code %d\n", ret);
428                 exit(CTDLEXIT_DB);
429         }
430
431         // open the file
432         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
433         printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
434         ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
435         if (ret) {
436                 printf("db: db_open: %s\n", db_strerror(ret));
437                 printf("db: exit code %d\n", ret);
438                 exit(CTDLEXIT_DB);
439         }
440
441         // Acquire a cursor
442         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
443                 printf("db: db_cursor: %s\n", db_strerror(ret));
444                 printf("db: exit code %d\n", ret);
445                 exit(CTDLEXIT_DB);
446         }
447
448         // Zero out these database keys
449         memset(&in_key,         0, sizeof(DBT));        // input
450         memset(&in_data,        0, sizeof(DBT));
451         memset(&out_key,        0, sizeof(DBT));        // output
452         memset(&out_data,       0, sizeof(DBT));
453         memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
454         memset(&recomp_data,    0, sizeof(DBT));        // recompressed input (the key doesn't change)
455
456         // Walk through the database, calling convert functions as we go and clearing buffers before each call.
457         while (out_key.size = 0, out_data.size = 0, (ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
458
459                 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);
460                 compressed = 0;
461
462                 // Do we need to decompress?
463                 static int32_t magic = COMPRESS_MAGIC;
464                 if ( (in_data.size >= sizeof(struct CtdlCompressHeader_32)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
465
466                         // yes, we need to decompress
467                         compressed = 1;
468                         struct CtdlCompressHeader_32 comp32;
469                         memcpy(&comp32, in_data.data, sizeof(struct CtdlCompressHeader_32));
470                         uncomp_data.size = comp32.uncompressed_len;
471                         uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
472                         destLen = (uLongf)comp32.uncompressed_len;
473
474                         ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader_32), (uLong)comp32.compressed_len);
475                         if (ret != Z_OK) {
476                                 printf("db: uncompress() error %d\n", ret);
477                                 exit(CTDLEXIT_DB);
478                         }
479                         printf("\033[31m\033[1mDB: %02x ,  in_keylen: %-3d ,  in_datalen: %-10d , dataptr: %012lx (decompressed)\033[0m\n",
480                                 which_cdb, (int)in_key.size, comp32.uncompressed_len, (long unsigned int)uncomp_data.data);
481
482                 }
483
484                 // Call the convert function registered to this table
485                 convert_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data), &out_key, &out_data);
486
487                 // The logic here is that if the source data was compressed, we compress the output too
488                 if (compressed) {
489                         printf("\033[31m\033[1mCOMPRESSING\033[0m\n");
490
491                         struct CtdlCompressHeader zheader;
492                         memset(&zheader, 0, sizeof(struct CtdlCompressHeader));
493                         zheader.magic = COMPRESS_MAGIC;
494                         zheader.uncompressed_len = out_data.size;
495                         recomp_data.data = realloc(recomp_data.data, ((out_data.size * 101) / 100) + 100 + sizeof(struct CtdlCompressHeader));
496
497                         if (compress2((Bytef *)(recomp_data.data + sizeof(struct CtdlCompressHeader)), &destLen, (Bytef *)out_data.data, (uLongf) out_data.size, 1) != Z_OK) {
498                                 printf("db: compress() error\n");
499                                 exit(CTDLEXIT_DB);
500                         }
501                         recomp_data.size = destLen;
502                 }
503
504                 // write the converted record to the new database
505                 if (out_key.size > 0) {
506
507
508
509                         // NOTE TO SELF: if we compressed the output, write recomp_data instead of out_data
510
511                         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);
512
513                         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
514                         ++num_rows;
515                 }
516         }
517
518         if (ret != DB_NOTFOUND) {
519                 printf("db: db_get: %s\n", db_strerror(ret));
520                 printf("db: exit code %d\n", ret);
521                 exit(CTDLEXIT_DB);
522         }
523
524         printf("%d rows\n", num_rows);
525
526         // free any leftover out_data pointers
527         free(out_key.data);
528         free(out_data.data);
529         free(uncomp_data.data);
530         free(recomp_data.data);
531
532         // Flush the logs...
533         //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
534         //if ((ret = dbenv->log_flush(dbenv, NULL))) {
535                 //printf("db: log_flush: %s\n", db_strerror(ret));
536         //}
537
538         // ...and close the database (table)
539         printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
540         ret = dbp->close(dbp, 0);
541         if (ret) {
542                 printf("db: db_close: %s\n", db_strerror(ret));
543         }
544
545 }
546
547
548 int main(int argc, char **argv) {
549         char *src_dir = NULL;
550         int confirmed = 0;
551
552         // Check to make sure we're running on the target 64-bit system
553         if (sizeof(void *) != 8) {
554                 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
555                 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
556                 exit(1);
557         }
558
559         // Parse command line
560         int a;
561         while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
562                 switch (a) {
563                 case 's':
564                         src_dir = optarg;
565                         break;
566                 case 'y':
567                         confirmed = 1;
568                         break;
569                 default:
570                         fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
571                         exit(2);
572                 }
573         }
574
575         // Warn the user
576         printf("------------------------------------------------------------------------\n");
577         printf("ctdl3264 converts a Citadel database written on a 32-bit system to one  \n");
578         printf("that can be run on a 64-bit system.  It is intended to be run OFFLINE.  \n");
579         printf("Neither the source nor the target data directories should be mounted by \n");
580         printf("a running Citadel server.  We \033[1mguarantee\033[0m data corruption if you do not   \n");
581         printf("observe this warning!  The source [-s] directory should contain a copy  \n");
582         printf("of the database from your 32-bit system.  The destination [-d] directory\n");
583         printf("should be empty and will receive your 64-bit database.                  \n");
584         printf("------------------------------------------------------------------------\n");
585         printf("     Source 32-bit directory: %s\n", src_dir);
586         printf("Destination 64-bit directory: %s\n", "FIXME");
587         printf("------------------------------------------------------------------------\n");
588
589         if (confirmed == 1) {
590                 printf("You have specified the [-y] flag, so processing will continue.\n");
591         }
592         else {
593                 printf("Please read [ https://www.citadel.org/ctdl3264.html ] to learn how to proceed.\n");
594                 exit(0);
595         }
596
597         open_dbenv(src_dir);
598         for (int i = 0; i < MAXCDB; ++i) {
599                 convert_table(i);
600         }
601         close_dbenv();
602
603         exit(0);
604 }