+# Test if the given file is an archive file
+# Arg. #1: File name
+is_archive()
+{
+ if [ ${#} -ne 1 ]; then
+ echo "${FUNCNAME}(), wrong number of arguments: ${*}"
+ return 1
+ fi
+
+ local F=${1}
+
+ if file ${F} | grep -q --ignore-case -e "compressed data" -e "Zip archive data"; then
+ return 0;
+ else
+ return 1
+ fi
+}
+
+# Remove any partially downloaded file.
+# Arg. #1: Partial file name without partial extension (.part).
+remove_partial_file()
+{
+ rm -f ${1}.part
+}
+
+# Download a file, and making sure it is valid (at least it's header!)
+# Arg. #1: Source URL.
+# Arg. #2: Source filename (on server)
+# Arg. #3: Output directory (optional). Default = LFS_PKG_DIR
+#
+# Return: 0 : success
+# 1 : wget error
+# 2 : File not found or not an archive (if file extension was archive type)
+wget_wrapper()
+{
+ if [ ${#} -lt 2 ]; then
+ echo "${FUNCNAME}(), wrong number of arguments: ${*}"
+ return 1
+ fi
+
+ local URL=${1}
+ local SOURCE=${2}
+ local DESTDIR=${LFS_PKG_DIR}
+
+ if [ ${#} -eq 3 ]; then
+ DESTDIR=${3}
+ fi
+
+ remove_partial_file ${DESTDIR}/${SOURCE}
+
+ local STRING="Fetching ${SOURCE}"
+ if [ "x${FTYPE}" = "x${FTYPE_PATCH}" ]; then
+ STRING="${STRING} from ${URL}"
+ fi
+
+ rcmd "${STRING}" ${WGETCMD} ${URL}/${SOURCE} \
+ --output-document=${DESTDIR}/${SOURCE}.part
+ wget_status=$?
+
+ # Append log to global log file
+ cat ${WGET_LOG_FILE} >> ${LFS_LOG_FILE}
+
+ # Total failure if it was a connection timeout.
+ if grep -q "failed: Connection timed out" ${WGET_LOG_FILE}; then
+ echo "Error, wget reported: Connection timed out"
+ return 1
+ fi
+
+ # Partial failure if file was not found.
+ if detect_file_not_found; then
+ remove_partial_file ${DESTDIR}/${SOURCE}
+ return 2
+ fi
+
+ if [ ${wget_status} -ne 0 ]; then
+ echo "Error: wget returned error status ${wget_status}" >> \
+ ${LFS_LOG_FILE}
+ remove_partial_file ${DESTDIR}/${SOURCE}
+ return 1
+ fi
+
+ local FEXT=${SOURCE##*.}
+ if is_extension_archive "${FEXT}"; then
+ # Just to be sure, test if downloaded file is really an archive:
+ if ! is_archive ${DESTDIR}/${SOURCE}.part; then
+ # Partial failure if file is invalid.
+ echo "Error: failed archive test" >> ${LFS_LOG_FILE}
+ remove_partial_file ${DESTDIR}/${SOURCE}
+ return 2
+ fi
+ fi
+
+ # Rename temporary file to final name
+ mv ${DESTDIR}/${SOURCE}{.part,}
+
+ return 0
+}
+