From 73f3179954ef8db5c8774c79272dd1215fe6f5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 5 Jul 2024 17:26:58 +0200 Subject: [PATCH 1/2] src/nix/prefetch: fix prefetch containing current directory instead of tarball When --unpack was used the nix would add the current directory to the nix store instead of the content of unpacked. The reason for this is that std::distance already consumes the iterator. To fix this we re-instantiate the directory iterator in case the directory only contains a single entry. (cherry picked from commit 8cea1fbd97903e58b4de780bbf428b981f196e20) --- src/nix/prefetch.cc | 11 ++++++----- tests/functional/tarball.sh | 12 ++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 3ce52acc5..2aa27441a 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -113,14 +113,15 @@ std::tuple prefetchFile( createDirs(unpacked); unpackTarfile(tmpFile.string(), unpacked); + auto entries = std::filesystem::directory_iterator{unpacked}; /* If the archive unpacks to a single file/directory, then use that as the top-level. */ - auto entries = std::filesystem::directory_iterator{unpacked}; - auto file_count = std::distance(entries, std::filesystem::directory_iterator{}); - if (file_count == 1) - tmpFile = entries->path(); - else + tmpFile = entries->path(); + unsigned fileCount = std::distance(entries, std::filesystem::directory_iterator{}); + if (fileCount != 1) { + /* otherwise, use the directory itself */ tmpFile = unpacked; + } } Activity act(*logger, lvlChatty, actUnknown, diff --git a/tests/functional/tarball.sh b/tests/functional/tarball.sh index ce162ddce..28ed62207 100755 --- a/tests/functional/tarball.sh +++ b/tests/functional/tarball.sh @@ -54,6 +54,18 @@ test_tarball() { # with the content-addressing (! nix-instantiate --eval -E "fetchTree { type = \"tarball\"; url = file://$tarball; narHash = \"$hash\"; name = \"foo\"; }") + store_path=$(nix store prefetch-file --json "file://$tarball" | jq -r .storePath) + if ! cmp -s "$store_path" "$tarball"; then + echo "prefetched tarball differs from original: $store_path vs $tarball" >&2 + exit 1 + fi + store_path2=$(nix store prefetch-file --json --unpack "file://$tarball" | jq -r .storePath) + diff_output=$(diff -r "$store_path2" "$tarroot") + if [ -n "$diff_output" ]; then + echo "prefetched tarball differs from original: $store_path2 vs $tarroot" >&2 + echo "$diff_output" + exit 1 + fi } test_tarball '' cat From d58592469db6ade17c047e39f2af44e45f3d07de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 5 Jul 2024 19:45:03 +0200 Subject: [PATCH 2/2] Update src/nix/prefetch.cc Co-authored-by: Eelco Dolstra (cherry picked from commit 05381c0b3093a0b5e091946ce2f6fa9f02b981ec) --- src/nix/prefetch.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nix/prefetch.cc b/src/nix/prefetch.cc index 2aa27441a..f19a01ca3 100644 --- a/src/nix/prefetch.cc +++ b/src/nix/prefetch.cc @@ -117,7 +117,7 @@ std::tuple prefetchFile( /* If the archive unpacks to a single file/directory, then use that as the top-level. */ tmpFile = entries->path(); - unsigned fileCount = std::distance(entries, std::filesystem::directory_iterator{}); + auto fileCount = std::distance(entries, std::filesystem::directory_iterator{}); if (fileCount != 1) { /* otherwise, use the directory itself */ tmpFile = unpacked;