From 2cc1246b11c6de5c73e0a1761e919e952bc48f04 Mon Sep 17 00:00:00 2001 From: Donald Buczek Date: Mon, 11 Sep 2023 09:23:33 +0200 Subject: [PATCH] beesh: Abort if download fails Currently, bee doesn't detect the situation when a partial file is downloaded. An incomplete archive usually makes the following extract to fail, but the broken file is kept in the cache an needs to be removed manually. Only the the situation, when a failed download produced an empty file is explicitly handled. Currently, bee contains explicit code to ignore errors from wget. This code existed in the initial commit 067ccf2 ("initial revision") from 2010. The probably reason for it is, that the option `--no-globber` is used to avoid downloading a file, which already exists, a second time. However, wget terminates with a non-zero exit status in that case. Handle the case of an existing file in the calling code, so that we can treat any wget error as a real error and abort (by the `-e` setting of the shell). While we are at it, change code to download into a tempfile and rename it in the success case to avoid problems with partial downloads. This is more robust than the EXIT trap used by current code. --- src/beesh.sh.in | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/beesh.sh.in b/src/beesh.sh.in index d02faa4..8a23688 100644 --- a/src/beesh.sh.in +++ b/src/beesh.sh.in @@ -274,27 +274,22 @@ function fetch_one_file() { nocheck="" fi - if [ ! -s "${F}/${file}" ] ; then - rm -vf "${F}/${file}" - fi - - trap "rm -f ${F}/${file}" EXIT - - print_info "fetching $url" - wget \ - --output-document="${F}/${file}" \ - --no-clobber \ - --timeout=60 \ - --tries=1 \ - "${url}" || true - - trap - EXIT - - ls -ld "${F}/${file}" - - if [ ! -s "${F}/${file}" ] ; then - print_error "ERROR: ${F}/${file} is empty, download failed." - exit 1 + if [ -s "${F}/${file}" ]; then + print_info "File ‘${F}/${file}‘ already there; not retrieving." + else + print_info "fetching $url" + wget \ + --output-document="${F}/${file}.tmp" \ + --timeout=60 \ + --tries=1 \ + "${url}" + ls -ld "${F}/${file}.tmp" + if [ ! -s "${F}/${file}.tmp" ] ; then + print_error "ERROR: ${F}/${file}.tmp is empty, download failed." + rm "${F}/${file}.tmp" + exit 1 + fi + mv "${F}/${file}.tmp" "${F}/${file}" fi fi