I'm using curl to download a 10MB file over a poor connection.
I use the -C -
option in a loop to resume the download if it breaks (see
the snippet below).
The resuming works when I test locally and interrupt the download - after restarting the script, it will resume the download.
However, in some IoT devices in the field, midway through the transfer, curl
errors out with a message like curl: (18) transfer closed with 2674439 bytes remaining to read
.
After that, the download is resumed and completes - but the file is corrupt. This has happened multiple times on different devices, which have in common that they are in China and have poor connection (the curl download takes longer).
How is it possible for the download to be corrupt, given the TCP protocol uses checksums to verify the packets? Am I missing something, maybe I'm using an incompatible set of options with curl?
downloadFileResume() {
# $1 contains the URL to download
local fileName=$(basename "$1")
for (( i=0; i<=$numCurlRetries; i++ ))
do
# capture curl http code (-w %{http_code}) in stdout
curlHttpCode=$(curl -kSLf -u "$REPO_USERNAME:$REPO_PASSWORD" -w %{http_code} -O -C - "$1")
local res=$?
[ $res -eq 0 ] && return
# curl gives error when attempting to resume a completely downloaded file, it's a 416 error from the server
[ $curlHttpCode -eq 416 ] && echo "Error 416, assuming file is downloaded completely" && return
done
echo "failed to download $fileName"
return 1
}
Many thanks, Mihail