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 the citadel.org team
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 *dbp[MAXCDB]; // One DB handle for each Citadel database
33 static DB_ENV *dbenv; // The DB environment (global)
35 // Open the various databases we'll be using. Any database which
36 // does not exist should be created. Note that we don't need a
37 // critical section here, because there aren't any active threads
38 // manipulating the database yet.
39 void open_databases(void) {
44 int dbversion_major, dbversion_minor, dbversion_patch;
46 printf( "db: open_databases() starting\n"
47 "db: Linked zlib: %s\n"
48 "db: Compiled libdb: %s\n"
49 "db: Linked libdb: %s\n",
52 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
55 // Create synthetic integer version numbers and compare them.
56 // Never allow citserver to run with a libdb older then the one with which it was compiled.
57 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
58 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
59 if (compiled_db_version > linked_db_version) {
60 printf( "db: citserver is running with a version of libdb older than the one with which it was compiled.\n"
61 "db: This is an invalid configuration. citserver will now exit to prevent data loss.");
65 printf("db: Setting up DB environment\n");
66 ret = db_env_create(&dbenv, 0);
68 printf("db: db_env_create: %s\n", db_strerror(ret));
69 printf("db: exit code %d\n", ret);
73 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
74 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
76 printf("db: set_cachesize: %s\n", db_strerror(ret));
77 dbenv->close(dbenv, 0);
78 printf("db: exit code %d\n", ret);
82 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
83 printf("db: set_lk_detect: %s\n", db_strerror(ret));
84 dbenv->close(dbenv, 0);
85 printf("db: exit code %d\n", ret);
89 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_TXN | DB_INIT_LOCK | DB_THREAD | DB_INIT_LOG;
90 printf("db: dbenv->open(dbenv, %s, %d, 0)\n", ctdl_db_dir, flags);
91 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try opening the database cleanly
92 if (ret == DB_RUNRECOVERY) {
93 printf("db: dbenv->open: %s\n", db_strerror(ret));
94 printf("db: attempting recovery...\n");
96 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try recovery
98 if (ret == DB_RUNRECOVERY) {
99 printf("db: dbenv->open: %s\n", db_strerror(ret));
100 printf("db: attempting catastrophic recovery...\n");
101 flags &= ~DB_RECOVER;
102 flags |= DB_RECOVER_FATAL;
103 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0); // try catastrophic recovery
106 printf("db: dbenv->open: %s\n", db_strerror(ret));
107 dbenv->close(dbenv, 0);
108 printf("db: exit code %d\n", ret);
112 for (i = 0; i < MAXCDB; ++i) {
113 printf("db: mounting database %d\n", i);
114 ret = db_create(&dbp[i], dbenv, 0); // Create a database handle
116 printf("db: db_create: %s\n", db_strerror(ret));
117 printf("db: exit code %d\n", ret);
121 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", i); // table names by number
122 ret = dbp[i]->open(dbp[i], NULL, dbfilename, NULL, DB_BTREE, DB_AUTO_COMMIT, 0600);
124 printf("db: db_open[%02x]: %s\n", i, db_strerror(ret));
126 printf("db: You may need to tune your database; please check http://www.citadel.org for more information.\n");
128 printf("db: exit code %d\n", ret);
135 // Close all of the db database files we've opened. This can be done in a loop, since it's just a bunch of closes.
136 void close_databases(void) {
140 //syslog(LOG_INFO, "db: performing final checkpoint");
141 //if ((ret = dbenv->txn_checkpoint(dbenv, 0, 0, 0))) {
142 //syslog(LOG_ERR, "db: txn_checkpoint: %s", db_strerror(ret));
145 printf("db: flushing the database logs\n");
146 if ((ret = dbenv->log_flush(dbenv, NULL))) {
147 printf("db: log_flush: %s\n", db_strerror(ret));
151 printf("db: closing databases\n");
152 for (i = 0; i < MAXCDB; ++i) {
153 printf("db: closing database %02x\n", i);
154 ret = dbp[i]->close(dbp[i], 0);
156 printf("db: db_close: %s\n", db_strerror(ret));
162 ret = dbenv->close(dbenv, 0);
164 printf("db: DBENV->close: %s\n", db_strerror(ret));
169 void null_function(void) {
170 printf("FIXME null_function() called which means we have more work to do!\n");
174 void (*convert_functions[])(void) = {
175 null_function, // CDB_MSGMAIN
176 null_function, // CDB_USERS
177 null_function, // CDB_ROOMS
178 null_function, // CDB_FLOORTAB
179 null_function, // CDB_MSGLISTS
180 null_function, // CDB_VISIT
181 null_function, // CDB_DIRECTORY
182 null_function, // CDB_USETABLE
183 null_function, // CDB_BIGMSGS
184 null_function, // CDB_FULLTEXT
185 null_function, // CDB_EUIDINDEX
186 null_function, // CDB_USERSBYNUMBER
187 null_function, // CDB_EXTAUTH
188 null_function // CDB_CONFIG
192 void convert_table(int which_cdb) {
193 printf("Converting table %d\n", which_cdb);
194 convert_functions[which_cdb]();
199 int main(int argc, char **argv) {
200 char ctdldir[PATH_MAX]=CTDLDIR;
202 // Check to make sure we're running on the target 64-bit system
203 if (sizeof(void *) != 8) {
204 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
205 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
209 // Parse command line
211 while ((a = getopt(argc, argv, "h:")) != EOF) {
214 strncpy(ctdldir, optarg, sizeof ctdldir);
217 fprintf(stderr, "%s: usage: %s [-h server_dir]\n", argv[0], argv[0]);
223 for (int i = 0; i < MAXCDB; ++i) {