@@ -141,14 +141,14 @@ struct NarAccessor : public SourceAccessor
141141 parseDump (indexer, indexer);
142142 }
143143
144- NarAccessor (const std::string & listing, GetNarBytes getNarBytes)
144+ NarAccessor (const nlohmann::json & listing, GetNarBytes getNarBytes)
145145 : getNarBytes(getNarBytes)
146146 {
147147 using json = nlohmann::json;
148148
149- std::function<void (NarMember &, json &)> recurse;
149+ std::function<void (NarMember &, const json &)> recurse;
150150
151- recurse = [&](NarMember & member, json & v) {
151+ recurse = [&](NarMember & member, const json & v) {
152152 std::string type = v[" type" ];
153153
154154 if (type == " directory" ) {
@@ -169,8 +169,7 @@ struct NarAccessor : public SourceAccessor
169169 return ;
170170 };
171171
172- json v = json::parse (listing);
173- recurse (root, v);
172+ recurse (root, listing);
174173 }
175174
176175 NarMember * find (const CanonPath & path)
@@ -251,11 +250,34 @@ ref<SourceAccessor> makeNarAccessor(Source & source)
251250 return make_ref<NarAccessor>(source);
252251}
253252
254- ref<SourceAccessor> makeLazyNarAccessor (const std::string & listing, GetNarBytes getNarBytes)
253+ ref<SourceAccessor> makeLazyNarAccessor (const nlohmann::json & listing, GetNarBytes getNarBytes)
255254{
256255 return make_ref<NarAccessor>(listing, getNarBytes);
257256}
258257
258+ GetNarBytes seekableGetNarBytes (const Path & path)
259+ {
260+ return [path](uint64_t offset, uint64_t length) {
261+ AutoCloseFD fd = toDescriptor (open (
262+ path.c_str (),
263+ O_RDONLY
264+ #ifndef _WIN32
265+ | O_CLOEXEC
266+ #endif
267+ ));
268+ if (!fd)
269+ throw SysError (" opening NAR cache file '%s'" , path);
270+
271+ if (lseek (fromDescriptorReadOnly (fd.get ()), offset, SEEK_SET) != (off_t ) offset)
272+ throw SysError (" seeking in '%s'" , path);
273+
274+ std::string buf (length, 0 );
275+ readFull (fd.get (), buf.data (), length);
276+
277+ return buf;
278+ };
279+ }
280+
259281using nlohmann::json;
260282
261283json listNar (ref<SourceAccessor> accessor, const CanonPath & path, bool recurse)
0 commit comments