forked from mk-fg/fgtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl-cache
executable file
·40 lines (35 loc) · 1.6 KB
/
curl-cache
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
#!/bin/sh
set -e
usage() {
bin=${0##*/}
echo >&2 "Usage: XferCommand = $bin [-q|-v] %o %u [cache-url-prefix...]"
echo >&2
echo >&2 "XferCommand for pacman.conf to use local http caches."
echo >&2 "Tries to download package file from each"
echo >&2 " cache-url-prefix and falls back to a mirror URL."
echo >&2 "cache-url-prefix must not have trailing slashes."
echo >&2 "-q disables printing message when file is missing in cache and progress bars."
exit ${1:-0}
}
[ -n "$1" -a -n "$2" -a "$1" != -h -a "$1" != --help -a "$2" != -h -a "$2" != --help ] || usage
[ "$1" != -q ] && quiet= || { quiet=t; shift; }
dst="$1" url="$2"; shift; shift
dst_local="${dst%.part}" url_file=${url##*/}
[ "${dst_local##*.}" = db -o ! -r "$dst_local" ] \
|| { mv "$dst_local" "$dst"; exit 0; } # use local file cache
cache_success_exit() {
[ -n "$quiet" ] || echo "curl-fetch - cache hit: $url_file"
exit 0; }
for pre in "$@"; do
url_cache="$pre/$url_file"
err_http=$(curl -sfL --write-out '%{http_code}' --continue-at - -o "$dst" "$url_cache" ||:)
[ "$err_http" -ne 200 -a "$err_http" -ne 206 ] || cache_success_exit
[ "$err_http" -ne 416 ] || { # range req fail
err_http=$(curl -sfL --write-out '%{http_code}' -o "$dst" "$pre/$url_file" ||:)
[ "$err_http" -ne 200 ] || cache_success_exit; }
[ "$err_http" -ne 404 -a -z "$quiet" ] || echo "curl-fetch - cache 404: $url_cache"
[ "$err_http" -eq 404 ] || echo >&2 "curl-fetch - http error $err_http: $url_cache"
done
[ -z "$quiet" ] && verbosity=--progress-bar || verbosity=--silent
curl $verbosity -fLC- -o "$dst" "$url" \
|| { echo "curl-fetch - mirror URL failed: $url"; exit 1; }