Skip to content

Commit

Permalink
Use PathReferences more widely
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericson2314 committed Oct 7, 2020
1 parent dae4409 commit f8d562c
Show file tree
Hide file tree
Showing 29 changed files with 431 additions and 205 deletions.
10 changes: 8 additions & 2 deletions perl/lib/Nix/Store.xs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SV * queryPathInfo(char * path, int base32)
mXPUSHi(info->registrationTime);
mXPUSHi(info->narSize);
AV * arr = newAV();
for (auto & i : info->references)
for (auto & i : info->referencesPossiblyToSelf())
av_push(arr, newSVpv(store()->printStorePath(i).c_str(), 0));
XPUSHs(sv_2mortal(newRV((SV *) arr)));
} catch (Error & e) {
Expand Down Expand Up @@ -287,7 +287,13 @@ SV * makeFixedOutputPath(int recursive, char * algo, char * hash, char * name)
try {
auto h = Hash::parseAny(hash, parseHashType(algo));
auto method = recursive ? FileIngestionMethod::Recursive : FileIngestionMethod::Flat;
auto path = store()->makeFixedOutputPath(method, h, name);
auto path = store()->makeFixedOutputPath(name, FixedOutputInfo {
{
.method = method,
.hash = h,
},
{},
});
XPUSHs(sv_2mortal(newSVpv(store()->printStorePath(path).c_str(), 0)));
} catch (Error & e) {
croak("%s", e.what());
Expand Down
16 changes: 14 additions & 2 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,13 @@ static void prim_derivationStrict(EvalState & state, const Pos & pos, Value * *
std::optional<HashType> ht = parseHashTypeOpt(outputHashAlgo);
Hash h = newHashAllowEmpty(*outputHash, ht);

auto outPath = state.store->makeFixedOutputPath(ingestionMethod, h, drvName);
auto outPath = state.store->makeFixedOutputPath(drvName, FixedOutputInfo {
{
.method = ingestionMethod,
.hash = h,
},
{},
});
drv.env["out"] = state.store->printStorePath(outPath);
drv.outputs.insert_or_assign("out", DerivationOutput {
.output = DerivationOutputCAFixed {
Expand Down Expand Up @@ -1764,7 +1770,13 @@ static void addPath(EvalState & state, const Pos & pos, const string & name, con

std::optional<StorePath> expectedStorePath;
if (expectedHash)
expectedStorePath = state.store->makeFixedOutputPath(method, *expectedHash, name);
expectedStorePath = state.store->makeFixedOutputPath(name, FixedOutputInfo {
{
.method = method,
.hash = *expectedHash,
},
{},
});
Path dstPath;
if (!expectedHash || !state.store->isValidPath(*expectedStorePath)) {
dstPath = state.store->printStorePath(settings.readOnlyMode
Expand Down
2 changes: 1 addition & 1 deletion src/libexpr/value-to-xml.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ namespace nix {

void printValueAsXML(EvalState & state, bool strict, bool location,
Value & v, std::ostream & out, PathSet & context);

}
8 changes: 7 additions & 1 deletion src/libfetchers/fetchers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,13 @@ StorePath Input::computeStorePath(Store & store) const
auto narHash = getNarHash();
if (!narHash)
throw Error("cannot compute store path for mutable input '%s'", to_string());
return store.makeFixedOutputPath(FileIngestionMethod::Recursive, *narHash, "source");
return store.makeFixedOutputPath("source", FixedOutputInfo {
{
.method = FileIngestionMethod::Recursive,
.hash = *narHash,
},
{},
});
}

std::string Input::getType() const
Expand Down
16 changes: 11 additions & 5 deletions src/libfetchers/tarball.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,20 @@ DownloadFileResult downloadFile(
dumpString(*res.data, sink);
auto hash = hashString(htSHA256, *res.data);
ValidPathInfo info {
store->makeFixedOutputPath(FileIngestionMethod::Flat, hash, name),
*store,
{
.name = name,
.info = FixedOutputInfo {
{
.method = FileIngestionMethod::Flat,
.hash = hash,
},
{},
},
},
hashString(htSHA256, *sink.s),
};
info.narSize = sink.s->size();
info.ca = FixedOutputHash {
.method = FileIngestionMethod::Flat,
.hash = hash,
};
auto source = StringSource { *sink.s };
store->addToStore(info, source, NoRepair, NoCheckSigs);
storePath = std::move(info.path);
Expand Down
43 changes: 34 additions & 9 deletions src/libstore/binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,17 @@ StorePath BinaryCacheStore::addToStoreFromDump(Source & dump, const string & nam
unsupported("addToStoreFromDump");
return addToStoreCommon(dump, repair, CheckSigs, [&](HashResult nar) {
ValidPathInfo info {
makeFixedOutputPath(method, nar.first, name),
*this,
{
.name = name,
.info = FixedOutputInfo {
{
.method = method,
.hash = nar.first,
},
{},
},
},
nar.first,
};
info.narSize = nar.second;
Expand Down Expand Up @@ -412,14 +422,20 @@ StorePath BinaryCacheStore::addToStore(const string & name, const Path & srcPath
});
return addToStoreCommon(*source, repair, CheckSigs, [&](HashResult nar) {
ValidPathInfo info {
makeFixedOutputPath(method, h, name),
*this,
{
.name = name,
.info = FixedOutputInfo {
{
.method = method,
.hash = h,
},
{},
},
},
nar.first,
};
info.narSize = nar.second;
info.ca = FixedOutputHash {
.method = method,
.hash = h,
};
return info;
})->path;
}
Expand All @@ -428,17 +444,26 @@ StorePath BinaryCacheStore::addTextToStore(const string & name, const string & s
const StorePathSet & references, RepairFlag repair)
{
auto textHash = hashString(htSHA256, s);
auto path = makeTextPath(name, textHash, references);
auto path = makeTextPath(name, TextInfo { textHash, references });

if (!repair && isValidPath(path))
return path;

auto source = StringSource { s };
return addToStoreCommon(source, repair, CheckSigs, [&](HashResult nar) {
ValidPathInfo info { path, nar.first };
ValidPathInfo info {
*this,
{
.name = name,
.info = TextInfo {
{ .hash = textHash },
references,
},
},
nar.first,
};
info.narSize = nar.second;
info.ca = TextHash { textHash };
info.references = references;
return info;
})->path;
}
Expand Down
30 changes: 16 additions & 14 deletions src/libstore/build.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4056,25 +4056,24 @@ void DerivationGoal::registerOutputs()
break;
}
auto got = caSink.finish().first;
auto refs = rewriteRefs();
HashModuloSink narSink { htSHA256, oldHashPart };
dumpPath(actualPath, narSink);
auto narHashAndSize = narSink.finish();
ValidPathInfo newInfo0 {
worker.store.makeFixedOutputPath(
outputHash.method,
got,
outputPathName(drv->name, outputName),
refs.references,
refs.hasSelfReference),
worker.store,
{
.name = outputPathName(drv->name, outputName),
.info = FixedOutputInfo {
{
.method = outputHash.method,
.hash = got,
},
rewriteRefs(),
},
},
narHashAndSize.first,
};
newInfo0.narSize = narHashAndSize.second;
newInfo0.ca = FixedOutputHash {
.method = outputHash.method,
.hash = got,
};
static_cast<PathReferences<StorePath> &>(newInfo0) = refs;

assert(newInfo0.ca);
return newInfo0;
Expand Down Expand Up @@ -4861,7 +4860,10 @@ void SubstitutionGoal::tryNext()
subs.pop_front();

if (ca) {
subPath = sub->makeFixedOutputPathFromCA(storePath.name(), *ca);
subPath = sub->makeFixedOutputPathFromCA({
.name = std::string { storePath.name() },
.info = caWithoutRefs(*ca),
});
if (sub->storeDir == worker.store.storeDir)
assert(subPath == storePath);
} else if (sub->storeDir != worker.store.storeDir) {
Expand Down Expand Up @@ -4891,7 +4893,7 @@ void SubstitutionGoal::tryNext()
}

if (info->path != storePath) {
if (info->isContentAddressed(*sub) && info->references.empty()) {
if (info->isContentAddressed(*sub) && info->references.empty() && !info->hasSelfReference) {
auto info2 = std::make_shared<ValidPathInfo>(*info);
info2->path = storePath;
info = info2;
Expand Down
24 changes: 19 additions & 5 deletions src/libstore/content-address.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ std::string FixedOutputHash::printMethodAlgo() const
return makeFileIngestionPrefix(method) + printHashType(hash.type);
}


std::string makeFileIngestionPrefix(const FileIngestionMethod m)
{
switch (m) {
case FileIngestionMethod::Flat:
return "";
case FileIngestionMethod::Recursive:
return "r:";
default:
throw Error("impossible, caught both cases");
}
assert(false);
}

std::string makeFixedOutputCA(FileIngestionMethod method, const Hash & hash)
Expand All @@ -32,10 +32,13 @@ std::string renderContentAddress(ContentAddress ca)
{
return std::visit(overloaded {
[](TextHash th) {
return "text:" + th.hash.to_string(Base32, true);
return "text:"
+ th.hash.to_string(Base32, true);
},
[](FixedOutputHash fsh) {
return makeFixedOutputCA(fsh.method, fsh.hash);
return "fixed:"
+ makeFileIngestionPrefix(fsh.method)
+ fsh.hash.to_string(Base32, true);
}
}, ca);
}
Expand Down Expand Up @@ -142,7 +145,18 @@ Hash getContentAddressHash(const ContentAddress & ca)
},
[](FixedOutputHash fsh) {
return fsh.hash;
}
},
}, ca);
}

ContentAddressWithReferences caWithoutRefs(const ContentAddress & ca) {
return std::visit(overloaded {
[&](TextHash h) -> ContentAddressWithReferences {
return TextInfo { h, {}};
},
[&](FixedOutputHash h) -> ContentAddressWithReferences {
return FixedOutputInfo { h, {}};
},
}, ca);
}

Expand Down
Loading

0 comments on commit f8d562c

Please sign in to comment.