Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from corrupted etags.json #4078

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading