From c6449013f95f292171966a953702a32533748ce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Sat, 16 Sep 2023 22:45:56 -0700 Subject: [PATCH] feature: incremental zstd mode --- lib/setupHooks/inheritCargoArtifactsHook.sh | 7 +++++ lib/setupHooks/installCargoArtifactsHook.sh | 35 +++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/setupHooks/inheritCargoArtifactsHook.sh b/lib/setupHooks/inheritCargoArtifactsHook.sh index 66dab689..bdbe1beb 100644 --- a/lib/setupHooks/inheritCargoArtifactsHook.sh +++ b/lib/setupHooks/inheritCargoArtifactsHook.sh @@ -19,8 +19,15 @@ inheritCargoArtifacts() { if [ -f "${preparedArtifacts}" ]; then echo "decompressing cargo artifacts from ${preparedArtifacts} to ${cargoTargetDir}" + if [ -f "${preparedArtifacts}.prev" ]; then + inheritCargoArtifacts "$(readlink -f "${preparedArtifacts}.prev")" "$cargoTargetDir" + fi + 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}" diff --git a/lib/setupHooks/installCargoArtifactsHook.sh b/lib/setupHooks/installCargoArtifactsHook.sh index 46fbe244..e1637624 100644 --- a/lib/setupHooks/installCargoArtifactsHook.sh +++ b/lib/setupHooks/installCargoArtifactsHook.sh @@ -18,6 +18,37 @@ 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 \ + --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}" @@ -55,6 +86,10 @@ prepareAndInstallCargoArtifactsDir() { case "${mode}" in "use-zstd") + compressAndInstallCargoArtifactsDirIncremental "${dir}" "${cargoTargetDir}" + ;; + + "use-zstd-no-incr") compressAndInstallCargoArtifactsDir "${dir}" "${cargoTargetDir}" ;;