Skip to content

Commit e89b5bd

Browse files
committed
Minimize the usage of Hash::dummy
1 parent 5e59b25 commit e89b5bd

18 files changed

Lines changed: 101 additions & 58 deletions

src/libfetchers/tarball.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ DownloadFileResult downloadFile(
6767
StringSink sink;
6868
dumpString(*res.data, sink);
6969
auto hash = hashString(htSHA256, *res.data);
70-
ValidPathInfo info(store->makeFixedOutputPath(FileIngestionMethod::Flat, hash, name));
71-
info.narHash = hashString(htSHA256, *sink.s);
70+
ValidPathInfo info {
71+
store->makeFixedOutputPath(FileIngestionMethod::Flat, hash, name),
72+
hashString(htSHA256, *sink.s),
73+
};
7274
info.narSize = sink.s->size();
7375
info.ca = FixedOutputHash {
7476
.method = FileIngestionMethod::Flat,

src/libstore/binary-cache-store.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,10 @@ StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath
385385
h = hashString(hashAlgo, s);
386386
}
387387

388-
ValidPathInfo info(makeFixedOutputPath(method, *h, name));
388+
ValidPathInfo info {
389+
makeFixedOutputPath(method, *h, name),
390+
Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash
391+
};
389392

390393
auto source = StringSource { *sink.s };
391394
addToStore(info, source, repair, CheckSigs);
@@ -396,7 +399,10 @@ StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath
396399
StorePath BinaryCacheStore::addTextToStore(const string & name, const string & s,
397400
const StorePathSet & references, RepairFlag repair)
398401
{
399-
ValidPathInfo info(computeStorePathForText(name, s, references));
402+
ValidPathInfo info {
403+
computeStorePathForText(name, s, references),
404+
Hash::dummy, // Will be fixed in addToStore, which recomputes nar hash
405+
};
400406
info.references = references;
401407

402408
if (repair || !isValidPath(info.path)) {

src/libstore/build.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,8 +3864,10 @@ void DerivationGoal::registerOutputs()
38643864
worker.markContentsGood(worker.store.parseStorePath(path));
38653865
}
38663866

3867-
ValidPathInfo info(worker.store.parseStorePath(path));
3868-
info.narHash = hash.first;
3867+
ValidPathInfo info {
3868+
worker.store.parseStorePath(path),
3869+
hash.first,
3870+
};
38693871
info.narSize = hash.second;
38703872
info.references = std::move(references);
38713873
info.deriver = drvPath;

src/libstore/daemon.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,12 @@ static void performOp(TunnelLogger * logger, ref<Store> store,
694694

695695
case wopAddToStoreNar: {
696696
bool repair, dontCheckSigs;
697-
ValidPathInfo info(store->parseStorePath(readString(from)));
697+
auto path = store->parseStorePath(readString(from));
698698
auto deriver = readString(from);
699+
auto narHash = Hash::parseAny(readString(from), htSHA256);
700+
ValidPathInfo info { path, narHash };
699701
if (deriver != "")
700702
info.deriver = store->parseStorePath(deriver);
701-
info.narHash = Hash::parseAny(readString(from), htSHA256);
702703
info.references = readStorePaths<StorePathSet>(*store, from);
703704
from >> info.registrationTime >> info.narSize >> info.ultimate;
704705
info.sigs = readStrings<StringSet>(from);

src/libstore/export-import.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,18 @@ StorePaths Store::importPaths(Source & source, CheckSigsFlag checkSigs)
6969
if (magic != exportMagic)
7070
throw Error("Nix archive cannot be imported; wrong format");
7171

72-
ValidPathInfo info(parseStorePath(readString(source)));
72+
auto path = parseStorePath(readString(source));
7373

7474
//Activity act(*logger, lvlInfo, format("importing path '%s'") % info.path);
7575

76-
info.references = readStorePaths<StorePathSet>(*this, source);
77-
76+
auto references = readStorePaths<StorePathSet>(*this, source);
7877
auto deriver = readString(source);
78+
auto narHash = hashString(htSHA256, *saved.s);
79+
80+
ValidPathInfo info { path, narHash };
7981
if (deriver != "")
8082
info.deriver = parseStorePath(deriver);
81-
82-
info.narHash = hashString(htSHA256, *saved.s);
83+
info.references = references;
8384
info.narSize = saved.s->size();
8485

8586
// Ignore optional legacy signature.

src/libstore/legacy-ssh-store.cc

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,20 @@ struct LegacySSHStore : public Store
9393
try {
9494
auto conn(connections->get());
9595

96+
/* No longer support missing NAR hash */
97+
assert(GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4);
98+
9699
debug("querying remote host '%s' for info on '%s'", host, printStorePath(path));
97100

98101
conn->to << cmdQueryPathInfos << PathSet{printStorePath(path)};
99102
conn->to.flush();
100103

101104
auto p = readString(conn->from);
102105
if (p.empty()) return callback(nullptr);
103-
auto info = std::make_shared<ValidPathInfo>(parseStorePath(p));
104-
assert(path == info->path);
106+
auto path2 = parseStorePath(p);
107+
assert(path == path2);
108+
/* Hash will be set below. FIXME construct ValidPathInfo at end. */
109+
auto info = std::make_shared<ValidPathInfo>(path, Hash::dummy);
105110

106111
PathSet references;
107112
auto deriver = readString(conn->from);
@@ -111,12 +116,14 @@ struct LegacySSHStore : public Store
111116
readLongLong(conn->from); // download size
112117
info->narSize = readLongLong(conn->from);
113118

114-
if (GET_PROTOCOL_MINOR(conn->remoteVersion) >= 4) {
119+
{
115120
auto s = readString(conn->from);
116-
info->narHash = s.empty() ? Hash::dummy : Hash::parseAnyPrefixed(s);
117-
info->ca = parseContentAddressOpt(readString(conn->from));
118-
info->sigs = readStrings<StringSet>(conn->from);
121+
if (s == "")
122+
throw Error("NAR hash is now mandatory");
123+
info->narHash = Hash::parseAnyPrefixed(s);
119124
}
125+
info->ca = parseContentAddressOpt(readString(conn->from));
126+
info->sigs = readStrings<StringSet>(conn->from);
120127

121128
auto s = readString(conn->from);
122129
assert(s == "");

src/libstore/local-store.cc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -641,25 +641,28 @@ void LocalStore::queryPathInfoUncached(const StorePath & path,
641641
Callback<std::shared_ptr<const ValidPathInfo>> callback) noexcept
642642
{
643643
try {
644-
auto info = std::make_shared<ValidPathInfo>(path);
645-
646644
callback(retrySQLite<std::shared_ptr<ValidPathInfo>>([&]() {
647645
auto state(_state.lock());
648646

649647
/* Get the path info. */
650-
auto useQueryPathInfo(state->stmtQueryPathInfo.use()(printStorePath(info->path)));
648+
auto useQueryPathInfo(state->stmtQueryPathInfo.use()(printStorePath(path)));
651649

652650
if (!useQueryPathInfo.next())
653651
return std::shared_ptr<ValidPathInfo>();
654652

655-
info->id = useQueryPathInfo.getInt(0);
653+
auto id = useQueryPathInfo.getInt(0);
656654

655+
auto narHash = Hash::dummy;
657656
try {
658-
info->narHash = Hash::parseAnyPrefixed(useQueryPathInfo.getStr(1));
657+
narHash = Hash::parseAnyPrefixed(useQueryPathInfo.getStr(1));
659658
} catch (BadHash & e) {
660-
throw Error("in valid-path entry for '%s': %s", printStorePath(path), e.what());
659+
throw Error("invalid-path entry for '%s': %s", printStorePath(path), e.what());
661660
}
662661

662+
auto info = std::make_shared<ValidPathInfo>(path, narHash);
663+
664+
info->id = id;
665+
663666
info->registrationTime = useQueryPathInfo.getInt(2);
664667

665668
auto s = (const char *) sqlite3_column_text(state->stmtQueryPathInfo, 3);
@@ -1152,8 +1155,7 @@ StorePath LocalStore::addToStoreFromDump(Source & source0, const string & name,
11521155

11531156
optimisePath(realPath);
11541157

1155-
ValidPathInfo info(dstPath);
1156-
info.narHash = narHash.first;
1158+
ValidPathInfo info { dstPath, narHash.first };
11571159
info.narSize = narHash.second;
11581160
info.ca = FixedOutputHash { .method = method, .hash = hash };
11591161
registerValidPath(info);
@@ -1196,8 +1198,7 @@ StorePath LocalStore::addTextToStore(const string & name, const string & s,
11961198

11971199
optimisePath(realPath);
11981200

1199-
ValidPathInfo info(dstPath);
1200-
info.narHash = narHash;
1201+
ValidPathInfo info { dstPath, narHash };
12011202
info.narSize = sink.s->size();
12021203
info.references = references;
12031204
info.ca = TextHash { .hash = hash };

src/libstore/nar-info-disk-cache.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,14 @@ class NarInfoDiskCacheImpl : public NarInfoDiskCache
189189
return {oInvalid, 0};
190190

191191
auto namePart = queryNAR.getStr(1);
192-
auto narInfo = make_ref<NarInfo>(StorePath(hashPart + "-" + namePart));
192+
auto narInfo = make_ref<NarInfo>(
193+
StorePath(hashPart + "-" + namePart),
194+
Hash::parseAnyPrefixed(queryNAR.getStr(6)));
193195
narInfo->url = queryNAR.getStr(2);
194196
narInfo->compression = queryNAR.getStr(3);
195197
if (!queryNAR.isNull(4))
196198
narInfo->fileHash = Hash::parseAnyPrefixed(queryNAR.getStr(4));
197199
narInfo->fileSize = queryNAR.getInt(5);
198-
narInfo->narHash = Hash::parseAnyPrefixed(queryNAR.getStr(6));
199200
narInfo->narSize = queryNAR.getInt(7);
200201
for (auto & r : tokenizeString<Strings>(queryNAR.getStr(8), " "))
201202
narInfo->references.insert(StorePath(r));

src/libstore/nar-info.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include "globals.hh"
22
#include "nar-info.hh"
3+
#include "store-api.hh"
34

45
namespace nix {
56

67
NarInfo::NarInfo(const Store & store, const std::string & s, const std::string & whence)
7-
: ValidPathInfo(StorePath(StorePath::dummy)) // FIXME: hack
8+
: ValidPathInfo(StorePath(StorePath::dummy), Hash(Hash::dummy)) // FIXME: hack
89
{
910
auto corrupt = [&]() {
1011
return Error("NAR info file '%1%' is corrupt", whence);
@@ -19,6 +20,7 @@ NarInfo::NarInfo(const Store & store, const std::string & s, const std::string &
1920
};
2021

2122
bool havePath = false;
23+
bool haveNarHash = false;
2224

2325
size_t pos = 0;
2426
while (pos < s.size()) {
@@ -46,8 +48,10 @@ NarInfo::NarInfo(const Store & store, const std::string & s, const std::string &
4648
else if (name == "FileSize") {
4749
if (!string2Int(value, fileSize)) throw corrupt();
4850
}
49-
else if (name == "NarHash")
51+
else if (name == "NarHash") {
5052
narHash = parseHashField(value);
53+
haveNarHash = true;
54+
}
5155
else if (name == "NarSize") {
5256
if (!string2Int(value, narSize)) throw corrupt();
5357
}
@@ -76,7 +80,7 @@ NarInfo::NarInfo(const Store & store, const std::string & s, const std::string &
7680

7781
if (compression == "") compression = "bzip2";
7882

79-
if (!havePath || url.empty() || narSize == 0) throw corrupt();
83+
if (!havePath || !haveNarHash || url.empty() || narSize == 0) throw corrupt();
8084
}
8185

8286
std::string NarInfo::to_string(const Store & store) const

src/libstore/nar-info.hh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include "types.hh"
44
#include "hash.hh"
5-
#include "store-api.hh"
5+
#include "path-info.hh"
66

77
namespace nix {
88

9+
class Store;
10+
911
struct NarInfo : ValidPathInfo
1012
{
1113
std::string url;
@@ -15,7 +17,7 @@ struct NarInfo : ValidPathInfo
1517
std::string system;
1618

1719
NarInfo() = delete;
18-
NarInfo(StorePath && path) : ValidPathInfo(std::move(path)) { }
20+
NarInfo(StorePath && path, Hash narHash) : ValidPathInfo(std::move(path), narHash) { }
1921
NarInfo(const ValidPathInfo & info) : ValidPathInfo(info) { }
2022
NarInfo(const Store & store, const std::string & s, const std::string & whence);
2123

0 commit comments

Comments
 (0)