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