review comments, init partly moved to restore, restore setting enabled, null check on filepath

pull/6932/head
paul-szczepanek-arm 2018-05-15 10:24:59 +01:00
parent 579cb5e222
commit f8244a3d87
1 changed files with 59 additions and 45 deletions

View File

@ -78,21 +78,10 @@ FileSecurityDb::FileSecurityDb(FILE *db_file)
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
/* restore if requested */
bool restore;
if ((fread(&restore, sizeof(bool), 1, _db_file) == 1) && restore) {
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
fseek(_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
fread(&_local_csrk, sizeof(_local_csrk), 1, _db_file);
fseek(_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
fread(&_local_sign_counter, sizeof(_local_sign_counter), 1, _db_file);
fseek(_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
/* we read the entries partially and fill the offsets ourselves*/
for (size_t i = 0; i < get_entry_count(); i++) {
fread(&_entries[i], DB_SIZE_ENTRY, 1, _db_file);
bool restore_toggle;
if (fread(&restore_toggle, sizeof(bool), 1, _db_file) == 1) {
if (restore_toggle) {
restore();
}
}
@ -107,8 +96,16 @@ FileSecurityDb::~FileSecurityDb() {
}
FILE* FileSecurityDb::open_db_file(const char *db_path) {
if (!db_path) {
return NULL;
}
FILE *db_file = fopen(db_path, "wb+");
if (db_file) {
if (!db_file) {
return NULL;
}
/* we will check the db file and if the version or size doesn't match
* what we expect we will blank it */
bool init = false;
@ -149,8 +146,6 @@ FILE* FileSecurityDb::open_db_file(const char *db_path) {
return db_file;
}
return NULL;
}
SecurityDistributionFlags_t* FileSecurityDb::get_distribution_flags(
entry_handle_t db_handle
@ -287,6 +282,21 @@ void FileSecurityDb::set_entry_peer_sign_counter(
/* saving and loading from nvm */
void FileSecurityDb::restore() {
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
fseek(_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
fread(&_local_csrk, sizeof(_local_csrk), 1, _db_file);
fseek(_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
fread(&_local_sign_counter, sizeof(_local_sign_counter), 1, _db_file);
fseek(_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
/* we read the entries partially and fill the offsets ourselves*/
for (size_t i = 0; i < get_entry_count(); i++) {
fread(&_entries[i], DB_SIZE_ENTRY, 1, _db_file);
}
}
void FileSecurityDb::sync(entry_handle_t db_handle) {
@ -300,8 +310,12 @@ void FileSecurityDb::sync(entry_handle_t db_handle) {
}
void FileSecurityDb::set_restore(bool reload) {
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
fwrite(&reload, sizeof(bool), 1, _db_file);
}
/* helper functions */
uint8_t FileSecurityDb::get_entry_count() {
return MAX_ENTRIES;
}