Apply patches sent by Harlow Solutions -- for WebCit
[citadel.git] / citadel / utils / ctdl3264.c
index 0bac1ba3a6ddb8f5a03db4badd571ab4d6f96fa3..57ddfcc0e7ce1651c9ec7e705acd5bf502ff1f1a 100644 (file)
 #include "../server/citadel_dirs.h"
 #include "ctdl3264_structs.h"
 
-static DB_ENV *dbenv;          // The DB environment (global)
+// Open a database environment
+DB_ENV *open_dbenv(char *dirname) {
+
+       DB_ENV *dbenv = NULL;
 
-// Open the database environment
-void open_dbenv(char *src_dir) {
        int ret;
        int i;
        u_int32_t flags = 0;
@@ -48,7 +49,7 @@ void open_dbenv(char *src_dir) {
        );
 
        // Create synthetic integer version numbers and compare them.
-       // Never allow citserver to run with a libdb older then the one with which it was compiled.
+       // Never run with a libdb older than the one with which it was compiled.
        int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
        int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
        if (compiled_db_version > linked_db_version) {
@@ -82,31 +83,28 @@ void open_dbenv(char *src_dir) {
        }
 
        flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
-       printf("db: dbenv open(dir=%s, flags=%d)\n", src_dir, flags);
-       ret = dbenv->open(dbenv, src_dir, flags, 0);
+       printf("db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
+       ret = dbenv->open(dbenv, dirname, flags, 0);
        if (ret) {
                printf("db: dbenv->open: %s\n", db_strerror(ret));
                dbenv->close(dbenv, 0);
                printf("db: exit code %d\n", ret);
                exit(CTDLEXIT_DB);
        }
+
+       return(dbenv);
 }
 
 
-void close_dbenv(void) {
+void close_dbenv(DB_ENV *dbenv) {
        printf("db: closing dbenv\n");
        int ret = dbenv->close(dbenv, 0);
        if (ret) {
-               printf("db: DBENV->close: %s\n", db_strerror(ret));
+               printf("db: dbenv->close: %s\n", db_strerror(ret));
        }
 }
 
 
-// placeholder convert function for the data types not yet implemented
-void null_function(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
-}
-
-
 // convert function for a message in msgmain
 void convert_msgmain(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
        int32_t in_msgnum;
@@ -435,7 +433,7 @@ void convert_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, D
        int i;
        char ch;
 
-       printf(" in_key:             ");
+       printf("  in_key:             ");
        for (i=0; i<in_key->size; ++i) {
                ch = 0;
                memcpy(&ch, in_key->data+i, 1);
@@ -443,7 +441,7 @@ void convert_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, D
        }
        printf("\n");
 
-       printf("out_key: ");
+       printf(" out_key: ");
        for (i=0; i<out_key->size; ++i) {
                ch = 0;
                memcpy(&ch, out_key->data+i, 1);
@@ -471,6 +469,63 @@ void convert_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, D
 }
 
 
+// convert users-by-number records
+void convert_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
+
+       // key is a long
+       // and remember ... "long" is int32_t on the source system
+       int32_t in_usernum;
+       long out_usernum;
+       memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
+       out_usernum = (long) in_usernum;
+
+       if (in_key->size != 4) {
+               printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
+               abort();
+       }
+
+       out_key->size = sizeof(out_usernum);
+       out_key->data = realloc(out_key->data, out_key->size);
+       memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
+
+       // value is a string
+       out_data->size = in_data->size;
+       out_data->data = realloc(out_data->data, out_data->size);
+       memcpy(out_data->data, in_data->data, in_data->size);
+
+       printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
+}
+
+
+// convert function for a config record
+void convert_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
+
+       // the key is a string
+       out_key->size = in_key->size;
+       out_key->data = realloc(out_key->data, out_key->size + 1);
+       memcpy(out_key->data, in_key->data, in_key->size);
+       char *k = (char *)out_key->data;
+       k[out_key->size] = 0;
+
+       // the data is a pair of strings
+       out_data->size = in_data->size;
+       out_data->data = realloc(out_data->data, out_data->size + 1);
+       memcpy(out_data->data, in_data->data, in_data->size);
+       char *d = (char *)out_data->data;
+       d[out_data->size] = 0;
+
+       // please excuse my friend, he isn't null terminated
+       printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
+}
+
+
+// For obsolete databases, zero all the output
+void zero_function(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
+       out_key->size = 0;
+       out_data->size = 0;
+}
+
+
 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
        convert_msgmain,        // CDB_MSGMAIN
        convert_users,          // CDB_USERS
@@ -483,48 +538,66 @@ void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_k
        convert_bigmsgs,        // CDB_BIGMSGS
        convert_msglists,       // CDB_FULLTEXT
        convert_euidindex,      // CDB_EUIDINDEX
-       null_function,          // CDB_USERSBYNUMBER
-       null_function,          // CDB_EXTAUTH
-       null_function           // CDB_CONFIG
+       convert_usersbynumber,  // CDB_USERSBYNUMBER
+       zero_function,          // CDB_UNUSED1 (obsolete)
+       convert_config          // CDB_CONFIG
 };
 
 
-void convert_table(int which_cdb) {
+void convert_table(int which_cdb, DB_ENV *src_dbenv, DB_ENV *dst_dbenv) {
        int ret;
        int compressed;
        char dbfilename[32];
        uLongf destLen = 0;
 
        printf("\033[43m\033[K\033[0m\n");
-       printf("\033[43m\033[30m Converting table %d \033[K\033[0m\n", which_cdb);
+       printf("\033[43m\033[30m Converting table %02x \033[K\033[0m\n", which_cdb);
        printf("\033[43m\033[K\033[0m\n");
 
        // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
-       DB *dbp;
-       DBC *dbcp;
+       DB *src_dbp, *dst_dbp;
+       DBC *src_dbcp;
        DBT in_key, in_data, out_key, out_data, uncomp_data, recomp_data;
        int num_rows = 0;
 
-       // create a database handle
-       ret = db_create(&dbp, dbenv, 0);
+       // create a database handle for the source table
+       ret = db_create(&src_dbp, src_dbenv, 0);
        if (ret) {
                printf("db: db_create: %s\n", db_strerror(ret));
                printf("db: exit code %d\n", ret);
                exit(CTDLEXIT_DB);
        }
 
-       // open the file
+       // open the file containing the source table
        snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
-       printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
-       ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
+       printf("\033[33m\033[1mdb: opening source %s\033[0m\n", dbfilename);
+       ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
        if (ret) {
                printf("db: db_open: %s\n", db_strerror(ret));
                printf("db: exit code %d\n", ret);
                exit(CTDLEXIT_DB);
        }
 
-       // Acquire a cursor
-       if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
+       // create a database handle for the destination table
+       ret = db_create(&dst_dbp, dst_dbenv, 0);
+       if (ret) {
+               printf("db: db_create: %s\n", db_strerror(ret));
+               printf("db: exit code %d\n", ret);
+               exit(CTDLEXIT_DB);
+       }
+
+       // open the file containing the destination table
+       snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
+       printf("\033[33m\033[1mdb: opening destination %s\033[0m\n", dbfilename);
+       ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
+       if (ret) {
+               printf("db: db_open: %s\n", db_strerror(ret));
+               printf("db: exit code %d\n", ret);
+               exit(CTDLEXIT_DB);
+       }
+
+       // Acquire a cursor to read the source table
+       if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
                printf("db: db_cursor: %s\n", db_strerror(ret));
                printf("db: exit code %d\n", ret);
                exit(CTDLEXIT_DB);
@@ -539,7 +612,7 @@ void convert_table(int which_cdb) {
        memset(&recomp_data,    0, sizeof(DBT));        // recompressed input (the key doesn't change)
 
        // Walk through the database, calling convert functions as we go and clearing buffers before each call.
-       while (out_key.size = 0, out_data.size = 0, (ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
+       while (out_key.size = 0, out_data.size = 0, (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
 
                // Do we need to decompress?
                static int32_t magic = COMPRESS_MAGIC;
@@ -583,18 +656,22 @@ void convert_table(int which_cdb) {
                        recomp_data.size = destLen;
                }
 
-               // write the ed record to the new database
+               // write the converted record to the target database
                if (out_key.size > 0) {
 
-
-
-                       // NOTE TO SELF: if we compressed the output, write recomp_data instead of out_data
-
+                       // If we compressed the output, write recomp_data instead of out_data
                        if (compressed) {
                                printf("DB: %02x , out_keylen: %-3d , out_datalen: %-10d , dataptr: %012lx \033[31m(compressed)\033[0m\n", which_cdb, (int)out_key.size, (int)recomp_data.size, (long unsigned int)recomp_data.data);
+                               ret = dst_dbp->put(dst_dbp, NULL, &out_key, &recomp_data, 0);
                        }
                        else {
                                printf("DB: %02x , out_keylen: %-3d , out_datalen: %-10d , dataptr: %012lx\n", which_cdb, (int)out_key.size, (int)out_data.size, (long unsigned int)out_data.data);
+                               ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
+                       }
+
+                       if (ret) {
+                               printf("db: cdb_put(%d): %s", which_cdb, db_strerror(ret));
+                               exit(CTDLEXIT_DB);
                        }
 
                        // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
@@ -618,13 +695,13 @@ void convert_table(int which_cdb) {
 
        // Flush the logs...
        //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
-       //if ((ret = dbenv->log_flush(dbenv, NULL))) {
+       //if ((ret = src_dbenv->log_flush(src_dbenv, NULL))) {
                //printf("db: log_flush: %s\n", db_strerror(ret));
        //}
 
        // ...and close the database (table)
        printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
-       ret = dbp->close(dbp, 0);
+       ret = src_dbp->close(src_dbp, 0);
        if (ret) {
                printf("db: db_close: %s\n", db_strerror(ret));
        }
@@ -634,7 +711,10 @@ void convert_table(int which_cdb) {
 
 int main(int argc, char **argv) {
        char *src_dir = NULL;
+       char *dst_dir = NULL;
        int confirmed = 0;
+       static DB_ENV *src_dbenv;               // Source DB environment (global)
+       static DB_ENV *dst_dbenv;               // Source DB environment (global)
 
        // Check to make sure we're running on the target 64-bit system
        if (sizeof(void *) != 8) {
@@ -650,6 +730,9 @@ int main(int argc, char **argv) {
                case 's':
                        src_dir = optarg;
                        break;
+               case 'd':
+                       dst_dir = optarg;
+                       break;
                case 'y':
                        confirmed = 1;
                        break;
@@ -670,7 +753,7 @@ int main(int argc, char **argv) {
        printf("should be empty and will receive your 64-bit database.                  \n");
        printf("------------------------------------------------------------------------\n");
        printf("     Source 32-bit directory: %s\n", src_dir);
-       printf("Destination 64-bit directory: %s\n", "FIXME");
+       printf("Destination 64-bit directory: %s\n", dst_dir);
        printf("------------------------------------------------------------------------\n");
 
        if (confirmed == 1) {
@@ -681,11 +764,13 @@ int main(int argc, char **argv) {
                exit(0);
        }
 
-       open_dbenv(src_dir);
+       src_dbenv = open_dbenv(src_dir);
+       dst_dbenv = open_dbenv(dst_dir);
        for (int i = 0; i < MAXCDB; ++i) {
-               convert_table(i);
+               convert_table(i, src_dbenv, dst_dbenv);
        }
-       close_dbenv();
+       close_dbenv(src_dbenv);
+       close_dbenv(dst_dbenv);
 
        exit(0);
 }