Sun Dec 14 14:09:33 2008 UTC ()
Added a -v flag to the verify_file function, which in case of a
verification failure prints a useful and concrete error message.


(rillig)
diff -r1.10 -r1.11 pkgsrc/mk/fetch/fetch

cvs diff -r1.10 -r1.11 pkgsrc/mk/fetch/fetch (expand / switch to unified diff)

--- pkgsrc/mk/fetch/fetch 2008/09/15 21:07:46 1.10
+++ pkgsrc/mk/fetch/fetch 2008/12/14 14:09:32 1.11
@@ -1,16 +1,16 @@ @@ -1,16 +1,16 @@
1#!/bin/sh 1#!/bin/sh
2# 2#
3# $NetBSD: fetch,v 1.10 2008/09/15 21:07:46 rillig Exp $ 3# $NetBSD: fetch,v 1.11 2008/12/14 14:09:32 rillig Exp $
4# 4#
5# Copyright (c) 2006 The NetBSD Foundation, Inc. 5# Copyright (c) 2006 The NetBSD Foundation, Inc.
6# All rights reserved. 6# All rights reserved.
7# 7#
8# This code is derived from software contributed to The NetBSD Foundation 8# This code is derived from software contributed to The NetBSD Foundation
9# by Johnny C. Lam. 9# by Johnny C. Lam.
10# 10#
11# Redistribution and use in source and binary forms, with or without 11# Redistribution and use in source and binary forms, with or without
12# modification, are permitted provided that the following conditions 12# modification, are permitted provided that the following conditions
13# are met: 13# are met:
14# 1. Redistributions of source code must retain the above copyright 14# 1. Redistributions of source code must retain the above copyright
15# notice, this list of conditions and the following disclaimer. 15# notice, this list of conditions and the following disclaimer.
16# 2. Redistributions in binary form must reproduce the above copyright 16# 2. Redistributions in binary form must reproduce the above copyright
@@ -179,42 +179,54 @@ if ${TEST} -n "$distinfo"; then @@ -179,42 +179,54 @@ if ${TEST} -n "$distinfo"; then
179 case "$d_type" in 179 case "$d_type" in
180 Size) ;; # only handle "Size" lines 180 Size) ;; # only handle "Size" lines
181 *) continue ;; 181 *) continue ;;
182 esac 182 esac
183 case "$fetchdir" in 183 case "$fetchdir" in
184 ".") ${TEST} "$d_file" = "($file)" || continue ;; 184 ".") ${TEST} "$d_file" = "($file)" || continue ;;
185 *) ${TEST} "$d_file" = "($path)" || continue ;; 185 *) ${TEST} "$d_file" = "($path)" || continue ;;
186 esac 186 esac
187 distsize="$d_size"; distunits="$d_units" 187 distsize="$d_size"; distunits="$d_units"
188 break 188 break
189 done < $distinfo 189 done < $distinfo
190fi 190fi
191 191
192# verify_file $file 192# verify_file [-v] $file
193# If we can checksum the file, then see if it matches the listed 193# If we can checksum the file, then see if it matches the listed
194# checksums in the distinfo file. If we can check the size, then 194# checksums in the distinfo file. If we can check the size, then
195# check that instead. We strip off ".pkgsrc.resume" from the 195# check that instead. We strip off ".pkgsrc.resume" from the
196# filename so that we can verify the checksum for the temporary 196# filename so that we can verify the checksum for the temporary
197# fetch file as well. 197# fetch file as well.
198# 198#
199verify_file() { 199verify_file() {
 200 _if_verbose=:; if [ "x$1" = "x-v" ]; then shift; _if_verbose=; fi
200 _file="${1#./}" 201 _file="${1#./}"
201 ${TEST} -f $_file || return 1 202 ${TEST} -f $_file || {
 203 $_if_verbose ${ECHO} 1>&2 "$self: File $_file does not exist."
 204 return 1
 205 }
202 if ${TEST} -n "$checksum"; then 206 if ${TEST} -n "$checksum"; then
203 ${CHECKSUM} -s ".pkgsrc.resume" $distinfo ${_file} || return 1 207 echo "foo"
 208 read line
 209 ${CHECKSUM} -s ".pkgsrc.resume" $distinfo ${_file} || {
 210 $_if_verbose ${ECHO} 1>&2 "$self: Checksum of the file $_file doesn't match."
 211 return 1
 212 }
204 return 0 213 return 0
205 elif ${TEST} -n "$distsize"; then 214 elif ${TEST} -n "$distsize"; then
206 _size=`${WC} -c < $_file` 215 _size=`${WC} -c < $_file`
207 ${TEST} "$_size" -eq "$distsize" || return 1 216 ${TEST} "$_size" -eq "$distsize" || {
 217 $_if_verbose ${ECHO} 1>&2 "$self: Size of the file $_file doesn't match."
 218 return 1
 219 }
208 return 0 220 return 0
209 fi 221 fi
210 return 0; 222 return 0;
211} 223}
212 224
213# If the file already exists and it verifies, then we don't need to fetch 225# If the file already exists and it verifies, then we don't need to fetch
214# it again. 226# it again.
215# 227#
216if verify_file $path; then 228if verify_file $path; then
217 exit 0 229 exit 0
218fi 230fi
219 231
220${TEST} -d $fetchdir || ${MKDIR} -p $fetchdir 2>/dev/null 232${TEST} -d $fetchdir || ${MKDIR} -p $fetchdir 2>/dev/null
@@ -260,31 +272,29 @@ while ${TEST} $# -gt 0; do @@ -260,31 +272,29 @@ while ${TEST} $# -gt 0; do
260 272
261 ( cd $fetchdir 273 ( cd $fetchdir
262 if ${TEST} -n "$resume"; then 274 if ${TEST} -n "$resume"; then
263 fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} ${FETCH_RESUME_ARGS} ${FETCH_OUTPUT_ARGS} $outputfile $site$file" 275 fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} ${FETCH_RESUME_ARGS} ${FETCH_OUTPUT_ARGS} $outputfile $site$file"
264 else 276 else
265 fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} $site$file ${FETCH_AFTER_ARGS}" 277 fetch_cmd="${FETCH_CMD} ${FETCH_BEFORE_ARGS} $site$file ${FETCH_AFTER_ARGS}"
266 fi 278 fi
267 ${TEST} -z "$verbose" || ${ECHO} "$fetch_cmd" 279 ${TEST} -z "$verbose" || ${ECHO} "$fetch_cmd"
268 $fetch_cmd ) 280 $fetch_cmd )
269 if ${TEST} $? -ne 0; then 281 if ${TEST} $? -ne 0; then
270 ${ECHO} 1>&2 "$self: Unable to fetch expected file $file" 282 ${ECHO} 1>&2 "$self: Unable to fetch expected file $file"
271 continue 283 continue
272 fi 284 fi
273 if verify_file $outputpath; then 285 if verify_file -v $outputpath; then
274 ${TEST} -z "$resume" || ${MV} -f $outputpath $path 286 ${TEST} -z "$resume" || ${MV} -f $outputpath $path
275 break 287 break
276 else 
277 ${ECHO} 1>&2 "$self: Unable to verify fetched file $file" 
278 fi 288 fi
279 if ${TEST} -n "$resume"; then 289 if ${TEST} -n "$resume"; then
280 if ${TEST} -f $path; then 290 if ${TEST} -f $path; then
281 ${CP} -f $path $outputpath 291 ${CP} -f $path $outputpath
282 else 292 else
283 ${TOUCH} $outputpath 293 ${TOUCH} $outputpath
284 fi 294 fi
285 fi 295 fi
286done 296done
287if ${TEST} -f $path; then 297if ${TEST} -f $path; then
288 exit 0 298 exit 0
289else 299else
290 exit 1 300 exit 1