mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Added extra measure to stop corrupting backup of setting files
This commit is contained in:
parent
775cfc5275
commit
14b9491664
2 changed files with 28 additions and 0 deletions
|
@ -75,5 +75,28 @@ public static T DeserializeFromFilePath<T>(string filePath)
|
|||
|
||||
return default(T);
|
||||
}
|
||||
|
||||
public static bool QuickVerifyJsonFile(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
|
||||
{
|
||||
if (fs.Length > 1 && fs.ReadByte() == (byte)'{')
|
||||
{
|
||||
fs.Seek(-1, SeekOrigin.End);
|
||||
return fs.ReadByte() == (byte)'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -157,6 +157,11 @@ private bool SaveInternal(string filePath)
|
|||
jsonWriter.Flush();
|
||||
}
|
||||
|
||||
if (!JsonHelpers.QuickVerifyJsonFile(tempFilePath))
|
||||
{
|
||||
throw new Exception($"{typeName} file is corrupt.");
|
||||
}
|
||||
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
if (CreateBackup)
|
||||
|
|
Loading…
Reference in a new issue