From 8b517954ef905a87b2ea54dd743c06fabffacca9 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 7 Jan 2023 13:11:43 -0600 Subject: [PATCH 1/5] Try Atomic Save for Setting --- Flow.Launcher.Infrastructure/Helper.cs | 6 +- .../Storage/JsonStorage.cs | 83 +++++++++++-------- .../Storage/PluginJsonStorage.cs | 2 +- 3 files changed, 53 insertions(+), 38 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Helper.cs b/Flow.Launcher.Infrastructure/Helper.cs index faa4c93b513..db575de9004 100644 --- a/Flow.Launcher.Infrastructure/Helper.cs +++ b/Flow.Launcher.Infrastructure/Helper.cs @@ -1,4 +1,6 @@ -using System; +#nullable enable + +using System; using System.IO; using System.Runtime.CompilerServices; using System.Text.Json; @@ -16,7 +18,7 @@ static Helper() /// /// http://www.yinwang.org/blog-cn/2015/11/21/programming-philosophy /// - public static T NonNull(this T obj) + public static T NonNull(this T? obj) { if (obj == null) { diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index 0083ccb87b0..20b2e4e2105 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -1,4 +1,5 @@ -using System; +#nullable enable +using System; using System.Globalization; using System.IO; using System.Text.Json; @@ -11,62 +12,73 @@ namespace Flow.Launcher.Infrastructure.Storage /// public class JsonStorage where T : new() { - protected T _data; + protected T? Data; // need a new directory name public const string DirectoryName = "Settings"; public const string FileSuffix = ".json"; - public string FilePath { get; set; } - public string DirectoryPath { get; set; } + protected string FilePath { get; init; } = null!; + private string TempFilePath => $"{FilePath}.tmp"; + private string BackupFilePath => $"{FilePath}.bak"; + protected string DirectoryPath { get; init; } = null!; public T Load() { + string? serialized = null; + if (File.Exists(FilePath)) { - var serialized = File.ReadAllText(FilePath); - if (!string.IsNullOrWhiteSpace(serialized)) + serialized = File.ReadAllText(FilePath); + } + + if (!string.IsNullOrEmpty(serialized)) + { + try { - Deserialize(serialized); + Data = JsonSerializer.Deserialize(serialized)?? TryLoadBackup() ?? LoadDefault(); } - else + catch (JsonException) { - LoadDefault(); + Data = TryLoadBackup() ?? LoadDefault(); } } else { - LoadDefault(); + Data = TryLoadBackup() ?? LoadDefault(); } - return _data.NonNull(); + return Data.NonNull(); } - private void Deserialize(string serialized) + private T LoadDefault() { - try - { - _data = JsonSerializer.Deserialize(serialized); - } - catch (JsonException e) + if (File.Exists(FilePath)) { - LoadDefault(); - Log.Exception($"|JsonStorage.Deserialize|Deserialize error for json <{FilePath}>", e); + BackupOriginFile(); } - if (_data == null) - { - LoadDefault(); - } + return new T(); } - private void LoadDefault() + private T? TryLoadBackup() { - if (File.Exists(FilePath)) + if (!File.Exists(BackupFilePath)) + return default; + + try { - BackupOriginFile(); + var data = JsonSerializer.Deserialize(File.ReadAllText(BackupFilePath)); + if (data != null) + { + Log.Info($"|JsonStorage.Load|Load backup file {BackupFilePath} successfully"); + File.Replace(BackupFilePath, FilePath, null); + return data; + } + return default; + } + catch (JsonException) + { + return default; } - - _data = new T(); - Save(); } private void BackupOriginFile() @@ -82,13 +94,14 @@ private void BackupOriginFile() public void Save() { - string serialized = JsonSerializer.Serialize(_data, new JsonSerializerOptions() { WriteIndented = true }); + string serialized = JsonSerializer.Serialize(Data, new JsonSerializerOptions + { + WriteIndented = true + }); - File.WriteAllText(FilePath, serialized); + File.WriteAllText(TempFilePath, serialized); + File.Replace(TempFilePath, FilePath, BackupFilePath); + File.Delete(TempFilePath); } } - - [Obsolete("Deprecated as of Flow Launcher v1.8.0, on 2021.06.21. " + - "This is used only for Everything plugin v1.4.9 or below backwards compatibility")] - public class JsonStrorage : JsonStorage where T : new() { } } diff --git a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs index 923a1a6b56f..abe3f55b5ad 100644 --- a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs @@ -18,7 +18,7 @@ public PluginJsonStorage() public PluginJsonStorage(T data) : this() { - _data = data; + Data = data; } } } From 43e38f648c92cc93512cab8ade7ccd5de75dec2b Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 7 Jan 2023 13:22:17 -0600 Subject: [PATCH 2/5] update expect.txt --- .github/actions/spelling/expect.txt | 100 ++++++++++++++-------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index a91287cc034..612ee395722 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -1,64 +1,64 @@ +actionkeyword +amazonaws +appdata +appref +appref-ms +appveyor +baidu +bak +Bokm +Bokmal +CMD crowdin +Customise +dlgtext +dotnet +dpi +duckduckgo DWM -workflows -wpf -actionkeyword -stackoverflow -Wox +EWX +facebook +findicon flowlauncher Fody -stackoverflow +fullscreen +gmail +Google +hotkeys IContext -IShell +Img +img IPlugin -appveyor +IShell +jjw24 +lnk +mscorlib netflix -youtube -appdata +pls +pluginindicator +popup Prioritise +ptr +pythonw +qianlifeng +resx +runas Segoe -Google -Customise -UWP -uwp -Bokmal -Bokm +srchadmin +stackoverflow +TobiasSekan +totalcmd +TSource uninstallation uninstalling +userdata +UWP +uwp voidtools -fullscreen -hotkeys -totalcmd -lnk -amazonaws -mscorlib -pythonw -dotnet +websearch winget -jjw24 wolframalpha -gmail -duckduckgo -facebook -findicon -baidu -pls -websearch -qianlifeng -userdata -srchadmin -EWX -dlgtext -CMD -appref-ms -appref -TSource -runas -dpi -popup -ptr -pluginindicator -TobiasSekan -Img -img -resx +workflows +Wox +wpf +youtube From 1e256e441816628e6ef479ac6f7d5d79dff8f4d1 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 7 Jan 2023 13:22:58 -0600 Subject: [PATCH 3/5] Revert "update expect.txt" This reverts commit 43e38f648c92cc93512cab8ade7ccd5de75dec2b. --- .github/actions/spelling/expect.txt | 100 ++++++++++++++-------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 612ee395722..a91287cc034 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -1,64 +1,64 @@ -actionkeyword -amazonaws -appdata -appref -appref-ms -appveyor -baidu -bak -Bokm -Bokmal -CMD crowdin -Customise -dlgtext -dotnet -dpi -duckduckgo DWM -EWX -facebook -findicon +workflows +wpf +actionkeyword +stackoverflow +Wox flowlauncher Fody -fullscreen -gmail -Google -hotkeys +stackoverflow IContext -Img -img -IPlugin IShell -jjw24 -lnk -mscorlib +IPlugin +appveyor netflix -pls -pluginindicator -popup +youtube +appdata Prioritise -ptr -pythonw -qianlifeng -resx -runas Segoe -srchadmin -stackoverflow -TobiasSekan -totalcmd -TSource -uninstallation -uninstalling -userdata +Google +Customise UWP uwp +Bokmal +Bokm +uninstallation +uninstalling voidtools -websearch +fullscreen +hotkeys +totalcmd +lnk +amazonaws +mscorlib +pythonw +dotnet winget +jjw24 wolframalpha -workflows -Wox -wpf -youtube +gmail +duckduckgo +facebook +findicon +baidu +pls +websearch +qianlifeng +userdata +srchadmin +EWX +dlgtext +CMD +appref-ms +appref +TSource +runas +dpi +popup +ptr +pluginindicator +TobiasSekan +Img +img +resx From 162add930700059ed901c79b3a66b292c77de491 Mon Sep 17 00:00:00 2001 From: Hongtao Zhang Date: Sat, 7 Jan 2023 13:24:19 -0600 Subject: [PATCH 4/5] update expect.txt --- .github/actions/spelling/expect.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index a91287cc034..ba6faec9ab0 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -62,3 +62,5 @@ TobiasSekan Img img resx +bak +tmp From f517343c11d6a5ae0d7b1ec9d6da5172873569e9 Mon Sep 17 00:00:00 2001 From: Jeremy Wu Date: Mon, 23 Jan 2023 09:17:09 +1100 Subject: [PATCH 5/5] update error message --- Flow.Launcher.Infrastructure/Storage/JsonStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs index 20b2e4e2105..500b0829ee2 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -69,7 +69,7 @@ private T LoadDefault() var data = JsonSerializer.Deserialize(File.ReadAllText(BackupFilePath)); if (data != null) { - Log.Info($"|JsonStorage.Load|Load backup file {BackupFilePath} successfully"); + Log.Info($"|JsonStorage.Load|Failed to load settings.json, {BackupFilePath} restored successfully"); File.Replace(BackupFilePath, FilePath, null); return data; }