diff options
author | Joseph Graham <joseph@xylon.me.uk> | 2017-05-28 13:20:18 +0100 |
---|---|---|
committer | Joseph Graham <joseph@xylon.me.uk> | 2017-05-28 13:20:18 +0100 |
commit | b7d9b4646a3ab2d86bd929826759a416030b78b8 (patch) | |
tree | c1cb0060f54d2508729ab9c5e22b1252d0cc76c3 | |
parent | 5fa4400675ffef4408852a6f12630454a706d0f7 (diff) |
Performance boost, now finds torrents faster.
Chooses three random mirrors and queries them simultaneously for a torrent.
-rw-r--r-- | pacman2pacman-get | 101 |
1 files changed, 56 insertions, 45 deletions
diff --git a/pacman2pacman-get b/pacman2pacman-get index aa4e715..a3e2839 100644 --- a/pacman2pacman-get +++ b/pacman2pacman-get @@ -1,7 +1,7 @@ #! /bin/bash # /usr/bin/pacman2pacman-get # -# Version 1.5.6 +# Version 1.5.7 # # Copyright (C) 2014,2017 Joseph Graham <joseph@xylon.me.uk> # @@ -21,7 +21,9 @@ debugmode=0 [[ ${3} == "-d" ]] && debugmode=1 # if 3rd arg is "-d", enable debug mode -(( debugmode )) && echo "pacman2pacman-get invoked" +echoerr() { cat <<< "$@" 1>&2; } # print errors to stdout + +(( debugmode )) && echoerr "pacman2pacman-get invoked" shopt -s extglob @@ -33,8 +35,8 @@ pkg_cache_location='/var/cache/pacman/pkg' # XferCommand = /usr/bin/pacman2pacman-get %u %o # Check that the args are not empty. -[[ -z ${1} ]] && { echo "The first argument should be the download url." ; exit 1 ; } -[[ -z ${2} ]] && { echo "The second argument should be the local filename, plus a “.part” extension." ; exit 1 ; } +[[ -z ${1} ]] && { echoerr "The first argument should be the download url." ; exit 1 ; } +[[ -z ${2} ]] && { echoerr "The second argument should be the local filename, plus a “.part” extension." ; exit 1 ; } url="${1}" # The download url. filename="${2}" # This is where the downloaded file needs to be saved @@ -43,7 +45,7 @@ filename="${2}" # This is where the downloaded file needs to be saved # Check if transmission daemon is running. if ! systemctl show --property='ActiveState' transmission | grep -q 'ActiveState=active' then - echo 'Error: The transmission daemon is not running.' + echoerr 'Error: The transmission daemon is not running.' exit 1 fi @@ -61,6 +63,10 @@ else dbfile=0 fi +fifoname="${TMPDIR-/tmp}/pacman2pacmanpipe" + +exittidy() { rm -rf "${fifoname}" "${TMPDIR-/tmp}/pacman2pacman_torrents"* ; } + # Check for a .torrent. # We will cycle through the mirrors in the mirrorlist, trying to get @@ -68,14 +74,32 @@ fi # mirrors until we get a 404 or a success, because only a 404 # indicates that the .torrent does not exist. -response='anomaly' +mkfifo "${fifoname}" + +function look_for_torrent() +{ + local torrent_url="${1}" + local tfile="$(mktemp ${TMPDIR-/tmp}/pacman2pacman_torrents.XXXXXXXXXX)" + + (( debugmode )) && echoerr "Looking for torrent at: '${torrent_url}'" + + # Attempt to download the torrent + curl -o "${tfile}" -f -L -s -A 'Pacman2pacman' -m 10 "${torrent_url}" + local curlstat="${?}" + + # Did curl download a torrent? + if [[ "${curlstat}" == 0 ]] && grep "mktorrent" "${tfile}" &>/dev/null + then + (( debugmode )) && echoerr "Found torrent" + echo "found_torrent:${tfile}" > "${fifoname}" + fi +} # If it's a .db file then we don't even check for a .torrent -if (( dbfile )) +if ! (( dbfile )) then - response='no_torrent' - -else + # OK so for three random mirrors we look for a torrent. The first + # one that responds, we use it. Give up and go to HTTP after 6 seconds. while read mirror do # Remove the `Server = ' part, whether it has spaces or not @@ -83,42 +107,21 @@ else torrent_url="${mirror%%\$*}torrents/${url##*/}.torrent" - (( debugmode )) && echo "Looking for torrent at: '${mirror%%\$*}torrents/${url##*/}.torrent'" - - curl -O -f -L -s -A 'Pacman2pacman' -m 10 "${torrent_url}" - curlstat="${?}" + look_for_torrent "${torrent_url}" & - if grep -i "404 Not Found" "${torrent_folder}/${url##*/}.torrent" &>/dev/null - then - (( debugmode )) && echo "404 response, no torrent available." - response='no_torrent' - break - fi + done < <(grep '^Server \?= \?' "/etc/pacman.d/mirrorlist" | shuf | head -n 3) - # This checks that curl downloaded something AND that it seems to be valid - if [[ "${curlstat}" == 0 ]] && grep "mktorrent" "${torrent_folder}/${url##*/}.torrent" &>/dev/null - then - (( debugmode )) && echo "Found torrent" - response='found_torrent' - break - fi - - # Loop only continues if we got neither a 404 nor a valid - # torrent, indicating the server must be down? - - done < <(grep '^Server \?= \?' "/etc/pacman.d/mirrorlist") + # Get the name of the torrent downloaded + read -t 6 response < "${fifoname}" fi -if [[ "${response}" == 'anomaly' ]] -then - (( debugmode )) && echo "No torrents were found on any mirror, nor were any 404s encountered. Is your internet working?" -fi +(( debugmode )) && echoerr "response var: '${response}'" gotourbaby=0 # to record if we've got the torrent yet. # If there's a .torrent we download the package with transmission # otherwize we just download it by http. -if [[ "${response}" == 'found_torrent' ]] +if [[ "${response}" == 'found_torrent:'* ]] then gotourbaby=1 # assume success unless proven otherwize @@ -127,12 +130,18 @@ then # Re-write the webseeds in the torrent to make it use the user's # own mirror. We can just use the ${url} var. + # The filename of the torrent + torntname="${torrent_folder}/${url##*/}.torrent" + + # Move the torrent to the correct location + mv "${response#found_torrent:}" "${torntname}" + # We need to find out the length of the ${url} var. len="${#url}" - sed "s#url-list[[:digit:]]\+.\+.pkg.tar.xz#url-list${len}:${url}#" < "${torrent_folder}/${url##*/}.torrent" > "${torrent_folder}/${url##*/}.torrent.modified" + sed "s#url-list[[:digit:]]\+.\+.pkg.tar.xz#url-list${len}:${url}#" < "${torntname}" > "${torntname}.modified" - mv "${torrent_folder}/${url##*/}.torrent.modified" "${torrent_folder}/${url##*/}.torrent" + mv "${torntname}.modified" "${torntname}" transmission_output=$(mktemp) @@ -144,8 +153,8 @@ then fi # Add the torrent to transmission - (( debugmode )) && transmission-remote -a "${torrent_folder}/${url##*/}.torrent" -w "${pkg_cache_location}" 2>&1 | tee "${transmission_output}" - (( debugmode )) || transmission-remote -a "${torrent_folder}/${url##*/}.torrent" -w "${pkg_cache_location}" 2>&1 > "${transmission_output}" + (( debugmode )) && transmission-remote -a "${torntname}" -w "${pkg_cache_location}" 2>&1 | tee "${transmission_output}" + (( debugmode )) || transmission-remote -a "${torntname}" -w "${pkg_cache_location}" 2>&1 > "${transmission_output}" if grep "Error: invalid or corrupt torrent file" "${transmission_output}" then @@ -177,8 +186,8 @@ then if ! [[ ${id} =~ [[:digit:]] ]] then - echo "Error, invalid torrent id: '${id}'" - exit 1 + echoerr "Error, invalid torrent id: '${id}'" + exittidy ; exit 1 fi # Display a progress bar until it's finished and then hardlink it to @@ -273,7 +282,7 @@ then then echo "error moving \"${pkg_cache_location}/${url##*/}\". removing torrent from transmission" transmission-remote -t "${id}" -r > /dev/null - exit 1 + exittidy ; exit 1 fi sleep 0.1 @@ -374,8 +383,10 @@ then cp "/srv/pacman2pacman/dbcache/${filename##*/}" "${filename}" 2>/dev/null fi - [[ -f "${filename}" ]] || exit 1 + [[ -f "${filename}" ]] || { exittidy ; exit 1 ; } fi +exittidy + exit |