forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsBackupAndRestoreUtils.cs
1073 lines (923 loc) · 45.5 KB
/
SettingsBackupAndRestoreUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Threading;
using Microsoft.PowerToys.Settings.UI.Library.Utilities;
namespace Microsoft.PowerToys.Settings.UI.Library
{
public class SettingsBackupAndRestoreUtils
{
private static SettingsBackupAndRestoreUtils instance;
private (bool Success, string Severity, bool LastBackupExists, DateTime? LastRan) lastBackupSettingsResults;
private static object backupSettingsInternalLock = new object();
private static object removeOldBackupsLock = new object();
public DateTime LastBackupStartTime { get; set; }
private SettingsBackupAndRestoreUtils()
{
LastBackupStartTime = DateTime.MinValue;
}
public static SettingsBackupAndRestoreUtils Instance
{
get
{
if (instance == null)
{
instance = new SettingsBackupAndRestoreUtils();
}
return instance;
}
}
private class JsonMergeHelper
{
// mostly from https://stackoverflow.com/questions/58694837/system-text-json-merge-two-objects
// but with some update to prevent array item duplicates
public static string Merge(string originalJson, string newContent)
{
var outputBuffer = new ArrayBufferWriter<byte>();
using (JsonDocument jDoc1 = JsonDocument.Parse(originalJson))
using (JsonDocument jDoc2 = JsonDocument.Parse(newContent))
using (var jsonWriter = new Utf8JsonWriter(outputBuffer, new JsonWriterOptions { Indented = true }))
{
JsonElement root1 = jDoc1.RootElement;
JsonElement root2 = jDoc2.RootElement;
if (root1.ValueKind != JsonValueKind.Array && root1.ValueKind != JsonValueKind.Object)
{
throw new InvalidOperationException($"The original JSON document to merge new content into must be a container type. Instead it is {root1.ValueKind}.");
}
if (root1.ValueKind != root2.ValueKind)
{
return originalJson;
}
if (root1.ValueKind == JsonValueKind.Array)
{
MergeArrays(jsonWriter, root1, root2, false);
}
else
{
MergeObjects(jsonWriter, root1, root2);
}
}
return Encoding.UTF8.GetString(outputBuffer.WrittenSpan);
}
private static void MergeObjects(Utf8JsonWriter jsonWriter, JsonElement root1, JsonElement root2)
{
jsonWriter.WriteStartObject();
// Write all the properties of the first document.
// If a property exists in both documents, either:
// * Merge them, if the value kinds match (e.g. both are objects or arrays),
// * Completely override the value of the first with the one from the second, if the value kind mismatches (e.g. one is object, while the other is an array or string),
// * Or favor the value of the first (regardless of what it may be), if the second one is null (i.e. don't override the first).
foreach (JsonProperty property in root1.EnumerateObject())
{
string propertyName = property.Name;
JsonValueKind newValueKind;
if (root2.TryGetProperty(propertyName, out JsonElement newValue) && (newValueKind = newValue.ValueKind) != JsonValueKind.Null)
{
jsonWriter.WritePropertyName(propertyName);
JsonElement originalValue = property.Value;
JsonValueKind originalValueKind = originalValue.ValueKind;
if (newValueKind == JsonValueKind.Object && originalValueKind == JsonValueKind.Object)
{
MergeObjects(jsonWriter, originalValue, newValue); // Recursive call
}
else if (newValueKind == JsonValueKind.Array && originalValueKind == JsonValueKind.Array)
{
MergeArrays(jsonWriter, originalValue, newValue, false);
}
else
{
newValue.WriteTo(jsonWriter);
}
}
else
{
property.WriteTo(jsonWriter);
}
}
// Write all the properties of the second document that are unique to it.
foreach (JsonProperty property in root2.EnumerateObject())
{
if (!root1.TryGetProperty(property.Name, out _))
{
property.WriteTo(jsonWriter);
}
}
jsonWriter.WriteEndObject();
}
private static void MergeArrays(Utf8JsonWriter jsonWriter, JsonElement root1, JsonElement root2, bool allowDupes)
{
// just does one level!!!
jsonWriter.WriteStartArray();
if (allowDupes)
{
// Write all the elements from both JSON arrays
foreach (JsonElement element in root1.EnumerateArray())
{
element.WriteTo(jsonWriter);
}
foreach (JsonElement element in root2.EnumerateArray())
{
element.WriteTo(jsonWriter);
}
}
else
{
var arrayItems = new HashSet<string>();
foreach (JsonElement element in root1.EnumerateArray())
{
element.WriteTo(jsonWriter);
arrayItems.Add(element.ToString());
}
foreach (JsonElement element in root2.EnumerateArray())
{
if (!arrayItems.Contains(element.ToString()))
{
element.WriteTo(jsonWriter);
}
}
}
jsonWriter.WriteEndArray();
}
}
private static bool TryCreateDirectory(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return true;
}
}
catch (Exception ex3)
{
Logger.LogError($"There was an error in TryCreateDirectory {path}: {ex3.Message}", ex3);
return false;
}
return true;
}
private static bool TryDeleteDirectory(string path)
{
try
{
if (Directory.Exists(path))
{
Directory.Delete(path, true);
return true;
}
}
catch (Exception ex3)
{
Logger.LogError($"There was an error in TryDeleteDirectory {path}: {ex3.Message}", ex3);
return false;
}
return true;
}
/// <summary>
/// Method <c>SetRegSettingsBackupAndRestoreItem</c> helper method to write to the registry.
/// </summary>
public static void SetRegSettingsBackupAndRestoreItem(string itemName, string itemValue)
{
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft", true))
{
var ptKey = key.OpenSubKey("PowerToys", true);
if (ptKey != null)
{
ptKey.SetValue(itemName, itemValue);
}
else
{
var newPtKey = key.CreateSubKey("PowerToys");
newPtKey.SetValue(itemName, itemValue);
}
}
}
/// <summary>
/// Method <c>GetRegSettingsBackupAndRestoreRegItem</c> helper method to read from the registry.
/// </summary>
public static string GetRegSettingsBackupAndRestoreRegItem(string itemName)
{
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\PowerToys"))
{
if (key != null)
{
var val = key.GetValue(itemName);
if (val != null)
{
return val.ToString();
}
}
}
return null;
}
/// <summary>
/// Method <c>RestoreSettings</c> returns a folder that has the latest backup in it.
/// </summary>
/// <returns>
/// A tuple that indicates if the backup was done or not, and a message.
/// The message usually is a localized reference key.
/// </returns>
public (bool Success, string Message, string Severity) RestoreSettings(string appBasePath, string settingsBackupAndRestoreDir)
{
try
{
// verify inputs
if (!Directory.Exists(appBasePath))
{
return (false, $"Invalid appBasePath {appBasePath}", "Error");
}
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
return (false, $"General_SettingsBackupAndRestore_NoBackupSyncPath", "Error");
}
if (!Directory.Exists(settingsBackupAndRestoreDir))
{
Logger.LogError($"Invalid settingsBackupAndRestoreDir {settingsBackupAndRestoreDir}");
return (false, "General_SettingsBackupAndRestore_InvalidBackupLocation", "Error");
}
var latestSettingsFolder = GetLatestSettingsFolder();
if (latestSettingsFolder == null)
{
return (false, $"General_SettingsBackupAndRestore_NoBackupsFound", "Warning");
}
// get data needed for process
var backupRetoreSettings = JsonNode.Parse(GetBackupRestoreSettingsJson());
var currentSettingsFiles = GetSettingsFiles(backupRetoreSettings, appBasePath).ToList().ToDictionary(x => x.Substring(appBasePath.Length));
var backupSettingsFiles = GetSettingsFiles(backupRetoreSettings, latestSettingsFolder).ToList().ToDictionary(x => x.Substring(latestSettingsFolder.Length));
if (backupSettingsFiles.Count == 0)
{
return (false, $"General_SettingsBackupAndRestore_NoBackupsFound", "Warning");
}
var anyFilesUpdated = false;
foreach (var currentFile in backupSettingsFiles)
{
var relativePath = currentFile.Value.Substring(latestSettingsFolder.Length + 1);
var retoreFullPath = Path.Combine(appBasePath, relativePath);
var settingsToRestoreJson = GetExportVersion(backupRetoreSettings, currentFile.Key, currentFile.Value);
if (currentSettingsFiles.ContainsKey(currentFile.Key))
{
// we have a setting file to restore to
var currentSettingsFileJson = GetExportVersion(backupRetoreSettings, currentFile.Key, currentSettingsFiles[currentFile.Key]);
if (JsonNormalizer.Normalize(settingsToRestoreJson) != JsonNormalizer.Normalize(currentSettingsFileJson))
{
// the settings file needs to be updated, update the real one with non-excluded stuff...
Logger.LogInfo($"Settings file {currentFile.Key} is different and is getting updated from backup");
var newCurrentSettingsFile = JsonMergeHelper.Merge(File.ReadAllText(currentSettingsFiles[currentFile.Key]), settingsToRestoreJson);
File.WriteAllText(currentSettingsFiles[currentFile.Key], newCurrentSettingsFile);
anyFilesUpdated = true;
}
}
else
{
// we don't have anything to merge this into, we need to use it as is
Logger.LogInfo($"Settings file {currentFile.Key} is in the backup but does not exist for restore");
var thisPathToRestore = Path.Combine(appBasePath, currentFile.Key.Substring(1));
TryCreateDirectory(Path.GetDirectoryName(thisPathToRestore));
File.WriteAllText(thisPathToRestore, settingsToRestoreJson);
anyFilesUpdated = true;
}
}
if (anyFilesUpdated)
{
// something was changed do we need to return true to indicate a restart is needed.
var restartAfterRestore = (bool?)backupRetoreSettings!["RestartAfterRestore"];
if (!restartAfterRestore.HasValue || restartAfterRestore.Value)
{
return (true, $"RESTART APP", "Success");
}
else
{
return (false, $"RESTART APP", "Success");
}
}
else
{
return (false, $"General_SettingsBackupAndRestore_NothingToRestore", "Informational");
}
}
catch (Exception ex2)
{
Logger.LogError("Error in RestoreSettings, " + ex2.ToString());
return (false, $"General_SettingsBackupAndRestore_BackupError", "Error");
}
}
/// <summary>
/// Method <c>GetSettingsBackupAndRestoreDir</c> returns the path the backup and restore location.
/// </summary>
/// <remarks>
/// This will return a default location based on user documents if non is set.
/// </remarks>
public string GetSettingsBackupAndRestoreDir()
{
string settingsBackupAndRestoreDir = GetRegSettingsBackupAndRestoreRegItem("SettingsBackupAndRestoreDir");
if (settingsBackupAndRestoreDir == null)
{
settingsBackupAndRestoreDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PowerToys\\Backup");
}
return settingsBackupAndRestoreDir;
}
private IList<string> GetBackupSettingsFiles(string settingsBackupAndRestoreDir)
{
return Directory.GetFiles(settingsBackupAndRestoreDir, "settings_*.ptb", SearchOption.TopDirectoryOnly).ToList().Where(f => Regex.IsMatch(f, "settings_(\\d{1,19}).ptb")).ToList();
}
/// <summary>
/// Method <c>GetLatestSettingsFolder</c> returns a folder that has the latest backup in it.
/// </summary>
/// <remarks>
/// The backup will usually be a backup file that has to be extracted to a temp folder. This will do that for us.
/// </remarks>
private string GetLatestSettingsFolder()
{
string settingsBackupAndRestoreDir = GetSettingsBackupAndRestoreDir();
if (settingsBackupAndRestoreDir == null)
{
return null;
}
if (!Directory.Exists(settingsBackupAndRestoreDir))
{
return null;
}
var settingsBackupFolders = new Dictionary<long, string>();
var settingsBackupFiles = GetBackupSettingsFiles(settingsBackupAndRestoreDir).ToDictionary(x => long.Parse(Path.GetFileName(x).Replace("settings_", string.Empty).Replace(".ptb", string.Empty), CultureInfo.InvariantCulture));
var latestFolder = 0L;
var latestFile = 0L;
if (settingsBackupFolders.Count > 0)
{
latestFolder = settingsBackupFolders.OrderByDescending(x => x.Key).FirstOrDefault().Key;
}
if (settingsBackupFiles.Count > 0)
{
latestFile = settingsBackupFiles.OrderByDescending(x => x.Key).FirstOrDefault().Key;
}
if (latestFile == 0 && latestFolder == 0)
{
return null;
}
else if (latestFolder >= latestFile)
{
return settingsBackupFolders[latestFolder];
}
else
{
var tempPath = Path.GetTempPath();
var fullBackupDir = Path.Combine(tempPath, "PowerToys_settings_" + latestFile.ToString(CultureInfo.InvariantCulture));
lock (backupSettingsInternalLock)
{
if (!Directory.Exists(fullBackupDir) || !File.Exists(Path.Combine(fullBackupDir, "manifest.json")))
{
TryDeleteDirectory(fullBackupDir);
ZipFile.ExtractToDirectory(settingsBackupFiles[latestFile], fullBackupDir);
}
}
ThreadPool.QueueUserWorkItem((x) =>
{
try
{
RemoveOldBackups(tempPath, 1, TimeSpan.FromDays(7));
}
catch
{
// hmm, ok
}
});
return fullBackupDir;
}
}
/// <summary>
/// Method <c>GetLatestBackupFileName</c> returns the name of the newest backup file.
/// </summary>
public string GetLatestBackupFileName()
{
string settingsBackupAndRestoreDir = GetSettingsBackupAndRestoreDir();
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir) || !Directory.Exists(settingsBackupAndRestoreDir))
{
return string.Empty;
}
var settingsBackupFiles = GetBackupSettingsFiles(settingsBackupAndRestoreDir);
if (settingsBackupFiles.Count > 0)
{
return Path.GetFileName(settingsBackupFiles.OrderByDescending(x => x).First());
}
else
{
return string.Empty;
}
}
/// <summary>
/// Method <c>GetLatestSettingsBackupManifest</c> get's the meta data from a backup file.
/// </summary>
public JsonNode GetLatestSettingsBackupManifest()
{
var folder = GetLatestSettingsFolder();
if (folder == null)
{
return null;
}
return JsonNode.Parse(File.ReadAllText(Path.Combine(folder, "manifest.json")));
}
/// <summary>
/// Method <c>IsIncludeFile</c> check's to see if a settings file is to be included during backup and restore.
/// </summary>
private static bool IsIncludeFile(JsonNode settings, string name)
{
foreach (var test in (JsonArray)settings["IncludeFiles"])
{
if (Regex.IsMatch(name, WildCardToRegular(test.ToString())))
{
return true;
}
}
return false;
}
/// <summary>
/// Method <c>IsIgnoreFile</c> check's to see if a settings file is to be ignored during backup and restore.
/// </summary>
private static bool IsIgnoreFile(JsonNode settings, string name)
{
foreach (var test in (JsonArray)settings["IgnoreFiles"])
{
if (Regex.IsMatch(name, WildCardToRegular(test.ToString())))
{
return true;
}
}
return false;
}
/// <summary>
/// Class <c>GetSettingsFiles</c> returns the effective list of settings files.
/// </summary>
/// <remarks>
/// Handles all the included/exclude files.
/// </remarks>
private static string[] GetSettingsFiles(JsonNode settings, string path)
{
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
{
return Array.Empty<string>();
}
return Directory.GetFiles(path, "*.json", SearchOption.AllDirectories).Where(s => IsIncludeFile(settings, s) && !IsIgnoreFile(settings, s)).ToArray();
}
/// <summary>
/// Method <c>BackupSettings</c> does the backup process.
/// </summary>
/// <returns>
/// A tuple that indicates if the backup was done or not, and a message.
/// The message usually is a localized reference key.
/// </returns>
/// <remarks>
/// This is a wrapper for BackupSettingsInternal, so we can check the time to run.
/// </remarks>
public (bool Success, string Message, string Severity, bool LastBackupExists) BackupSettings(string appBasePath, string settingsBackupAndRestoreDir, bool dryRun)
{
var sw = Stopwatch.StartNew();
var results = BackupSettingsInternal(appBasePath, settingsBackupAndRestoreDir, dryRun);
sw.Stop();
Logger.LogInfo($"BackupSettings took {sw.ElapsedMilliseconds}");
lastBackupSettingsResults = (results.Success, results.Severity, results.LastBackupExists, DateTime.UtcNow);
return results;
}
/// <summary>
/// Method <c>DryRunBackup</c> wrapper function to do a dry-run backup
/// </summary>
public (bool Success, string Message, string Severity, bool LastBackupExists) DryRunBackup()
{
var settingsUtils = new SettingsUtils();
var appBasePath = Path.GetDirectoryName(settingsUtils.GetSettingsFilePath());
string settingsBackupAndRestoreDir = GetSettingsBackupAndRestoreDir();
var results = BackupSettings(appBasePath, settingsBackupAndRestoreDir, true);
lastBackupSettingsResults = (results.Success, results.Severity, results.LastBackupExists, DateTime.UtcNow);
return results;
}
/// <summary>
/// Method <c>GetLastBackupSettingsResults</c> gets the results from the last backup process
/// </summary>
/// <returns>
/// A tuple that indicates if the backup was done or not, and other information
/// </returns>
public (bool Success, bool HadError, bool LastBackupExists, DateTime? LastRan) GetLastBackupSettingsResults()
{
return (lastBackupSettingsResults.Success, lastBackupSettingsResults.Severity == "Error", lastBackupSettingsResults.LastBackupExists, lastBackupSettingsResults.LastRan);
}
/// <summary>
/// Method <c>BackupSettingsInternal</c> does the backup process.
/// </summary>
/// <returns>
/// A tuple that indicates if the backup was done or not, and a message.
/// The message usually is a localized reference key.
/// </returns>
private (bool Success, string Message, string Severity, bool LastBackupExists) BackupSettingsInternal(string appBasePath, string settingsBackupAndRestoreDir, bool dryRun)
{
var lastBackupExists = false;
lock (backupSettingsInternalLock)
{
// simulated delay to validate behavior
// Thread.Sleep(1000);
try
{
// verify inputs
if (!Directory.Exists(appBasePath))
{
return (false, $"Invalid appBasePath {appBasePath}", "Error", lastBackupExists);
}
if (string.IsNullOrEmpty(settingsBackupAndRestoreDir))
{
return (false, $"General_SettingsBackupAndRestore_NoBackupSyncPath", "Error", lastBackupExists);
}
if (!Path.IsPathRooted(settingsBackupAndRestoreDir))
{
return (false, $"Invalid settingsBackupAndRestoreDir, not rooted", "Error", lastBackupExists);
}
if (settingsBackupAndRestoreDir.StartsWith(appBasePath, StringComparison.InvariantCultureIgnoreCase))
{
// backup cannot be under app
Logger.LogError($"BackupSettings, backup cannot be under app");
return (false, "General_SettingsBackupAndRestore_InvalidBackupLocation", "Error", lastBackupExists);
}
var dirExists = TryCreateDirectory(settingsBackupAndRestoreDir);
if (!dirExists)
{
Logger.LogError($"Failed to create dir {settingsBackupAndRestoreDir}");
return (false, $"General_SettingsBackupAndRestore_BackupError", "Error", lastBackupExists);
}
// get data needed for process
var backupRetoreSettings = JsonNode.Parse(GetBackupRestoreSettingsJson());
var currentSettingsFiles = GetSettingsFiles(backupRetoreSettings, appBasePath).ToList().ToDictionary(x => x.Substring(appBasePath.Length));
var fullBackupDir = Path.Combine(Path.GetTempPath(), $"settings_{DateTime.UtcNow.ToFileTimeUtc().ToString(CultureInfo.InvariantCulture)}");
var latestSettingsFolder = GetLatestSettingsFolder();
var lastBackupSettingsFiles = GetSettingsFiles(backupRetoreSettings, latestSettingsFolder).ToList().ToDictionary(x => x.Substring(latestSettingsFolder.Length));
lastBackupExists = lastBackupSettingsFiles.Count > 0;
if (currentSettingsFiles.Count == 0)
{
return (false, "General_SettingsBackupAndRestore_NoSettingsFilesFound", "Error", lastBackupExists);
}
var anyFileBackedUp = false;
var skippedSettingsFiles = new Dictionary<string, (string Path, string Settings)>();
var updatedSettingsFiles = new Dictionary<string, string>();
foreach (var currentFile in currentSettingsFiles)
{
// need to check and back this up;
var currentSettingsFileToBackup = GetExportVersion(backupRetoreSettings, currentFile.Key, currentFile.Value);
var doBackup = false;
if (lastBackupSettingsFiles.ContainsKey(currentFile.Key))
{
// there is a previous backup for this, get an export version of it.
var lastSettingsFileDoc = GetExportVersion(backupRetoreSettings, currentFile.Key, lastBackupSettingsFiles[currentFile.Key]);
// check to see if the new export version would be same as last export version.
if (JsonNormalizer.Normalize(currentSettingsFileToBackup) != JsonNormalizer.Normalize(lastSettingsFileDoc))
{
doBackup = true;
Logger.LogInfo($"BackupSettings, {currentFile.Value} content is different.");
}
}
else
{
// this has never been backed up, we need to do it now.
Logger.LogInfo($"BackupSettings, {currentFile.Value} does not exists.");
doBackup = true;
}
if (doBackup)
{
// add to list of files we noted as needing backup
updatedSettingsFiles.Add(currentFile.Key, currentFile.Value);
// mark overall flag that a backup will be made
anyFileBackedUp = true;
// write the export version of the settings file to backup location.
var relativePath = currentFile.Value.Substring(appBasePath.Length + 1);
var backupFullPath = Path.Combine(fullBackupDir, relativePath);
TryCreateDirectory(fullBackupDir);
TryCreateDirectory(Path.GetDirectoryName(backupFullPath));
Logger.LogInfo($"BackupSettings writing, {backupFullPath}, dryRun:{dryRun}.");
if (!dryRun)
{
File.WriteAllText(backupFullPath, currentSettingsFileToBackup);
}
}
else
{
// if we found no reason to backup this settings file, record that in this collection
skippedSettingsFiles.Add(currentFile.Key, (currentFile.Value, currentSettingsFileToBackup));
}
}
if (!anyFileBackedUp)
{
// nothing was done!
return (false, $"General_SettingsBackupAndRestore_NothingToBackup", "Informational", lastBackupExists);
}
// add skipped.
foreach (var currentFile in skippedSettingsFiles)
{
// if we did do a backup, we need to copy in all the settings files we skipped so the backup is complete.
// this is needed since we might use the backup on another machine/
var relativePath = currentFile.Value.Path.Substring(appBasePath.Length + 1);
var backupFullPath = Path.Combine(fullBackupDir, relativePath);
Logger.LogInfo($"BackupSettings writing, {backupFullPath}, dryRun:{dryRun}");
if (!dryRun)
{
TryCreateDirectory(fullBackupDir);
TryCreateDirectory(Path.GetDirectoryName(backupFullPath));
File.WriteAllText(backupFullPath, currentFile.Value.Settings);
}
}
// add manifest
var manifestData = new
{
CreateDateTime = DateTime.UtcNow.ToString("u", CultureInfo.InvariantCulture),
@Version = Helper.GetProductVersion(),
UpdatedFiles = updatedSettingsFiles.Keys.ToList(),
BackupSource = Environment.MachineName,
UnchangedFiles = skippedSettingsFiles.Keys.ToList(),
};
var manifest = JsonSerializer.Serialize(manifestData, new JsonSerializerOptions() { WriteIndented = true });
if (!dryRun)
{
File.WriteAllText(Path.Combine(fullBackupDir, "manifest.json"), manifest);
// clean up, to prevent runaway disk usage.
RemoveOldBackups(settingsBackupAndRestoreDir, 10, TimeSpan.FromDays(60));
// compress the backup
var zipName = Path.Combine(settingsBackupAndRestoreDir, Path.GetFileName(fullBackupDir) + ".ptb");
ZipFile.CreateFromDirectory(fullBackupDir, zipName);
TryDeleteDirectory(fullBackupDir);
}
return (true, $"General_SettingsBackupAndRestore_BackupComplete", "Success", lastBackupExists);
}
catch (Exception ex2)
{
Logger.LogError($"There was an error: {ex2.Message}", ex2);
return (false, $"General_SettingsBackupAndRestore_BackupError", "Error", lastBackupExists);
}
}
}
/// <summary>
/// Searches for the config file (Json) in two possible paths and returns its content.
/// </summary>
/// <returns>Returns the content of the config file (Json) as string.</returns>
/// <exception cref="FileNotFoundException">Thrown if file is not found.</exception>
/// <remarks>If the settings window is launched from an installed instance of PT we need the path "...\Settings\\backup_restore_settings.json" and if the settings window is launched from a local VS build of PT we need the path "...\backup_restore_settings.json".</remarks>
private static string GetBackupRestoreSettingsJson()
{
if (File.Exists("backup_restore_settings.json"))
{
return File.ReadAllText("backup_restore_settings.json");
}
else if (File.Exists("Settings\\backup_restore_settings.json"))
{
return File.ReadAllText("Settings\\backup_restore_settings.json");
}
else
{
throw new FileNotFoundException($"The backup_restore_settings.json could not be found at {Environment.CurrentDirectory}");
}
}
/// <summary>
/// Method <c>WildCardToRegular</c> is so we can use 'normal' wildcard syntax and instead of regex
/// </summary>
private static string WildCardToRegular(string value)
{
return "^" + Regex.Escape(value).Replace("\\*", ".*") + "$";
}
/// <summary>
/// Method <c>GetExportVersion</c> gets the version of the settings file that we want to backup.
/// It will be formatted and all problematic settings removed from it.
/// </summary>
public static string GetExportVersion(JsonNode backupRetoreSettings, string settingFileKey, string settingsFileName)
{
var ignoredSettings = GetIgnoredSettings(backupRetoreSettings, settingFileKey);
var settingsFile = JsonDocument.Parse(File.ReadAllText(settingsFileName));
var outputBuffer = new ArrayBufferWriter<byte>();
using (var jsonWriter = new Utf8JsonWriter(outputBuffer, new JsonWriterOptions { Indented = true }))
{
jsonWriter.WriteStartObject();
foreach (var property in settingsFile.RootElement.EnumerateObject().OrderBy(p => p.Name))
{
if (!ignoredSettings.Contains(property.Name))
{
property.WriteTo(jsonWriter);
}
}
jsonWriter.WriteEndObject();
}
if (settingFileKey.Equals("\\PowerToys Run\\settings.json", StringComparison.OrdinalIgnoreCase))
{
// PowerToys Run hack fix-up
var ptRunIgnoredSettings = GetPTRunIgnoredSettings(backupRetoreSettings);
var ptrSettings = JsonNode.Parse(Encoding.UTF8.GetString(outputBuffer.WrittenSpan));
foreach (JsonObject pluginToChange in ptRunIgnoredSettings)
{
foreach (JsonObject plugin in (JsonArray)ptrSettings["plugins"])
{
if (plugin["Id"].ToString() == pluginToChange["Id"].ToString())
{
foreach (var nameOfPropertyToRemove in (JsonArray)pluginToChange["Names"])
{
plugin.Remove(nameOfPropertyToRemove.ToString());
}
}
}
}
return ptrSettings.ToJsonString(new JsonSerializerOptions { WriteIndented = true });
}
else
{
return JsonNode.Parse(Encoding.UTF8.GetString(outputBuffer.WrittenSpan)).ToJsonString(new JsonSerializerOptions { WriteIndented = true });
}
}
/// <summary>
/// Method <c>GetPTRunIgnoredSettings</c> gets the 'Run-Plugin-level' settings we should ignore because they are problematic to backup/restore.
/// </summary>
private static JsonArray GetPTRunIgnoredSettings(JsonNode backupRetoreSettings)
{
if (backupRetoreSettings == null)
{
throw new ArgumentNullException(nameof(backupRetoreSettings));
}
if (backupRetoreSettings["IgnoredPTRunSettings"] != null)
{
return (JsonArray)backupRetoreSettings["IgnoredPTRunSettings"];
}
return new JsonArray();
}
/// <summary>
/// Method <c>GetIgnoredSettings</c> gets the 'top-level' settings we should ignore because they are problematic to backup/restore.
/// </summary>
private static string[] GetIgnoredSettings(JsonNode backupRetoreSettings, string settingFileKey)
{
if (backupRetoreSettings == null)
{
throw new ArgumentNullException(nameof(backupRetoreSettings));
}
if (settingFileKey.StartsWith("\\", StringComparison.OrdinalIgnoreCase))
{
settingFileKey = settingFileKey.Substring(1);
}
if (backupRetoreSettings["IgnoredSettings"] != null)
{
if (backupRetoreSettings["IgnoredSettings"][settingFileKey] != null)
{
var settingsArray = (JsonArray)backupRetoreSettings["IgnoredSettings"][settingFileKey];
Console.WriteLine("settingsArray " + settingsArray.GetType().FullName);
var settingsList = new List<string>();
foreach (var setting in settingsArray)
{
settingsList.Add(setting.ToString());
}
return settingsList.ToArray();
}
else
{
return Array.Empty<string>();
}
}
return Array.Empty<string>();
}
/// <summary>
/// Method <c>RemoveOldBackups</c> is a helper that prevents is from having some runaway disk usages.
/// </summary>
private static void RemoveOldBackups(string location, int minNumberToKeep, TimeSpan deleteIfOlderThanTs)
{
if (!Monitor.TryEnter(removeOldBackupsLock, 1000))
{
return;
}
try
{
DateTime deleteIfOlder = DateTime.UtcNow.Subtract(deleteIfOlderThanTs);
var settingsBackupFolders = Directory.GetDirectories(location, "settings_*", SearchOption.TopDirectoryOnly).ToList().Where(f => Regex.IsMatch(f, "settings_(\\d{1,19})")).ToDictionary(x => long.Parse(Path.GetFileName(x).Replace("settings_", string.Empty), CultureInfo.InvariantCulture)).ToList();
settingsBackupFolders.AddRange(Directory.GetDirectories(location, "PowerToys_settings_*", SearchOption.TopDirectoryOnly).ToList().Where(f => Regex.IsMatch(f, "PowerToys_settings_(\\d{1,19})")).ToDictionary(x => long.Parse(Path.GetFileName(x).Replace("PowerToys_settings_", string.Empty), CultureInfo.InvariantCulture)));
var settingsBackupFiles = Directory.GetFiles(location, "settings_*.ptb", SearchOption.TopDirectoryOnly).ToList().Where(f => Regex.IsMatch(f, "settings_(\\d{1,19}).ptb")).ToDictionary(x => long.Parse(Path.GetFileName(x).Replace("settings_", string.Empty).Replace(".ptb", string.Empty), CultureInfo.InvariantCulture));
if (settingsBackupFolders.Count + settingsBackupFiles.Count <= minNumberToKeep)
{
return;
}
foreach (var item in settingsBackupFolders)
{
var backupTime = DateTime.FromFileTimeUtc(item.Key);
if (item.Value.Contains("PowerToys_settings_", StringComparison.OrdinalIgnoreCase))
{
// this is a temp backup and we want to clean based on the time it was created in the temp place, not the time the backup was made.
var folderCreatedTime = new DirectoryInfo(item.Value).CreationTimeUtc;
if (folderCreatedTime > backupTime)
{
backupTime = folderCreatedTime;
}
}
if (backupTime < deleteIfOlder)
{
try
{
Logger.LogInfo($"RemoveOldBackups killing {item.Value}");
Directory.Delete(item.Value, true);
}
catch (Exception ex2)
{
Logger.LogError($"Failed to remove a setting backup folder ({item.Value}), because: ({ex2.Message})");
}
}
}
foreach (var item in settingsBackupFiles)
{
var backupTime = DateTime.FromFileTimeUtc(item.Key);
if (backupTime < deleteIfOlder)
{
try
{
Logger.LogInfo($"RemoveOldBackups killing {item.Value}");
File.Delete(item.Value);
}
catch (Exception ex2)
{
Logger.LogError($"Failed to remove a setting backup folder ({item.Value}), because: ({ex2.Message})");
}
}
}
}
finally
{
Monitor.Exit(removeOldBackupsLock);
}
}
/// <summary>
/// Class <c>JsonNormalizer</c> is a utility class to 'normalize' a JSON file so that it can be compared to another JSON file.
/// This really just means to fully sort it. This does not work for any JSON file where the order of the node is relevant.
/// </summary>
private class JsonNormalizer
{
public static string Normalize(string json)
{
var doc1 = JsonNormalizer.Deserialize(json);
var newJson1 = JsonSerializer.Serialize(doc1, new JsonSerializerOptions { WriteIndented = true });
return newJson1;
}