Verify cached pack zips against the release SHA256SUMS file.
This commit is contained in:
1 parent
428760fef6
commit
ad18b93b7a
3 files changed
+130
-12
No files matched your search
@@ -466,24 +466,63 @@ public sealed class MainForm : Form
|
||||
"cache");
|
||||
PathUtils.EnsureDirectory(cacheDir);
|
||||
|
||||
var sumsAsset = _gitea.FindChecksumAsset(_latestRelease)
|
||||
?? throw new InvalidOperationException(
|
||||
$"Release {_latestRelease.TagName} has no SHA256SUMS asset.");
|
||||
|
||||
Log($"Fetching {sumsAsset.Name}...");
|
||||
var sumsText = await _gitea.DownloadAssetTextAsync(sumsAsset, token);
|
||||
var expectedHash = Sha256Sums.FindHash(sumsText, asset.Name)
|
||||
?? throw new InvalidOperationException(
|
||||
$"{sumsAsset.Name} has no entry for {asset.Name}.");
|
||||
|
||||
var zipPath = Path.Combine(cacheDir, asset.Name);
|
||||
if (!File.Exists(zipPath))
|
||||
var cacheMatches = false;
|
||||
if (File.Exists(zipPath))
|
||||
{
|
||||
Log($"Checking cached {asset.Name}...");
|
||||
var cachedHash = await Sha256Sums.HashFileAsync(zipPath, token);
|
||||
cacheMatches = Sha256Sums.Matches(cachedHash, expectedHash);
|
||||
if (cacheMatches)
|
||||
Log("Cached pack matches SHA256SUMS.");
|
||||
else
|
||||
{
|
||||
Log("Cached pack hash differs from SHA256SUMS. Downloading again.");
|
||||
PathUtils.DeletePath(zipPath);
|
||||
}
|
||||
}
|
||||
|
||||
if (!cacheMatches)
|
||||
{
|
||||
Log($"Downloading {asset.Name} ({asset.Size / (1024 * 1024)} MB)...");
|
||||
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
||||
try
|
||||
{
|
||||
var (downloaded, total) = p;
|
||||
if (total > 0)
|
||||
await _gitea.DownloadAssetAsync(asset, zipPath, new Progress<(long, long)>(p =>
|
||||
{
|
||||
var pct = (int)(downloaded * 100 / total);
|
||||
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
||||
var (downloaded, total) = p;
|
||||
if (total > 0)
|
||||
{
|
||||
var pct = (int)(downloaded * 100 / total);
|
||||
_progressBar.Value = Math.Clamp(pct, 0, 100);
|
||||
}
|
||||
}), token);
|
||||
|
||||
Log("Verifying download...");
|
||||
var downloadedHash = await Sha256Sums.HashFileAsync(zipPath, token);
|
||||
if (!Sha256Sums.Matches(downloadedHash, expectedHash))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"SHA256 mismatch for {asset.Name}. Expected {expectedHash}, got {downloadedHash}.");
|
||||
}
|
||||
}), token);
|
||||
Log("Download complete.");
|
||||
}
|
||||
else
|
||||
{
|
||||
Log($"Using cached pack: {zipPath}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (File.Exists(zipPath))
|
||||
PathUtils.DeletePath(zipPath);
|
||||
throw;
|
||||
}
|
||||
|
||||
Log("Download verified.");
|
||||
}
|
||||
|
||||
var extractRoot = Path.Combine(cacheDir, "extract", _latestRelease.TagName);
|
||||
|
||||
Reference in new issue
Block a user