2006-07-17 17:34:22 +02:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
2021-12-16 15:10:46 +01:00
|
|
|
# $NetBSD: fetch,v 1.21 2021/12/16 14:10:46 schmonz Exp $
|
2006-07-17 17:34:22 +02:00
|
|
|
#
|
2015-09-08 13:13:12 +02:00
|
|
|
# Copyright (c) 2006, 2015 The NetBSD Foundation, Inc.
|
2006-07-17 17:34:22 +02:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
# by Johnny C. Lam.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions
|
|
|
|
# are met:
|
|
|
|
# 1. Redistributions of source code must retain the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
# notice, this list of conditions and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
#
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
#
|
|
|
|
# NAME
|
|
|
|
# fetch -- fetch files via URLs
|
|
|
|
#
|
|
|
|
# SYNOPSIS
|
2015-09-19 13:45:56 +02:00
|
|
|
# fetch [-c] [-d dir] [-f distinfo] [-p hook] [-r] [-v] file [site ...]
|
2006-07-17 17:34:22 +02:00
|
|
|
#
|
|
|
|
# DESCRIPTION
|
|
|
|
# fetch will attempt to fetch the file from the list of specified
|
|
|
|
# sites in the order given. The complete URL to the file on each
|
|
|
|
# site should be the concatenation of the specified site and file.
|
2006-07-19 00:41:06 +02:00
|
|
|
# If the file cannot be fetched successfully, then we try the next
|
2006-07-17 17:34:22 +02:00
|
|
|
# listed site.
|
|
|
|
#
|
2006-07-19 00:41:06 +02:00
|
|
|
# If the file already exists on the disk and is verified, then
|
|
|
|
# no fetch action is taken.
|
|
|
|
#
|
2006-07-17 17:34:22 +02:00
|
|
|
# OPTIONS
|
|
|
|
# -c Verify the checksum for the file. If the checksum
|
|
|
|
# does not match, then the fetch is determined to be
|
2006-08-03 16:02:58 +02:00
|
|
|
# not successful.
|
2006-07-17 17:34:22 +02:00
|
|
|
#
|
2006-07-19 00:41:06 +02:00
|
|
|
# -d dir Fetch the files into the specified directory.
|
|
|
|
#
|
2006-07-17 17:34:22 +02:00
|
|
|
# -f distinfo
|
|
|
|
# The path to the distinfo file containing the checksums
|
|
|
|
# for the file. The file format should match what is
|
|
|
|
# needed by the pkgsrc/mk/checksum/checksum script.
|
|
|
|
#
|
2015-09-08 13:13:12 +02:00
|
|
|
# -p hook
|
2015-09-08 14:37:56 +02:00
|
|
|
# After a successful fetch, run hook. The first argument is
|
|
|
|
# the relative path of the distfile and the second argument
|
|
|
|
# the full URL the file was obtained from.
|
2015-09-08 13:13:12 +02:00
|
|
|
#
|
2006-07-19 00:41:06 +02:00
|
|
|
# -r Resume a previous fetch for the file. In this case,
|
|
|
|
# the file is first saved to a ".pkgsrc.resume" file,
|
|
|
|
# and is later renamed to the final file name if the
|
|
|
|
# complete file has been sucessfully fetched.
|
|
|
|
#
|
2006-07-19 16:16:29 +02:00
|
|
|
# -v Show the actual command line used to fetch the file
|
|
|
|
# from each site.
|
|
|
|
#
|
2006-07-19 00:41:06 +02:00
|
|
|
# ENVIRONMENT
|
|
|
|
# PKGSRCDIR This is a hint to help locate the default
|
|
|
|
# checksum script.
|
|
|
|
#
|
|
|
|
# CHECKSUM This is the path to the checksum script used
|
|
|
|
# when "-c" is specified.
|
|
|
|
#
|
|
|
|
# FETCH_CMD This is the actual command used for transferring
|
|
|
|
# files from the various sites.
|
|
|
|
#
|
|
|
|
# The following are lists of options to pass to ${FETCH_CMD}:
|
|
|
|
#
|
|
|
|
# FETCH_BEFORE_ARGS
|
|
|
|
# These options appear before all other options.
|
|
|
|
#
|
|
|
|
# FETCH_AFTER_ARGS
|
|
|
|
# These options appear after all other options.
|
|
|
|
#
|
|
|
|
# FETCH_RESUME_ARGS
|
|
|
|
# These options appear just after FETCH_BEFORE_ARGS
|
|
|
|
# options and cause ${FETCH_CMD} to resume a
|
|
|
|
# previous file transfer.
|
|
|
|
#
|
|
|
|
# FETCH_OUTPUT_ARGS
|
|
|
|
# These options specify the name of the local file
|
|
|
|
# that will hold the contents of the fetched file.
|
|
|
|
#
|
2006-07-17 17:34:22 +02:00
|
|
|
######################################################################
|
|
|
|
|
2006-07-19 00:41:06 +02:00
|
|
|
: ${PKGSRCDIR:=/usr/pkgsrc}
|
mk: Rewrite the checksum script in awk.
The previous shell script version's runtime was quadratic against the
number of distfiles to verify. Historically this has not been an issue,
with usually only a handful of files per package. However, with the
introduction of Go modules the number of distfiles used by a single
package can be very high.
For example, in an upcoming update of www/grafana to version 7.1.5, the
number of GO_MODULE_FILES is 821. Running 'bmake checksum' takes:
real 18m20.743s
user 17m27.975s
sys 0m49.239s
With the awk code, this is reduced to a far more sensible:
real 0m4.330s
user 0m3.241s
sys 0m0.875s
The script has been written to emulate the previous version precisely,
preserving the same output and error messages and supporting all of its
behaviour, with the one exception that previous exit values of 128 have
been changed to 3, in order to avoid any potential signed 8-bit issues.
The one change in the pkgsrc infrastructure is that the mk/fetch/fetch
script no longer sets a working default value for ${CHECKSUM}. This is
not a problem in a pkgsrc environment as all of the required variables
are set correctly, but if there happen to be any users who are using
this script in a standalone environment, they will need to set it
accordingly. This was probably required in many situations previously
anyway, as none of the script's environment variables were set, and
trying to support this would be fragile at best.
2020-08-27 13:45:45 +02:00
|
|
|
: ${CHECKSUM:=false}
|
2006-07-19 00:41:06 +02:00
|
|
|
: ${CP:=cp}
|
2006-07-17 17:34:22 +02:00
|
|
|
: ${ECHO:=echo}
|
|
|
|
: ${FETCH_CMD:=ftp}
|
2006-07-19 00:41:06 +02:00
|
|
|
: ${MKDIR:=mkdir}
|
|
|
|
: ${MV:=mv}
|
2006-07-17 17:34:22 +02:00
|
|
|
: ${TEST:=test}
|
2006-07-19 00:41:06 +02:00
|
|
|
: ${TOUCH:=touch}
|
2006-07-17 17:34:22 +02:00
|
|
|
: ${WC:=wc}
|
|
|
|
|
|
|
|
self="${0##*/}"
|
|
|
|
|
|
|
|
usage() {
|
2015-09-19 13:45:56 +02:00
|
|
|
${ECHO} 1>&2 "usage: $self [-c] [-d dir] [-f distinfo] [-p hook] [-r] [-v] file [site ...]"
|
2006-07-17 17:34:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Process optional arguments
|
|
|
|
checksum=
|
|
|
|
distinfo=
|
2007-01-18 11:51:48 +01:00
|
|
|
fetchdir=. # A relative directory or "."
|
2015-09-08 13:13:12 +02:00
|
|
|
post_fetch=
|
2006-07-19 00:41:06 +02:00
|
|
|
resume=
|
2006-07-19 16:16:29 +02:00
|
|
|
verbose=
|
2006-07-17 17:34:22 +02:00
|
|
|
while ${TEST} $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
-c) checksum=yes; shift ;;
|
2006-07-19 00:41:06 +02:00
|
|
|
-d) fetchdir="$2"; shift 2 ;;
|
2006-07-17 17:34:22 +02:00
|
|
|
-f) distinfo="$2"; shift 2 ;;
|
2015-09-08 13:13:12 +02:00
|
|
|
-p) post_fetch="$2"; shift 2 ;;
|
2006-07-19 00:41:06 +02:00
|
|
|
-r) resume=yes; shift ;;
|
2006-07-19 16:16:29 +02:00
|
|
|
-v) verbose=yes; shift ;;
|
2006-07-17 17:34:22 +02:00
|
|
|
--) shift; break ;;
|
|
|
|
-*) ${ECHO} 1>&2 "$self: unknown option -- ${1#-}"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*) break ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
if ${TEST} -n "$checksum" -a -z "$distinfo"; then
|
2006-07-19 00:41:06 +02:00
|
|
|
${ECHO} 1>&2 "$self: \`\`-c'' requires \`\`-f distinfo''."
|
2006-07-17 17:34:22 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
2006-07-19 00:41:06 +02:00
|
|
|
if ${TEST} -n "$resume"; then
|
|
|
|
if ${TEST} -z "$distinfo"; then
|
|
|
|
${ECHO} 1>&2 "$self: \`\`-r'' requires \`\`-f distinfo''."
|
|
|
|
resume=
|
|
|
|
elif ${TEST} "x${FETCH_RESUME_ARGS}" = "x"; then
|
|
|
|
${ECHO} 1>&2 "$self: \`\`-r'' requires FETCH_RESUME_ARGS to be non-empty."
|
|
|
|
resume=
|
|
|
|
fi
|
|
|
|
${TEST} -n "$resume" ||
|
|
|
|
${ECHO} 1>&2 "$self: Falling back to non-resume fetch."
|
2006-07-17 17:34:22 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Process required arguments
|
2015-09-19 13:45:56 +02:00
|
|
|
if ${TEST} $# -lt 1; then
|
2006-07-17 17:34:22 +02:00
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
file="$1"; shift
|
2006-07-19 00:41:06 +02:00
|
|
|
path="$fetchdir/$file"
|
2006-07-17 17:34:22 +02:00
|
|
|
|
|
|
|
if ${TEST} -n "$distinfo" && ${TEST} ! -f "$distinfo"; then
|
|
|
|
${ECHO} 1>&2 "$self: distinfo file missing: $distinfo"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Compute the expected size of the fetched file.
|
|
|
|
distsize=
|
|
|
|
distunits=
|
|
|
|
if ${TEST} -n "$distinfo"; then
|
|
|
|
while read d_type d_file d_equals d_size d_units; do
|
|
|
|
case "$d_type" in
|
|
|
|
Size) ;; # only handle "Size" lines
|
|
|
|
*) continue ;;
|
|
|
|
esac
|
2006-07-19 00:41:06 +02:00
|
|
|
case "$fetchdir" in
|
|
|
|
".") ${TEST} "$d_file" = "($file)" || continue ;;
|
|
|
|
*) ${TEST} "$d_file" = "($path)" || continue ;;
|
|
|
|
esac
|
2006-07-17 17:34:22 +02:00
|
|
|
distsize="$d_size"; distunits="$d_units"
|
|
|
|
break
|
|
|
|
done < $distinfo
|
|
|
|
fi
|
|
|
|
|
2008-12-14 15:09:32 +01:00
|
|
|
# verify_file [-v] $file
|
2006-07-19 00:41:06 +02:00
|
|
|
# If we can checksum the file, then see if it matches the listed
|
|
|
|
# checksums in the distinfo file. If we can check the size, then
|
|
|
|
# check that instead. We strip off ".pkgsrc.resume" from the
|
|
|
|
# filename so that we can verify the checksum for the temporary
|
|
|
|
# fetch file as well.
|
|
|
|
#
|
|
|
|
verify_file() {
|
2008-12-14 15:09:32 +01:00
|
|
|
_if_verbose=:; if [ "x$1" = "x-v" ]; then shift; _if_verbose=; fi
|
2006-07-19 00:41:06 +02:00
|
|
|
_file="${1#./}"
|
2008-12-14 15:09:32 +01:00
|
|
|
${TEST} -f $_file || {
|
|
|
|
$_if_verbose ${ECHO} 1>&2 "$self: File $_file does not exist."
|
|
|
|
return 1
|
|
|
|
}
|
2006-07-19 00:41:06 +02:00
|
|
|
if ${TEST} -n "$checksum"; then
|
2008-12-14 15:09:32 +01:00
|
|
|
${CHECKSUM} -s ".pkgsrc.resume" $distinfo ${_file} || {
|
|
|
|
$_if_verbose ${ECHO} 1>&2 "$self: Checksum of the file $_file doesn't match."
|
|
|
|
return 1
|
|
|
|
}
|
2006-07-19 00:41:06 +02:00
|
|
|
return 0
|
|
|
|
elif ${TEST} -n "$distsize"; then
|
|
|
|
_size=`${WC} -c < $_file`
|
2008-12-14 15:09:32 +01:00
|
|
|
${TEST} "$_size" -eq "$distsize" || {
|
|
|
|
$_if_verbose ${ECHO} 1>&2 "$self: Size of the file $_file doesn't match."
|
|
|
|
return 1
|
|
|
|
}
|
2006-07-19 00:41:06 +02:00
|
|
|
return 0
|
2006-07-17 17:34:22 +02:00
|
|
|
fi
|
2006-07-19 00:41:06 +02:00
|
|
|
return 0;
|
2006-07-17 17:34:22 +02:00
|
|
|
}
|
|
|
|
|
2006-07-19 00:41:06 +02:00
|
|
|
# If the file already exists and it verifies, then we don't need to fetch
|
|
|
|
# it again.
|
|
|
|
#
|
|
|
|
if verify_file $path; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2007-08-04 17:00:29 +02:00
|
|
|
${TEST} -d $fetchdir || ${MKDIR} -p $fetchdir 2>/dev/null
|
2021-12-16 15:10:46 +01:00
|
|
|
|
|
|
|
${TEST} -w $fetchdir || ${ECHO} 1>&2 "$self: WARNING: DISTDIR `cd $fetchdir && pwd` looks non-writable."
|
2007-08-04 17:00:29 +02:00
|
|
|
|
2006-07-19 00:41:06 +02:00
|
|
|
# Set the name of the output file. In the "resume" case, we initialize
|
|
|
|
# the fetch loop by ensuring that the temporary output file already
|
|
|
|
# exists.
|
|
|
|
#
|
|
|
|
outputfile="$file"
|
|
|
|
outputpath="$fetchdir/$outputfile"
|
|
|
|
if ${TEST} -n "$resume"; then
|
|
|
|
outputfile="${file}.pkgsrc.resume"
|
|
|
|
outputpath="$fetchdir/$outputfile"
|
|
|
|
if ${TEST} ! -f $outputpath; then
|
|
|
|
if ${TEST} -f $path; then
|
|
|
|
${CP} -f $path $outputpath
|
|
|
|
else
|
2013-02-16 08:32:28 +01:00
|
|
|
${RM} -f $outputpath
|
2006-07-19 00:41:06 +02:00
|
|
|
${TOUCH} $outputpath
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
#
|
|
|
|
# If the temporary file verifies, then we don't need to resume
|
|
|
|
# fetching it.
|
|
|
|
#
|
|
|
|
if verify_file $outputpath; then
|
|
|
|
${MV} -f $outputpath $path
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
size=`${WC} -c < $outputpath`
|
|
|
|
${ECHO} "=> Downloaded size: $size bytes"
|
|
|
|
fi
|
|
|
|
${TEST} -z "$distsize" || ${ECHO} "=> Total size: $distsize $distunits"
|
|
|
|
|
2006-07-19 17:13:40 +02:00
|
|
|
# Iterate over each site and try to fetch the file. We verify the fetched
|
|
|
|
# file to see if we need to try fetching from the next site.
|
|
|
|
#
|
2006-07-17 17:34:22 +02:00
|
|
|
while ${TEST} $# -gt 0; do
|
|
|
|
site="$1"; shift
|
2009-08-02 04:38:52 +02:00
|
|
|
case "$site" in
|
|
|
|
-*)
|
|
|
|
url=${site#-}
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
url=$site$file
|
|
|
|
;;
|
|
|
|
esac
|
2006-07-19 00:41:06 +02:00
|
|
|
|
|
|
|
( cd $fetchdir
|
|
|
|
if ${TEST} -n "$resume"; then
|
2009-08-02 04:38:52 +02:00
|
|
|
fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} ${FETCH_RESUME_ARGS} ${FETCH_OUTPUT_ARGS} $outputfile $url"
|
2006-07-19 00:41:06 +02:00
|
|
|
else
|
2009-08-02 04:38:52 +02:00
|
|
|
fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} ${FETCH_OUTPUT_ARGS} $outputfile $url ${FETCH_AFTER_ARGS}"
|
2006-07-19 16:16:29 +02:00
|
|
|
fi
|
|
|
|
${TEST} -z "$verbose" || ${ECHO} "$fetch_cmd"
|
|
|
|
$fetch_cmd )
|
2006-07-19 00:41:06 +02:00
|
|
|
if ${TEST} $? -ne 0; then
|
|
|
|
${ECHO} 1>&2 "$self: Unable to fetch expected file $file"
|
|
|
|
continue
|
|
|
|
fi
|
2008-12-14 15:09:32 +01:00
|
|
|
if verify_file -v $outputpath; then
|
2006-07-19 00:41:06 +02:00
|
|
|
${TEST} -z "$resume" || ${MV} -f $outputpath $path
|
|
|
|
break
|
2006-07-17 17:34:22 +02:00
|
|
|
fi
|
2006-07-19 00:41:06 +02:00
|
|
|
if ${TEST} -n "$resume"; then
|
|
|
|
if ${TEST} -f $path; then
|
|
|
|
${CP} -f $path $outputpath
|
2006-07-17 17:34:22 +02:00
|
|
|
else
|
2006-07-19 00:41:06 +02:00
|
|
|
${TOUCH} $outputpath
|
2006-07-17 17:34:22 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
2015-09-08 13:13:12 +02:00
|
|
|
if ${TEST} -f $path && ${TEST} -n "$post_fetch"; then
|
2015-09-08 14:37:56 +02:00
|
|
|
"$post_fetch" $path "$url"
|
2015-09-08 13:13:12 +02:00
|
|
|
fi
|
2006-07-19 00:41:06 +02:00
|
|
|
if ${TEST} -f $path; then
|
2006-07-17 17:34:22 +02:00
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|