2014-03-05 10:02:37 +00:00
|
|
|
# This hook downloads the distfiles specified in a template by
|
|
|
|
# the $distfiles variable and then verifies its sha256 checksum comparing
|
|
|
|
# its value with the one stored in the $checksum variable.
|
|
|
|
|
2015-07-03 16:30:34 +00:00
|
|
|
# Get the checksum for $curfile at index $dfcount
|
|
|
|
get_cksum() {
|
|
|
|
local curfile="$1" dfcount="$2" ckcount cksum i
|
2014-03-05 10:02:37 +00:00
|
|
|
|
2014-03-12 14:23:08 +00:00
|
|
|
ckcount=0
|
2015-07-03 16:30:34 +00:00
|
|
|
cksum=0
|
2014-03-12 14:23:08 +00:00
|
|
|
for i in ${checksum}; do
|
|
|
|
if [ $dfcount -eq $ckcount -a -n "$i" ]; then
|
|
|
|
cksum=$i
|
|
|
|
fi
|
2015-07-03 16:30:34 +00:00
|
|
|
ckcount=$((ckcount + 1))
|
2014-03-12 14:23:08 +00:00
|
|
|
done
|
2015-07-03 16:30:34 +00:00
|
|
|
if [ -z "$cksum" ]; then
|
2014-03-12 14:23:08 +00:00
|
|
|
msg_error "$pkgver: cannot find checksum for $curfile.\n"
|
|
|
|
fi
|
2015-07-03 16:30:34 +00:00
|
|
|
echo "$cksum"
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:07:30 +00:00
|
|
|
# Return the checksum of the contents of a tarball
|
|
|
|
contents_cksum() {
|
|
|
|
local curfile="$1" cursufx cksum
|
|
|
|
|
|
|
|
case $curfile in
|
|
|
|
*.tar.lzma) cursufx="txz";;
|
|
|
|
*.tar.lz) cursufx="tlz";;
|
|
|
|
*.tlz) cursufx="tlz";;
|
|
|
|
*.tar.xz) cursufx="txz";;
|
|
|
|
*.txz) cursufx="txz";;
|
|
|
|
*.tar.bz2) cursufx="tbz";;
|
|
|
|
*.tbz) cursufx="tbz";;
|
|
|
|
*.tar.gz) cursufx="tgz";;
|
|
|
|
*.tgz) cursufx="tgz";;
|
|
|
|
*.gz) cursufx="gz";;
|
|
|
|
*.bz2) cursufx="bz2";;
|
|
|
|
*.tar) cursufx="tar";;
|
|
|
|
*.zip) cursufx="zip";;
|
|
|
|
*.rpm) cursufx="rpm";;
|
|
|
|
*.patch) cursufx="txt";;
|
|
|
|
*.diff) cursufx="txt";;
|
|
|
|
*.txt) cursufx="txt";;
|
|
|
|
*.7z) cursufx="7z";;
|
2018-10-26 19:39:18 +00:00
|
|
|
*.gem) cursufx="gem";;
|
2018-10-31 21:11:01 +00:00
|
|
|
*.crate) cursufx="crate";;
|
2018-03-02 22:07:30 +00:00
|
|
|
*) msg_error "$pkgver: unknown distfile suffix for $curfile.\n";;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case ${cursufx} in
|
2018-10-31 21:11:01 +00:00
|
|
|
tar|txz|tbz|tlz|tgz|crate)
|
2018-03-02 22:07:30 +00:00
|
|
|
cksum=$(tar xf "$curfile" --to-stdout | sha256sum | awk '{print $1}')
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
msg_error "$pkgver: extracting $curfile to pipe.\n"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
gz)
|
|
|
|
cksum=$(gunzip -c "$curfile" | sha256sum | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
bz2)
|
|
|
|
cksum=$(bunzip2 -c "$curfile" | sha256sum | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
zip)
|
|
|
|
if command -v unzip &>/dev/null; then
|
|
|
|
cksum=$(unzip -p "$curfile" | sha256sum | awk '{print $1}')
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
msg_error "$pkgver: extracting $curfile to pipe.\n"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
msg_error "$pkgver: cannot find unzip bin for extraction.\n"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
rpm)
|
|
|
|
if command -v rpmextract &>/dev/null; then
|
|
|
|
cksum=$(rpm2cpio "$curfile" | bsdtar xf - --to-stdout | sha256sum | awk '{print $1}')
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
msg_error "$pkgver: extracting $curfile to pipe.\n"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
msg_error "$pkgver: cannot find rpmextract for extraction.\n"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
txt)
|
|
|
|
cksum=$(cat "$curfile" | sha256sum | awk '{print $1}')
|
|
|
|
;;
|
|
|
|
7z)
|
|
|
|
if command -v 7z &>/dev/null; then
|
|
|
|
cksum=$(7z x -o "$curfile" | sha256sum | awk '{print $1}')
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
msg_error "$pkgver: extracting $curfile to pipe.\n"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
msg_error "$pkgver: cannot find 7z bin for extraction.\n"
|
|
|
|
fi
|
|
|
|
;;
|
2018-10-26 19:39:18 +00:00
|
|
|
gem)
|
|
|
|
cksum=$(tar -xf "$curfile" data.tar.gz --to-stdout | tar -xzO | sha256sum | awk '{print $1}')
|
|
|
|
;;
|
2018-03-02 22:07:30 +00:00
|
|
|
*)
|
|
|
|
msg_error "$pkgver: cannot guess $curfile extract suffix. ($cursufx)\n"
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ -z "$cksum" ]; then
|
|
|
|
msg_error "$pkgver: cannot find contents checksum for $curfile.\n"
|
|
|
|
fi
|
|
|
|
echo "$cksum"
|
|
|
|
}
|
|
|
|
|
2015-07-03 16:30:34 +00:00
|
|
|
# Verify the checksum for $curfile stored at $distfile and index $dfcount
|
|
|
|
verify_cksum() {
|
|
|
|
local curfile="$1" distfile="$2" dfcount="$3" filesum cksum
|
2014-03-05 10:02:37 +00:00
|
|
|
|
2015-07-03 16:30:34 +00:00
|
|
|
cksum=$(get_cksum $curfile $dfcount)
|
2018-03-02 22:07:30 +00:00
|
|
|
|
|
|
|
# If the checksum starts with an commercial at (@) it is the contents checksum
|
|
|
|
if [ "${cksum:0:1}" = "@" ]; then
|
|
|
|
cksum=${cksum:1}
|
|
|
|
msg_normal "$pkgver: verifying contents checksum for distfile '$curfile'... "
|
|
|
|
filesum=$(contents_cksum "$curfile")
|
|
|
|
if [ "${cksum}" != "$filesum" ]; then
|
|
|
|
echo
|
|
|
|
msg_red "SHA256 mismatch for '$curfile:'\n@$filesum\n"
|
|
|
|
errors=$((errors + 1))
|
|
|
|
else
|
|
|
|
msg_normal_append "OK.\n"
|
|
|
|
fi
|
2014-03-05 10:02:37 +00:00
|
|
|
else
|
2018-03-02 22:07:30 +00:00
|
|
|
msg_normal "$pkgver: verifying checksum for distfile '$curfile'... "
|
|
|
|
filesum=$(${XBPS_DIGEST_CMD} "$distfile")
|
|
|
|
if [ "$cksum" != "$filesum" ]; then
|
|
|
|
echo
|
|
|
|
msg_red "SHA256 mismatch for '$curfile:'\n$filesum\n"
|
|
|
|
errors=$((errors + 1))
|
|
|
|
else
|
|
|
|
if [ ! -f "$XBPS_SRCDISTDIR/by_sha256/${cksum}_${curfile}" ]; then
|
|
|
|
mkdir -p "$XBPS_SRCDISTDIR/by_sha256"
|
|
|
|
ln -f "$distfile" "$XBPS_SRCDISTDIR/by_sha256/${cksum}_${curfile}"
|
|
|
|
fi
|
|
|
|
msg_normal_append "OK.\n"
|
2015-07-03 16:30:34 +00:00
|
|
|
fi
|
2014-03-05 10:02:37 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-07-03 16:30:34 +00:00
|
|
|
# Link an existing cksum $distfile for $curfile at index $dfcount
|
|
|
|
link_cksum() {
|
|
|
|
local curfile="$1" distfile="$2" dfcount="$3" filesum cksum
|
|
|
|
|
|
|
|
cksum=$(get_cksum $curfile $dfcount)
|
2018-03-02 22:07:30 +00:00
|
|
|
|
2015-07-03 16:30:34 +00:00
|
|
|
if [ -n "$cksum" -a -f "$XBPS_SRCDISTDIR/by_sha256/${cksum}_${curfile}" ]; then
|
|
|
|
ln -f "$XBPS_SRCDISTDIR/by_sha256/${cksum}_${curfile}" "$distfile"
|
|
|
|
msg_normal "$pkgver: using known distfile $curfile.\n"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-08-19 12:45:47 +00:00
|
|
|
try_mirrors() {
|
|
|
|
local curfile="$1" distfile="$2" dfcount="$3" subdir="$4" f="$5"
|
|
|
|
local filesum cksum basefile mirror path scheme
|
|
|
|
[ -z "$XBPS_DISTFILES_MIRROR" ] && return
|
2015-09-11 05:55:40 +00:00
|
|
|
basefile="${f##*/}"
|
2015-08-19 12:45:47 +00:00
|
|
|
cksum=$(get_cksum $curfile $dfcount)
|
|
|
|
for mirror in $XBPS_DISTFILES_MIRROR; do
|
|
|
|
scheme="file"
|
|
|
|
if [[ $mirror == *://* ]]; then
|
|
|
|
scheme="${mirror%%:/*}"
|
|
|
|
path="${mirror#${scheme}://}"
|
|
|
|
else
|
|
|
|
path="$mirror"
|
|
|
|
fi
|
|
|
|
if [ "$scheme" == "file" ]; then
|
|
|
|
# Skip file:// mirror locations (/some/where or file:///some/where)
|
|
|
|
# where the specified directory does not exist
|
|
|
|
if [ ! -d "$path" ]; then
|
|
|
|
msg_warn "$pkgver: mount point $path does not exist...\n"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
if [[ "$mirror" == *voidlinux* ]]; then
|
|
|
|
# For distfiles.voidlinux.* append the subdirectory
|
|
|
|
mirror="$mirror/$subdir"
|
|
|
|
fi
|
|
|
|
msg_normal "$pkgver: fetching distfile '$curfile' from '$mirror'...\n"
|
2015-09-04 15:13:31 +00:00
|
|
|
$XBPS_FETCH_CMD "$mirror/$curfile"
|
2015-08-19 12:45:47 +00:00
|
|
|
# If basefile was not found, but a curfile file may exist, try to fetch it
|
|
|
|
if [ ! -f "$distfile" -a "$basefile" != "$curfile" ]; then
|
2015-09-04 15:13:31 +00:00
|
|
|
$XBPS_FETCH_CMD "$mirror/$basefile"
|
2015-08-19 12:45:47 +00:00
|
|
|
fi
|
|
|
|
[ ! -f "$distfile" ] && continue
|
|
|
|
flock -n ${distfile}.part rm -f ${distfile}.part
|
|
|
|
filesum=$(${XBPS_DIGEST_CMD} "$distfile")
|
|
|
|
[ "$cksum" == "$filesum" ] && break
|
|
|
|
msg_normal "$pkgver: checksum failed - removing '$curfile'...\n"
|
|
|
|
rm -f ${distfile}
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2014-03-05 10:02:37 +00:00
|
|
|
hook() {
|
|
|
|
local srcdir="$XBPS_SRCDISTDIR/$pkgname-$version"
|
2015-11-05 21:05:25 +00:00
|
|
|
local dfcount=0 dfgood=0 errors=0
|
2014-03-05 10:02:37 +00:00
|
|
|
|
|
|
|
if [ ! -d "$srcdir" ]; then
|
|
|
|
mkdir -p -m775 "$srcdir"
|
|
|
|
chgrp $(id -g) "$srcdir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
cd $srcdir || msg_error "$pkgver: cannot change dir to $srcdir!\n"
|
|
|
|
|
2014-03-05 12:58:46 +00:00
|
|
|
# Disable trap on ERR; the code is smart enough to report errors and abort.
|
|
|
|
trap - ERR
|
|
|
|
|
2015-11-03 16:33:08 +00:00
|
|
|
# Detect distfiles with obsolete checksum and purge them from the cache
|
|
|
|
for f in ${distfiles}; do
|
|
|
|
curfile=$(basename "${f#*>}")
|
|
|
|
distfile="$srcdir/$curfile"
|
|
|
|
if [ -f "$distfile" ]; then
|
|
|
|
cksum=$(get_cksum $curfile $dfcount)
|
2018-03-02 22:07:30 +00:00
|
|
|
if [ "${cksum:0:1}" = "@" ]; then
|
|
|
|
cksum=${cksum:1}
|
|
|
|
filesum=$(contents_cksum "$distfile")
|
|
|
|
else
|
|
|
|
filesum=$(${XBPS_DIGEST_CMD} "$distfile")
|
|
|
|
fi
|
2015-11-05 21:05:25 +00:00
|
|
|
if [ "$cksum" = "$filesum" ]; then
|
|
|
|
dfgood=$((dfgood + 1))
|
|
|
|
else
|
2015-11-03 16:33:08 +00:00
|
|
|
inode=$(stat "$distfile" --printf "%i")
|
2018-03-02 22:07:30 +00:00
|
|
|
msg_warn "$pkgver: wrong checksum found for ${curfile} - purging\n"
|
2015-11-03 16:33:08 +00:00
|
|
|
find ${XBPS_SRCDISTDIR} -inum ${inode} -delete -print
|
|
|
|
fi
|
|
|
|
fi
|
2015-11-05 21:05:25 +00:00
|
|
|
dfcount=$((dfcount + 1))
|
2015-11-03 16:33:08 +00:00
|
|
|
done
|
|
|
|
|
2015-11-05 21:05:25 +00:00
|
|
|
# We're done, if all distfiles were found and had good checksums
|
|
|
|
[ $dfcount -eq $dfgood ] && return
|
|
|
|
|
2015-11-03 16:33:08 +00:00
|
|
|
# Download missing distfiles and verify their checksums
|
|
|
|
dfcount=0
|
2014-03-05 10:02:37 +00:00
|
|
|
for f in ${distfiles}; do
|
2014-03-12 12:47:57 +00:00
|
|
|
curfile=$(basename "${f#*>}")
|
2014-03-05 10:02:37 +00:00
|
|
|
distfile="$srcdir/$curfile"
|
2014-03-12 14:23:08 +00:00
|
|
|
|
|
|
|
# If file lock cannot be acquired wait until it's available.
|
2014-03-05 10:02:37 +00:00
|
|
|
while true; do
|
|
|
|
flock -w 1 ${distfile}.part true
|
2014-03-12 14:23:08 +00:00
|
|
|
[ $? -eq 0 ] && break
|
|
|
|
msg_warn "$pkgver: ${curfile} is being already downloaded, waiting for 1s ...\n"
|
2014-03-05 10:02:37 +00:00
|
|
|
done
|
2015-07-03 16:30:34 +00:00
|
|
|
# If distfile does not exist, try to link to it.
|
|
|
|
if [ ! -f "$distfile" ]; then
|
|
|
|
link_cksum $curfile $distfile $dfcount
|
|
|
|
fi
|
2015-08-19 12:45:47 +00:00
|
|
|
# If distfile does not exist, download it from a mirror location.
|
|
|
|
if [ ! -f "$distfile" ]; then
|
|
|
|
try_mirrors $curfile $distfile $dfcount $pkgname-$version $f
|
|
|
|
fi
|
|
|
|
# If distfile does not exist, download it from the original location.
|
2014-03-12 14:23:08 +00:00
|
|
|
if [ ! -f "$distfile" ]; then
|
|
|
|
msg_normal "$pkgver: fetching distfile '$curfile'...\n"
|
2014-03-12 14:47:49 +00:00
|
|
|
flock "${distfile}.part" $XBPS_FETCH_CMD "$f"
|
2015-08-19 12:45:47 +00:00
|
|
|
fi
|
|
|
|
if [ ! -f "$distfile" ]; then
|
|
|
|
msg_error "$pkgver: failed to fetch $curfile.\n"
|
2014-03-05 10:02:37 +00:00
|
|
|
fi
|
2014-03-12 14:23:08 +00:00
|
|
|
# distfile downloaded, verify sha256 hash.
|
|
|
|
flock -n ${distfile}.part rm -f ${distfile}.part
|
|
|
|
verify_cksum $curfile $distfile $dfcount
|
2015-11-05 21:05:25 +00:00
|
|
|
dfcount=$((dfcount + 1))
|
2014-03-05 10:02:37 +00:00
|
|
|
done
|
2014-03-07 10:12:52 +00:00
|
|
|
|
2014-03-07 10:22:51 +00:00
|
|
|
if [ $errors -gt 0 ]; then
|
2014-03-07 10:12:52 +00:00
|
|
|
msg_error "$pkgver: couldn't verify distfiles, exiting...\n"
|
|
|
|
fi
|
2014-03-05 10:02:37 +00:00
|
|
|
}
|