about summary refs log tree commit diff
path: root/pkgs/build-support/fetchurl/builder.sh
blob: 511839655be06f33a96e18fab5f8a063489e3826 (plain) (blame)
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
source $stdenv/setup

header "downloading file $name with $outputHashAlgo hash $outputHash..."

# Curl flags to handle redirects, not use EPSV, handle cookies for
# servers to need them during redirects, and work on SSL without a
# certificate (this isn't a security problem because we check the
# cryptographic hash of the output anyway). 
curl="curl \
 --location --max-redirs 20 \
 --disable-epsv \
 --cookie-jar cookies \
 --insecure"


tryDownload() {
    local url="$1"
    echo
    header "trying $url"
    success=
    if $curl --fail "$url" --output "$out"; then
        success=1
    fi
    stopNest
}


finish() {
    # On old versions of Nix, verify the hash of the output.  On newer
    # versions, Nix verifies the hash itself.
    if test "$NIX_OUTPUT_CHECKED" != "1"; then
        if test "$outputHashAlgo" != "md5"; then
            echo "hashes other than md5 are unsupported in Nix <= 0.7, upgrade to Nix 0.8"
            exit 1
        fi
        actual=$(md5sum -b "$out" | cut -c1-32)
        if test "$actual" != "$id"; then
            echo "hash is $actual, expected $id"
            exit 1
        fi
    fi

    stopNest
    exit 0
}


tryHashedMirrors() {
    if test -n "$NIX_HASHED_MIRRORS"; then
        hashedMirrors="$NIX_HASHED_MIRRORS"
    fi
    
    for mirror in $hashedMirrors; do
        url="$mirror/$outputHashAlgo/$outputHash"
        if $curl --fail --silent --show-error --head "$url" \
            --write-out "%{http_code}" --output /dev/null > code 2> log; then
            tryDownload "$url"
            if test -n "$success"; then finish; fi
        else
            # Be quiet about 404 errors, which we interpret as the file
            # not being present on this particular mirror.
            if test "$(cat code)" != 404; then
                echo "error checking the existence of $url:"
                cat log
            fi
        fi
    done
}


urls2=
for url in $urls; do
    if test "${url:0:9}" != "mirror://"; then
        urls2="$urls2 $url"
    else
        url2="${url:9}"; echo "${url2/\// }" > split; read site fileName < split
        #varName="mirror_$site"
        varName="$site" # !!! danger of name clash, fix this
        if test -z "${!varName}"; then
            echo "warning: unknown mirror:// site \`$site'"
        else
            # Assume that SourceForge/GNU/kernel mirrors have better
            # bandwidth than nix.cs.uu.nl.
            preferHashedMirrors=
            
            for url3 in ${!varName}; do
                urls2="$urls2 $url3$fileName";
            done
        fi
    fi
done
urls="$urls2"


if test -n "$preferHashedMirrors"; then
    tryHashedMirrors
fi

success=
for url in $urls; do
    tryDownload "$url"
    if test -n "$success"; then finish; fi
done

if test -z "$preferHashedMirrors"; then
    tryHashedMirrors
fi


echo "error: cannot download $name from any mirror"
exit 1