Skip to content

Commit

Permalink
Merge pull request #11052 from NixOS/backport-11051-to-2.23-maintenance
Browse files Browse the repository at this point in the history
[Backport 2.23-maintenance] src/nix/prefetch: fix prefetch containing current directory instead o…
  • Loading branch information
roberth committed Jul 5, 2024
2 parents df877f4 + d585924 commit f1deb42
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/nix/prefetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,15 @@ std::tuple<StorePath, Hash> 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();
auto fileCount = std::distance(entries, std::filesystem::directory_iterator{});
if (fileCount != 1) {
/* otherwise, use the directory itself */
tmpFile = unpacked;
}
}

Activity act(*logger, lvlChatty, actUnknown,
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/tarball.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f1deb42

Please sign in to comment.