hydra-queue-runner/nar-extractor: address review comments

* Turn `NarMemberData` in `MyFileHandle` into a reference.
* `expectedSize` now uses an initializer.
* Used `std::make_unique` instead of `std::unique_ptr(new ...)` as
  suggested by clangd.
This commit is contained in:
Maximilian Bosch 2024-10-06 19:43:34 +02:00
parent e92ac734e6
commit 06a30ec8df
Signed by: ma27
SSH key fingerprint: SHA256:d7dmwHmpai66L6KIXA+wxzVbkPq0nGLrcHK3ZNroqZY

View file

@ -11,29 +11,28 @@ struct Extractor : NARParseVisitor
{ {
class MyFileHandle : public FileHandle class MyFileHandle : public FileHandle
{ {
NarMemberData * memberData; NarMemberData & memberData;
uint64_t expectedSize; uint64_t expectedSize;
std::unique_ptr<HashSink> hashSink; std::unique_ptr<HashSink> hashSink;
public: public:
MyFileHandle(NarMemberData * memberData, uint64_t size) : memberData(memberData) MyFileHandle(NarMemberData & memberData, uint64_t size) : memberData(memberData), expectedSize(size)
{ {
expectedSize = size;
hashSink = std::make_unique<HashSink>(HashType::SHA256); hashSink = std::make_unique<HashSink>(HashType::SHA256);
} }
void receiveContents(std::string_view data) override void receiveContents(std::string_view data) override
{ {
*memberData->fileSize += data.size(); *memberData.fileSize += data.size();
(*hashSink)(data); (*hashSink)(data);
if (memberData->contents) { if (memberData.contents) {
memberData->contents->append(data); memberData.contents->append(data);
} }
assert(memberData->fileSize <= expectedSize); assert(memberData.fileSize <= expectedSize);
if (memberData->fileSize == expectedSize) { if (memberData.fileSize == expectedSize) {
auto [hash, len] = hashSink->finish(); auto [hash, len] = hashSink->finish();
assert(memberData->fileSize == len); assert(memberData.fileSize == len);
memberData->sha256 = hash; memberData.sha256 = hash;
hashSink.reset(); hashSink.reset();
} }
} }
@ -65,7 +64,7 @@ struct Extractor : NARParseVisitor
.contents = filesToKeep.count(path) ? std::optional("") : std::nullopt, .contents = filesToKeep.count(path) ? std::optional("") : std::nullopt,
}).first->second; }).first->second;
return std::unique_ptr<MyFileHandle>(new MyFileHandle{memberData, size}); return std::make_unique<MyFileHandle>(*memberData, size);
} }
void createSymlink(const Path & path, const std::string & target) override void createSymlink(const Path & path, const std::string & target) override