- 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;
- }
-
- 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;
-
- 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);
- }
- }
- }
-
- // free any leftover out_data pointers
- free(uncomp_data.data);
-
- // ...and close the database (table)
- ret = src_dbp->close(src_dbp, 0);
- if (ret) {
- fprintf(stderr, "ctdldump: db_close: %s\n", db_strerror(ret));
- }
-