]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
ctdlload: skeleton code for table ingestion
[citadel.git] / citadel / utils / ctdlload.c
1 // Don't run this.  It doesn't work and if you try to run it you will immediately die.
2 //
3 // Copyright (c) 2023 by Art Cancro citadel.org
4 //
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.
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <limits.h>
22 #include <libcitadel.h>
23 #include <zlib.h>
24 #include <db.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/citadel_dirs.h"
29
30
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);
34         if (!p) {
35                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
36                 exit(1);
37         }
38         return p;
39 }
40
41
42 // Open a database environment
43 DB_ENV *open_dbenv(char *dirname) {
44
45         DB_ENV *dbenv = NULL;
46
47         int ret;
48         int i;
49         u_int32_t flags = 0;
50         int dbversion_major, dbversion_minor, dbversion_patch;
51
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",
56                 zlibVersion(),
57                 DB_VERSION_STRING,
58                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
59         );
60
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.");
68                 exit(CTDLEXIT_DB);
69         }
70
71         fprintf(stderr,"db: Setting up DB environment\n");
72         ret = db_env_create(&dbenv, 0);
73         if (ret) {
74                 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
75                 fprintf(stderr,"db: exit code %d\n", ret);
76                 exit(CTDLEXIT_DB);
77         }
78
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);
81         if (ret) {
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);
85                 exit(CTDLEXIT_DB);
86         }
87
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);
92                 exit(CTDLEXIT_DB);
93         }
94
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);
98         if (ret) {
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);
102                 exit(CTDLEXIT_DB);
103         }
104
105         return(dbenv);
106 }
107
108
109 void close_dbenv(DB_ENV *dbenv) {
110         fprintf(stderr,"db: closing dbenv\n");
111         int ret = dbenv->close(dbenv, 0);
112         if (ret) {
113                 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
114         }
115 }
116
117
118 // Ingest one line of dump data.  NOT REENTRANT
119 void ingest_one(char *line, DB_ENV *dst_dbenv) {
120
121         static int previous_cdb = -1 ;
122         static int current_cdb = -1 ;
123         static DB *dst_dbp;
124         char record_type[32];
125         int ret;
126         char dbfilename[32];
127
128         // We are assuming that the lines of the dump file will generally be sorted by table.
129         // By remembering the last table we worked with, we can do close/open if the table changes.
130
131         extract_token(record_type, line, 0, '|', sizeof record_type);
132
133         if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
134         else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
135         else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
136         else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
137         else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
138         else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
139         else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
140         else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
141         else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
142         else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
143         else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
144         else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
145         else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
146         else                                                    current_cdb = -1 ;
147
148
149         if (current_cdb != previous_cdb) {
150
151                 if (previous_cdb >= 0) {
152                         fprintf(stderr, "Close %d\n", previous_cdb);
153                         ret = dst_dbp->close(dst_dbp, 0);
154                         if (ret) {
155                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
156                         }
157                 }
158
159                 if (current_cdb >= 0) {
160                         fprintf(stderr, " Open %d\n", current_cdb);
161                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
162
163                         // create a database handle for the destination table
164                         ret = db_create(&dst_dbp, dst_dbenv, 0);
165                         if (ret) {
166                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
167                                 fprintf(stderr, "db: exit code %d\n", ret);
168                                 exit(CTDLEXIT_DB);
169                         }
170                 
171                         // open the file containing the destination table
172                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
173                         if (ret) {
174                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
175                                 fprintf(stderr, "db: exit code %d\n", ret);
176                                 exit(CTDLEXIT_DB);
177                         }
178                 }
179
180
181                 previous_cdb = current_cdb;
182         }
183
184 }
185
186
187 // This is the loop that loads the dump data.  NOT REENTRANT
188 void ingest(DB_ENV *dst_dbenv) {
189         static size_t line_alloc = 1;
190         static char *line;
191         static size_t line_len = 0;
192         char ch;
193
194         line = reallok(NULL, line_alloc);
195
196         do {
197                 line_len = 0;
198
199                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
200                         if ((line_len+2) > line_alloc) {
201                                 line_alloc *= 2;
202                                 line = reallok(line, line_alloc);
203                         }
204                         line[line_len++] = ch;
205                         line[line_len] = 0;
206                 }
207         
208                 if (ch <= 0) {
209                         return;
210                 }
211
212                 if (line_len > 0) {
213                         ingest_one(line, dst_dbenv);
214                 }
215
216         } while (ch >= 0);
217 }
218
219
220 // Main entry point
221 int main(int argc, char **argv) {
222         char *dst_dir = NULL;
223         int confirmed = 0;
224         static DB_ENV *dst_dbenv;               // Source DB environment (global)
225
226         // Parse command line
227         int a;
228         while ((a = getopt(argc, argv, "h:y")) != EOF) {
229                 switch (a) {
230                 case 'h':
231                         dst_dir = optarg;
232                         break;
233                 case 'y':
234                         confirmed = 1;
235                         break;
236                 default:
237                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
238                         exit(2);
239                 }
240         }
241
242         if (confirmed == 1) {
243                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
244         }
245         else {
246                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
247                 exit(0);
248         }
249
250         char cmd[1024];
251         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
252         system(cmd);
253
254         dst_dbenv = open_dbenv(dst_dir);
255         ingest(dst_dbenv);
256         close_dbenv(dst_dbenv);
257
258         exit(0);
259 }