1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-21 01:39:36 +01:00

Merge pull request #14273 from fzakaria/fzakaria/issue-13944

Make `nix nar [cat|ls]` lazy
This commit is contained in:
Farid Zakaria 2025-10-16 16:16:54 -07:00 committed by GitHub
parent 27767a6094
commit 64c55961eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 29 deletions

View file

@ -27,7 +27,12 @@ ref<SourceAccessor> makeNarAccessor(Source & source);
*/
using GetNarBytes = std::function<std::string(uint64_t, uint64_t)>;
ref<SourceAccessor> makeLazyNarAccessor(const std::string & listing, GetNarBytes getNarBytes);
/**
* The canonical GetNarBytes function for a seekable Source.
*/
GetNarBytes seekableGetNarBytes(const Path & path);
ref<SourceAccessor> makeLazyNarAccessor(const nlohmann::json & listing, GetNarBytes getNarBytes);
/**
* Write a JSON representation of the contents of a NAR (except file

View file

@ -141,14 +141,14 @@ struct NarAccessor : public SourceAccessor
parseDump(indexer, indexer);
}
NarAccessor(const std::string & listing, GetNarBytes getNarBytes)
NarAccessor(const nlohmann::json & listing, GetNarBytes getNarBytes)
: getNarBytes(getNarBytes)
{
using json = nlohmann::json;
std::function<void(NarMember &, json &)> recurse;
std::function<void(NarMember &, const json &)> recurse;
recurse = [&](NarMember & member, json & v) {
recurse = [&](NarMember & member, const json & v) {
std::string type = v["type"];
if (type == "directory") {
@ -169,8 +169,7 @@ struct NarAccessor : public SourceAccessor
return;
};
json v = json::parse(listing);
recurse(root, v);
recurse(root, listing);
}
NarMember * find(const CanonPath & path)
@ -251,11 +250,34 @@ ref<SourceAccessor> makeNarAccessor(Source & source)
return make_ref<NarAccessor>(source);
}
ref<SourceAccessor> makeLazyNarAccessor(const std::string & listing, GetNarBytes getNarBytes)
ref<SourceAccessor> makeLazyNarAccessor(const nlohmann::json & listing, GetNarBytes getNarBytes)
{
return make_ref<NarAccessor>(listing, getNarBytes);
}
GetNarBytes seekableGetNarBytes(const Path & path)
{
return [path](uint64_t offset, uint64_t length) {
AutoCloseFD fd = toDescriptor(open(
path.c_str(),
O_RDONLY
#ifndef _WIN32
| O_CLOEXEC
#endif
));
if (!fd)
throw SysError("opening NAR cache file '%s'", path);
if (lseek(fromDescriptorReadOnly(fd.get()), offset, SEEK_SET) != (off_t) offset)
throw SysError("seeking in '%s'", path);
std::string buf(length, 0);
readFull(fd.get(), buf.data(), length);
return buf;
};
}
using nlohmann::json;
json listNar(ref<SourceAccessor> accessor, const CanonPath & path, bool recurse)

View file

@ -70,26 +70,8 @@ std::shared_ptr<SourceAccessor> RemoteFSAccessor::accessObject(const StorePath &
try {
listing = nix::readFile(makeCacheFile(storePath.hashPart(), "ls"));
auto narAccessor = makeLazyNarAccessor(listing, [cacheFile](uint64_t offset, uint64_t length) {
AutoCloseFD fd = toDescriptor(open(
cacheFile.c_str(),
O_RDONLY
#ifndef _WIN32
| O_CLOEXEC
#endif
));
if (!fd)
throw SysError("opening NAR cache file '%s'", cacheFile);
if (lseek(fromDescriptorReadOnly(fd.get()), offset, SEEK_SET) != (off_t) offset)
throw SysError("seeking in '%s'", cacheFile);
std::string buf(length, 0);
readFull(fd.get(), buf.data(), length);
return buf;
});
auto listingJson = nlohmann::json::parse(listing);
auto narAccessor = makeLazyNarAccessor(listingJson, seekableGetNarBytes(cacheFile));
nars.emplace(storePath.hashPart(), narAccessor);
return narAccessor;