From c6609b4739bf45f76360f40c3be2f2b235f728bd Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Wed, 3 Oct 2018 09:23:16 +0000 Subject: [PATCH] move logic of curl's http range error to abuild-fetch Move the logic of deleting partial downloads to abuild-fetch, which knows if it is curl or wget that was executed. --- abuild-fetch.c | 50 +++++++++++++++++++++++++++++++------------------- abuild.in | 13 +------------ 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/abuild-fetch.c b/abuild-fetch.c index 5be2601..3285ddc 100644 --- a/abuild-fetch.c +++ b/abuild-fetch.c @@ -54,11 +54,32 @@ int usage(int eval) return eval; } +int fork_exec(char *argv[], int showerr) +{ + int r = 202; + int status = 0; + pid_t childpid = fork(); + if (childpid < 0 ) + err(200, "fork"); + + if (childpid == 0) { + execvp(argv[0], argv); + if (showerr) + warn("%s", argv[0]); + _exit(201); + } + + /* wait for curl/wget and get the exit code */ + wait(&status); + if (WIFEXITED(status)) + r = WEXITSTATUS(status); + return r; +} + /* create or wait for an NFS-safe lockfile and fetch url with curl or wget */ int fetch(char *url, const char *destdir) { int lockfd, status=0; - pid_t childpid; char outfile[PATH_MAX], partfile[PATH_MAX]; char *name, *p; struct flock fl = { @@ -122,27 +143,18 @@ int fetch(char *url, const char *destdir) add_opt(&curlcmd, url); add_opt(&wgetcmd, url); - childpid = fork(); - if (childpid < 0 ) - err(200, "fork"); + status = fork_exec(curlcmd.argv, 0); - if (childpid == 0) { - execvp(curlcmd.argv[0], curlcmd.argv); - printf("Using wget\n"); - execvp(wgetcmd.argv[0], wgetcmd.argv); - warn("%s", wgetcmd.argv[0]); - unlink(lockfile); - _exit(201); - } + /* CURLE_RANGE_ERROR (33) + The server does not support or accept range requests. */ + if (status == 33) + unlink(partfile); - /* wait for curl/wget and get the exit code */ - wait(&status); - if (WIFEXITED(status)) - status = WEXITSTATUS(status); - else - status = 202; + /* is we failed execute curl, then fallback to wget */ + if (status == 201) + status = fork_exec(wgetcmd.argv, 1); - /* don't rename partial downloads that we can't continue */ + /* only rename completed downloads */ if (status == 0) rename(partfile, outfile); diff --git a/abuild.in b/abuild.in index 8f2af25..d52c0bf 100644 --- a/abuild.in +++ b/abuild.in @@ -332,20 +332,9 @@ sourcecheck() { uri_fetch() { local uri="$1" - local status=0 mkdir -p "$SRCDEST" msg "Fetching $uri" - abuild-fetch -d "$SRCDEST" "$uri" || status=$? - - # try again when server does not support resume - if [ "$status" -eq 33 ] && command -v curl > /dev/null; then - local partfile="$SRCDEST/$(filename_from_uri $uri).part" - msg "Removing partial download and trying again: $partfile" - rm "$partfile" - uri_fetch "$uri" - else - return $status - fi + abuild-fetch -d "$SRCDEST" "$uri" } is_remote() {