diff --git a/src/db_dump/db_dump.c b/src/db_dump/db_dump.c index 3228a819..660cc4b2 100644 --- a/src/db_dump/db_dump.c +++ b/src/db_dump/db_dump.c @@ -29,6 +29,7 @@ Contributors: #include static uint32_t db_version; +static int stats = 0; static int _db_client_chunk_restore(struct mosquitto_db *db, FILE *db_fd) { @@ -51,17 +52,17 @@ static int _db_client_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } read_e(db_fd, client_id, slen); - printf("\tClient ID: %s\n", client_id); + if(!stats) printf("\tClient ID: %s\n", client_id); read_e(db_fd, &i16temp, sizeof(uint16_t)); last_mid = ntohs(i16temp); - printf("\tLast MID: %d\n", last_mid); + if(!stats) printf("\tLast MID: %d\n", last_mid); if(db_version == 2){ disconnect_t = time(NULL); }else{ read_e(db_fd, &disconnect_t, sizeof(time_t)); - printf("\tDisconnect time: %ld\n", disconnect_t); + if(!stats) printf("\tDisconnect time: %ld\n", disconnect_t); } free(client_id); @@ -95,26 +96,26 @@ static int _db_client_msg_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } read_e(db_fd, client_id, slen); - printf("\tClient ID: %s\n", client_id); + if(!stats) printf("\tClient ID: %s\n", client_id); read_e(db_fd, &i64temp, sizeof(dbid_t)); store_id = i64temp; - printf("\tStore ID: %ld\n", (long )store_id); + if(!stats) printf("\tStore ID: %ld\n", (long )store_id); read_e(db_fd, &i16temp, sizeof(uint16_t)); mid = ntohs(i16temp); - printf("\tMID: %d\n", mid); + if(!stats) printf("\tMID: %d\n", mid); read_e(db_fd, &qos, sizeof(uint8_t)); - printf("\tQoS: %d\n", qos); + if(!stats) printf("\tQoS: %d\n", qos); read_e(db_fd, &retain, sizeof(uint8_t)); - printf("\tRetain: %d\n", retain); + if(!stats) printf("\tRetain: %d\n", retain); read_e(db_fd, &direction, sizeof(uint8_t)); - printf("\tDirection: %d\n", direction); + if(!stats) printf("\tDirection: %d\n", direction); read_e(db_fd, &state, sizeof(uint8_t)); - printf("\tState: %d\n", state); + if(!stats) printf("\tState: %d\n", state); read_e(db_fd, &dup, sizeof(uint8_t)); - printf("\tDup: %d\n", dup); + if(!stats) printf("\tDup: %d\n", dup); free(client_id); @@ -140,7 +141,7 @@ static int _db_msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fd) read_e(db_fd, &i64temp, sizeof(dbid_t)); store_id = i64temp; - printf("\tStore ID: %ld\n", (long)store_id); + if(!stats) printf("\tStore ID: %ld\n", (long)store_id); read_e(db_fd, &i16temp, sizeof(uint16_t)); slen = ntohs(i16temp); @@ -157,16 +158,16 @@ static int _db_msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fd) free(source_id); return 1; } - printf("\tSource ID: %s\n", source_id); + if(!stats) printf("\tSource ID: %s\n", source_id); free(source_id); } read_e(db_fd, &i16temp, sizeof(uint16_t)); source_mid = ntohs(i16temp); - printf("\tSource MID: %d\n", source_mid); + if(!stats) printf("\tSource MID: %d\n", source_mid); read_e(db_fd, &i16temp, sizeof(uint16_t)); mid = ntohs(i16temp); - printf("\tMID: %d\n", mid); + if(!stats) printf("\tMID: %d\n", mid); read_e(db_fd, &i16temp, sizeof(uint16_t)); slen = ntohs(i16temp); @@ -185,7 +186,7 @@ static int _db_msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fd) free(topic); return 1; } - printf("\tTopic: %s\n", topic); + if(!stats) printf("\tTopic: %s\n", topic); free(topic); }else{ fprintf(stderr, "Error: Invalid msg_store chunk when restoring persistent database."); @@ -194,13 +195,13 @@ static int _db_msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } read_e(db_fd, &qos, sizeof(uint8_t)); - printf("\tQoS: %d\n", qos); + if(!stats) printf("\tQoS: %d\n", qos); read_e(db_fd, &retain, sizeof(uint8_t)); - printf("\tRetain: %d\n", retain); + if(!stats) printf("\tRetain: %d\n", retain); read_e(db_fd, &i32temp, sizeof(uint32_t)); payloadlen = ntohl(i32temp); - printf("\tPayload Length: %d\n", payloadlen); + if(!stats) printf("\tPayload Length: %d\n", payloadlen); if(payloadlen){ payload = malloc(payloadlen+1); @@ -225,7 +226,7 @@ static int _db_msg_store_chunk_restore(struct mosquitto_db *db, FILE *db_fd) if(payload[i] == 0) binary = true; } if(binary == false && payloadlen<256){ - printf("\tPayload: %s\n", payload); + if(!stats) printf("\tPayload: %s\n", payload); } free(payload); } @@ -249,7 +250,7 @@ static int _db_retain_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } store_id = i64temp; - printf("\tStore ID: %ld\n", (long int)store_id); + if(!stats) printf("\tStore ID: %ld\n", (long int)store_id); return 0; } @@ -270,7 +271,7 @@ static int _db_sub_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } read_e(db_fd, client_id, slen); - printf("\tClient ID: %s\n", client_id); + if(!stats) printf("\tClient ID: %s\n", client_id); read_e(db_fd, &i16temp, sizeof(uint16_t)); slen = ntohs(i16temp); topic = calloc(slen+1, sizeof(char)); @@ -281,9 +282,9 @@ static int _db_sub_chunk_restore(struct mosquitto_db *db, FILE *db_fd) return 1; } read_e(db_fd, topic, slen); - printf("\tTopic: %s\n", topic); + if(!stats) printf("\tTopic: %s\n", topic); read_e(db_fd, &qos, sizeof(uint8_t)); - printf("\tQoS: %d\n", qos); + if(!stats) printf("\tQoS: %d\n", qos); free(client_id); free(topic); @@ -306,23 +307,35 @@ int main(int argc, char *argv[]) uint8_t i8temp; ssize_t rlen; struct mosquitto_db db; - - if(argc != 2){ - fprintf(stderr, "Usage: db_dump \n"); + char *filename; + long cfg_count = 0; + long msg_store_count = 0; + long client_msg_count = 0; + long retain_count = 0; + long sub_count = 0; + long client_count = 0; + + if(argc == 2){ + filename = argv[1]; + }else if(argc == 3 && !strcmp(argv[1], "--stats")){ + stats = 1; + filename = argv[2]; + }else{ + fprintf(stderr, "Usage: db_dump [--stats] \n"); return 1; } memset(&db, 0, sizeof(struct mosquitto_db)); - fd = fopen(argv[1], "rb"); + fd = fopen(filename, "rb"); if(!fd) return 0; read_e(fd, &header, 15); if(!memcmp(header, magic, 15)){ - printf("Mosquitto DB dump\n"); + if(!stats) printf("Mosquitto DB dump\n"); // Restore DB as normal read_e(fd, &crc, sizeof(uint32_t)); - printf("CRC: %d\n", crc); + if(!stats) printf("CRC: %d\n", crc); read_e(fd, &i32temp, sizeof(uint32_t)); db_version = ntohl(i32temp); - printf("DB version: %d\n", db_version); + if(!stats) printf("DB version: %d\n", db_version); while(rlen = fread(&i16temp, sizeof(uint16_t), 1, fd), rlen == 1){ chunk = ntohs(i16temp); @@ -330,12 +343,13 @@ int main(int argc, char *argv[]) length = ntohl(i32temp); switch(chunk){ case DB_CHUNK_CFG: - printf("DB_CHUNK_CFG:\n"); - printf("\tLength: %d\n", length); + cfg_count++; + if(!stats) printf("DB_CHUNK_CFG:\n"); + if(!stats) printf("\tLength: %d\n", length); read_e(fd, &i8temp, sizeof(uint8_t)); // shutdown - printf("\tShutdown: %d\n", i8temp); + if(!stats) printf("\tShutdown: %d\n", i8temp); read_e(fd, &i8temp, sizeof(uint8_t)); // sizeof(dbid_t) - printf("\tDB ID size: %d\n", i8temp); + if(!stats) printf("\tDB ID size: %d\n", i8temp); if(i8temp != sizeof(dbid_t)){ fprintf(stderr, "Error: Incompatible database configuration (dbid size is %d bytes, expected %ld)", i8temp, sizeof(dbid_t)); @@ -343,36 +357,41 @@ int main(int argc, char *argv[]) return 1; } read_e(fd, &i64temp, sizeof(dbid_t)); - printf("\tLast DB ID: %ld\n", (long)i64temp); + if(!stats) printf("\tLast DB ID: %ld\n", (long)i64temp); break; case DB_CHUNK_MSG_STORE: - printf("DB_CHUNK_MSG_STORE:\n"); - printf("\tLength: %d\n", length); + msg_store_count++; + if(!stats) printf("DB_CHUNK_MSG_STORE:\n"); + if(!stats) printf("\tLength: %d\n", length); if(_db_msg_store_chunk_restore(&db, fd)) return 1; break; case DB_CHUNK_CLIENT_MSG: - printf("DB_CHUNK_CLIENT_MSG:\n"); - printf("\tLength: %d\n", length); + client_msg_count++; + if(!stats) printf("DB_CHUNK_CLIENT_MSG:\n"); + if(!stats) printf("\tLength: %d\n", length); if(_db_client_msg_chunk_restore(&db, fd)) return 1; break; case DB_CHUNK_RETAIN: - printf("DB_CHUNK_RETAIN:\n"); - printf("\tLength: %d\n", length); + retain_count++; + if(!stats) printf("DB_CHUNK_RETAIN:\n"); + if(!stats) printf("\tLength: %d\n", length); if(_db_retain_chunk_restore(&db, fd)) return 1; break; case DB_CHUNK_SUB: - printf("DB_CHUNK_SUB:\n"); - printf("\tLength: %d\n", length); + sub_count++; + if(!stats) printf("DB_CHUNK_SUB:\n"); + if(!stats) printf("\tLength: %d\n", length); if(_db_sub_chunk_restore(&db, fd)) return 1; break; case DB_CHUNK_CLIENT: - printf("DB_CHUNK_CLIENT:\n"); - printf("\tLength: %d\n", length); + client_count++; + if(!stats) printf("DB_CHUNK_CLIENT:\n"); + if(!stats) printf("\tLength: %d\n", length); if(_db_client_chunk_restore(&db, fd)) return 1; break; @@ -390,6 +409,14 @@ int main(int argc, char *argv[]) fclose(fd); + if(stats){ + printf("DB_CHUNK_CFG: %ld\n", cfg_count); + printf("DB_CHUNK_MSG_STORE: %ld\n", msg_store_count); + printf("DB_CHUNK_CLIENT_MSG: %ld\n", client_msg_count); + printf("DB_CHUNK_RETAIN: %ld\n", retain_count); + printf("DB_CHUNK_SUB: %ld\n", sub_count); + printf("DB_CHUNK_CLIENT: %ld\n", client_count); + } return rc; error: fprintf(stderr, "Error: %s.", strerror(errno));