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 // Ingest one line of dump data. NOT REENTRANT
119 void ingest_one(char *line) {
120 static char last_cdb[16] = "";
123 // We are assuming that the lines of the dump file will generally be sorted by table.
124 // By remembering the last table we worked with, we can do close/open if the table changes.
126 extract_token(this_cdb, line, 0, '|', sizeof this_cdb);
127 if (strcmp(this_cdb, last_cdb)) {
129 printf("Close %s\n", last_cdb);
130 printf(" Open %s\n", this_cdb);
132 strcpy(last_cdb, this_cdb);
138 // This is the loop that loads the dump data. NOT REENTRANT
140 static size_t line_alloc = 1;
142 static size_t line_len = 0;
145 line = reallok(NULL, line_alloc);
150 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
151 if ((line_len+2) > line_alloc) {
153 line = reallok(line, line_alloc);
155 line[line_len++] = ch;
172 int main(int argc, char **argv) {
173 char *dst_dir = NULL;
175 static DB_ENV *dst_dbenv; // Source DB environment (global)
177 // Parse command line
179 while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
188 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
193 if (confirmed == 1) {
194 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
197 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
202 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
205 dst_dbenv = open_dbenv(dst_dir);
207 close_dbenv(dst_dbenv);