From e5996fad2076f3640a3da53cdc3fe182442ab6cf Mon Sep 17 00:00:00 2001 From: Joseph Graham Date: Mon, 15 May 2017 11:38:25 +0100 Subject: Various fixes. --- pacman2pacman-get | 279 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 162 insertions(+), 117 deletions(-) (limited to 'pacman2pacman-get') diff --git a/pacman2pacman-get b/pacman2pacman-get index 9300af4..f027e0b 100644 --- a/pacman2pacman-get +++ b/pacman2pacman-get @@ -1,9 +1,9 @@ #! /bin/bash # /usr/bin/pacman2pacman-get # -# Version 1.5.3 +# Version 1.5.4 # -# Copyright (C) 2014 Joseph Graham +# Copyright (C) 2014,2017 Joseph Graham # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -18,6 +18,10 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +debugmode=1 + +(( debugmode )) && echo "pacman2pacman-get invoked" + shopt -s extglob torrent_folder='/srv/pacman2pacman/torrents' @@ -63,6 +67,8 @@ fi # mirrors until we get a 404 or a success, because only a 404 # indicates that the .torrent does not exist. +response='anomaly' + # If it's a .db file then we don't even check for a .torrent if (( dbfile )) then @@ -75,26 +81,46 @@ else mirror="${mirror##Server?( )=?( )}" torrent_url="${mirror%%\$*}torrents/${url##*/}.torrent" + + (( debugmode )) && echo "Looking for torrent at: '${mirror%%\$*}torrents/${url##*/}.torrent'" - err=$( curl -O -f -L -s -A 'Pacman2pacman' "${torrent_url}" ) + curl -O -f -L -s -A 'Pacman2pacman' -m 10 "${torrent_url}" + curlstat="${?}" - if [[ "${?}" == 0 ]] + if grep -i "404 Not Found" "${torrent_folder}/${url##*/}.torrent" &>/dev/null then - response='found_torrent' + (( debugmode )) && echo "404 response, no torrent available." + response='no_torrent' break + fi - elif [[ "${err}" == *'404'* ]] + # 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 - response='no_torrent' + (( debugmode )) && echo "Found torrent" + response='found_torrent' break - fi + 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") fi +if [[ "${response}" == 'anomaly' ]] +then + (( debugmode )) && echo "No torrents were found on any mirror, nor were any 404s encountered. Is your internet working?" +fi + +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' ]] then + gotourbaby=1 # assume success unless proven otherwize + cd "${pkg_cache_location}" # Re-write the webseeds in the torrent to make it use the user's @@ -107,136 +133,155 @@ then mv "${torrent_folder}/${url##*/}.torrent.modified" "${torrent_folder}/${url##*/}.torrent" + transmission_output=$(mktemp) + # Add the torrent to transmission - transmission-remote -a "${torrent_folder}/${url##*/}.torrent" -w "${pkg_cache_location}" &>/dev/null - - # The torrent is now downloading. To get info about the torrent in - # order to display a progress bar we need to know it's id. - - # Find out the id of the torrent - id=$(transmission-remote -l | grep "${url##*/}") - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" - id="${id## }" # Once should be enough but somehow it's not so I do - # it loads of times. - id="${id%% *}" - - # If there is an error there will be an asterix displayed on the - # end of the id. We must remove it. - id="${id%\*}" - - if ! [[ ${id} =~ [[:digit:]] ]] + (( 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}" + + if grep "Error: invalid or corrupt torrent file" "${transmission_output}" then - echo 'Error.' - exit 1 - fi + echo "Invalid or corrupt torrent file - falling back to HTTP" + gotourbaby=0 + else + # The torrent is now downloading. To get info about the torrent in + # order to display a progress bar we need to know it's id. + + # Find out the id of the torrent + id=$(transmission-remote -l | grep "${url##*/}") + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" + id="${id## }" # Once should be enough but somehow it's not so I do + # it loads of times. + id="${id%% *}" + + # If there is an error there will be an asterix displayed on the + # end of the id. We must remove it. + id="${id%\*}" + + if ! [[ ${id} =~ [[:digit:]] ]] + then + echo "Error, invalid torrent id: '${id}'" + exit 1 + fi - # Display a progress bar until it's finished and then hardlink it to - # the right place. - progress='0.0%' + # Display a progress bar until it's finished and then hardlink it to + # the right place. + progress='0.0%' - echo -n "Pacman2pacman p2p download: ${pname}: ${progress}" + echo -n "Pacman2pacman p2p download: ${pname}: ${progress}" - count=0 + count=0 - declare -a last_webseed_check=(0 'working') + declare -a last_webseed_check=(0 'working') - webseed_check_after='100' + webseed_check_after='100' - until [[ "${progress}" == '100%' ]] - do - progress=$(transmission-remote -t "${id}" -i | grep 'Percent Done:') - progress="${progress##* }" # Remove stuff we don't want + until [[ "${progress}" == '100%' ]] + do + progress=$(transmission-remote -t "${id}" -i | grep 'Percent Done:') + progress="${progress##* }" # Remove stuff we don't want - # If we're still at 0.0% after ${webseed_check_after} attempts - # then we check if the webseed is actually valid. - if (( count >= webseed_check_after )) && [[ "${progress}" == '0.0%' ]] - then - # Find out if we already checked recently. - if (( (last_webseed_check[0] + (webseed_check_after / 2)) > count )) + # If we're still at 0.0% after ${webseed_check_after} attempts + # then we check if the webseed is actually valid. + if (( count >= webseed_check_after )) && [[ "${progress}" == '0.0%' ]] then - if [[ last_webseed_check[1] == 'broken' ]] - then - # Just set the message without checking. - progress='0.0% Warning: Webseed dead...' - else - # We don't need to do anything - true - fi - else - if ! $(curl -IL "${url}" | grep '200 OK' &>/dev/null) - then - # The webseed seems to be invalid so we print an error - # message. - - progress='0.0% Warning: Webseed dead...' # This - # most - # likely - # means - # the - # user - # needs - # to run - # pacman - # -Sy, or - # that - # their - # internet - # is - # down. - - # If there are no peers we remove the torrent from - # transmission, print an error message and exit. - if (( $(transmission-remote -t "${id}" -pi | wc -l) < 2 )) + # Find out if we already checked recently. + if (( (last_webseed_check[0] + (webseed_check_after / 2)) > count )) + then + if [[ last_webseed_check[1] == 'broken' ]] then - transmission-remote -t "${id}" -r - echo "Webseed dead and no peers, removing torrent from transmission and exiting." - exit 1 + # Just set the message without checking. + progress='0.0% Warning: Webseed dead...' + else + # We don't need to do anything + true fi - - last_webseed_check[0]="${count}" - last_webseed_check[1]="broken" - else - # The webseed is valid so we just record that we checked - last_webseed_check[0]="${count}" - last_webseed_check[1]="working" - fi + else + if ! $(curl -IL "${url}" | grep '200 OK' &>/dev/null) + then + # The webseed seems to be invalid so we print an error + # message. + + progress='0.0% Warning: Webseed dead...' # This + # most + # likely + # means + # the + # user + # needs + # to run + # pacman + # -Sy, or + # that + # their + # internet + # is + # down. + + # If there are no peers we remove the torrent from + # transmission, print an error message and exit. + if (( $(transmission-remote -t "${id}" -pi | wc -l) < 2 )) + then + echo "Webseed dead and no peers, removing torrent from transmission and try by HTTP." + transmission-remote -t "${id}" -r > /dev/null + gotourbaby=0 + break + fi + + last_webseed_check[0]="${count}" + last_webseed_check[1]="broken" + else + # The webseed is valid so we just record that we checked + last_webseed_check[0]="${count}" + last_webseed_check[1]="working" + fi + fi fi - fi - printf "\rPacman2pacman p2p download: %s: %s " "${pname}" "${progress}" + printf "\rPacman2pacman p2p download: %s: %s " "${pname}" "${progress}" - sleep 0.7 + sleep 0.7 - (( count ++ )) - done + (( count ++ )) + done - echo + echo - err_count=0 - until mv -f "${pkg_cache_location}/${url##*/}" "${filename}" 2>/dev/null - do - if (( err_count > 50 )) - then - echo "error moving \"${pkg_cache_location}/${url##*/}\"" - exit 1 - fi + if (( gotourbaby )) + then + err_count=0 + until mv -f "${pkg_cache_location}/${url##*/}" "${filename}" 2>/dev/null + do + if (( err_count > 50 )) + then + echo "error moving \"${pkg_cache_location}/${url##*/}\". removing torrent from transmission" + transmission-remote -t "${id}" -r > /dev/null + exit 1 + fi - sleep 0.1 + sleep 0.1 - (( ++ err_count )) - done + (( ++ err_count )) + done + fi + fi -else + rm -f "${transmission_output}" +fi + +if ! (( gotourbaby )) +then cd "${pkg_cache_location}" + # There's no .torrent so we download it by just HTTP. # We must do some complicated stuff to customise the progress @@ -249,7 +294,7 @@ else echo -n "Pacman2pacman http download: ${pname}: ${progress}" - # The first loop gets rid of all the backspace and `#'es and + # The first loop gets rid of all the backspace and '#'es and # stuff. The second loop isolates the percent done number. { # For dbs we check if they are modified -- cgit v1.2.2