@@ -213,9 +213,12 @@ bool JsonSettings::flush()
213213 }
214214
215215 // Before writing the configuration file, make a backup copy.
216- if (!createBackupFor (path_ ))
216+ if (!map (). empty ( ))
217217 {
218- LOG (LS_WARNING) << " createBackupFor failed for: " << path_;
218+ if (!createBackupFor (path_))
219+ {
220+ LOG (LS_WARNING) << " createBackupFor failed for: " << path_;
221+ }
219222 }
220223
221224 // Write to the configuration file.
@@ -473,8 +476,23 @@ bool JsonSettings::hasBackupFor(const std::filesystem::path& source_file_path)
473476 std::error_code ignored_code;
474477 if (std::filesystem::exists (backup_file_path, ignored_code))
475478 {
476- if (std::filesystem::file_size (backup_file_path, ignored_code) > 0 )
479+ if (std::filesystem::file_size (backup_file_path, ignored_code) > 8 )
480+ {
477481 return true ;
482+ }
483+ else
484+ {
485+ std::error_code error_code;
486+ if (!std::filesystem::remove (backup_file_path, error_code))
487+ {
488+ LOG (LS_WARNING) << " Unable to remove invalid backup: "
489+ << utf16FromLocal8Bit (error_code.message ());
490+ }
491+ else
492+ {
493+ LOG (LS_INFO) << " Invalid backup removed: " << backup_file_path;
494+ }
495+ }
478496 }
479497
480498 return false ;
@@ -568,7 +586,7 @@ bool JsonSettings::createBackupFor(const std::filesystem::path& source_file_path
568586 return false ;
569587 }
570588
571- if (std::filesystem::file_size (source_file_path, error_code) <= 0 )
589+ if (std::filesystem::file_size (source_file_path, error_code) <= 8 )
572590 {
573591 LOG (LS_INFO) << " Source file '" << source_file_path << " ' is empty" ;
574592 return false ;
0 commit comments