Skip to content

Commit 50e490a

Browse files
committed
changed the ID section/tag for validating local pref storage. In past, used app name, BUT the app name can change over time or during startup - which happened with the .04 release. Now using the fixed, app class ID to write this tag to storage. Also added logic to migrate old tag to new.
1 parent 2f0ba9f commit 50e490a

File tree

1 file changed

+62
-12
lines changed

1 file changed

+62
-12
lines changed

src/Flux/spSpark.cpp

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,12 @@ bool flxFlux::save(flxStorage *pStorage)
187187
{
188188
// Write a block to the storage system that has a has of or name/desc
189189
// Use this to validate that the settings in the storage system are ours
190-
flxStorageBlock *stBlk = pStorage->beginBlock(name());
190+
191+
// 11/2023 - originally used the name to find our start key, but names change
192+
// switching to appliction class name - which doesn't change
193+
194+
195+
flxStorageBlock *stBlk = pStorage->beginBlock(appClassID());
191196
if (!stBlk)
192197
return false;
193198

@@ -197,13 +202,13 @@ bool flxFlux::save(flxStorage *pStorage)
197202
// This allows rapid ID of a valid storage source
198203
if (pStorage->kind() == flxStorage::flxStorageKindInternal)
199204
{
200-
char szBuffer[128] = {0};
201-
strlcpy(szBuffer, name(), sizeof(szBuffer));
202-
strlcat(szBuffer, description(), sizeof(szBuffer));
203-
205+
// 11/2023
206+
// we just want to write a key at this level that can be used on restore
207+
// to indicate this is this applications prefs/state. Just put hash of
208+
// the app class id in this store
204209
char szHash[kApplicationHashIDSize];
205210

206-
status = flx_utils::id_hash_string_to_string(szBuffer, szHash, sizeof(szHash));
211+
status = flx_utils::id_hash_string_to_string(appClassID(), szHash, sizeof(szHash));
207212

208213
// Write out the ID tag
209214
if (status)
@@ -217,7 +222,7 @@ bool flxFlux::save(flxStorage *pStorage)
217222
// everything go okay?
218223
if (!status)
219224
{
220-
flxLog_D(F("Unable to store application ID key"));
225+
flxLog_W(F("Unable to store application ID key"));
221226
return false;
222227
}
223228

@@ -231,7 +236,9 @@ bool flxFlux::restore(flxStorage *pStorage)
231236
// Do we have our ID block in storage? If not, then there's no need to continue
232237
// since the data isn't for this app
233238

234-
flxStorageBlock *stBlk = pStorage->beginBlock(name());
239+
// 11/2023 - originally used the name to find our start key, but names change
240+
// switching to appliction class name - which doesn't change
241+
flxStorageBlock *stBlk = pStorage->beginBlock(appClassID());
235242
if (!stBlk)
236243
return false;
237244

@@ -241,15 +248,40 @@ bool flxFlux::restore(flxStorage *pStorage)
241248
// Note for external sources (files...etc), we load in and validate based on
242249
// source name. This makes it easier to manually write out a settings file
243250
bool status;
251+
bool oldIDFound = false;
252+
char szBuffer[128] = {0};
244253
if (pStorage->kind() == flxStorage::flxStorageKindInternal)
245254
{
246255
status = stBlk->valueExists(kApplicationHashIDTag);
247-
if (status)
256+
257+
if (!status)
248258
{
249-
char szBuffer[128] = {0};
250-
strlcpy(szBuffer, name(), sizeof(szBuffer));
251-
strlcat(szBuffer, description(), sizeof(szBuffer));
259+
// 11/2023
260+
// The App pref block and key value doesn't exist, it could be we have an older system,
261+
// which used the app name() not app classID to name the block store the app ID key
262+
// so try the name(). Over time, re-saves of prefs will transition things to the
263+
// app ID, but we'll also transition below
264+
pStorage->endBlock(stBlk);
265+
stBlk = pStorage->beginBlock(name());
266+
if (!stBlk)
267+
return false;
268+
269+
status = stBlk->valueExists(kApplicationHashIDTag);
270+
if (status)
271+
{
272+
oldIDFound = true;
252273

274+
// use the original method for the hash ID gen - name and desc
275+
strlcpy(szBuffer, name(), sizeof(szBuffer));
276+
strlcat(szBuffer, description(), sizeof(szBuffer));
277+
}
278+
}
279+
else
280+
strlcpy(szBuffer, appClassID(), sizeof(szBuffer));
281+
282+
if (status)
283+
{
284+
// just hash the app class id
253285
char szHash[kApplicationHashIDSize];
254286

255287
status = flx_utils::id_hash_string_to_string(szBuffer, szHash, sizeof(szHash));
@@ -262,6 +294,24 @@ bool flxFlux::restore(flxStorage *pStorage)
262294
if (status)
263295
status = strncmp(szHash, szBuffer, strlen(szHash)) == 0;
264296
}
297+
298+
// 11/23
299+
// if we have success and we used an old key, transition to the new key
300+
if (status && oldIDFound)
301+
{
302+
pStorage->endBlock(stBlk);
303+
stBlk = pStorage->beginBlock(appClassID());
304+
if (stBlk)
305+
{
306+
if (flx_utils::id_hash_string_to_string(appClassID(), szHash, sizeof(szHash)))
307+
{
308+
stBlk->setReadOnly(false);
309+
if(!stBlk->write(kApplicationHashIDTag, szHash))
310+
flxLog_W("Preferences app key failed to write");
311+
312+
}
313+
}
314+
}
265315
}
266316

267317
pStorage->endBlock(stBlk);

0 commit comments

Comments
 (0)