ctdldump/ctdlload: more testing
authorArt Cancro <ajc@citadel.org>
Tue, 25 Jul 2023 23:08:20 +0000 (14:08 -0900)
committerArt Cancro <ajc@citadel.org>
Tue, 25 Jul 2023 23:08:20 +0000 (14:08 -0900)
citadel/utils/ctdldump.c
citadel/utils/ctdlload.c

index b518de4c83de55ec232b8c7ce78bf9ae2d5d247a..f340509b35045ed86bac81d156e70f5ea1fe2768 100644 (file)
@@ -425,60 +425,60 @@ void export_table(int which_cdb, DB_ENV *src_dbenv) {
        // open the file containing the source table
        ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
        if (ret) {
-               fprintf(stderr, "ctdldump: db_open(%s): %s\n", dbfilename, db_strerror(ret));
-               fprintf(stderr, "ctdldump: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
+               fprintf(stderr, "ctdldump: db_open(%s): %s (skipping empty or missing table)\n", dbfilename, db_strerror(ret));
        }
+       else {
 
-       // Acquire a cursor to read the source table
-       if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
-               fprintf(stderr, "ctdldump: db_cursor: %s\n", db_strerror(ret));
-               fprintf(stderr, "ctdldump: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
-       }
-
-       // Zero out these database keys
-       memset(&in_key,         0, sizeof(DBT));        // input
-       memset(&in_data,        0, sizeof(DBT));
-       memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
-
-       // Walk through the database, calling export functions as we go and clearing buffers before each call.
-       while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
-       
-               // If either the key or data are zero length, skip this record
-               if ((in_key.size == 0) || (in_data.size == 0)) {
-                       ++num_bad_rows;
+               // Acquire a cursor to read the source table
+               if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
+                       fprintf(stderr, "ctdldump: db_cursor: %s\n", db_strerror(ret));
+                       fprintf(stderr, "ctdldump: exit code %d\n", ret);
+                       exit(CTDLEXIT_DB);
                }
-
-               else {  // Both key and data are >0 length so we're good to go
-
-                       // Do we need to decompress?
-                       static int32_t magic = COMPRESS_MAGIC;
-                       compressed = 0;
-                       if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
        
-                               // yes, we need to decompress
-                               compressed = 1;
-                               struct CtdlCompressHeader comp;
-                               memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
-                               uncomp_data.size = comp.uncompressed_len;
-                               uncomp_data.data = reallok(uncomp_data.data, uncomp_data.size);
-                               destLen = (uLongf)comp.uncompressed_len;
+               // Zero out these database keys
+               memset(&in_key,         0, sizeof(DBT));        // input
+               memset(&in_data,        0, sizeof(DBT));
+               memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
        
-                               ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
-                                               (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
-                                               (uLong)comp.compressed_len);
-                               if (ret != Z_OK) {
-                                       fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
-                                       exit(CTDLEXIT_DB);
-                               }
+               // Walk through the database, calling export functions as we go and clearing buffers before each call.
+               while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
+               
+                       // If either the key or data are zero length, skip this record
+                       if ((in_key.size == 0) || (in_data.size == 0)) {
+                               ++num_bad_rows;
                        }
        
-                       // Call the export function registered to this table
-                       export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
+                       else {  // Both key and data are >0 length so we're good to go
        
-                       // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
-                       fflush(stdout);
+                               // Do we need to decompress?
+                               static int32_t magic = COMPRESS_MAGIC;
+                               compressed = 0;
+                               if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
+               
+                                       // yes, we need to decompress
+                                       compressed = 1;
+                                       struct CtdlCompressHeader comp;
+                                       memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
+                                       uncomp_data.size = comp.uncompressed_len;
+                                       uncomp_data.data = reallok(uncomp_data.data, uncomp_data.size);
+                                       destLen = (uLongf)comp.uncompressed_len;
+               
+                                       ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
+                                                       (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
+                                                       (uLong)comp.compressed_len);
+                                       if (ret != Z_OK) {
+                                               fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
+                                               exit(CTDLEXIT_DB);
+                                       }
+                               }
+               
+                               // Call the export function registered to this table
+                               export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
+               
+                               // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
+                               fflush(stdout);
+                       }
                }
        }
 
@@ -526,20 +526,25 @@ int main(int argc, char **argv) {
                }
        }
 
+       if (src_dir == NULL) {
+               fprintf(stderr, "ctdldump: no source directory was specified.\n");
+               exit(1);
+       }
+
        if (confirmed == 1) {
                fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
        }
        else {
-               fprintf(stderr, "ctdldump: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
-               exit(0);
+               fprintf(stderr, "ctdldump: usage: ctdldump -y -h[data_dir] >[dump_file]\n");
+               fprintf(stderr, "          [data_dir] is your database directory, usually /usr/local/citadel/data\n");
+               fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
+               exit(1);
        }
 
        src_dbenv = open_dbenv(src_dir);
        printf("begin|\n");
        for (i = 0; i < MAXCDB; ++i) {
-               if (i != CDB_UNUSED1) {                 // nothing in this slot so don't complain if it's missing
-                       export_table(i, src_dbenv);
-               }
+               export_table(i, src_dbenv);
        }
        close_dbenv(src_dbenv);
        printf("end|\n");
index cf651a98fcf84568eea304d630abb27a41414c13..5c660dab7e6b2d0d1f5d198b6bf438cc266f3db4 100644 (file)
@@ -466,8 +466,11 @@ int import_dir(char *line, DBT *out_key, DBT *out_data) {
 
        out_key->size = strlen(dirkey);
        out_key->data = reallok(NULL, out_key->size);
+       memcpy(out_key->data, dirkey, strlen(dirkey));
+
        out_data->size = strlen(username) + 1;
        out_data->data = strdup(username);
+
        return(1);
 }
 
@@ -847,12 +850,19 @@ int main(int argc, char **argv) {
                }
        }
 
+       if (dst_dir == NULL) {
+               fprintf(stderr, "ctdlload: no destination directory was specified.\n");
+               exit(1);
+       }
+
        if (confirmed == 1) {
                fprintf(stderr, "ctdlload: You have specified the [-y] flag, so processing will continue.\n");
        }
        else {
-               fprintf(stderr, "ctdlload: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
-               exit(0);
+               fprintf(stderr, "ctdlload: usage: ctdlload -y -h[data_dir] <[dump_file]\n");
+               fprintf(stderr, "          [data_dir] is your database directory, usually /usr/local/citadel/data\n");
+               fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
+               exit(1);
        }
 
        // Remove any database that is already in the target directory (yes, delete it, be careful)