distro: Update bootstrap binaries to an nscd-less libc.
[jackhill/guix/guix.git] / build-aux / download.scm
1 ;;; Guix --- Nix package management from Guile. -*- coding: utf-8 -*-
2 ;;; Copyright (C) 2012, 2013 Ludovic Courtès <ludo@gnu.org>
3 ;;;
4 ;;; This file is part of Guix.
5 ;;;
6 ;;; Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
15 ;;;
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 ;;;
20 ;;; Download a binary file from an external source.
21 ;;;
22
23 (use-modules (ice-9 match)
24 (web uri)
25 (web client)
26 (rnrs io ports)
27 (srfi srfi-11)
28 (guix utils))
29
30 (define %url-base
31 "http://www.fdn.fr/~lcourtes/software/guix/packages")
32
33 (define (file-name->uri file)
34 "Return the URI for FILE."
35 (match (string-tokenize file (char-set-complement (char-set #\/)))
36 ((_ ... system basename)
37 (string->uri (string-append %url-base "/" system
38 "/20130105/" basename)))))
39
40 (match (command-line)
41 ((_ file expected-hash)
42 (let ((uri (file-name->uri file)))
43 (format #t "downloading file `~a' from `~a'...~%"
44 file (uri->string uri))
45 (let*-values (((resp data) (http-get uri #:decode-body? #f))
46 ((hash) (bytevector->base16-string (sha256 data)))
47 ((part) (string-append file ".part")))
48 (if (string=? expected-hash hash)
49 (begin
50 (call-with-output-file part
51 (lambda (port)
52 (put-bytevector port data)))
53 (rename-file part file))
54 (begin
55 (format (current-error-port)
56 "file at `~a' has SHA256 ~a; expected ~a~%"
57 (uri->string uri) hash expected-hash)
58 (exit 1)))))))