From 79b294d7cb4b48a371dd6698d85e08771ed79eb1 Mon Sep 17 00:00:00 2001 From: Ajay Panicker Date: Thu, 11 Feb 2016 12:33:30 -0800 Subject: [PATCH] Create backup for bt_config.conf Move current config file to a backup when a new config file is saved. This backup can be used in case there is an error loading the current config file. Change-Id: Icc210c457dc71a72fc2e82ff72c91207b25deaf1 --- btif/src/btif_config.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/btif/src/btif_config.c b/btif/src/btif_config.c index 85bbd05d3..3949a2085 100644 --- a/btif/src/btif_config.c +++ b/btif/src/btif_config.c @@ -43,8 +43,10 @@ // TODO(armansito): Find a better way than searching by a hardcoded path. #if defined(OS_GENERIC) static const char *CONFIG_FILE_PATH = "bt_config.conf"; +static const char *CONFIG_BACKUP_PATH = "bt_config.bak"; #else // !defined(OS_GENERIC) static const char *CONFIG_FILE_PATH = "/data/misc/bluedroid/bt_config.conf"; +static const char *CONFIG_BACKUP_PATH = "/data/misc/bluedroid/bt_config.bak"; #endif // defined(OS_GENERIC) static const period_ms_t CONFIG_SETTLE_PERIOD_MS = 3000; @@ -100,12 +102,16 @@ static future_t *init(void) { pthread_mutex_init(&lock, NULL); config = config_new(CONFIG_FILE_PATH); if (!config) { - LOG_WARN(LOG_TAG, "%s unable to load config file: %s; starting unconfigured.", + LOG_WARN(LOG_TAG, "%s unable to load config file: %s; using backup.", __func__, CONFIG_FILE_PATH); - config = config_new_empty(); + config = config_new(CONFIG_BACKUP_PATH); if (!config) { - LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__); - goto error; + LOG_ERROR(LOG_TAG, "%s unable to load backup; creating empty config.", __func__); + config = config_new_empty(); + if (!config) { + LOG_ERROR(LOG_TAG, "%s unable to allocate a config object.", __func__); + goto error; + } } } @@ -397,6 +403,7 @@ static void btif_config_write(UNUSED_ATTR UINT16 event, UNUSED_ATTR char *p_para btif_config_devcache_cleanup(); pthread_mutex_lock(&lock); + rename(CONFIG_FILE_PATH, CONFIG_BACKUP_PATH); config_save(config, CONFIG_FILE_PATH); pthread_mutex_unlock(&lock); } -- 2.11.0