9
9
10
10
#define CONFIG_FILE_DIRECTORY_PATH EXT_PATH("authenticator")
11
11
#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"
13
13
#define CONFIG_FILE_TEMP_PATH CONFIG_FILE_PATH ".tmp"
14
14
#define CONFIG_FILE_ORIG_PATH CONFIG_FILE_PATH ".orig"
15
15
#define CONFIG_FILE_PATH_PREVIOUS EXT_PATH("apps/Misc") "/totp.conf"
@@ -39,6 +39,34 @@ static void totp_close_config_file(FlipperFormat* file) {
39
39
flipper_format_free (file );
40
40
}
41
41
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
+
42
70
/**
43
71
* @brief Opens or creates TOTP application standard config file
44
72
* @param storage storage record to use
@@ -250,6 +278,13 @@ static TotpConfigFileUpdateResult
250
278
return update_result ;
251
279
}
252
280
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
+
253
288
TotpConfigFileUpdateResult totp_config_file_save_new_token (const TokenInfo * token_info ) {
254
289
Storage * cfg_storage = totp_open_storage ();
255
290
FlipperFormat * file ;
@@ -513,20 +548,16 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
513
548
CONFIG_FILE_ACTUAL_VERSION );
514
549
totp_close_config_file (fff_data_file );
515
550
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 );
519
552
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 ) {
522
554
if (totp_open_config_file (storage , & fff_data_file ) != TotpConfigFileOpenSuccess ) {
523
555
result = TotpConfigFileOpenError ;
524
556
break ;
525
557
}
526
558
527
559
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 )) {
530
561
flipper_format_file_close (fff_backup_data_file );
531
562
flipper_format_free (fff_backup_data_file );
532
563
result = TotpConfigFileOpenError ;
@@ -551,12 +582,12 @@ TotpConfigFileOpenResult totp_config_file_load_base(PluginState* const plugin_st
551
582
flipper_format_file_close (fff_backup_data_file );
552
583
flipper_format_free (fff_backup_data_file );
553
584
flipper_format_rewind (fff_data_file );
585
+ free (backup_path );
554
586
} else {
555
587
FURI_LOG_E (
556
588
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 );
560
591
result = TotpConfigFileOpenError ;
561
592
break ;
562
593
}
0 commit comments