Skip to content

Commit

Permalink
Closing #123 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
akopachov authored Apr 7, 2023
1 parent b0493cb commit 3eae66a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
13 changes: 13 additions & 0 deletions totp/cli/commands/pin/pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
memset(&new_pin[0], 0, TOTP_IV_SIZE);
}

char* backup_path = totp_config_file_backup();
if(backup_path != NULL) {
TOTP_CLI_PRINTF_WARNING("Backup conf file %s has been created\r\n", backup_path);
TOTP_CLI_PRINTF_WARNING(
"Once you make sure everything is fine and works as expected, please delete this backup file\r\n");
free(backup_path);
} else {
memset_s(&new_pin[0], TOTP_IV_SIZE, 0, TOTP_IV_SIZE);
TOTP_CLI_PRINTF_ERROR(
"An error has occurred during taking backup of config file\r\n");
break;
}

if(plugin_state->current_scene == TotpSceneGenerateToken) {
totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
load_generate_token_scene = true;
Expand Down
4 changes: 3 additions & 1 deletion totp/features_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
// End of list

// Target firmware to build for
#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
#ifndef TOTP_TARGET_FIRMWARE
#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
#endif
53 changes: 42 additions & 11 deletions totp/services/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
#define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
#define CONFIG_FILE_BACKUP_PATH CONFIG_FILE_PATH ".backup"
#define CONFIG_FILE_BACKUP_BASE_PATH CONFIG_FILE_PATH ".backup"
#define CONFIG_FILE_TEMP_PATH CONFIG_FILE_PATH ".tmp"
#define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
#define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
Expand Down Expand Up @@ -39,6 +39,34 @@ static void totp_close_config_file(FlipperFormat* file) {
flipper_format_free(file);
}

/**
* @brief Tries to take a config file backup
* @param storage storage record
* @return backup path if backup successfully taken; \c NULL otherwise
*/
static char* totp_config_file_backup_i(Storage* storage) {
uint8_t backup_path_size = sizeof(CONFIG_FILE_BACKUP_BASE_PATH) + 5;
char* backup_path = malloc(backup_path_size);
furi_check(backup_path != NULL);
memcpy(backup_path, CONFIG_FILE_BACKUP_BASE_PATH, sizeof(CONFIG_FILE_BACKUP_BASE_PATH));
uint16_t i = 1;
bool backup_file_exists;
while((backup_file_exists = storage_common_exists(storage, backup_path)) && i <= 9999) {
snprintf(backup_path, backup_path_size, CONFIG_FILE_BACKUP_BASE_PATH ".%" PRIu16, i);
i++;
}

if(backup_file_exists ||
!storage_common_copy(storage, CONFIG_FILE_PATH, backup_path) == FSE_OK) {
FURI_LOG_E(LOGGING_TAG, "Unable to take a backup");
free(backup_path);
return NULL;
}

FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", backup_path);
return backup_path;
}

/**
* @brief Opens or creates TOTP application standard config file
* @param storage storage record to use
Expand Down Expand Up @@ -250,6 +278,13 @@ static TotpConfigFileUpdateResult
return update_result;
}

char* totp_config_file_backup() {
Storage* storage = totp_open_storage();
char* result = totp_config_file_backup_i(storage);
totp_close_storage();
return result;
}

TotpConfigFileUpdateResult totp_config_file_save_new_token(const TokenInfo* token_info) {
Storage* cfg_storage = totp_open_storage();
FlipperFormat* file;
Expand Down Expand Up @@ -513,20 +548,16 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
CONFIG_FILE_ACTUAL_VERSION);
totp_close_config_file(fff_data_file);

if(storage_common_stat(storage, CONFIG_FILE_BACKUP_PATH, NULL) == FSE_OK) {
storage_simply_remove(storage, CONFIG_FILE_BACKUP_PATH);
}
char* backup_path = totp_config_file_backup_i(storage);

if(storage_common_copy(storage, CONFIG_FILE_PATH, CONFIG_FILE_BACKUP_PATH) == FSE_OK) {
FURI_LOG_I(LOGGING_TAG, "Took config file backup to %s", CONFIG_FILE_BACKUP_PATH);
if(backup_path != NULL) {
if(totp_open_config_file(storage, &fff_data_file) != TotpConfigFileOpenSuccess) {
result = TotpConfigFileOpenError;
break;
}

FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
if(!flipper_format_file_open_existing(
fff_backup_data_file, CONFIG_FILE_BACKUP_PATH)) {
if(!flipper_format_file_open_existing(fff_backup_data_file, backup_path)) {
flipper_format_file_close(fff_backup_data_file);
flipper_format_free(fff_backup_data_file);
result = TotpConfigFileOpenError;
Expand All @@ -551,12 +582,12 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
flipper_format_file_close(fff_backup_data_file);
flipper_format_free(fff_backup_data_file);
flipper_format_rewind(fff_data_file);
free(backup_path);
} else {
FURI_LOG_E(
LOGGING_TAG,
"An error occurred during taking backup of %s into %s before migration",
CONFIG_FILE_PATH,
CONFIG_FILE_BACKUP_PATH);
"An error occurred during taking backup of %s before migration",
CONFIG_FILE_PATH);
result = TotpConfigFileOpenError;
break;
}
Expand Down
6 changes: 6 additions & 0 deletions totp/services/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ enum TotpConfigFileUpdateResults {
TotpConfigFileUpdateError
};

/**
* @brief Tries to take a config file backup
* @return backup path if backup successfully taken; \c NULL otherwise
*/
char* totp_config_file_backup();

/**
* @brief Saves all the settings and tokens to an application config file
* @param plugin_state application state
Expand Down

0 comments on commit 3eae66a

Please sign in to comment.