summaryrefslogtreecommitdiff
path: root/mk/fetch
diff options
context:
space:
mode:
authorjlam <jlam@pkgsrc.org>2006-07-17 15:34:22 +0000
committerjlam <jlam@pkgsrc.org>2006-07-17 15:34:22 +0000
commite65b7c137143f780ada530f2d8a4e7c00c1678a4 (patch)
tree15796fc0edbac5d4ffd7d4122faaa72ba871393e /mk/fetch
parentf53379e5a4e8384bc2f69021e1a40c5427773bba (diff)
downloadpkgsrc-e65b7c137143f780ada530f2d8a4e7c00c1678a4.tar.gz
First cut at a fetch script to replace the humungous fetch "macros" in
fetch.mk. This script currently completely replaces the functionality in _FETCH_FILE. I will eventually add the ability to resume a file transfer to this script.
Diffstat (limited to 'mk/fetch')
-rwxr-xr-xmk/fetch/fetch173
1 files changed, 173 insertions, 0 deletions
diff --git a/mk/fetch/fetch b/mk/fetch/fetch
new file mode 100755
index 00000000000..97f584cfe56
--- /dev/null
+++ b/mk/fetch/fetch
@@ -0,0 +1,173 @@
+#!/bin/sh
+#
+# $NetBSD: fetch,v 1.1 2006/07/17 15:34:22 jlam Exp $
+#
+# Copyright (c) 2006 The NetBSD Foundation, Inc.
+# 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.
+# 3. All advertising materials mentioning features or use of this software
+# must display the following acknowledgement:
+# This product includes software developed by the NetBSD
+# Foundation, Inc. and its contributors.
+# 4. Neither the name of The NetBSD Foundation nor the names of its
+# contributors may be used to endorse or promote products derived
+# from this software without specific prior written permission.
+#
+# 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
+# fetch [-c] [-f distinfo] file site ..."
+#
+# 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.
+# If the file cannot be fetched successfully, then try the next
+# listed site.
+#
+# OPTIONS
+# -c Verify the checksum for the file. If the checksum
+# does not match, then the fetch is determined to be
+# not sucessful.
+#
+# -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.
+#
+######################################################################
+
+set -e # exit on errors
+
+: ${CHECKSUM:=checksum}
+: ${ECHO:=echo}
+: ${FETCH_CMD:=ftp}
+: ${RM:=rm}
+: ${TEST:=test}
+: ${WC:=wc}
+
+self="${0##*/}"
+
+usage() {
+ ${ECHO} 1>&2 "usage: $self [-c] [-f distinfo] file site ..."
+}
+
+# Process optional arguments
+checksum=
+distinfo=
+while ${TEST} $# -gt 0; do
+ case "$1" in
+ -c) checksum=yes; shift ;;
+ -f) distinfo="$2"; shift 2 ;;
+ --) shift; break ;;
+ -*) ${ECHO} 1>&2 "$self: unknown option -- ${1#-}"
+ usage
+ exit 1
+ ;;
+ *) break ;;
+ esac
+done
+if ${TEST} -n "$checksum" -a -z "$distinfo"; then
+ ${ECHO} 1>&2 "$self: \`\`-c'' requires \``-f distinfo''"
+ exit 1
+fi
+if ${TEST} -n "$resume" -a ! -z "$distinfo"; then
+ ${ECHO} 1>&2 "$self: \`\`-r'' requires \``-f distinfo''"
+ exit 1
+fi
+
+# Process required arguments
+if ${TEST} $# -lt 2; then
+ usage
+ exit 1
+fi
+file="$1"; shift
+
+if ${TEST} -n "$distinfo" && ${TEST} ! -f "$distinfo"; then
+ ${ECHO} 1>&2 "$self: distinfo file missing: $distinfo"
+ exit 1
+fi
+
+dir="${file%/*}"
+${TEST} "$dir" != "$file" || dir=.
+if ${TEST} ! -w "$dir/."; then
+ ${ECHO} 1>&2: "$self: Cannot write to $dir"
+ 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
+ ${TEST} "d_file" != "($file)" || continue
+ distsize="$d_size"; distunits="$d_units"
+ break
+ done < $distinfo
+fi
+
+checksum_file() {
+ _file="$1"
+ if ${TEST} -z "$checksum" || ${CHECKSUM} $distinfo $_file; then
+ return 0;
+ fi
+ return 1
+}
+
+bfile="${file##*/}"
+while ${TEST} $# -gt 0; do
+ site="$1"; shift
+ ${TEST} -z "$distsize" || ${ECHO} "=> [$distsize $distunits]"
+ fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} ${site}${bfile} ${FETCH_AFTER_ARGS}"
+ if $fetch_cmd; then
+ : # successful fetch of file
+ else
+ ${RM} -f $file
+ fi
+ if ${TEST} -f $file; then
+ if checksum_file $file; then
+ break
+ else
+ ${ECHO} 1>&2 "$self: Checksum failure for $bfile"
+ ${RM} -f $file
+ fi
+ else
+ ${ECHO} 1>&2 "$self: Unable to fetch expected file $bfile"
+ fi
+done
+if ${TEST} -f "$file"; then
+ exit 0
+else
+ exit 1
+fi