Skip to content

Commit 02a88e9

Browse files
committed
mk: fetch to temporary file
Download to a temporary file and move the file to the final location when the download is finished - so the file is there or not, but not a partially downloaded copy. The script already supported resuming downloads - the support is still there but basically will never happen again, because it only looks at a partial file of the correct name (which can still happen if the download finishes but provides an incomplete file, and the file cannot be verified by during download e.g. because the distinfo file is not available - a theoretically supported use case) As proposed on tech-pkg.
1 parent a376565 commit 02a88e9

1 file changed

Lines changed: 22 additions & 27 deletions

File tree

mk/fetch/fetch

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22
#
3-
# $NetBSD: fetch,v 1.22 2026/02/07 15:15:02 wiz Exp $
3+
# $NetBSD: fetch,v 1.23 2026/02/15 09:46:33 wiz Exp $
44
#
55
# Copyright (c) 2006, 2015 The NetBSD Foundation, Inc.
66
# All rights reserved.
@@ -45,6 +45,9 @@
4545
# If the file cannot be fetched successfully, then we try the next
4646
# listed site.
4747
#
48+
# The file will be downloaded under a temporary name and
49+
# renamed to the proper name when the download is done.
50+
#
4851
# If the file already exists on the disk and is verified, then
4952
# no fetch action is taken.
5053
#
@@ -189,7 +192,7 @@ if ${TEST} -n "$distinfo"; then
189192
done < $distinfo
190193
fi
191194

192-
# verify_file [-v] $file
195+
# verify_file [-v] $file $suffix
193196
# If we can checksum the file, then see if it matches the listed
194197
# checksums in the distinfo file. If we can check the size, then
195198
# check that instead. We strip off ".pkgsrc.resume" from the
@@ -199,12 +202,13 @@ fi
199202
verify_file() {
200203
_if_verbose=:; if [ "x$1" = "x-v" ]; then shift; _if_verbose=; fi
201204
_file="${1#./}"
205+
_suffix="$2"
202206
${TEST} -f $_file || {
203207
$_if_verbose ${ECHO} 1>&2 "$self: File $_file does not exist."
204208
return 1
205209
}
206210
if ${TEST} -n "$checksum"; then
207-
${CHECKSUM} -s ".pkgsrc.resume" $distinfo ${_file} || {
211+
${CHECKSUM} -s "${_suffix}" $distinfo ${_file} || {
208212
$_if_verbose ${ECHO} 1>&2 "$self: Checksum of the file $_file doesn't match."
209213
return 1
210214
}
@@ -223,7 +227,7 @@ verify_file() {
223227
# If the file already exists and it verifies, then we don't need to fetch
224228
# it again.
225229
#
226-
if verify_file $path; then
230+
if verify_file $path ""; then
227231
exit 0
228232
fi
229233

@@ -232,32 +236,23 @@ ${TEST} -d $fetchdir || ${MKDIR} -p $fetchdir 2>/dev/null
232236
${TEST} -w $fetchdir || ${ECHO} 1>&2 "$self: WARNING: DISTDIR `cd $fetchdir && pwd` looks non-writable."
233237

234238
# Set the name of the output file. In the "resume" case, we initialize
235-
# the fetch loop by ensuring that the temporary output file already
236-
# exists.
239+
# the fetch loop by providing the existing, possibly incomplete, file.
237240
#
238-
outputfile="$file"
241+
tmp_suffix=".$$.part"
242+
outputfile="$file$tmp_suffix"
239243
outputpath="$fetchdir/$outputfile"
244+
if ${TEST} -f $outputpath; then
245+
${ECHO} "Temporary download file ${outputpath} already existed, deleting"
246+
${RM} -f $outputpath
247+
fi
240248
if ${TEST} -n "$resume"; then
241-
outputfile="${file}.pkgsrc.resume"
242-
outputpath="$fetchdir/$outputfile"
243-
if ${TEST} ! -f $outputpath; then
244-
if ${TEST} -f $path; then
245-
${CP} -f $path $outputpath
246-
else
247-
${RM} -f $outputpath
248-
${TOUCH} $outputpath
249-
fi
250-
fi
251-
#
252-
# If the temporary file verifies, then we don't need to resume
253-
# fetching it.
254-
#
255-
if verify_file $outputpath; then
256-
${MV} -f $outputpath $path
257-
exit 0
249+
if ${TEST} -f $path; then
250+
${CP} -f $path $outputpath
251+
else
252+
${TOUCH} $outputpath
258253
fi
259254
size=`${WC} -c < $outputpath`
260-
${ECHO} "=> Downloaded size: $size bytes"
255+
${ECHO} "=> Downloaded size (before resume): $size bytes"
261256
fi
262257
${TEST} -z "$distsize" || ${ECHO} "=> Total size: $distsize $distunits"
263258

@@ -287,8 +282,8 @@ while ${TEST} $# -gt 0; do
287282
${ECHO} 1>&2 "$self: Unable to fetch expected file $file"
288283
continue
289284
fi
290-
if verify_file -v $outputpath; then
291-
${TEST} -z "$resume" || ${MV} -f $outputpath $path
285+
if verify_file -v $outputpath $tmp_suffix; then
286+
${MV} -f $outputpath $path
292287
break
293288
fi
294289
if ${TEST} -n "$resume"; then

0 commit comments

Comments
 (0)