fetchExpectedChecksum downloads the SHA256SUMS.txt file and returns the expected checksum for the given archive name.
(httpClient *http.Client, checksumsURL safeurl.SafeURL, archiveName string)
| 340 | |
| 341 | // fetchExpectedChecksum downloads the SHA256SUMS.txt file and returns the expected checksum for the given archive name. |
| 342 | func fetchExpectedChecksum(httpClient *http.Client, checksumsURL safeurl.SafeURL, archiveName string) (string, error) { |
| 343 | resp, err := httpClient.Get(checksumsURL.String()) |
| 344 | if err != nil { |
| 345 | return "", err |
| 346 | } |
| 347 | defer resp.Body.Close() |
| 348 | |
| 349 | if resp.StatusCode != http.StatusOK { |
| 350 | return "", fmt.Errorf("failed to download checksums: %s", resp.Status) |
| 351 | } |
| 352 | |
| 353 | // Parse the checksums file. Possible formats are: |
| 354 | // - "<checksum> <filename>" (two whitespaces) |
| 355 | // - "<checksum> <filename>" |
| 356 | scanner := bufio.NewScanner(resp.Body) |
| 357 | for scanner.Scan() { |
| 358 | line := scanner.Text() |
| 359 | fields := strings.Fields(line) |
| 360 | if len(fields) >= 2 { |
| 361 | checksum := fields[0] |
| 362 | filename := fields[1] |
| 363 | if filename == archiveName { |
| 364 | return checksum, nil |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | if err := scanner.Err(); err != nil { |
| 369 | return "", fmt.Errorf("failed to read checksums: %w", err) |
| 370 | } |
| 371 | |
| 372 | return "", fmt.Errorf("checksum not found for %s", archiveName) |
| 373 | } |
| 374 | |
| 375 | // extractZip reads a ZIP archive at path and extracts its contents into destDir. |
| 376 | // It returns an error if the archive cannot be read, |