aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/binary-cache-store.cc
diff options
context:
space:
mode:
authorJohn Ericson <John.Ericson@Obsidian.Systems>2020-08-15 16:41:28 +0000
committerJohn Ericson <John.Ericson@Obsidian.Systems>2020-09-23 04:56:04 +0000
commit9fbc31a65bab50cd60a882517b3c8030485ce096 (patch)
tree4a6f4d92801af298c7b5826a4ce5161f2e9e3c8b /src/libstore/binary-cache-store.cc
parent980edd1f3a31eefe297d073f6a7cff099f21eb4a (diff)
Get rid of Hash::dummy from BinaryCacheStore
Diffstat (limited to 'src/libstore/binary-cache-store.cc')
-rw-r--r--src/libstore/binary-cache-store.cc106
1 files changed, 63 insertions, 43 deletions
diff --git a/src/libstore/binary-cache-store.cc b/src/libstore/binary-cache-store.cc
index ebc0bd6a4..6679eb16f 100644
--- a/src/libstore/binary-cache-store.cc
+++ b/src/libstore/binary-cache-store.cc
@@ -142,17 +142,10 @@ struct FileSource : FdSource
}
};
-void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource,
- RepairFlag repair, CheckSigsFlag checkSigs)
+StorePath BinaryCacheStore::addToStoreCommon(
+ Source & narSource, RepairFlag repair, CheckSigsFlag checkSigs,
+ std::function<ValidPathInfo(HashResult)> mkInfo)
{
- assert(info.narSize);
-
- if (!repair && isValidPath(info.path)) {
- // FIXME: copyNAR -> null sink
- narSource.drain();
- return;
- }
-
auto [fdTemp, fnTemp] = createTempFile();
AutoDelete autoDelete(fnTemp);
@@ -162,13 +155,15 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource
/* Read the NAR simultaneously into a CompressionSink+FileSink (to
write the compressed NAR to disk), into a HashSink (to get the
NAR hash), and into a NarAccessor (to get the NAR listing). */
- HashSink fileHashSink(htSHA256);
+ HashSink fileHashSink { htSHA256 };
std::shared_ptr<FSAccessor> narAccessor;
+ HashSink narHashSink { htSHA256 };
{
FdSink fileSink(fdTemp.get());
- TeeSink teeSink(fileSink, fileHashSink);
- auto compressionSink = makeCompressionSink(compression, teeSink);
- TeeSource teeSource(narSource, *compressionSink);
+ TeeSink teeSinkCompressed { fileSink, fileHashSink };
+ auto compressionSink = makeCompressionSink(compression, teeSinkCompressed);
+ TeeSink teeSinkUncompressed { *compressionSink, narHashSink };
+ TeeSource teeSource { narSource, teeSinkUncompressed };
narAccessor = makeNarAccessor(teeSource);
compressionSink->finish();
fileSink.flush();
@@ -176,9 +171,10 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource
auto now2 = std::chrono::steady_clock::now();
+ auto info = mkInfo(narHashSink.finish());
auto narInfo = make_ref<NarInfo>(info);
- narInfo->narSize = info.narSize;
- narInfo->narHash = info.narHash;
+ narInfo->narSize = info.narSize; // FIXME needed?
+ narInfo->narHash = info.narHash; // FIXME needed?
narInfo->compression = compression;
auto [fileHash, fileSize] = fileHashSink.finish();
narInfo->fileHash = fileHash;
@@ -300,6 +296,41 @@ void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource
writeNarInfo(narInfo);
stats.narInfoWrite++;
+
+ return narInfo->path;
+}
+
+void BinaryCacheStore::addToStore(const ValidPathInfo & info, Source & narSource,
+ RepairFlag repair, CheckSigsFlag checkSigs)
+{
+ if (!repair && isValidPath(info.path)) {
+ // FIXME: copyNAR -> null sink
+ narSource.drain();
+ return;
+ }
+
+ (void) addToStoreCommon(narSource, repair, checkSigs, {[&](HashResult nar) {
+ /* FIXME reinstate these, once we can correctly do hash modulo sink as
+ needed. */
+ // assert(info.narHash == nar.first);
+ // assert(info.narSize == nar.second);
+ return info;
+ }});
+}
+
+StorePath BinaryCacheStore::addToStoreFromDump(Source & dump, const string & name,
+ FileIngestionMethod method, HashType hashAlgo, RepairFlag repair)
+{
+ if (method != FileIngestionMethod::Recursive || hashAlgo != htSHA256)
+ unsupported("addToStoreFromDump");
+ return addToStoreCommon(dump, repair, CheckSigs, [&](HashResult nar) {
+ ValidPathInfo info {
+ makeFixedOutputPath(method, nar.first, name),
+ nar.first,
+ };
+ info.narSize = nar.second;
+ return info;
+ });
}
bool BinaryCacheStore::isValidPathUncached(const StorePath & storePath)
@@ -367,11 +398,9 @@ void BinaryCacheStore::queryPathInfoUncached(const StorePath & storePath,
StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath,
FileIngestionMethod method, HashType hashAlgo, PathFilter & filter, RepairFlag repair)
{
- // FIXME: some cut&paste from LocalStore::addToStore().
-
- /* Read the whole path into memory. This is not a very scalable
- method for very large paths, but `copyPath' is mainly used for
- small files. */
+ /* FIXME: Make BinaryCacheStore::addToStoreCommon support
+ non-recursive+sha256 so we can just use the default
+ implementation of this method in terms of addToStoreFromDump. */
StringSink sink;
std::optional<Hash> h;
if (method == FileIngestionMethod::Recursive) {
@@ -383,34 +412,25 @@ StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath
h = hashString(hashAlgo, s);
}
- ValidPathInfo info {
- makeFixedOutputPath(method, *h, name),
- Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash
- };
-
auto source = StringSource { *sink.s };
- addToStore(info, source, repair, CheckSigs);
-
- return std::move(info.path);
+ return addToStoreFromDump(source, name, FileIngestionMethod::Recursive, htSHA256, repair);
}
StorePath BinaryCacheStore::addTextToStore(const string & name, const string & s,
const StorePathSet & references, RepairFlag repair)
{
- ValidPathInfo info {
- computeStorePathForText(name, s, references),
- Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash
- };
- info.references = references;
-
- if (repair || !isValidPath(info.path)) {
- StringSink sink;
- dumpString(s, sink);
- auto source = StringSource { *sink.s };
- addToStore(info, source, repair, CheckSigs);
- }
-
- return std::move(info.path);
+ auto path = computeStorePathForText(name, s, references);
+
+ if (!repair && isValidPath(path))
+ return path;
+
+ auto source = StringSource { s };
+ return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) {
+ ValidPathInfo info { path, nar.first };
+ info.narSize = nar.second;
+ info.references = references;
+ return info;
+ });
}
ref<FSAccessor> BinaryCacheStore::getFSAccessor()