Skip to content

Commit 329de8f

Browse files
committed
[nrf fromlist] settings: zms: code style clean up
Clean some parts of the code and refactor it to avoid multiple nested conditions Upstream PR #: 87792 Signed-off-by: Riadh Ghaddab <[email protected]> (cherry picked from commit 559e0269aa52f5ddfa99b20b3f82905be5597ba6)
1 parent 0efeab4 commit 329de8f

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

subsys/settings/src/settings_zms.c

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,44 @@ static ssize_t settings_zms_get_val_len(struct settings_store *cs, const char *n
660660
return 0;
661661
}
662662

663+
/* This function inits the linked list head if it doesn't exist or recover it
664+
* if the ll_last_hash_id is different than the head hash ID
665+
*/
666+
static int settings_zms_init_or_recover_ll(struct settings_zms *cf, uint32_t ll_last_hash_id)
667+
{
668+
struct settings_hash_linked_list settings_element;
669+
int rc = 0;
670+
671+
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
672+
/* header doesn't exist */
673+
settings_element.previous_hash = 0;
674+
settings_element.next_hash = 0;
675+
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
676+
sizeof(struct settings_hash_linked_list));
677+
if (rc < 0) {
678+
return rc;
679+
}
680+
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
681+
cf->second_to_last_hash_id = 0;
682+
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
683+
/* store the LL header in cache */
684+
cf->ll_cache_next = 0;
685+
cf->ll_cache[cf->ll_cache_next++] = settings_element;
686+
#endif
687+
} else {
688+
/* let's recover it by keeping all nodes until the last one */
689+
settings_element.previous_hash = cf->second_to_last_hash_id;
690+
settings_element.next_hash = 0;
691+
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
692+
sizeof(struct settings_hash_linked_list));
693+
if (rc < 0) {
694+
return rc;
695+
}
696+
}
697+
698+
return 0;
699+
}
700+
663701
static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
664702
{
665703
struct settings_hash_linked_list settings_element;
@@ -679,34 +717,7 @@ static int settings_zms_get_last_hash_ids(struct settings_zms *cf)
679717
/* header doesn't exist or linked list broken, reinitialize the header
680718
* if it doesn't exist and recover it if it is broken
681719
*/
682-
if (ll_last_hash_id == ZMS_LL_HEAD_HASH_ID) {
683-
/* header doesn't exist */
684-
const struct settings_hash_linked_list settings_element = {
685-
.previous_hash = 0, .next_hash = 0};
686-
rc = zms_write(&cf->cf_zms, ZMS_LL_HEAD_HASH_ID, &settings_element,
687-
sizeof(struct settings_hash_linked_list));
688-
if (rc < 0) {
689-
return rc;
690-
}
691-
cf->last_hash_id = ZMS_LL_HEAD_HASH_ID;
692-
cf->second_to_last_hash_id = 0;
693-
#ifdef CONFIG_SETTINGS_ZMS_LL_CACHE
694-
/* store the LL header in cache */
695-
cf->ll_cache_next = 0;
696-
cf->ll_cache[cf->ll_cache_next++] = settings_element;
697-
#endif
698-
} else {
699-
/* let's recover it by keeping all nodes until the last one */
700-
const struct settings_hash_linked_list settings_element = {
701-
.previous_hash = cf->second_to_last_hash_id,
702-
.next_hash = 0};
703-
rc = zms_write(&cf->cf_zms, cf->last_hash_id, &settings_element,
704-
sizeof(struct settings_hash_linked_list));
705-
if (rc < 0) {
706-
return rc;
707-
}
708-
}
709-
return 0;
720+
return settings_zms_init_or_recover_ll(cf, ll_last_hash_id);
710721
} else if (rc < 0) {
711722
return rc;
712723
}

0 commit comments

Comments
 (0)