Skip to content

Commit

Permalink
Merge #4078 Recover from corrupted etags.json
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 15, 2024
2 parents 4de7a79 + da5d124 commit 02c84ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ All notable changes to this project will be documented in this file.
- [GUI] Use better console hiding API (#4051 by: HebaruSan)
- [Core] Trigger progress updates for frozen downloads (#4052 by: HebaruSan)
- [GUI] Fix NRE on trying to update all when there's nothing to update (#4054 by: HebaruSan)
- [Core] Tolerate null repo URLs (#4077 by: HebaruSan)
- [Core] Fix NRE in repo update with corrupted etags.json file (#4077, #4078 by: HebaruSan)

### Internal

Expand Down
20 changes: 16 additions & 4 deletions Core/Repositories/RepositoryDataManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -240,11 +238,15 @@ public UpdateResult Update(Repository[] repos,
/// </summary>
public event Action<Repository[]> Updated;

#region ETags

private void loadETags()
{
try
{
etags = JsonConvert.DeserializeObject<Dictionary<Uri, string>>(File.ReadAllText(etagsPath));
etags = JsonConvert.DeserializeObject<Dictionary<Uri, string>>(File.ReadAllText(etagsPath))
// An empty or all-null file can deserialize as null
?? new Dictionary<Uri, string>();
}
catch
{
Expand All @@ -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
Expand Down

0 comments on commit 02c84ba

Please sign in to comment.