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 | 89 +++++++++++-------- .../Storage/PluginJsonStorage.cs | 2 +- 3 files changed, 56 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Helper.cs b/Flow.Launcher.Infrastructure/Helper.cs index faa4c93b5..db575de90 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 @@ namespace Flow.Launcher.Infrastructure /// /// 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 0083ccb87..20b2e4e21 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) - { - try - { - _data = JsonSerializer.Deserialize(serialized); - } - catch (JsonException e) - { - LoadDefault(); - Log.Exception($"|JsonStorage.Deserialize|Deserialize error for json <{FilePath}>", e); - } - - if (_data == null) - { - LoadDefault(); - } - } - - private void LoadDefault() + private T LoadDefault() { if (File.Exists(FilePath)) { BackupOriginFile(); } - _data = new T(); - Save(); + return new T(); + } + + private T? TryLoadBackup() + { + if (!File.Exists(BackupFilePath)) + return default; + + try + { + 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; + } } private void BackupOriginFile() @@ -82,13 +94,14 @@ namespace Flow.Launcher.Infrastructure.Storage 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 923a1a6b5..abe3f55b5 100644 --- a/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/PluginJsonStorage.cs @@ -18,7 +18,7 @@ namespace Flow.Launcher.Infrastructure.Storage 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 | 110 ++++++++++++++-------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index a91287cc0..612ee3957 100644 --- a/.github/actions/spelling/expect.txt +++ b/.github/actions/spelling/expect.txt @@ -1,64 +1,64 @@ -crowdin -DWM -workflows -wpf actionkeyword -stackoverflow -Wox -flowlauncher -Fody -stackoverflow -IContext -IShell -IPlugin -appveyor -netflix -youtube -appdata -Prioritise -Segoe -Google -Customise -UWP -uwp -Bokmal -Bokm -uninstallation -uninstalling -voidtools -fullscreen -hotkeys -totalcmd -lnk amazonaws -mscorlib -pythonw +appdata +appref +appref-ms +appveyor +baidu +bak +Bokm +Bokmal +CMD +crowdin +Customise +dlgtext dotnet -winget -jjw24 -wolframalpha -gmail +dpi duckduckgo +DWM +EWX facebook findicon -baidu -pls -websearch -qianlifeng -userdata -srchadmin -EWX -dlgtext -CMD -appref-ms -appref -TSource -runas -dpi -popup -ptr -pluginindicator -TobiasSekan +flowlauncher +Fody +fullscreen +gmail +Google +hotkeys +IContext Img img +IPlugin +IShell +jjw24 +lnk +mscorlib +netflix +pls +pluginindicator +popup +Prioritise +ptr +pythonw +qianlifeng resx +runas +Segoe +srchadmin +stackoverflow +TobiasSekan +totalcmd +TSource +uninstallation +uninstalling +userdata +UWP +uwp +voidtools +websearch +winget +wolframalpha +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 | 108 ++++++++++++++-------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/.github/actions/spelling/expect.txt b/.github/actions/spelling/expect.txt index 612ee3957..a91287cc0 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 -IContext -Img -img -IPlugin -IShell -jjw24 -lnk -mscorlib -netflix -pls -pluginindicator -popup -Prioritise -ptr -pythonw -qianlifeng -resx -runas -Segoe -srchadmin stackoverflow -TobiasSekan -totalcmd -TSource -uninstallation -uninstalling -userdata +IContext +IShell +IPlugin +appveyor +netflix +youtube +appdata +Prioritise +Segoe +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 a91287cc0..ba6faec9a 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 20b2e4e21..500b0829e 100644 --- a/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs +++ b/Flow.Launcher.Infrastructure/Storage/JsonStorage.cs @@ -69,7 +69,7 @@ namespace Flow.Launcher.Infrastructure.Storage 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; }