Skip to content

Commit

Permalink
src/nix/prefetch: fix prefetch containing current directory instead o…
Browse files Browse the repository at this point in the history
…f 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.
  • Loading branch information
Mic92 committed Jul 5, 2024
1 parent 8f280d7 commit 592360d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/nix/prefetch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,19 @@ std::tuple<StorePath, Hash> prefetchFile(
createDirs(unpacked);
unpackTarfile(tmpFile.string(), unpacked);

unsigned fileCount = 0;
/* 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
for (auto & entry : std::filesystem::directory_iterator{unpacked}) {
fileCount++;
if (fileCount > 1) {
break;
}
tmpFile = entry.path();
}
if (fileCount != 1) {
tmpFile = unpacked;
}
}

Activity act(*logger, lvlChatty, actUnknown,
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/tarball.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ test_tarball() {
# with the content-addressing
(! nix-instantiate --eval -E "fetchTree { type = \"tarball\"; url = file://$tarball; narHash = \"$hash\"; name = \"foo\"; }")

data=$(nix store prefetch-file --json "file://$tarball")
diff "$(echo "$data" | jq -r .storePath)" "$tarball"
data2=$(nix store prefetch-file --json --unpack "file://$tarball")
diff -r "$(echo "$data2" | jq -r .storePath)" "$tarroot"
}

test_tarball '' cat
Expand Down

0 comments on commit 592360d

Please sign in to comment.