From da5d1242a3d6db659baa2e5dd1392f6a97123fbc Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Mon, 15 Apr 2024 15:08:11 -0500 Subject: [PATCH] Recover from corrupted etags.json --- Core/Repositories/RepositoryDataManager.cs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Core/Repositories/RepositoryDataManager.cs b/Core/Repositories/RepositoryDataManager.cs index 4e76e8363..3baef358c 100644 --- a/Core/Repositories/RepositoryDataManager.cs +++ b/Core/Repositories/RepositoryDataManager.cs @@ -146,9 +146,7 @@ public UpdateResult Update(Repository[] repos, .Where(r => r.uri != null && (r.uri.IsFile || skipETags - || (!etags.TryGetValue(r.uri, out string etag) - || !File.Exists(GetRepoDataPath(r)) - || etag != Net.CurrentETag(r.uri)))) + || repoDataStale(r))) .ToArray(); if (toUpdate.Length < 1) { @@ -240,11 +238,15 @@ public UpdateResult Update(Repository[] repos, /// public event Action Updated; + #region ETags + private void loadETags() { try { - etags = JsonConvert.DeserializeObject>(File.ReadAllText(etagsPath)); + etags = JsonConvert.DeserializeObject>(File.ReadAllText(etagsPath)) + // An empty or all-null file can deserialize as null + ?? new Dictionary(); } catch { @@ -270,6 +272,16 @@ private void setETag(Uri url, string filename, Exception error, string etag) } } + private bool repoDataStale(Repository r) + // No ETag on file + => !etags.TryGetValue(r.uri, out string etag) + // No data on disk + || !File.Exists(GetRepoDataPath(r)) + // Current ETag doesn't match + || etag != Net.CurrentETag(r.uri); + + #endregion + private RepositoryData GetRepoData(Repository repo) => repositoriesData.TryGetValue(repo, out RepositoryData data) ? data