Skip to content

Commit 49db7e2

Browse files
restoring db file blanks file if set to not restore, allow reusing the db
1 parent f8244a3 commit 49db7e2

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

features/FEATURE_BLE/ble/generic/FileSecurityDb.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ class FileSecurityDb : public SecurityDb {
126126
virtual SecurityEntryKeys_t* read_in_entry_local_keys(entry_handle_t db_handle);
127127
virtual SecurityEntrySigning_t* read_in_entry_peer_signing(entry_handle_t db_handle);
128128

129+
/**
130+
* Zero the db file.
131+
* @param db_file filehandle for file to erase
132+
* @return filehandle when successful, otherwise NULL
133+
*/
134+
static FILE* erase_db_file(FILE* db_file);
135+
129136
private:
130137
entry_t _entries[MAX_ENTRIES];
131138
FILE *_db_file;

features/FEATURE_BLE/source/generic/FileSecurityDb.cpp

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,6 @@ typedef SecurityDb::entry_handle_t entry_handle_t;
7575
FileSecurityDb::FileSecurityDb(FILE *db_file)
7676
: SecurityDb(),
7777
_db_file(db_file) {
78-
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
79-
80-
/* restore if requested */
81-
bool restore_toggle;
82-
if (fread(&restore_toggle, sizeof(bool), 1, _db_file) == 1) {
83-
if (restore_toggle) {
84-
restore();
85-
}
86-
}
87-
8878
/* init the offset in entries so they point to file positions */
8979
for (size_t i = 0; i < get_entry_count(); i++) {
9080
_entries[i].file_offset = DB_OFFSET_STORES + i * DB_SIZE_STORE;
@@ -126,24 +116,30 @@ FILE* FileSecurityDb::open_db_file(const char *db_path) {
126116
}
127117

128118
if (init) {
129-
fseek(db_file, 0, SEEK_SET);
130-
131-
/* zero the file */
132-
const uint32_t zero = 0;
133-
size_t count = DB_SIZE / 4;
134-
while (count--) {
135-
if (fwrite(&zero, sizeof(zero), 1, db_file) != 1) {
136-
fclose(db_file);
137-
return NULL;
138-
}
139-
}
119+
return erase_db_file(db_file);
120+
}
121+
122+
return db_file;
123+
}
124+
125+
FILE* FileSecurityDb::erase_db_file(FILE* db_file) {
126+
fseek(db_file, 0, SEEK_SET);
140127

141-
if (fflush(db_file)) {
128+
/* zero the file */
129+
const uint32_t zero = 0;
130+
size_t count = DB_SIZE / 4;
131+
while (count--) {
132+
if (fwrite(&zero, sizeof(zero), 1, db_file) != 1) {
142133
fclose(db_file);
143134
return NULL;
144135
}
145136
}
146137

138+
if (fflush(db_file)) {
139+
fclose(db_file);
140+
return NULL;
141+
}
142+
147143
return db_file;
148144
}
149145

@@ -282,6 +278,18 @@ void FileSecurityDb::set_entry_peer_sign_counter(
282278
/* saving and loading from nvm */
283279

284280
void FileSecurityDb::restore() {
281+
282+
fseek(_db_file, DB_OFFSET_RESTORE, SEEK_SET);
283+
284+
/* restore if requested */
285+
bool restore_toggle;
286+
if (fread(&restore_toggle, sizeof(bool), 1, _db_file) == 1) {
287+
if (!restore_toggle) {
288+
erase_db_file(_db_file);
289+
return;
290+
}
291+
}
292+
285293
fseek(_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
286294
fread(&_local_identity, sizeof(_local_identity), 1, _db_file);
287295

features/FEATURE_BLE/source/generic/GenericSecurityManager.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ ble_error_t GenericSecurityManager::init(
4242
bool signing,
4343
const char* db_path
4444
) {
45-
if (_db) {
46-
return BLE_ERROR_OPERATION_NOT_PERMITTED;
47-
}
4845

4946
ble_error_t err = _pal.initialize();
5047
if (err) {
5148
return err;
5249
}
5350

54-
FILE* db_file = FileSecurityDb::open_db_file(db_path);
55-
if (db_file) {
56-
_db = new (std::nothrow) FileSecurityDb(db_file);
57-
} else {
58-
_db = new (std::nothrow) MemorySecurityDb();
51+
if (!_db) {
52+
FILE* db_file = FileSecurityDb::open_db_file(db_path);
53+
if (db_file) {
54+
_db = new (std::nothrow) FileSecurityDb(db_file);
55+
} else {
56+
_db = new (std::nothrow) MemorySecurityDb();
57+
}
5958
}
6059

6160
_db->restore();

0 commit comments

Comments
 (0)