@@ -78,21 +78,10 @@ FileSecurityDb::FileSecurityDb(FILE *db_file)
78
78
fseek (_db_file, DB_OFFSET_RESTORE, SEEK_SET);
79
79
80
80
/* restore if requested */
81
- bool restore;
82
- if ((fread (&restore, sizeof (bool ), 1 , _db_file) == 1 ) && restore) {
83
- fseek (_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
84
- fread (&_local_identity, sizeof (_local_identity), 1 , _db_file);
85
-
86
- fseek (_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
87
- fread (&_local_csrk, sizeof (_local_csrk), 1 , _db_file);
88
-
89
- fseek (_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
90
- fread (&_local_sign_counter, sizeof (_local_sign_counter), 1 , _db_file);
91
-
92
- fseek (_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
93
- /* we read the entries partially and fill the offsets ourselves*/
94
- for (size_t i = 0 ; i < get_entry_count (); i++) {
95
- fread (&_entries[i], DB_SIZE_ENTRY, 1 , _db_file);
81
+ bool restore_toggle;
82
+ if (fread (&restore_toggle, sizeof (bool ), 1 , _db_file) == 1 ) {
83
+ if (restore_toggle) {
84
+ restore ();
96
85
}
97
86
}
98
87
@@ -107,49 +96,55 @@ FileSecurityDb::~FileSecurityDb() {
107
96
}
108
97
109
98
FILE* FileSecurityDb::open_db_file (const char *db_path) {
99
+ if (!db_path) {
100
+ return NULL ;
101
+ }
102
+
110
103
FILE *db_file = fopen (db_path, " wb+" );
111
- if (db_file) {
112
- /* we will check the db file and if the version or size doesn't match
113
- * what we expect we will blank it */
114
- bool init = false ;
115
- uint16_t version;
116
-
117
- fseek (db_file, DB_OFFSET_VERSION, SEEK_SET);
118
-
119
- if ((fread (&version, sizeof (version), 1 , db_file) == 1 ) &&
120
- (version == DB_VERSION)) {
121
- /* version checks out, try the size */
122
- fseek (db_file, DB_SIZE - 1 , SEEK_SET);
123
- /* read one byte and expect to hit EOF */
124
- if ((fread (&version, 1 , 1 , db_file) != 1 ) || !feof (db_file)) {
125
- init = true ;
126
- }
127
- } else {
104
+
105
+ if (!db_file) {
106
+ return NULL ;
107
+ }
108
+
109
+ /* we will check the db file and if the version or size doesn't match
110
+ * what we expect we will blank it */
111
+ bool init = false ;
112
+ uint16_t version;
113
+
114
+ fseek (db_file, DB_OFFSET_VERSION, SEEK_SET);
115
+
116
+ if ((fread (&version, sizeof (version), 1 , db_file) == 1 ) &&
117
+ (version == DB_VERSION)) {
118
+ /* version checks out, try the size */
119
+ fseek (db_file, DB_SIZE - 1 , SEEK_SET);
120
+ /* read one byte and expect to hit EOF */
121
+ if ((fread (&version, 1 , 1 , db_file) != 1 ) || !feof (db_file)) {
128
122
init = true ;
129
123
}
124
+ } else {
125
+ init = true ;
126
+ }
130
127
131
- if (init) {
132
- fseek (db_file, 0 , SEEK_SET);
133
-
134
- /* zero the file */
135
- const uint32_t zero = 0 ;
136
- size_t count = DB_SIZE / 4 ;
137
- while (count--) {
138
- if (fwrite (&zero, sizeof (zero), 1 , db_file) != 1 ) {
139
- fclose (db_file);
140
- return NULL ;
141
- }
142
- }
128
+ if (init) {
129
+ fseek (db_file, 0 , SEEK_SET);
143
130
144
- if (fflush (db_file)) {
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 ) {
145
136
fclose (db_file);
146
137
return NULL ;
147
138
}
148
139
}
149
140
150
- return db_file;
141
+ if (fflush (db_file)) {
142
+ fclose (db_file);
143
+ return NULL ;
144
+ }
151
145
}
152
- return NULL ;
146
+
147
+ return db_file;
153
148
}
154
149
155
150
SecurityDistributionFlags_t* FileSecurityDb::get_distribution_flags (
@@ -287,6 +282,21 @@ void FileSecurityDb::set_entry_peer_sign_counter(
287
282
/* saving and loading from nvm */
288
283
289
284
void FileSecurityDb::restore () {
285
+ fseek (_db_file, DB_OFFSET_LOCAL_IDENTITY, SEEK_SET);
286
+ fread (&_local_identity, sizeof (_local_identity), 1 , _db_file);
287
+
288
+ fseek (_db_file, DB_OFFSET_LOCAL_CSRK, SEEK_SET);
289
+ fread (&_local_csrk, sizeof (_local_csrk), 1 , _db_file);
290
+
291
+ fseek (_db_file, DB_OFFSET_LOCAL_SIGN_COUNT, SEEK_SET);
292
+ fread (&_local_sign_counter, sizeof (_local_sign_counter), 1 , _db_file);
293
+
294
+ fseek (_db_file, DB_OFFSET_ENTRIES, SEEK_SET);
295
+ /* we read the entries partially and fill the offsets ourselves*/
296
+ for (size_t i = 0 ; i < get_entry_count (); i++) {
297
+ fread (&_entries[i], DB_SIZE_ENTRY, 1 , _db_file);
298
+ }
299
+
290
300
}
291
301
292
302
void FileSecurityDb::sync (entry_handle_t db_handle) {
@@ -300,8 +310,12 @@ void FileSecurityDb::sync(entry_handle_t db_handle) {
300
310
}
301
311
302
312
void FileSecurityDb::set_restore (bool reload) {
313
+ fseek (_db_file, DB_OFFSET_RESTORE, SEEK_SET);
314
+ fwrite (&reload, sizeof (bool ), 1 , _db_file);
303
315
}
304
316
317
+ /* helper functions */
318
+
305
319
uint8_t FileSecurityDb::get_entry_count () {
306
320
return MAX_ENTRIES;
307
321
}
0 commit comments