Skip to content

Commit 3eae66a

Browse files
authored
Closing #123 (#124)
1 parent b0493cb commit 3eae66a

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

totp/cli/commands/pin/pin.c

+13
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
121121
memset(&new_pin[0], 0, TOTP_IV_SIZE);
122122
}
123123

124+
char* backup_path = totp_config_file_backup();
125+
if(backup_path != NULL) {
126+
TOTP_CLI_PRINTF_WARNING("Backup conf file %s has been created\r\n", backup_path);
127+
TOTP_CLI_PRINTF_WARNING(
128+
"Once you make sure everything is fine and works as expected, please delete this backup file\r\n");
129+
free(backup_path);
130+
} else {
131+
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
132+
TOTP_CLI_PRINTF_ERROR(
133+
"An error has occurred during taking backup of config file\r\n");
134+
break;
135+
}
136+
124137
if(plugin_state->current_scene == TotpSceneGenerateToken) {
125138
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
126139
load_generate_token_scene = true;

totp/features_config.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@
1111
// End of list
1212

1313
// Target firmware to build for
14-
#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
14+
#ifndef TOTP_TARGET_FIRMWARE
15+
#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
16+
#endif

totp/services/config/config.c

+42-11
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
1111
#define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
12-
#define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
12+
#define CONFIG_FILE_BACKUP_BASE_PATH CONFIG_FILE_PATH ".backup"
1313
#define CONFIG_FILE_TEMP_PATH CONFIG_FILE_PATH ".tmp"
1414
#define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
1515
#define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
@@ -39,6 +39,34 @@ static void totp_close_config_file(FlipperFormat* file) {
3939
flipper_format_free(file);
4040
}
4141

42+
/**
43+
* @brief Tries to take a config file backup
44+
* @param storage storage record
45+
* @return backup path if backup successfully taken; \c NULL otherwise
46+
*/
47+
static char* totp_config_file_backup_i(Storage* storage) {
48+
uint8_t backup_path_size = sizeof(CONFIG_FILE_BACKUP_BASE_PATH) + 5;
49+
char* backup_path = malloc(backup_path_size);
50+
furi_check(backup_path != NULL);
51+
memcpy(backup_path, CONFIG_FILE_BACKUP_BASE_PATH, sizeof(CONFIG_FILE_BACKUP_BASE_PATH));
52+
uint16_t i = 1;
53+
bool backup_file_exists;
54+
while((backup_file_exists = storage_common_exists(storage, backup_path)) && i <= 9999) {
55+
snprintf(backup_path, backup_path_size, CONFIG_FILE_BACKUP_BASE_PATH ".%" PRIu16, i);
56+
i++;
57+
}
58+
59+
if(backup_file_exists ||
60+
!storage_common_copy(storage, CONFIG_FILE_PATH, backup_path) == FSE_OK) {
61+
FURI_LOG_E(LOGGING_TAG, "Unable to take a backup");
62+
free(backup_path);
63+
return NULL;
64+
}
65+
66+
FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", backup_path);
67+
return backup_path;
68+
}
69+
4270
/**
4371
* @brief Opens or creates TOTP application standard config file
4472
* @param storage storage record to use
@@ -250,6 +278,13 @@ static TotpConfigFileUpdateResult
250278
return update_result;
251279
}
252280

281+
char* totp_config_file_backup() {
282+
Storage* storage = totp_open_storage();
283+
char* result = totp_config_file_backup_i(storage);
284+
totp_close_storage();
285+
return result;
286+
}
287+
253288
TotpConfigFileUpdateResult totp_config_file_save_new_token(const TokenInfo* token_info) {
254289
Storage* cfg_storage = totp_open_storage();
255290
FlipperFormat* file;
@@ -513,20 +548,16 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
513548
CONFIG_FILE_ACTUAL_VERSION);
514549
totp_close_config_file(fff_data_file);
515550

516-
if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
517-
storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
518-
}
551+
char* backup_path = totp_config_file_backup_i(storage);
519552

520-
if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
521-
FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
553+
if(backup_path != NULL) {
522554
if(totp_open_config_file(storage, &fff_data_file) != TotpConfigFileOpenSuccess) {
523555
result = TotpConfigFileOpenError;
524556
break;
525557
}
526558

527559
FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
528-
if(!flipper_format_file_open_existing(
529-
fff_backup_data_file, CONFIG_FILE_BACKUP_PATH)) {
560+
if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
530561
flipper_format_file_close(fff_backup_data_file);
531562
flipper_format_free(fff_backup_data_file);
532563
result = TotpConfigFileOpenError;
@@ -551,12 +582,12 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
551582
flipper_format_file_close(fff_backup_data_file);
552583
flipper_format_free(fff_backup_data_file);
553584
flipper_format_rewind(fff_data_file);
585+
free(backup_path);
554586
} else {
555587
FURI_LOG_E(
556588
LOGGING_TAG,
557-
"An error occurred during taking backup of %s into %s before migration",
558-
CONFIG_FILE_PATH,
559-
CONFIG_FILE_BACKUP_PATH);
589+
"An error occurred during taking backup of %s before migration",
590+
CONFIG_FILE_PATH);
560591
result = TotpConfigFileOpenError;
561592
break;
562593
}

totp/services/config/config.h

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ enum TotpConfigFileUpdateResults {
6060
TotpConfigFileUpdateError
6161
};
6262

63+
/**
64+
* @brief Tries to take a config file backup
65+
* @return backup path if backup successfully taken; \c NULL otherwise
66+
*/
67+
char* totp_config_file_backup();
68+
6369
/**
6470
* @brief Saves all the settings and tokens to an application config file
6571
* @param plugin_state application state

0 commit comments

Comments
 (0)