Skip to content

Commit ed26b18

Browse files
Ericson2314roberth
andcommitted
Remove now-redundant text-hashing store methods
`addTextToStore` and `computeStorePathFromDump` are now redundant. Co-authored-by: Robert Hensing <[email protected]>
1 parent dfc8765 commit ed26b18

18 files changed

+83
-212
lines changed

src/libexpr/primops.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,8 +2072,14 @@ static void prim_toFile(EvalState & state, const PosIdx pos, Value * * args, Val
20722072
}
20732073

20742074
auto storePath = settings.readOnlyMode
2075-
? state.store->computeStorePathForText(name, contents, refs)
2076-
: state.store->addTextToStore(name, contents, refs, state.repair);
2075+
? state.store->makeFixedOutputPathFromCA(name, TextInfo {
2076+
.hash = hashString(HashAlgorithm::SHA256, contents),
2077+
.references = std::move(refs),
2078+
})
2079+
: ({
2080+
StringSource s { contents };
2081+
state.store->addToStoreFromDump(s, name, TextIngestionMethod {}, HashAlgorithm::SHA256, refs, state.repair);
2082+
});
20772083

20782084
/* Note: we don't need to add `context' to the context of the
20792085
result, since `storePath' itself has references to the paths

src/libstore/binary-cache-store.cc

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "thread-pool.hh"
1313
#include "callback.hh"
1414
#include "signals.hh"
15+
#include "archive.hh"
1516

1617
#include <chrono>
1718
#include <future>
@@ -308,15 +309,47 @@ StorePath BinaryCacheStore::addToStoreFromDump(
308309
const StorePathSet & references,
309310
RepairFlag repair)
310311
{
311-
if (method != FileIngestionMethod::Recursive || hashAlgo != HashAlgorithm::SHA256)
312-
unsupported("addToStoreFromDump");
313-
return addToStoreCommon(dump, repair, CheckSigs, [&](HashResult nar) {
312+
std::optional<Hash> caHash;
313+
std::string nar;
314+
315+
if (auto * dump2p = dynamic_cast<StringSource *>(&dump)) {
316+
auto & dump2 = *dump2p;
317+
// Hack, this gives us a "replayable" source so we can compute
318+
// multiple hashes more easily.
319+
caHash = hashString(HashAlgorithm::SHA256, dump2.s);
320+
switch (method.getFileIngestionMethod()) {
321+
case FileIngestionMethod::Recursive:
322+
// The dump is already NAR in this case, just use it.
323+
nar = dump2.s;
324+
break;
325+
case FileIngestionMethod::Flat:
326+
// The dump is Flat, so we need to convert it to NAR with a
327+
// single file.
328+
StringSink s;
329+
dumpString(dump2.s, s);
330+
nar = std::move(s.s);
331+
break;
332+
}
333+
} else {
334+
// Otherwise, we have to do th same hashing as NAR so our single
335+
// hash will suffice for both purposes.
336+
if (method != FileIngestionMethod::Recursive || hashAlgo != HashAlgorithm::SHA256)
337+
unsupported("addToStoreFromDump");
338+
}
339+
StringSource narDump { nar };
340+
341+
// Use `narDump` if we wrote to `nar`.
342+
Source & narDump2 = nar.size() > 0
343+
? static_cast<Source &>(narDump)
344+
: dump;
345+
346+
return addToStoreCommon(narDump2, repair, CheckSigs, [&](HashResult nar) {
314347
ValidPathInfo info {
315348
*this,
316349
name,
317350
ContentAddressWithReferences::fromParts(
318351
method,
319-
nar.first,
352+
caHash ? *caHash : nar.first,
320353
{
321354
.others = references,
322355
// caller is not capable of creating a self-reference, because this is content-addressed without modulus
@@ -440,36 +473,6 @@ StorePath BinaryCacheStore::addToStore(
440473
})->path;
441474
}
442475

443-
StorePath BinaryCacheStore::addTextToStore(
444-
std::string_view name,
445-
std::string_view s,
446-
const StorePathSet & references,
447-
RepairFlag repair)
448-
{
449-
auto textHash = hashString(HashAlgorithm::SHA256, s);
450-
auto path = makeTextPath(name, TextInfo { { textHash }, references });
451-
452-
if (!repair && isValidPath(path))
453-
return path;
454-
455-
StringSink sink;
456-
dumpString(s, sink);
457-
StringSource source(sink.s);
458-
return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) {
459-
ValidPathInfo info {
460-
*this,
461-
std::string { name },
462-
TextInfo {
463-
.hash = textHash,
464-
.references = references,
465-
},
466-
nar.first,
467-
};
468-
info.narSize = nar.second;
469-
return info;
470-
})->path;
471-
}
472-
473476
void BinaryCacheStore::queryRealisationUncached(const DrvOutput & id,
474477
Callback<std::shared_ptr<const Realisation>> callback) noexcept
475478
{

src/libstore/binary-cache-store.hh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ public:
141141
PathFilter & filter,
142142
RepairFlag repair) override;
143143

144-
StorePath addTextToStore(
145-
std::string_view name,
146-
std::string_view s,
147-
const StorePathSet & references,
148-
RepairFlag repair) override;
149-
150144
void registerDrvOutput(const Realisation & info) override;
151145

152146
void queryRealisationUncached(const DrvOutput &,

src/libstore/build/local-derivation-goal.cc

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,17 +1308,6 @@ struct RestrictedStore : public virtual RestrictedStoreConfig, public virtual In
13081308
goal.addDependency(info.path);
13091309
}
13101310

1311-
StorePath addTextToStore(
1312-
std::string_view name,
1313-
std::string_view s,
1314-
const StorePathSet & references,
1315-
RepairFlag repair = NoRepair) override
1316-
{
1317-
auto path = next->addTextToStore(name, s, references, repair);
1318-
goal.addDependency(path);
1319-
return path;
1320-
}
1321-
13221311
StorePath addToStoreFromDump(
13231312
Source & dump,
13241313
std::string_view name,

src/libstore/content-address.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ struct ContentAddressMethod
109109
* serialisation methods (flat file vs NAR). Thus, ‘ca’ has one of the
110110
* following forms:
111111
*
112-
* - ‘text:sha256:<sha256 hash of file contents>’: For paths
113-
* computed by Store::makeTextPath() / Store::addTextToStore().
112+
* - `TextIngestionMethod`:
113+
* ‘text:sha256:<sha256 hash of file contents>’
114114
*
115-
* - ‘fixed:<r?>:<ht>:<h>’: For paths computed by
116-
* Store::makeFixedOutputPath() / Store::addToStore().
115+
* - `FixedIngestionMethod`:
116+
* ‘fixed:<r?>:<hash type>:<hash of file contents>’
117117
*/
118118
struct ContentAddress
119119
{

src/libstore/daemon.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,10 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
483483
std::string s = readString(from);
484484
auto refs = WorkerProto::Serialise<StorePathSet>::read(*store, rconn);
485485
logger->startWork();
486-
auto path = store->addTextToStore(suffix, s, refs, NoRepair);
486+
auto path = ({
487+
StringSource source { s };
488+
store->addToStoreFromDump(source, suffix, TextIngestionMethod {}, HashAlgorithm::SHA256, refs, NoRepair);
489+
});
487490
logger->stopWork();
488491
to << store->printStorePath(path);
489492
break;

src/libstore/derivations.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,14 @@ StorePath writeDerivation(Store & store,
143143
auto suffix = std::string(drv.name) + drvExtension;
144144
auto contents = drv.unparse(store, false);
145145
return readOnly || settings.readOnlyMode
146-
? store.computeStorePathForText(suffix, contents, references)
147-
: store.addTextToStore(suffix, contents, references, repair);
146+
? store.makeFixedOutputPathFromCA(suffix, TextInfo {
147+
.hash = hashString(HashAlgorithm::SHA256, contents),
148+
.references = std::move(references),
149+
})
150+
: ({
151+
StringSource s { contents };
152+
store.addToStoreFromDump(s, suffix, TextIngestionMethod {}, HashAlgorithm::SHA256, references, repair);
153+
});
148154
}
149155

150156

src/libstore/dummy-store.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,6 @@ struct DummyStore : public virtual DummyStoreConfig, public virtual Store
5858
RepairFlag repair, CheckSigsFlag checkSigs) override
5959
{ unsupported("addToStore"); }
6060

61-
StorePath addTextToStore(
62-
std::string_view name,
63-
std::string_view s,
64-
const StorePathSet & references,
65-
RepairFlag repair) override
66-
{ unsupported("addTextToStore"); }
67-
6861
void narFromPath(const StorePath & path, Sink & sink) override
6962
{ unsupported("narFromPath"); }
7063

src/libstore/legacy-ssh-store.hh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ struct LegacySSHStore : public virtual LegacySSHStoreConfig, public virtual Stor
6969
RepairFlag repair) override
7070
{ unsupported("addToStore"); }
7171

72-
StorePath addTextToStore(
73-
std::string_view name,
74-
std::string_view s,
75-
const StorePathSet & references,
76-
RepairFlag repair) override
77-
{ unsupported("addTextToStore"); }
78-
7972
private:
8073

8174
void putBuildSettings(Connection & conn);

src/libstore/local-store.cc

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,58 +1257,6 @@ StorePath LocalStore::addToStoreFromDump(
12571257
}
12581258

12591259

1260-
StorePath LocalStore::addTextToStore(
1261-
std::string_view name,
1262-
std::string_view s,
1263-
const StorePathSet & references, RepairFlag repair)
1264-
{
1265-
auto hash = hashString(HashAlgorithm::SHA256, s);
1266-
auto dstPath = makeTextPath(name, TextInfo {
1267-
.hash = hash,
1268-
.references = references,
1269-
});
1270-
1271-
addTempRoot(dstPath);
1272-
1273-
if (repair || !isValidPath(dstPath)) {
1274-
1275-
auto realPath = Store::toRealPath(dstPath);
1276-
1277-
PathLocks outputLock({realPath});
1278-
1279-
if (repair || !isValidPath(dstPath)) {
1280-
1281-
deletePath(realPath);
1282-
1283-
autoGC();
1284-
1285-
writeFile(realPath, s);
1286-
1287-
canonicalisePathMetaData(realPath, {});
1288-
1289-
StringSink sink;
1290-
dumpString(s, sink);
1291-
auto narHash = hashString(HashAlgorithm::SHA256, sink.s);
1292-
1293-
optimisePath(realPath, repair);
1294-
1295-
ValidPathInfo info { dstPath, narHash };
1296-
info.narSize = sink.s.size();
1297-
info.references = references;
1298-
info.ca = {
1299-
.method = TextIngestionMethod {},
1300-
.hash = hash,
1301-
};
1302-
registerValidPath(info);
1303-
}
1304-
1305-
outputLock.setDeletion(true);
1306-
}
1307-
1308-
return dstPath;
1309-
}
1310-
1311-
13121260
/* Create a temporary directory in the store that won't be
13131261
garbage-collected until the returned FD is closed. */
13141262
std::pair<Path, AutoCloseFD> LocalStore::createTempDirInStore()

src/libstore/local-store.hh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ public:
185185
const StorePathSet & references,
186186
RepairFlag repair) override;
187187

188-
StorePath addTextToStore(
189-
std::string_view name,
190-
std::string_view s,
191-
const StorePathSet & references,
192-
RepairFlag repair) override;
193-
194188
void addTempRoot(const StorePath & path) override;
195189

196190
private:

src/libstore/remote-store.cc

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,16 +608,6 @@ void RemoteStore::addMultipleToStore(
608608
}
609609

610610

611-
StorePath RemoteStore::addTextToStore(
612-
std::string_view name,
613-
std::string_view s,
614-
const StorePathSet & references,
615-
RepairFlag repair)
616-
{
617-
StringSource source(s);
618-
return addCAToStore(source, name, TextIngestionMethod {}, HashAlgorithm::SHA256, references, repair)->path;
619-
}
620-
621611
void RemoteStore::registerDrvOutput(const Realisation & info)
622612
{
623613
auto conn(getConnection());

src/libstore/remote-store.hh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,6 @@ public:
106106
RepairFlag repair,
107107
CheckSigsFlag checkSigs) override;
108108

109-
StorePath addTextToStore(
110-
std::string_view name,
111-
std::string_view s,
112-
const StorePathSet & references,
113-
RepairFlag repair) override;
114-
115109
void registerDrvOutput(const Realisation & info) override;
116110

117111
void queryRealisationUncached(const DrvOutput &,

src/libstore/store-api.cc

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -205,25 +205,19 @@ StorePath StoreDirConfig::makeFixedOutputPath(std::string_view name, const Fixed
205205
}
206206

207207

208-
StorePath StoreDirConfig::makeTextPath(std::string_view name, const TextInfo & info) const
209-
{
210-
assert(info.hash.algo == HashAlgorithm::SHA256);
211-
return makeStorePath(
212-
makeType(*this, "text", StoreReferences {
213-
.others = info.references,
214-
.self = false,
215-
}),
216-
info.hash,
217-
name);
218-
}
219-
220-
221208
StorePath StoreDirConfig::makeFixedOutputPathFromCA(std::string_view name, const ContentAddressWithReferences & ca) const
222209
{
223210
// New template
224211
return std::visit(overloaded {
225212
[&](const TextInfo & ti) {
226-
return makeTextPath(name, ti);
213+
assert(ti.hash.algo == HashAlgorithm::SHA256);
214+
return makeStorePath(
215+
makeType(*this, "text", StoreReferences {
216+
.others = ti.references,
217+
.self = false,
218+
}),
219+
ti.hash,
220+
name);
227221
},
228222
[&](const FixedOutputInfo & foi) {
229223
return makeFixedOutputPath(name, foi);
@@ -257,18 +251,6 @@ std::pair<StorePath, Hash> StoreDirConfig::computeStorePath(
257251
}
258252

259253

260-
StorePath StoreDirConfig::computeStorePathForText(
261-
std::string_view name,
262-
std::string_view s,
263-
const StorePathSet & references) const
264-
{
265-
return makeTextPath(name, TextInfo {
266-
.hash = hashString(HashAlgorithm::SHA256, s),
267-
.references = references,
268-
});
269-
}
270-
271-
272254
StorePath Store::addToStore(
273255
std::string_view name,
274256
SourceAccessor & accessor,

src/libstore/store-api.hh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,6 @@ public:
468468
RepairFlag repair = NoRepair)
469469
{ unsupported("addToStoreFromDump"); }
470470

471-
/**
472-
* Like addToStore, but the contents written to the output path is a
473-
* regular file containing the given string.
474-
*/
475-
virtual StorePath addTextToStore(
476-
std::string_view name,
477-
std::string_view s,
478-
const StorePathSet & references,
479-
RepairFlag repair = NoRepair) = 0;
480-
481471
/**
482472
* Add a mapping indicating that `deriver!outputName` maps to the output path
483473
* `output`.

0 commit comments

Comments
 (0)