libutil: turn HashModuloSink into a free function
Change-Id: I5878007502fa68c2816a0f4c61f7d0e60bdde702
This commit is contained in:
parent
4162a66cee
commit
5af76dee37
|
@ -2216,23 +2216,21 @@ SingleDrvOutputs LocalDerivationGoal::registerOutputs()
|
||||||
rewriteOutput(outputRewrites);
|
rewriteOutput(outputRewrites);
|
||||||
/* FIXME optimize and deduplicate with addToStore */
|
/* FIXME optimize and deduplicate with addToStore */
|
||||||
std::string oldHashPart { scratchPath->hashPart() };
|
std::string oldHashPart { scratchPath->hashPart() };
|
||||||
HashModuloSink caSink { outputHash.hashType, oldHashPart };
|
auto input = std::visit(overloaded {
|
||||||
std::visit(overloaded {
|
[&](const TextIngestionMethod &) -> GeneratorSource {
|
||||||
[&](const TextIngestionMethod &) {
|
return GeneratorSource(readFileSource(actualPath));
|
||||||
caSink << readFileSource(actualPath);
|
|
||||||
},
|
},
|
||||||
[&](const FileIngestionMethod & m2) {
|
[&](const FileIngestionMethod & m2) -> GeneratorSource {
|
||||||
switch (m2) {
|
switch (m2) {
|
||||||
case FileIngestionMethod::Recursive:
|
case FileIngestionMethod::Recursive:
|
||||||
caSink << dumpPath(actualPath);
|
return GeneratorSource(dumpPath(actualPath));
|
||||||
break;
|
|
||||||
case FileIngestionMethod::Flat:
|
case FileIngestionMethod::Flat:
|
||||||
caSink << readFileSource(actualPath);
|
return GeneratorSource(readFileSource(actualPath));
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
assert(false);
|
||||||
},
|
},
|
||||||
}, outputHash.method.raw);
|
}, outputHash.method.raw);
|
||||||
auto got = caSink.finish().first;
|
auto got = computeHashModulo(outputHash.hashType, oldHashPart, input).first;
|
||||||
|
|
||||||
auto optCA = ContentAddressWithReferences::fromPartsOpt(
|
auto optCA = ContentAddressWithReferences::fromPartsOpt(
|
||||||
outputHash.method,
|
outputHash.method,
|
||||||
|
|
|
@ -1887,25 +1887,23 @@ ContentAddress LocalStore::hashCAPath(
|
||||||
const std::string_view pathHash
|
const std::string_view pathHash
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
HashModuloSink caSink ( hashType, std::string(pathHash) );
|
auto data = std::visit(overloaded {
|
||||||
std::visit(overloaded {
|
[&](const TextIngestionMethod &) -> GeneratorSource {
|
||||||
[&](const TextIngestionMethod &) {
|
return GeneratorSource(readFileSource(path));
|
||||||
caSink << readFileSource(path);
|
|
||||||
},
|
},
|
||||||
[&](const FileIngestionMethod & m2) {
|
[&](const FileIngestionMethod & m2) -> GeneratorSource {
|
||||||
switch (m2) {
|
switch (m2) {
|
||||||
case FileIngestionMethod::Recursive:
|
case FileIngestionMethod::Recursive:
|
||||||
caSink << dumpPath(path);
|
return GeneratorSource(dumpPath(path));
|
||||||
break;
|
|
||||||
case FileIngestionMethod::Flat:
|
case FileIngestionMethod::Flat:
|
||||||
caSink << readFileSource(path);
|
return GeneratorSource(readFileSource(path));
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
assert(false);
|
||||||
},
|
},
|
||||||
}, method.raw);
|
}, method.raw);
|
||||||
return ContentAddress {
|
return ContentAddress {
|
||||||
.method = method,
|
.method = method,
|
||||||
.hash = caSink.finish().first,
|
.hash = computeHashModulo(hashType, std::string(pathHash), data).first,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,10 +43,10 @@ std::map<StorePath, StorePath> makeContentAddressed(
|
||||||
|
|
||||||
sink.s = rewriteStrings(sink.s, rewrites);
|
sink.s = rewriteStrings(sink.s, rewrites);
|
||||||
|
|
||||||
HashModuloSink hashModuloSink(htSHA256, oldHashPart);
|
auto narModuloHash = [&] {
|
||||||
hashModuloSink(sink.s);
|
StringSource source{sink.s};
|
||||||
|
return computeHashModulo(htSHA256, oldHashPart, source).first;
|
||||||
auto narModuloHash = hashModuloSink.finish().first;
|
}();
|
||||||
|
|
||||||
ValidPathInfo info {
|
ValidPathInfo info {
|
||||||
dstStore,
|
dstStore,
|
||||||
|
|
|
@ -203,5 +203,12 @@ public:
|
||||||
HashResult currentHash();
|
HashResult currentHash();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline HashResult hashSource(HashType ht, Source & source)
|
||||||
|
{
|
||||||
|
HashSink h(ht);
|
||||||
|
source.drainInto(h);
|
||||||
|
return h.finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,19 +109,12 @@ void RewritingSink::flush()
|
||||||
prev.clear();
|
prev.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
HashModuloSink::HashModuloSink(HashType ht, const std::string & modulus)
|
HashResult computeHashModulo(HashType ht, const std::string & modulus, Source & source)
|
||||||
: hashSink(ht)
|
|
||||||
, rewritingSink(modulus, std::string(modulus.size(), 0), hashSink)
|
|
||||||
{
|
{
|
||||||
}
|
HashSink hashSink(ht);
|
||||||
|
RewritingSink rewritingSink(modulus, std::string(modulus.size(), 0), hashSink);
|
||||||
|
|
||||||
void HashModuloSink::operator () (std::string_view data)
|
source.drainInto(rewritingSink);
|
||||||
{
|
|
||||||
rewritingSink(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
HashResult HashModuloSink::finish()
|
|
||||||
{
|
|
||||||
rewritingSink.flush();
|
rewritingSink.flush();
|
||||||
|
|
||||||
/* Hash the positions of the self-references. This ensures that a
|
/* Hash the positions of the self-references. This ensures that a
|
||||||
|
|
|
@ -41,16 +41,6 @@ struct RewritingSink : Sink
|
||||||
void flush();
|
void flush();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct HashModuloSink : AbstractHashSink
|
HashResult computeHashModulo(HashType ht, const std::string & modulus, Source & source);
|
||||||
{
|
|
||||||
HashSink hashSink;
|
|
||||||
RewritingSink rewritingSink;
|
|
||||||
|
|
||||||
HashModuloSink(HashType ht, const std::string & modulus);
|
|
||||||
|
|
||||||
void operator () (std::string_view data) override;
|
|
||||||
|
|
||||||
HashResult finish() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,23 +76,19 @@ struct CmdHashBase : Command
|
||||||
void run() override
|
void run() override
|
||||||
{
|
{
|
||||||
for (auto path : paths) {
|
for (auto path : paths) {
|
||||||
|
auto source = [&] () -> GeneratorSource {
|
||||||
|
switch (mode) {
|
||||||
|
case FileIngestionMethod::Flat:
|
||||||
|
return GeneratorSource(readFileSource(path));
|
||||||
|
case FileIngestionMethod::Recursive:
|
||||||
|
return GeneratorSource(dumpPath(path));
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
|
}();
|
||||||
|
|
||||||
std::unique_ptr<AbstractHashSink> hashSink;
|
Hash h = modulus
|
||||||
if (modulus)
|
? computeHashModulo(ht, *modulus, source).first
|
||||||
hashSink = std::make_unique<HashModuloSink>(ht, *modulus);
|
: hashSource(ht, source).first;
|
||||||
else
|
|
||||||
hashSink = std::make_unique<HashSink>(ht);
|
|
||||||
|
|
||||||
switch (mode) {
|
|
||||||
case FileIngestionMethod::Flat:
|
|
||||||
*hashSink << readFileSource(path);
|
|
||||||
break;
|
|
||||||
case FileIngestionMethod::Recursive:
|
|
||||||
*hashSink << dumpPath(path);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Hash h = hashSink->finish().first;
|
|
||||||
if (truncate && h.hashSize > 20) h = compressHash(h, 20);
|
if (truncate && h.hashSize > 20) h = compressHash(h, 20);
|
||||||
logger->cout(h.to_string(base, base == SRI));
|
logger->cout(h.to_string(base, base == SRI));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue