From cc1f99d5d4b568329e92a002a44ae69ca43a1eb1 Mon Sep 17 00:00:00 2001 From: JustArchi Date: Tue, 2 Jan 2018 08:19:37 +0100 Subject: [PATCH] Misc --- ArchiSteamFarm/BotDatabase.cs | 6 ++++-- ArchiSteamFarm/GlobalDatabase.cs | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/ArchiSteamFarm/BotDatabase.cs b/ArchiSteamFarm/BotDatabase.cs index 463b5fdfe..ac5cf6359 100644 --- a/ArchiSteamFarm/BotDatabase.cs +++ b/ArchiSteamFarm/BotDatabase.cs @@ -256,11 +256,13 @@ namespace ArchiSteamFarm { return; } + // We always want to write entire content to temporary file first, in order to never load corrupted data, also when target file doesn't exist + await File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false); + if (File.Exists(FilePath)) { - await File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false); File.Replace(newFilePath, FilePath, null); } else { - await File.WriteAllTextAsync(FilePath, json).ConfigureAwait(false); + File.Move(newFilePath, FilePath); } } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e); diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index 083d80a52..3f1ae9971 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -166,11 +166,13 @@ namespace ArchiSteamFarm { await FileSemaphore.WaitAsync().ConfigureAwait(false); try { + // We always want to write entire content to temporary file first, in order to never load corrupted data, also when target file doesn't exist + await File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false); + if (File.Exists(FilePath)) { - await File.WriteAllTextAsync(newFilePath, json).ConfigureAwait(false); File.Replace(newFilePath, FilePath, null); } else { - await File.WriteAllTextAsync(FilePath, json).ConfigureAwait(false); + File.Move(newFilePath, FilePath); } } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e);