From 0fd1a6da318194d7d035b393bceff3942fcfc111 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 | 8 +++++ lib/setupHooks/installCargoArtifactsHook.sh | 36 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/setupHooks/inheritCargoArtifactsHook.sh b/lib/setupHooks/inheritCargoArtifactsHook.sh index 66dab689..c8d79399 100644 --- a/lib/setupHooks/inheritCargoArtifactsHook.sh +++ b/lib/setupHooks/inheritCargoArtifactsHook.sh @@ -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}" diff --git a/lib/setupHooks/installCargoArtifactsHook.sh b/lib/setupHooks/installCargoArtifactsHook.sh index 46fbe244..eb22f590 100644 --- a/lib/setupHooks/installCargoArtifactsHook.sh +++ b/lib/setupHooks/installCargoArtifactsHook.sh @@ -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}" @@ -55,6 +87,10 @@ prepareAndInstallCargoArtifactsDir() { case "${mode}" in "use-zstd") + compressAndInstallCargoArtifactsDirIncremental "${dir}" "${cargoTargetDir}" + ;; + + "use-zstd-no-incr") compressAndInstallCargoArtifactsDir "${dir}" "${cargoTargetDir}" ;;