@@ -159,7 +159,7 @@ void JsonSettings::sync()
159159 // If there is a backup copy of the configuration, then we restore it.
160160 if (hasBackupFor (path_))
161161 {
162- restoreBackupFor (path_);
162+ restoreBackupFor (path_, encrypted_ );
163163 continue ;
164164 }
165165 else
@@ -188,7 +188,7 @@ void JsonSettings::sync()
188188 // If there is a backup copy of the configuration, then we restore it.
189189 if (hasBackupFor (path_))
190190 {
191- restoreBackupFor (path_);
191+ restoreBackupFor (path_, encrypted_ );
192192 continue ;
193193 }
194194 else
@@ -516,10 +516,50 @@ bool JsonSettings::removeBackupFileFor(const std::filesystem::path& source_file_
516516}
517517
518518// static
519- bool JsonSettings::restoreBackupFor (const std::filesystem::path& source_file_path )
519+ bool JsonSettings::isValidBackup (const std::filesystem::path& backup_file_path, Encrypted encrypted )
520520{
521521 std::error_code error_code;
522522
523+ // If a corrupted configuration file exists.
524+ if (!std::filesystem::exists (backup_file_path, error_code))
525+ {
526+ LOG (LS_WARNING) << " Backup file not exists" ;
527+ return false ;
528+ }
529+
530+ Map map;
531+ if (!readFile (backup_file_path, map, encrypted))
532+ {
533+ LOG (LS_WARNING) << " Unable to read backup file" ;
534+ return false ;
535+ }
536+
537+ if (map.empty ())
538+ {
539+ LOG (LS_WARNING) << " Empty backup file" ;
540+ return false ;
541+ }
542+
543+ return true ;
544+ }
545+
546+ // static
547+ bool JsonSettings::restoreBackupFor (const std::filesystem::path& source_file_path, Encrypted encrypted)
548+ {
549+ std::filesystem::path backup_file_path = backupFilePathFor (source_file_path);
550+ if (!isValidBackup (backup_file_path, encrypted))
551+ {
552+ std::error_code error_code;
553+ if (!std::filesystem::remove (backup_file_path, error_code))
554+ {
555+ LOG (LS_WARNING) << " Unable to remove corrupted backup file: "
556+ << utf16FromLocal8Bit (error_code.message ());
557+ }
558+ return false ;
559+ }
560+
561+ std::error_code error_code;
562+
523563 // If a corrupted configuration file exists.
524564 if (std::filesystem::exists (source_file_path, error_code))
525565 {
@@ -561,8 +601,6 @@ bool JsonSettings::restoreBackupFor(const std::filesystem::path& source_file_pat
561601 }
562602 }
563603
564- std::filesystem::path backup_file_path = backupFilePathFor (source_file_path);
565-
566604 // Restoring a corrupted configuration from a backup.
567605 if (!std::filesystem::copy_file (backup_file_path, source_file_path, error_code))
568606 {
0 commit comments