From aa0df680db8bc419b578fd7a404cf12c40e7d652 Mon Sep 17 00:00:00 2001 From: Mingye Wang Date: Fri, 20 Sep 2019 02:05:59 +0800 Subject: [PATCH] implement apt-transport-mirror handling close #88 --- apt-fast | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/apt-fast b/apt-fast index 229fe88..1b8a8e4 100755 --- a/apt-fast +++ b/apt-fast @@ -271,8 +271,52 @@ urldecode() printf '%b' "${1//%/\\x}" } +# Maps a mirror uri to the first entry of that list, so get_mirrors can take it. +# We definitely don't want to resolve those more than once each run... +declare -A mirror_uri_map + +# Handles a mirror URI, with caching. Parses the given list into MIRRORS. +handle_mirror() +{ + # The most recognizable split point is the "pool" thing. This + # will be apt-ftparchive-specific, but I suspect the transport is doing that + # itself. + local head="${1%%/pool/*}" body="/pool/${1#*/pool/}" + + if ! [[ ${mirror_uri_seen[$head]} ]]; then + local protocol="${head%%:*}" path="${head#*:}" + local mirrorfile list_protocol + + case $protocol in + (mirror+file) + mirrorfile=$path;; + (*) + # Well, gotta fetch... + if [[ $protocol == mirror ]]; then protocol=mirror+http; fi + aria2c -d /tmp -o apt-file-mirror.list "${protocol#mirror+}:${path}" + mirrorfile=/tmp/apt-file-mirror.list;; + esac + + # Before I could read the parsing code for the list, + # I'm gonna assume it splits on whitespace. + local mirrors + read -r -a mirrors < "$mirrorfile" + # They say spaces are fine... no local IFS=, needed here. + MIRRORS=("${MIRRORS[@]}" "${mirrors[*]}") + mirror_uri_seen[$head]=${mirrors[0]} + fi + + printf '%s\n' "${mirror_uri_seen[$head]}$body" +} + # Check if mirrors are available. And if so add all mirrors to download list. get_mirrors(){ + local mirrors + # Handle apt-transport-mirror + if [[ $1 == mirror* ]]; then + set -- "$(handle_mirror "$1")" + fi + # Check all mirror lists. for mirrorstr in "${MIRRORS[@]}"; do # Build mirrors array from comma separated string. @@ -411,7 +455,7 @@ get_uris(){ fi echo " out=$filename" } >> "$DLLIST" - done <<<"$(echo "$uris_full" | grep -E "^'(http(s|)|(s|)ftp)://")" + done <<<"$(echo "$uris_full" | grep -E "^'(http(s|)|(s|)ftp|mirror[^:]+)://")" #cat "$DLLIST" #LCK_RM