ctdlload: load msgtext
authorArt Cancro <ajc@citadel.org>
Fri, 14 Jul 2023 03:09:31 +0000 (18:09 -0900)
committerArt Cancro <ajc@citadel.org>
Fri, 14 Jul 2023 03:09:31 +0000 (18:09 -0900)
citadel/utils/ctdlload.c

index 430486152a15b3e67984094efa45c2476ded39de..d96d234f4878ff04fcdb921b662751132aecd8f8 100644 (file)
@@ -115,6 +115,37 @@ void close_dbenv(DB_ENV *dbenv) {
 }
 
 
+// Skeleton convert function
+int convert_foo(char *line, DBT *out_key, DBT *out_data) {
+       return(0);
+}
+
+
+// Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
+int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
+
+       static char *b64_decoded_msg = NULL;
+       static size_t b64_decoded_alloc = 0;
+       long msgnum;
+       char *token;
+
+       token = strtok(line, "|");
+       msgnum = atol(strtok(NULL, "|"));
+       token = strtok(NULL, "|");
+
+       // The record key will be the message number
+       out_key->size = sizeof(long);
+       out_key->data = reallok(out_key->data, out_key->size);
+       memcpy(out_key->data, &msgnum, out_key->size);
+
+       // The record data will be the decoded message text.
+       // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
+       out_data->data = reallok(out_data->data, strlen(token));
+       out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
+       return(1);
+}
+
+
 // Ingest one line of dump data.  NOT REENTRANT
 void ingest_one(char *line, DB_ENV *dst_dbenv) {
 
@@ -124,12 +155,14 @@ void ingest_one(char *line, DB_ENV *dst_dbenv) {
        char record_type[32];
        int ret;
        char dbfilename[32];
+       int row_was_good;
+       DBT out_key, out_data;
 
        // We are assuming that the lines of the dump file will generally be sorted by table.
        // By remembering the last table we worked with, we can do close/open if the table changes.
 
+       // Identify the record type we are currently working with
        extract_token(record_type, line, 0, '|', sizeof record_type);
-
        if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
        else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
        else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
@@ -145,9 +178,7 @@ void ingest_one(char *line, DB_ENV *dst_dbenv) {
        else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
        else                                                    current_cdb = -1 ;
 
-
        if (current_cdb != previous_cdb) {
-
                if (previous_cdb >= 0) {
                        fprintf(stderr, "Close %d\n", previous_cdb);
                        ret = dst_dbp->close(dst_dbp, 0);
@@ -177,10 +208,38 @@ void ingest_one(char *line, DB_ENV *dst_dbenv) {
                        }
                }
 
-
                previous_cdb = current_cdb;
        }
 
+       // If we have a valid record type and a target database open, dispatch the correct record type handler.
+       memset(&out_key, 0, sizeof(DBT));
+       memset(&out_data, 0, sizeof(DBT));
+       row_was_good = 0;
+       if      (!strcasecmp(record_type, "msgtext"))           row_was_good = convert_msgtext(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "msgmeta"))           row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "user"))              row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "room"))              row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "floor"))             row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "msglist"))           row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "visit"))             row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "dir"))               row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "use"))               row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "bigmsg"))            row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "euidindex"))         row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "usersbynumber"))     row_was_good = convert_foo(line, &out_key, &out_data);
+       else if (!strcasecmp(record_type, "config"))            row_was_good = convert_foo(line, &out_key, &out_data);
+       else                                                    row_was_good = 0;
+
+       if (row_was_good) {
+               ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
+               if (ret) {
+                       fprintf(stderr, "db: cdb_put(%d): %s", current_cdb, db_strerror(ret));
+                       exit(CTDLEXIT_DB);
+               }
+       }
+
+       free(out_key.data);
+       free(out_data.data);
 }