gvsn 1.0.1

A fast, cross-platform Go version manager written in Rust
Documentation
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! Single-stream download engine with resume and retry support.
//!
//! [`fetch`] is the single entry point for all downloads in `gvsn`. Every
//! download uses one HTTP connection end to end - no chunking, no
//! multi-bar coordination, nothing to race against. A large read buffer
//! keeps per-syscall overhead low so the stream can use as much of the
//! link's actual throughput as the OS scheduler allows.
//!
//! There is no separate HEAD probe: go.dev's download links 302-redirect to
//! a CDN host, and a HEAD request pays that redirect round-trip just to
//! learn the file size. The GET response carries the same size headers, so
//! [`try_fetch`] reads them off the real transfer instead of a throwaway
//! request.
//!
//! Resume is transparent: if a `.part` file already exists from a previous
//! attempt, the download picks up from the last written byte using a
//! `Range: bytes=N-` request. If the server ignores the Range header (no
//! `Accept-Ranges` support) the transfer restarts from scratch.
//!
//! On any network error the download retries up to [`HttpClient::retries`]
//! times using exponential back-off (1 s, 2 s, 4 s, …).
//!
//! [`verify_sha256`] checks the integrity of a completed file against the
//! expected hex digest published by go.dev.

use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;

use anyhow::{bail, Context, Result};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use sha2::{Digest, Sha256};

use crate::http::HttpClient;

/// Size of the buffer used to read the response body. Large enough to keep
/// per-syscall overhead low and let the single stream use as much of the
/// link's throughput as possible.
const READ_BUF_SIZE: usize = 1024 * 1024;

/// Downloads `url` to `dest` over a single HTTP stream.
///
/// Reads the retry limit from the `HttpClient`.
///
/// # Errors
///
/// Returns an error if the server is unreachable, all retries are
/// exhausted, or `dest` cannot be written to.
pub fn fetch(client: &HttpClient, url: &str, dest: &Path) -> Result<()> {
    let retries = client.retries();
    fetch_single(client, url, dest, retries)
}

/// Verifies the SHA-256 digest of `file` against the `expected` hex string.
///
/// If `expected` is empty the check is skipped (some older go.dev entries do
/// not include a checksum). When the digests differ an informative error is
/// returned that includes both the expected and actual values.
///
/// # Errors
///
/// Returns an error if `file` cannot be read or the digest does not match.
pub fn verify_sha256(file: &Path, expected: &str) -> Result<()> {
    if expected.is_empty() {
        eprintln!(
            "  {} No published checksum for {} - integrity not verified.",
            "!".yellow(),
            file.display()
        );
        return Ok(());
    }

    let mut hasher = Sha256::new();
    let mut f = std::fs::File::open(file)
        .with_context(|| format!("Cannot open {} for checksum", file.display()))?;
    let mut buf = [0u8; 65_536];
    loop {
        let n = f
            .read(&mut buf)
            .context("Failed to read file for checksum")?;
        if n == 0 {
            break;
        }
        hasher.update(&buf[..n]);
    }

    let actual: String = hasher
        .finalize()
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect();

    if actual != expected {
        bail!(
            "Checksum mismatch for {}!\n  expected: {expected}\n  got:      {actual}",
            file.display()
        );
    }
    Ok(())
}

/// Single-connection download with retry and transparent resume.
fn fetch_single(client: &HttpClient, url: &str, dest: &Path, retries: u8) -> Result<()> {
    let part = part_path(dest);
    let mut attempt = 0u8;

    loop {
        let existing = part.metadata().map(|m| m.len()).unwrap_or(0);

        match try_fetch(client, url, &part, existing) {
            Ok(()) => {
                std::fs::rename(&part, dest)
                    .with_context(|| format!("Cannot finalise {}", dest.display()))?;
                return Ok(());
            }
            Err(e) => {
                if attempt >= retries {
                    let _ = std::fs::remove_file(&part);
                    return Err(e).context(format!("Download failed after {retries} retries"));
                }
                attempt += 1;
                eprintln!(
                    "  {} Network error, retrying ({}/{retries})...",
                    "!".yellow(),
                    attempt
                );
                thread::sleep(Duration::from_secs(backoff(attempt)));
            }
        }
    }
}

/// Performs a single GET request and streams the body to `part` with a
/// large read buffer ([`READ_BUF_SIZE`]) to keep per-syscall overhead low.
///
/// Sizes the progress bar from the GET response's own headers - no separate
/// HEAD request, so there is only ever one redirect round-trip per attempt
/// instead of two.
fn try_fetch(client: &HttpClient, url: &str, part: &Path, offset: u64) -> Result<()> {
    crate::http::log_request(client, "GET", url);

    let mut req = client
        .agent()
        .get(url)
        // Prevent transparent gzip so the raw binary body is never decoded.
        .header("Accept-Encoding", "identity");
    if offset > 0 {
        req = req.header("Range", &format!("bytes={offset}-"));
    }

    let mut response = req
        .call()
        .with_context(|| format!("Failed to connect to {url}"))?;

    crate::http::log_response(
        client,
        response.status().as_u16(),
        response.status().canonical_reason().unwrap_or(""),
        response.headers(),
    );

    // 206 means the server honoured our Range header and we can append.
    // Any other status (200 for a server that ignores Range) means restart.
    let resuming = offset > 0 && response.status().as_u16() == 206;

    // go.dev's CDN always reports the full file size via
    // x-identity-content-length, even on a 206 response, so it doesn't need
    // adjusting for the current offset. Plain content-length is only the
    // size of the remaining bytes on a 206, so add offset back to get the
    // total for the progress bar.
    let headers = response.headers();
    let total_length = headers
        .get("x-identity-content-length")
        .and_then(|v| v.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok())
        .unwrap_or_else(|| {
            let len = headers
                .get("content-length")
                .and_then(|v| v.to_str().ok())
                .and_then(|s| s.parse::<u64>().ok())
                .unwrap_or(0);
            if resuming {
                offset + len
            } else {
                len
            }
        });

    let pb = progress_bar(total_length, if resuming { offset } else { 0 });
    pb.enable_steady_tick(Duration::from_millis(120));

    let mut file: File = if resuming {
        OpenOptions::new()
            .append(true)
            .open(part)
            .with_context(|| format!("Cannot open {} for appending", part.display()))?
    } else {
        File::create(part).with_context(|| format!("Cannot create {}", part.display()))?
    };

    let mut reader = response.body_mut().as_reader();
    let mut buf = vec![0u8; READ_BUF_SIZE];
    let result = (|| -> Result<()> {
        loop {
            let n = reader.read(&mut buf).context("Download interrupted")?;
            if n == 0 {
                break;
            }
            file.write_all(&buf[..n]).context("Write failed")?;
            pb.inc(n as u64);
        }
        Ok(())
    })();

    pb.finish_and_clear();
    result
}

/// Returns the path used for the in-progress download: `"{dest}.part"`.
fn part_path(dest: &Path) -> PathBuf {
    PathBuf::from(format!("{}.part", dest.to_string_lossy()))
}

/// Builds the progress bar for the download.
///
/// When `content_length > 0` returns a determinate bar pre-positioned at
/// `existing` bytes (to reflect a resumed transfer). Otherwise returns a
/// spinner.
fn progress_bar(content_length: u64, existing: u64) -> ProgressBar {
    if content_length > 0 {
        let bar = ProgressBar::new(content_length);
        bar.set_position(existing);
        bar.set_style(
            ProgressStyle::default_bar()
                .template(
                    "  [{bar:40.cyan/blue}] {bytes}/{total_bytes}  {bytes_per_sec}  eta {eta}",
                )
                .unwrap()
                .progress_chars("=>-"),
        );
        bar
    } else {
        let bar = ProgressBar::new_spinner();
        bar.set_style(
            ProgressStyle::default_spinner()
                .template("  {spinner:.cyan}  {bytes}  {bytes_per_sec}")
                .unwrap(),
        );
        bar
    }
}

/// Exponential back-off in seconds for retry attempt `n` (1-based): 1, 2, 4, 8 …
fn backoff(n: u8) -> u64 {
    2u64.pow(n as u32 - 1)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn part_path_appends_suffix() {
        let dest = Path::new("/tmp/go1.22.4.linux-amd64.tar.gz");
        assert_eq!(
            part_path(dest),
            PathBuf::from("/tmp/go1.22.4.linux-amd64.tar.gz.part")
        );
    }

    #[test]
    fn backoff_doubles_each_attempt() {
        assert_eq!(backoff(1), 1);
        assert_eq!(backoff(2), 2);
        assert_eq!(backoff(3), 4);
        assert_eq!(backoff(4), 8);
    }

    #[test]
    fn verify_sha256_accepts_matching_digest() {
        let dir = tempdir().unwrap();
        let file = dir.path().join("data.bin");
        std::fs::write(&file, b"hello world").unwrap();

        // sha256("hello world")
        let expected = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
        verify_sha256(&file, expected).unwrap();
    }

    #[test]
    fn verify_sha256_rejects_mismatched_digest() {
        let dir = tempdir().unwrap();
        let file = dir.path().join("data.bin");
        std::fs::write(&file, b"hello world").unwrap();

        let err = verify_sha256(
            &file,
            "0000000000000000000000000000000000000000000000000000000000000000",
        )
        .unwrap_err();
        assert!(err.to_string().contains("Checksum mismatch"));
    }

    #[test]
    fn verify_sha256_skips_check_when_expected_is_empty() {
        let dir = tempdir().unwrap();
        let file = dir.path().join("data.bin");
        std::fs::write(&file, b"anything").unwrap();

        verify_sha256(&file, "").unwrap();
    }

    #[test]
    fn verify_sha256_errors_when_file_missing() {
        let dir = tempdir().unwrap();
        let file = dir.path().join("missing.bin");
        let err = verify_sha256(&file, "deadbeef").unwrap_err();
        assert!(err.to_string().contains("Cannot open"));
    }

    #[test]
    fn progress_bar_is_determinate_when_length_known() {
        let bar = progress_bar(1000, 250);
        assert_eq!(bar.length(), Some(1000));
        assert_eq!(bar.position(), 250);
    }

    #[test]
    fn progress_bar_is_spinner_when_length_unknown() {
        let bar = progress_bar(0, 0);
        assert_eq!(bar.length(), None);
    }

    #[test]
    fn part_path_handles_paths_without_extension() {
        let dest = Path::new("go-archive");
        assert_eq!(part_path(dest), PathBuf::from("go-archive.part"));
    }

    /// End-to-end regression test for resume: a `.part` file with some bytes
    /// already on disk must produce a `Range` request for the remaining
    /// bytes, and the response must be appended (not overwritten) so the
    /// final file matches the full original content. Previously only the
    /// pure `backoff()` helper was tested - this exercises the actual
    /// resume path against a real (minimal) HTTP server.
    #[test]
    fn fetch_resumes_from_existing_partial_file_via_range_request() {
        use std::io::{BufRead, BufReader, Write};
        use std::net::TcpListener;

        let full_content = b"Hello, resumable download world!".to_vec();
        let already_written = 5usize; // "Hello"
        let remaining = full_content[already_written..].to_vec();

        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();

        let remaining_for_server = remaining.clone();
        let server = thread::spawn(move || {
            let (stream, _) = listener.accept().unwrap();
            let mut reader = BufReader::new(stream.try_clone().unwrap());

            let mut request_line = String::new();
            reader.read_line(&mut request_line).unwrap();

            let mut saw_range_header = false;
            loop {
                let mut line = String::new();
                reader.read_line(&mut line).unwrap();
                if line == "\r\n" || line.is_empty() {
                    break;
                }
                if line.to_ascii_lowercase().starts_with("range:") && line.contains("bytes=5-") {
                    saw_range_header = true;
                }
            }
            assert!(
                saw_range_header,
                "expected a 'Range: bytes=5-' header on the resumed request"
            );

            let mut stream = stream;
            let response = format!(
                "HTTP/1.1 206 Partial Content\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
                remaining_for_server.len()
            );
            stream.write_all(response.as_bytes()).unwrap();
            stream.write_all(&remaining_for_server).unwrap();
            stream.flush().unwrap();
        });

        let dir = tempdir().unwrap();
        let dest = dir.path().join("out.bin");
        std::fs::write(part_path(&dest), &full_content[..already_written]).unwrap();

        let client = HttpClient::new(false, 0).unwrap();
        let url = format!("http://{addr}/file.bin");
        fetch(&client, &url, &dest).unwrap();

        server.join().unwrap();

        assert_eq!(std::fs::read(&dest).unwrap(), full_content);
        assert!(!part_path(&dest).exists());
    }
}