Back up unreadable config file before overwriting

This commit is contained in:
Ben Olden-Cooligan 2024-04-07 13:20:01 -07:00
parent d41e9f0046
commit 88f7027ed4

View File

@ -100,7 +100,8 @@ public class FileConfigScope<TConfig> : ConfigScope<TConfig>
catch (Exception)
{
// Failed to load. Since we're using FileShare.None, it can't be concurrent modification.
// Either the file is newly created, or it was corrupted. In either case we can ignore and overwrite.
// Either the file is newly created, or it was corrupted. In either case we can backup and overwrite.
Backup(stream);
}
// Merge the cache and our changes into a local copy (so in case of exceptions nothing is changed)
copy = new ConfigStorage<TConfig>();
@ -120,4 +121,19 @@ public class FileConfigScope<TConfig> : ConfigScope<TConfig>
Log.ErrorException($"Error writing {_filePath}", ex);
}
}
private void Backup(FileStream stream)
{
try
{
using var backupStream = new FileStream(_filePath + ".bak", FileMode.Create);
backupStream.SetLength(0);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(backupStream);
}
catch (Exception)
{
// Ignore, we did our best
}
}
}