Alexander Kopachov il y a 2 ans
Parent
commit
a8ea396421
4 fichiers modifiés avec 64 ajouts et 12 suppressions
  1. 13 0
      cli/commands/pin/pin.c
  2. 3 1
      features_config.h
  3. 42 11
      services/config/config.c
  4. 6 0
      services/config/config.h

+ 13 - 0
cli/commands/pin/pin.c

@@ -121,6 +121,19 @@ void totp_cli_command_pin_handle(PluginState* plugin_state, FuriString* args, Cl
                 memset(&new_pin[0], 0, TOTP_IV_SIZE);
                 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) {
             if(plugin_state->current_scene == TotpSceneGenerateToken) {
                 totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
                 totp_scene_director_activate_scene(plugin_state, TotpSceneNone, NULL);
                 load_generate_token_scene = true;
                 load_generate_token_scene = true;

+ 3 - 1
features_config.h

@@ -11,4 +11,6 @@
 // End of list
 // End of list
 
 
 // Target firmware to build for
 // Target firmware to build for
-#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
+#ifndef TOTP_TARGET_FIRMWARE
+#define TOTP_TARGET_FIRMWARE TOTP_FIRMWARE_XTREME
+#endif

+ 42 - 11
services/config/config.c

@@ -9,7 +9,7 @@
 
 
 #define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
 #define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
 #define CONFIG_FILE_PATH CONFIG_FILE_DIRECTORY_PATH "/totp.conf"
 #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_TEMP_PATH CONFIG_FILE_PATH ".tmp"
 #define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
 #define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
 #define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
 #define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
@@ -39,6 +39,34 @@ static void totp_close_config_file(FlipperFormat* file) {
     flipper_format_free(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
  * @brief Opens or creates TOTP application standard config file
  * @param storage storage record to use
  * @param storage storage record to use
@@ -250,6 +278,13 @@ static TotpConfigFileUpdateResult
     return update_result;
     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) {
 TotpConfigFileUpdateResult totp_config_file_save_new_token(const TokenInfo* token_info) {
     Storage* cfg_storage = totp_open_storage();
     Storage* cfg_storage = totp_open_storage();
     FlipperFormat* file;
     FlipperFormat* file;
@@ -513,20 +548,16 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
                 CONFIG_FILE_ACTUAL_VERSION);
                 CONFIG_FILE_ACTUAL_VERSION);
             totp_close_config_file(fff_data_file);
             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) {
                 if(totp_open_config_file(storage, &fff_data_file) != TotpConfigFileOpenSuccess) {
                     result = TotpConfigFileOpenError;
                     result = TotpConfigFileOpenError;
                     break;
                     break;
                 }
                 }
 
 
                 FlipperFormat* fff_backup_data_file = flipper_format_file_alloc(storage);
                 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_file_close(fff_backup_data_file);
                     flipper_format_free(fff_backup_data_file);
                     flipper_format_free(fff_backup_data_file);
                     result = TotpConfigFileOpenError;
                     result = TotpConfigFileOpenError;
@@ -551,12 +582,12 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
                 flipper_format_file_close(fff_backup_data_file);
                 flipper_format_file_close(fff_backup_data_file);
                 flipper_format_free(fff_backup_data_file);
                 flipper_format_free(fff_backup_data_file);
                 flipper_format_rewind(fff_data_file);
                 flipper_format_rewind(fff_data_file);
+                free(backup_path);
             } else {
             } else {
                 FURI_LOG_E(
                 FURI_LOG_E(
                     LOGGING_TAG,
                     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;
                 result = TotpConfigFileOpenError;
                 break;
                 break;
             }
             }

+ 6 - 0
services/config/config.h

@@ -60,6 +60,12 @@ enum TotpConfigFileUpdateResults {
     TotpConfigFileUpdateError
     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
  * @brief Saves all the settings and tokens to an application config file
  * @param plugin_state application state
  * @param plugin_state application state