Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4282 from tweag/fix-ca-hash-rewriting
fix the hash rewriting for ca-derivations
- Loading branch information
Showing
8 changed files
with
193 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "path-references.hh" | ||
#include "hash.hh" | ||
#include "util.hh" | ||
#include "archive.hh" | ||
|
||
#include <map> | ||
#include <cstdlib> | ||
#include <mutex> | ||
#include <algorithm> | ||
|
||
|
||
namespace nix { | ||
|
||
|
||
PathRefScanSink::PathRefScanSink(StringSet && hashes, std::map<std::string, StorePath> && backMap) | ||
: RefScanSink(std::move(hashes)) | ||
, backMap(std::move(backMap)) | ||
{ } | ||
|
||
PathRefScanSink PathRefScanSink::fromPaths(const StorePathSet & refs) | ||
{ | ||
StringSet hashes; | ||
std::map<std::string, StorePath> backMap; | ||
|
||
for (auto & i : refs) { | ||
std::string hashPart(i.hashPart()); | ||
auto inserted = backMap.emplace(hashPart, i).second; | ||
assert(inserted); | ||
hashes.insert(hashPart); | ||
} | ||
|
||
return PathRefScanSink(std::move(hashes), std::move(backMap)); | ||
} | ||
|
||
StorePathSet PathRefScanSink::getResultPaths() | ||
{ | ||
/* Map the hashes found back to their store paths. */ | ||
StorePathSet found; | ||
for (auto & i : getResult()) { | ||
auto j = backMap.find(i); | ||
assert(j != backMap.end()); | ||
found.insert(j->second); | ||
} | ||
|
||
return found; | ||
} | ||
|
||
|
||
std::pair<StorePathSet, HashResult> scanForReferences( | ||
const std::string & path, | ||
const StorePathSet & refs) | ||
{ | ||
HashSink hashSink { htSHA256 }; | ||
auto found = scanForReferences(hashSink, path, refs); | ||
auto hash = hashSink.finish(); | ||
return std::pair<StorePathSet, HashResult>(found, hash); | ||
} | ||
|
||
StorePathSet scanForReferences( | ||
Sink & toTee, | ||
const Path & path, | ||
const StorePathSet & refs) | ||
{ | ||
PathRefScanSink refsSink = PathRefScanSink::fromPaths(refs); | ||
TeeSink sink { refsSink, toTee }; | ||
|
||
/* Look for the hashes in the NAR dump of the path. */ | ||
dumpPath(path, sink); | ||
|
||
return refsSink.getResultPaths(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include "references.hh" | ||
#include "path.hh" | ||
|
||
namespace nix { | ||
|
||
std::pair<StorePathSet, HashResult> scanForReferences(const Path & path, const StorePathSet & refs); | ||
|
||
StorePathSet scanForReferences(Sink & toTee, const Path & path, const StorePathSet & refs); | ||
|
||
class PathRefScanSink : public RefScanSink | ||
{ | ||
std::map<std::string, StorePath> backMap; | ||
|
||
PathRefScanSink(StringSet && hashes, std::map<std::string, StorePath> && backMap); | ||
|
||
public: | ||
|
||
static PathRefScanSink fromPaths(const StorePathSet & refs); | ||
|
||
StorePathSet getResultPaths(); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "references.hh" | ||
#include <gtest/gtest.h> | ||
|
||
namespace nix { | ||
|
||
using std::string; | ||
|
||
struct RewriteParams { | ||
string originalString, finalString; | ||
StringMap rewrites; | ||
|
||
friend std::ostream& operator<<(std::ostream& os, const RewriteParams& bar) { | ||
StringSet strRewrites; | ||
for (auto & [from, to] : bar.rewrites) | ||
strRewrites.insert(from + "->" + to); | ||
return os << | ||
"OriginalString: " << bar.originalString << std::endl << | ||
"Rewrites: " << concatStringsSep(",", strRewrites) << std::endl << | ||
"Expected result: " << bar.finalString; | ||
} | ||
}; | ||
|
||
class RewriteTest : public ::testing::TestWithParam<RewriteParams> { | ||
}; | ||
|
||
TEST_P(RewriteTest, IdentityRewriteIsIdentity) { | ||
RewriteParams param = GetParam(); | ||
StringSink rewritten; | ||
auto rewriter = RewritingSink(param.rewrites, rewritten); | ||
rewriter(param.originalString); | ||
rewriter.flush(); | ||
ASSERT_EQ(rewritten.s, param.finalString); | ||
} | ||
|
||
INSTANTIATE_TEST_CASE_P( | ||
references, | ||
RewriteTest, | ||
::testing::Values( | ||
RewriteParams{ "foooo", "baroo", {{"foo", "bar"}, {"bar", "baz"}}}, | ||
RewriteParams{ "foooo", "bazoo", {{"fou", "bar"}, {"foo", "baz"}}}, | ||
RewriteParams{ "foooo", "foooo", {}} | ||
) | ||
); | ||
|
||
} | ||
|
Oops, something went wrong.