ansible-later: 2.0.22 -> 2.0.23
[NixPkgs.git] / pkgs / build-support / fetchipfs / builder.sh
blob7a6a517566f5ee66af00f03b7f93f5be2dae40c8
1 source $stdenv/setup
3 # Curl flags to handle redirects, not use EPSV, handle cookies for
4 # servers to need them during redirects, and work on SSL without a
5 # certificate (this isn't a security problem because we check the
6 # cryptographic hash of the output anyway).
8 set -o noglob
10 curl="curl \
11 --location \
12 --max-redirs 20 \
13 --retry 2 \
14 --disable-epsv \
15 --cookie-jar cookies \
16 --insecure \
17 --speed-time 5 \
18 -# \
19 --fail \
20 $curlOpts \
21 $NIX_CURL_FLAGS"
23 finish() {
24 runHook postFetch
25 set +o noglob
26 exit 0
29 ipfs_add() {
30 if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
31 echo "\e[0m\e[01;36m=IPFS=\e[0m add $ipfs"
32 tar --owner=root --group=root -cWf "source.tar" $(echo *)
33 res=$(curl -# -F "file=@source.tar" "localhost:5001/api/v0/tar/add" | sed 's/.*"Hash":"\(.*\)".*/\1/')
34 if [ $ipfs != $res ]; then
35 echo "\`ipfs tar add' results in $res when $ipfs is expected"
36 exit 1
38 rm "source.tar"
42 echo
44 mkdir download
45 cd download
47 if curl --retry 0 --head --silent "localhost:5001" > /dev/null; then
48 curlexit=18;
49 echo "\e[0m\e[01;36m=IPFS=\e[0m get $ipfs"
50 # if we get error code 18, resume partial download
51 while [ $curlexit -eq 18 ]; do
52 # keep this inside an if statement, since on failure it doesn't abort the script
53 if $curl -C - "http://localhost:5001/api/v0/tar/cat?arg=$ipfs" --output "$ipfs.tar"; then
54 unpackFile "$ipfs.tar"
55 rm "$ipfs.tar"
56 set +o noglob
57 mv $(echo *) "$out"
58 finish
59 else
60 curlexit=$?;
62 done
65 if test -n "$url"; then
66 curlexit=18;
67 echo "Downloading $url"
68 while [ $curlexit -eq 18 ]; do
69 # keep this inside an if statement, since on failure it doesn't abort the script
70 if $curl "$url" -O; then
71 set +o noglob
72 tmpfile=$(echo *)
73 unpackFile $tmpfile
74 rm $tmpfile
75 ipfs_add
76 mv $(echo *) "$out"
77 finish
78 else
79 curlexit=$?;
81 done
84 echo "\e[01;31merror:\e[0m cannot download $ipfs from ipfs or the given url"
85 echo
86 set +o noglob
87 exit 1