]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdl3264.c
8161ab725db083f6c617d75550383dd547f31af4
[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 the citadel.org team
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 *dbp[MAXCDB];         // One DB handle for each Citadel database
33 static DB_ENV *dbenv;           // The DB environment (global)
34
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) {
40         int ret;
41         int i;
42         char dbfilename[32];
43         u_int32_t flags = 0;
44         int dbversion_major, dbversion_minor, dbversion_patch;
45
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",
50                 zlibVersion(),
51                 DB_VERSION_STRING,
52                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
53         );
54
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.");
62                 exit(CTDLEXIT_DB);
63         }
64
65         printf("db: Setting up DB environment\n");
66         ret = db_env_create(&dbenv, 0);
67         if (ret) {
68                 printf("db: db_env_create: %s\n", db_strerror(ret));
69                 printf("db: exit code %d\n", ret);
70                 exit(CTDLEXIT_DB);
71         }
72
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);
75         if (ret) {
76                 printf("db: set_cachesize: %s\n", db_strerror(ret));
77                 dbenv->close(dbenv, 0);
78                 printf("db: exit code %d\n", ret);
79                 exit(CTDLEXIT_DB);
80         }
81
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);
86                 exit(CTDLEXIT_DB);
87         }
88
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");
95                 flags |= DB_RECOVER;
96                 ret = dbenv->open(dbenv, ctdl_db_dir, flags, 0);                        // try recovery
97         }
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
104         }
105         if (ret) {
106                 printf("db: dbenv->open: %s\n", db_strerror(ret));
107                 dbenv->close(dbenv, 0);
108                 printf("db: exit code %d\n", ret);
109                 exit(CTDLEXIT_DB);
110         }
111
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
115                 if (ret) {
116                         printf("db: db_create: %s\n", db_strerror(ret));
117                         printf("db: exit code %d\n", ret);
118                         exit(CTDLEXIT_DB);
119                 }
120
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);
123                 if (ret) {
124                         printf("db: db_open[%02x]: %s\n", i, db_strerror(ret));
125                         if (ret == ENOMEM) {
126                                 printf("db: You may need to tune your database; please check http://www.citadel.org for more information.\n");
127                         }
128                         printf("db: exit code %d\n", ret);
129                         exit(CTDLEXIT_DB);
130                 }
131         }
132 }
133
134
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) {
137         int i;
138         int ret;
139
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));
143         //}
144
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));
148         }
149
150         // close the tables
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);
155                 if (ret) {
156                         printf("db: db_close: %s\n", db_strerror(ret));
157                 }
158
159         }
160
161         // Close the handle.
162         ret = dbenv->close(dbenv, 0);
163         if (ret) {
164                 printf("db: DBENV->close: %s\n", db_strerror(ret));
165         }
166 }
167
168
169 void null_function(void) {
170         printf("FIXME null_function() called which means we have more work to do!\n");
171 }
172
173
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
189 };
190
191
192 void convert_table(int which_cdb) {
193         printf("Converting table %d\n", which_cdb);
194         convert_functions[which_cdb]();
195
196 }
197
198
199 int main(int argc, char **argv) {
200         char ctdldir[PATH_MAX]=CTDLDIR;
201
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]);
206                 exit(1);
207         }
208
209         // Parse command line
210         int a;
211         while ((a = getopt(argc, argv, "h:")) != EOF) {
212                 switch (a) {
213                 case 'h':
214                         strncpy(ctdldir, optarg, sizeof ctdldir);
215                         break;
216                 default:
217                         fprintf(stderr, "%s: usage: %s [-h server_dir]\n", argv[0], argv[0]);
218                         exit(2);
219                 }
220         }
221
222         open_databases();
223         for (int i = 0; i < MAXCDB; ++i) {
224                 convert_table(i);
225         }
226         close_databases();
227
228         exit(0);
229 }