mirror of
https://github.com/cyanfish/naps2.git
synced 2024-08-17 11:10:23 +03:00
Back up unreadable config file before overwriting
This commit is contained in:
parent
d41e9f0046
commit
88f7027ed4
@ -100,7 +100,8 @@ public class FileConfigScope<TConfig> : ConfigScope<TConfig>
|
|||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
// Failed to load. Since we're using FileShare.None, it can't be concurrent modification.
|
// 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)
|
// Merge the cache and our changes into a local copy (so in case of exceptions nothing is changed)
|
||||||
copy = new ConfigStorage<TConfig>();
|
copy = new ConfigStorage<TConfig>();
|
||||||
@ -120,4 +121,19 @@ public class FileConfigScope<TConfig> : ConfigScope<TConfig>
|
|||||||
Log.ErrorException($"Error writing {_filePath}", ex);
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user