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