// 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);
+ }
}
}
}
}
+ 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");