1 // Don't run this. It doesn't work and if you try to run it you will immediately die.
3 // Copyright (c) 2023 by Art Cancro citadel.org
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
13 #include <sys/types.h>
14 #include <sys/socket.h>
22 #include <libcitadel.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/citadel_dirs.h"
31 // Wrapper for realloc() that crashes and burns if the call fails.
32 void *reallok(void *ptr, size_t size) {
33 void *p = realloc(ptr, size);
35 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
42 // Open a database environment
43 DB_ENV *open_dbenv(char *dirname) {
50 int dbversion_major, dbversion_minor, dbversion_patch;
52 fprintf(stderr, "db: open_dbenv() starting\n"
53 "db: Linked zlib: %s\n"
54 "db: Compiled libdb: %s\n"
55 "db: Linked libdb: %s\n",
58 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
61 // Create synthetic integer version numbers and compare them.
62 // Never run with a libdb older than the one with which it was compiled.
63 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
64 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
65 if (compiled_db_version > linked_db_version) {
66 fprintf(stderr, "db: ctdlload is running with a version of libdb older than the one with which it was compiled.\n"
67 "db: This is an invalid configuration. ctdlload will now exit to prevent data loss.");
71 fprintf(stderr,"db: Setting up DB environment\n");
72 ret = db_env_create(&dbenv, 0);
74 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
75 fprintf(stderr,"db: exit code %d\n", ret);
79 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
80 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
82 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
83 dbenv->close(dbenv, 0);
84 fprintf(stderr,"db: exit code %d\n", ret);
88 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
89 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
90 dbenv->close(dbenv, 0);
91 fprintf(stderr,"db: exit code %d\n", ret);
95 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
96 fprintf(stderr,"db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
97 ret = dbenv->open(dbenv, dirname, flags, 0);
99 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
100 dbenv->close(dbenv, 0);
101 fprintf(stderr,"db: exit code %d\n", ret);
109 void close_dbenv(DB_ENV *dbenv) {
110 fprintf(stderr,"db: closing dbenv\n");
111 int ret = dbenv->close(dbenv, 0);
113 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
118 // Skeleton convert function
119 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
124 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
125 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
127 static char *b64_decoded_msg = NULL;
128 static size_t b64_decoded_alloc = 0;
132 token = strtok(line, "|");
133 msgnum = atol(strtok(NULL, "|"));
134 token = strtok(NULL, "|");
136 // The record key will be the message number
137 out_key->size = sizeof(long);
138 out_key->data = reallok(out_key->data, out_key->size);
139 memcpy(out_key->data, &msgnum, out_key->size);
141 // The record data will be the decoded message text.
142 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
143 out_data->data = reallok(out_data->data, strlen(token));
144 out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
149 // Ingest one line of dump data. NOT REENTRANT
150 void ingest_one(char *line, DB_ENV *dst_dbenv) {
152 static int previous_cdb = -1 ;
153 static int current_cdb = -1 ;
155 char record_type[32];
159 DBT out_key, out_data;
161 // We are assuming that the lines of the dump file will generally be sorted by table.
162 // By remembering the last table we worked with, we can do close/open if the table changes.
164 // Identify the record type we are currently working with
165 extract_token(record_type, line, 0, '|', sizeof record_type);
166 if (!strcasecmp(record_type, "msgtext")) current_cdb = CDB_MSGMAIN;
167 else if (!strcasecmp(record_type, "msgmeta")) current_cdb = CDB_MSGMAIN;
168 else if (!strcasecmp(record_type, "user")) current_cdb = CDB_USERS;
169 else if (!strcasecmp(record_type, "room")) current_cdb = CDB_ROOMS;
170 else if (!strcasecmp(record_type, "floor")) current_cdb = CDB_FLOORTAB;
171 else if (!strcasecmp(record_type, "msglist")) current_cdb = CDB_MSGLISTS;
172 else if (!strcasecmp(record_type, "visit")) current_cdb = CDB_VISIT;
173 else if (!strcasecmp(record_type, "dir")) current_cdb = CDB_DIRECTORY;
174 else if (!strcasecmp(record_type, "use")) current_cdb = CDB_USETABLE;
175 else if (!strcasecmp(record_type, "bigmsg")) current_cdb = CDB_BIGMSGS;
176 else if (!strcasecmp(record_type, "euidindex")) current_cdb = CDB_EUIDINDEX;
177 else if (!strcasecmp(record_type, "usersbynumber")) current_cdb = CDB_USERSBYNUMBER;
178 else if (!strcasecmp(record_type, "config")) current_cdb = CDB_CONFIG;
179 else current_cdb = -1 ;
181 if (current_cdb != previous_cdb) {
182 if (previous_cdb >= 0) {
183 fprintf(stderr, "Close %d\n", previous_cdb);
184 ret = dst_dbp->close(dst_dbp, 0);
186 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
190 if (current_cdb >= 0) {
191 fprintf(stderr, " Open %d\n", current_cdb);
192 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
194 // create a database handle for the destination table
195 ret = db_create(&dst_dbp, dst_dbenv, 0);
197 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
198 fprintf(stderr, "db: exit code %d\n", ret);
202 // open the file containing the destination table
203 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
205 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
206 fprintf(stderr, "db: exit code %d\n", ret);
211 previous_cdb = current_cdb;
214 // If we have a valid record type and a target database open, dispatch the correct record type handler.
215 memset(&out_key, 0, sizeof(DBT));
216 memset(&out_data, 0, sizeof(DBT));
218 if (!strcasecmp(record_type, "msgtext")) row_was_good = convert_msgtext(line, &out_key, &out_data);
219 else if (!strcasecmp(record_type, "msgmeta")) row_was_good = convert_foo(line, &out_key, &out_data);
220 else if (!strcasecmp(record_type, "user")) row_was_good = convert_foo(line, &out_key, &out_data);
221 else if (!strcasecmp(record_type, "room")) row_was_good = convert_foo(line, &out_key, &out_data);
222 else if (!strcasecmp(record_type, "floor")) row_was_good = convert_foo(line, &out_key, &out_data);
223 else if (!strcasecmp(record_type, "msglist")) row_was_good = convert_foo(line, &out_key, &out_data);
224 else if (!strcasecmp(record_type, "visit")) row_was_good = convert_foo(line, &out_key, &out_data);
225 else if (!strcasecmp(record_type, "dir")) row_was_good = convert_foo(line, &out_key, &out_data);
226 else if (!strcasecmp(record_type, "use")) row_was_good = convert_foo(line, &out_key, &out_data);
227 else if (!strcasecmp(record_type, "bigmsg")) row_was_good = convert_foo(line, &out_key, &out_data);
228 else if (!strcasecmp(record_type, "euidindex")) row_was_good = convert_foo(line, &out_key, &out_data);
229 else if (!strcasecmp(record_type, "usersbynumber")) row_was_good = convert_foo(line, &out_key, &out_data);
230 else if (!strcasecmp(record_type, "config")) row_was_good = convert_foo(line, &out_key, &out_data);
231 else row_was_good = 0;
234 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
236 fprintf(stderr, "db: cdb_put(%d): %s", current_cdb, db_strerror(ret));
246 // This is the loop that loads the dump data. NOT REENTRANT
247 void ingest(DB_ENV *dst_dbenv) {
248 static size_t line_alloc = 1;
250 static size_t line_len = 0;
253 line = reallok(NULL, line_alloc);
258 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
259 if ((line_len+2) > line_alloc) {
261 line = reallok(line, line_alloc);
263 line[line_len++] = ch;
272 ingest_one(line, dst_dbenv);
280 int main(int argc, char **argv) {
281 char *dst_dir = NULL;
283 static DB_ENV *dst_dbenv; // Source DB environment (global)
285 // Parse command line
287 while ((a = getopt(argc, argv, "h:y")) != EOF) {
296 fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
301 if (confirmed == 1) {
302 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
305 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
310 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
313 dst_dbenv = open_dbenv(dst_dir);
315 close_dbenv(dst_dbenv);