-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDownloadUtils.cs
More file actions
166 lines (138 loc) · 6.34 KB
/
DownloadUtils.cs
File metadata and controls
166 lines (138 loc) · 6.34 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
namespace Xamarin.Android.Tools
{
/// <summary>
/// Shared helpers for downloading files, verifying checksums, and extracting archives.
/// </summary>
static class DownloadUtils
{
const int BufferSize = 81920;
const long BytesPerMB = 1024 * 1024;
static readonly char[] WhitespaceChars = [' ', '\t', '\n', '\r'];
static Task<Stream> ReadAsStreamAsync (HttpContent content, CancellationToken cancellationToken)
{
#if NET5_0_OR_GREATER
return content.ReadAsStreamAsync (cancellationToken);
#else
return content.ReadAsStreamAsync ();
#endif
}
static Task<string> ReadAsStringAsync (HttpContent content, CancellationToken cancellationToken)
{
#if NET5_0_OR_GREATER
return content.ReadAsStringAsync (cancellationToken);
#else
return content.ReadAsStringAsync ();
#endif
}
/// <summary>Downloads a file from the given URL with optional progress reporting.</summary>
public static async Task DownloadFileAsync (HttpClient client, string url, string destinationPath, long expectedSize, IProgress<(double percent, string message)>? progress, CancellationToken cancellationToken)
{
using var response = await client.GetAsync (url, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait (false);
response.EnsureSuccessStatusCode ();
var totalBytes = response.Content.Headers.ContentLength ?? expectedSize;
using var contentStream = await ReadAsStreamAsync (response.Content, cancellationToken).ConfigureAwait (false);
var dirPath = Path.GetDirectoryName (destinationPath);
if (!string.IsNullOrEmpty (dirPath))
Directory.CreateDirectory (dirPath);
using var fileStream = new FileStream (destinationPath, FileMode.Create, FileAccess.Write, FileShare.None, BufferSize, useAsync: true);
var buffer = ArrayPool<byte>.Shared.Rent (BufferSize);
try {
long totalRead = 0;
int bytesRead;
while ((bytesRead = await contentStream.ReadAsync (buffer, 0, buffer.Length, cancellationToken).ConfigureAwait (false)) > 0) {
await fileStream.WriteAsync (buffer, 0, bytesRead, cancellationToken).ConfigureAwait (false);
totalRead += bytesRead;
if (progress is not null && totalBytes > 0) {
var pct = (double) totalRead / totalBytes * 100;
progress.Report ((pct, $"Downloaded {totalRead / BytesPerMB} MB / {totalBytes / BytesPerMB} MB"));
}
}
}
finally {
ArrayPool<byte>.Shared.Return (buffer);
}
}
/// <summary>Verifies a file's SHA-256 hash against an expected value.</summary>
public static void VerifyChecksum (string filePath, string expectedChecksum)
{
using var sha256 = SHA256.Create ();
using var stream = File.OpenRead (filePath);
var hash = sha256.ComputeHash (stream);
var actual = BitConverter.ToString (hash).Replace ("-", "").ToLowerInvariant ();
if (!string.Equals (actual, expectedChecksum, StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException ($"Checksum verification failed. Expected: {expectedChecksum}, Actual: {actual}");
}
/// <summary>Extracts a ZIP archive with Zip Slip protection.</summary>
public static void ExtractZipSafe (string archivePath, string destinationPath, CancellationToken cancellationToken)
{
using var archive = ZipFile.OpenRead (archivePath);
var fullExtractRoot = Path.GetFullPath (destinationPath);
foreach (var entry in archive.Entries) {
cancellationToken.ThrowIfCancellationRequested ();
if (string.IsNullOrEmpty (entry.Name))
continue;
var destinationFile = Path.GetFullPath (Path.Combine (fullExtractRoot, entry.FullName));
// Zip Slip protection
if (!FileUtil.IsUnderDirectory (destinationFile, fullExtractRoot)) {
throw new InvalidOperationException ($"Archive entry '{entry.FullName}' would extract outside target directory.");
}
var entryDir = Path.GetDirectoryName (destinationFile);
if (!string.IsNullOrEmpty (entryDir))
Directory.CreateDirectory (entryDir);
entry.ExtractToFile (destinationFile, overwrite: true);
}
}
/// <summary>Extracts a tar.gz archive using the system tar command.</summary>
public static async Task ExtractTarGzAsync (string archivePath, string destinationPath, Action<TraceLevel, string> logger, CancellationToken cancellationToken)
{
var psi = ProcessUtils.CreateProcessStartInfo ("/usr/bin/tar", "-xzf", archivePath, "-C", destinationPath);
using var stdout = new StringWriter ();
using var stderr = new StringWriter ();
var exitCode = await ProcessUtils.StartProcess (psi, stdout: stdout, stderr: stderr, cancellationToken).ConfigureAwait (false);
if (exitCode != 0) {
var errorOutput = stderr.ToString ();
logger (TraceLevel.Error, $"tar extraction failed (exit code {exitCode}): {errorOutput}");
throw new IOException ($"Failed to extract archive '{archivePath}': {errorOutput}");
}
}
/// <summary>Fetches a SHA-256 checksum from a remote URL, returning null on failure.</summary>
public static async Task<string?> FetchChecksumAsync (HttpClient httpClient, string checksumUrl, string label, Action<TraceLevel, string> logger, CancellationToken cancellationToken)
{
try {
using var response = await httpClient.GetAsync (checksumUrl, cancellationToken).ConfigureAwait (false);
response.EnsureSuccessStatusCode ();
var content = await ReadAsStringAsync (response.Content, cancellationToken).ConfigureAwait (false);
var checksum = ParseChecksumFile (content);
logger (TraceLevel.Verbose, $"{label}: checksum={checksum}");
return checksum;
}
catch (OperationCanceledException) {
throw;
}
catch (Exception ex) {
logger (TraceLevel.Warning, $"Could not fetch checksum for {label}: {ex.Message}");
return null;
}
}
/// <summary>Parses "hash filename" or just "hash" from .sha256sum.txt content.</summary>
public static string? ParseChecksumFile (string content)
{
if (string.IsNullOrWhiteSpace (content))
return null;
var trimmed = content.Trim ();
var end = trimmed.IndexOfAny (WhitespaceChars);
return end >= 0 ? trimmed.Substring (0, end) : trimmed;
}
}
}