Skip to content

Commit c9c3ed2

Browse files
committed
pkg,dist/tools: repl. DOWNLOAD_TO_FILE w/ DLCACHE, enh. dlcache.sh
The dlcache script is useful to avoid unnecessary repeated downloads of zip archives, similar to the git-cache scripts. The command order was changed to stay compatible with DOWNLOAD_TO_FILE and the only other usage of $(DLCACHE) was changed accordingly. To avoid the risk of collisions, dlcache now uses SHA512 sums instead of MD5. If no SHA512 checksum is given to dlcache.sh, it will just act as $(DOWNLOAD_TO_FILE).
1 parent 7a8ade7 commit c9c3ed2

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

Makefile.include

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ ifeq (,$(and $(DOWNLOAD_TO_STDOUT),$(DOWNLOAD_TO_FILE)))
352352
DOWNLOAD_TO_STDOUT ?= $(if $(CURL),$(CURL) -s,$(WGET) -q -O-)
353353
endif
354354
ifeq (,$(DOWNLOAD_TO_FILE))
355-
DOWNLOAD_TO_FILE ?= $(if $(WGET),$(WGET) -nv -c -O,$(CURL) -s -o)
355+
DOWNLOAD_TO_FILE ?= $(DLCACHE)
356356
endif
357357
endif
358358

dist/tools/dlcache/README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
# Introduction
22

3-
This script aims to cache http(s)-downloads
3+
This script aims to cache http(s)-downloads of (mainly) zip archives.
44

55
## How it works
66

7-
- if a file with the right name and md5 exists, nothing happens
8-
- if a file with the right name and md5 is in cache, use that
9-
- if a file with the right name but wrong md5 is in cache, redownload
7+
- if a file with the right name and SHA512 exists, nothing happens
8+
- if a file with the right name and SHA512 is in cache, use that
9+
- if a file with the right name but wrong SHA512 is in cache, redownload
1010
download to cache
11-
- after download, check md5, then copy to target
11+
- after download, check SHA512, then copy to target
12+
- if no SHA512 checksum is given, always redownload
13+
14+
## Usage
15+
16+
In order to stay compatible with the `$(DOWNLOAD_TO_FILE)` command,
17+
the command usage was changed for Release 2025.10.
18+
19+
If no SHA512 checksum was given, the file will *always* be downloaded,
20+
regardless of whether it is in the cache or not.
21+
22+
```sh
23+
dist/tools/dlcache/dlcache.sh <output_folder> <URL> [SHA512 checksum]
24+
```

dist/tools/dlcache/dlcache.sh

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,16 @@ else
3333
}
3434
fi
3535

36-
if [ "$(uname)" = Darwin ]; then
37-
MD5="md5 -r"
38-
else
39-
MD5=md5sum
40-
fi
36+
# shasum is supported on Linux and Darwin
37+
SHA512="shasum -a 512"
4138

42-
calcmd5() {
39+
calcsha512() {
4340
local file="$1"
44-
local md5="$2"
45-
local file_md5
46-
file_md5=$(${MD5} "$file" | cut -d\ -f1)
41+
local sha512="$2"
42+
local file_sha512
43+
file_sha512=$(${SHA512} "$file" | cut -d\ -f1)
4744

48-
test "$md5" = "$file_md5"
45+
test "$sha512" = "$file_sha512"
4946
}
5047

5148
downloader() {
@@ -60,28 +57,28 @@ downloader() {
6057
}
6158

6259
download() {
63-
local url="$1"
64-
local _md5="$2"
60+
local url="$2"
61+
local _sha512="$3"
6562
local basename_url
6663
basename_url="$(basename "${url}")"
67-
local target="${3:-"${basename_url}"}"
64+
local target="${1:-"${basename_url}"}"
6865

69-
[ -f "$target" ] && {
70-
# if our target file exists, check it's md5.
71-
calcmd5 "$target" "$_md5" && {
72-
_echo "$0: target exists, md5 matches."
66+
if [ -f "$target" ] && [ -n "$_sha512" ]; then
67+
# if our target file exists and an SHA512 sum was given, check it's SHA512
68+
calcsha512 "$target" "$_sha512" && {
69+
_echo "$0: target exists, SHA512 matches."
7370
exit 0
7471
}
75-
}
72+
fi
7673

7774
local filename
7875
filename="$(basename "$url")"
7976
[ -f "$DLCACHE_DIR/$filename" ] && {
80-
# if the file exists in cache, check it's md5 and possibly remove it.
81-
if calcmd5 "$DLCACHE_DIR/$filename" "$_md5"; then
77+
# if the file exists in cache, check it's SHA512 and possibly remove it.
78+
if calcsha512 "$DLCACHE_DIR/$filename" "$_sha512"; then
8279
_echo "$0: getting \"$url\" from cache"
8380
else
84-
_echo "$0: \"$DLCACHE_DIR/$filename\" has wrong checksum, re-downloading"
81+
_echo "$0: \"$DLCACHE_DIR/$filename\" has wrong or no checksum, re-downloading"
8582
rm "$DLCACHE_DIR/$filename"
8683
fi
8784
}
@@ -95,10 +92,13 @@ download() {
9592
_echo "$0: done downloading \"$url\""
9693
}
9794

98-
calcmd5 "$DLCACHE_DIR/$filename" "$_md5" || {
99-
_echo "$0: checksum mismatch!"
100-
exit 1
101-
}
95+
# only try to calculate the checksum if a checksum was given
96+
if [ -n "$_sha512" ]; then
97+
calcsha512 "$DLCACHE_DIR/$filename" "$_sha512" || {
98+
_echo "$0: checksum mismatch!"
99+
exit 1
100+
}
101+
fi
102102

103103
if [ "$target" = "-" ]; then
104104
cat "$DLCACHE_DIR/$filename"

makefiles/mcuboot.mk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ SIGN_BINFILE = $(BINDIR)/signed-$(APPLICATION).bin
88
MCUBOOT_KEYFILE ?= $(BINDIR)/key.pem
99
MCUBOOT_BIN ?= $(BINDIR)/mcuboot.bin
1010
MCUBOOT_BIN_URL ?= http://download.riot-os.org/mynewt.mcuboot.bin
11-
MCUBOOT_BIN_MD5 ?= 0c71a0589bd3709fc2d90f07a0035ce7
11+
MCUBOOT_BIN_SHA512 ?=
1212

1313
export IMAGE_HDR_SIZE ?= 512
1414

@@ -37,7 +37,7 @@ mcuboot: mcuboot-create-key link
3737
@$(COLOR_ECHO)
3838

3939
$(MCUBOOT_BIN):
40-
$(Q)$(DLCACHE) $(MCUBOOT_BIN_URL) $(MCUBOOT_BIN_MD5) $@
40+
$(Q)$(DLCACHE) $@ $(MCUBOOT_BIN_URL) $(MCUBOOT_BIN_SHA512)
4141

4242
.PHONY: mcuboot-flash-bootloader mcuboot-flash
4343

pkg/Makefile.http

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PKG_URL = http://example.com/downloads # source url of the package e.g. a gi
33
PKG_VERSION = v1.2.3 # version of the package to use e.g. a git commit/ref
44
PKG_EXT = zip # extension of this package
55
PKG_LICENSE = MIT # license of the package
6+
PKG_SHA512 = 5bcc4310f09ea247f5b65d63afae872fb92bb5c39029f336d94503fe04feccfcd22aa42b377b0927ab7f0a19111fd5a0a6842ecab147761c8f218f7f115e0da5 # SHA512 checksum of the package
67

78
ifneq ($(RIOTBASE),)
89
include $(RIOTBASE)/Makefile.base
@@ -30,7 +31,9 @@ $(CURDIR)/$(PKG_NAME)-$(PKG_VERSION)/: $(CURDIR)/$(PKG_NAME)-$(PKG_VERSION).$(PK
3031

3132
$(CURDIR)/$(PKG_NAME)-$(PKG_VERSION).$(PKG_EXT):
3233
# Get PKG_VERSION of package from PKG_URL
33-
$(Q)$(DOWNLOAD_TO_FILE) $@ $(PKG_URL)/$(PKG_NAME)-$(PKG_VERSION).$(PKG_EXT)
34+
$(Q)$(DOWNLOAD_TO_FILE) $@ $(PKG_URL)/$(PKG_NAME)-$(PKG_VERSION).$(PKG_EXT) $(PKG_SHA512)
35+
# Note: Since Release 2025.10, DOWNLOAD_TO_FILE uses dlcache and
36+
# requires an SHA512 checksum for caching!
3437

3538
clean::
3639
# Reset package to checkout state.

0 commit comments

Comments
 (0)