Skip to content

Commit

Permalink
feature: incremental zstd mode
Browse files Browse the repository at this point in the history
  • Loading branch information
dpc committed Sep 17, 2023
1 parent 8b4f7a4 commit 0fd1a6d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/setupHooks/inheritCargoArtifactsHook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ inheritCargoArtifacts() {

mkdir -p "${cargoTargetDir}"
if [ -f "${preparedArtifacts}" ]; then

if [ -f "${preparedArtifacts}.prev" ]; then
inheritCargoArtifacts "$(readlink -f "${preparedArtifacts}.prev")" "$cargoTargetDir"
fi

echo "decompressing cargo artifacts from ${preparedArtifacts} to ${cargoTargetDir}"

zstd -d "${preparedArtifacts}" --stdout | \
tar -x -C "${cargoTargetDir}" --strip-components=1
rm -f "${cargoTargetDir}/.crane-previous-archive"
ln -s "${preparedArtifacts}" "${cargoTargetDir}/.crane-previous-archive"

elif [ -d "${preparedArtifacts}" ]; then
echo "copying cargo artifacts from ${preparedArtifacts} to ${cargoTargetDir}"

Expand Down
36 changes: 36 additions & 0 deletions lib/setupHooks/installCargoArtifactsHook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ compressAndInstallCargoArtifactsDir() {
)
}

compressAndInstallCargoArtifactsDirIncremental() {
local dir="${1:?destination directory not defined}"
local cargoTargetDir="${2:?cargoTargetDir not defined}"

mkdir -p "${dir}"

local dest="${dir}/target.tar.zst"
echo "compressing ${cargoTargetDir} to ${dest}"
(
export SOURCE_DATE_EPOCH=1
touch -d @${SOURCE_DATE_EPOCH} "${TMPDIR}/.crane.source-date-epoch"

find "${cargoTargetDir}" \
-newer "${TMPDIR}/.crane.source-date-epoch" \
-print0 \
| tar \
--null \
--no-recursion \
--sort=name \
--mtime="@${SOURCE_DATE_EPOCH}" \
--owner=0 \
--group=0 \
--numeric-owner \
--pax-option=exthdr.name=%d/PaxHeaders/%f,delete=atime,delete=ctime \
-c -f - -T - \
| zstd "-T${NIX_BUILD_CORES:-0}" -o "${dest}"
if [ -e "${cargoTargetDir}/.crane-previous-archive" ]; then
cp -a "${cargoTargetDir}/.crane-previous-archive" "${dest}.prev"
fi
)
}

dedupAndInstallCargoArtifactsDir() {
local dest="${1:?destination directory not defined}"
local cargoTargetDir="${2:?cargoTargetDir not defined}"
Expand Down Expand Up @@ -55,6 +87,10 @@ prepareAndInstallCargoArtifactsDir() {

case "${mode}" in
"use-zstd")
compressAndInstallCargoArtifactsDirIncremental "${dir}" "${cargoTargetDir}"
;;

"use-zstd-no-incr")
compressAndInstallCargoArtifactsDir "${dir}" "${cargoTargetDir}"
;;

Expand Down

0 comments on commit 0fd1a6d

Please sign in to comment.