-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add Store API-based HashedMirrors #3673
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,202 @@ | ||
#include "binary-cache-store.hh" | ||
#include "filetransfer.hh" | ||
#include "globals.hh" | ||
#include "archive.hh" | ||
|
||
namespace nix { | ||
|
||
class HashedMirrorStore : public Store | ||
{ | ||
private: | ||
|
||
Path cacheUri; | ||
Path cacheDir; | ||
|
||
public: | ||
|
||
HashedMirrorStore( | ||
const Params & params, const Path & _cacheUri) | ||
: Store(params) | ||
, cacheUri(_cacheUri) | ||
{ | ||
if (hasPrefix(cacheUri, "hashed-mirror+")) | ||
cacheUri = cacheUri.substr(14); | ||
|
||
if (cacheUri.back() == '/') | ||
cacheUri.pop_back(); | ||
|
||
if (!hasPrefix(cacheUri, "file://")) | ||
throw Error("only file:// cache is currently supported in hashed mirror store"); | ||
|
||
cacheDir = cacheUri.substr(7); | ||
} | ||
|
||
std::string getUri() override | ||
{ | ||
return cacheUri; | ||
} | ||
|
||
void init() | ||
{ | ||
} | ||
|
||
void narFromPath(const StorePath & storePath, Sink & sink, const std::string ca) override | ||
{ | ||
dumpPath(cacheDir + getPath(ca), sink); | ||
} | ||
|
||
static Hash getHash(std::string ca) | ||
{ | ||
if (ca == "") | ||
throw Error("ca cannot be empty in hashed mirror store"); | ||
|
||
if (!hasPrefix(ca, "fixed:")) | ||
throw Error("hashed mirror must be fixed-output"); | ||
|
||
ca = ca.substr(6); | ||
|
||
if (hasPrefix(ca, "r:")) | ||
throw Error("hashed mirror cannot be recursive"); | ||
|
||
return Hash(ca); | ||
} | ||
|
||
static std::string getPath(std::string ca) | ||
{ | ||
Hash h = getHash(ca); | ||
|
||
return "/" + printHashType(h.type) + "/" + h.to_string(Base16, false); | ||
} | ||
|
||
bool isValidPathUncached(const StorePath & storePath, std::string ca) override | ||
{ | ||
return fileExists(getPath(ca)); | ||
} | ||
|
||
void queryPathInfoUncached(const StorePath & path, | ||
Callback<std::shared_ptr<const ValidPathInfo>> callback, const std::string ca) noexcept override | ||
{ | ||
auto info = std::make_shared<ValidPathInfo>(path.clone()); | ||
|
||
// not efficient! | ||
StringSink sink; | ||
dumpPath(cacheDir + getPath(ca), sink); | ||
info->narHash = hashString(htSHA256, *sink.s); | ||
info->narSize = sink.s->size(); | ||
|
||
info->ca = ca; | ||
callback(std::move(info)); | ||
} | ||
|
||
std::optional<StorePath> queryPathFromHashPart(const std::string & hashPart) override | ||
{ | ||
unsupported("queryPathFromHashPart"); | ||
} | ||
|
||
void addToStore(const ValidPathInfo & info, Source & source, | ||
RepairFlag repair, CheckSigsFlag checkSigs, std::shared_ptr<FSAccessor> accessor) override | ||
{ | ||
if (info.references.size() > 0) | ||
throw Error("references are not supported in a hashed mirror store"); | ||
|
||
auto dirname = std::string(dirOf(cacheDir + getPath(info.ca))); | ||
if (!pathExists(dirname)) | ||
if (mkdir(dirname.c_str(), 0777) == -1) | ||
throw SysError(format("creating directory '%1%'") % dirname); | ||
|
||
auto path = cacheDir + getPath(info.ca); | ||
restorePath(path, source); | ||
|
||
Hash h(info.ca.substr(6)); | ||
|
||
HashSink hashSink(h.type); | ||
readFile(path, hashSink); | ||
|
||
Hash gotHash = hashSink.finish().first; | ||
if (gotHash != h) | ||
throw Error("path '%s' does not have correct hash: expected %s, got %s", path, h.to_string(), gotHash.to_string()); | ||
} | ||
|
||
StorePath addToStore(const string & name, const Path & srcPath, | ||
FileIngestionMethod method, HashType hashAlgo, | ||
PathFilter & filter, RepairFlag repair) override | ||
{ | ||
unsupported("addToStore"); | ||
} | ||
|
||
void ensurePath(const StorePath & path) override | ||
{ | ||
unsupported("ensurePath"); | ||
} | ||
|
||
StorePath addTextToStore(const string & name, const string & s, | ||
const StorePathSet & references, RepairFlag repair = NoRepair) override | ||
{ unsupported("addTextToStore"); } | ||
|
||
BuildResult buildDerivation(const StorePath & drvPath, const BasicDerivation & drv, | ||
BuildMode buildMode = bmNormal) override | ||
{ unsupported("buildDerivation"); } | ||
|
||
StorePathSet queryValidPaths(const StorePathSet & paths, SubstituteFlag maybeSubstitute, std::map<std::string, std::string> pathsInfo) override | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
{ | ||
StorePathSet res; | ||
for (auto & i : paths) { | ||
auto ca = pathsInfo.find(printStorePath(i)); | ||
if (isValidPath(i, ca != pathsInfo.end() ? ca->second : "")) | ||
res.insert(i.clone()); | ||
} | ||
return res; | ||
} | ||
|
||
// Taken from local-binary-cache.cc, should make this also support http | ||
|
||
static void atomicWrite(const Path & path, const std::string & s) | ||
{ | ||
Path tmp = path + ".tmp." + std::to_string(getpid()); | ||
AutoDelete del(tmp, false); | ||
writeFile(tmp, s); | ||
if (rename(tmp.c_str(), path.c_str())) | ||
throw SysError(format("renaming '%1%' to '%2%'") % tmp % path); | ||
del.cancel(); | ||
} | ||
|
||
bool fileExists(const std::string & path) | ||
{ | ||
return pathExists(cacheDir + "/" + path); | ||
} | ||
|
||
void upsertFile(const std::string & path, | ||
const std::string & data, | ||
const std::string & mimeType) | ||
{ | ||
atomicWrite(cacheDir + "/" + path, data); | ||
} | ||
|
||
void getFile(const std::string & path, Sink & sink) | ||
{ | ||
try { | ||
readFile(cacheDir + "/" + path, sink); | ||
} catch (SysError & e) { | ||
if (e.errNo == ENOENT) | ||
throw NoSuchBinaryCacheFile("file '%s' does not exist in binary cache", path); | ||
} | ||
} | ||
|
||
bool supportsOtherStoreDir() { | ||
return true; | ||
} | ||
|
||
}; | ||
|
||
static RegisterStoreImplementation regStore([]( | ||
const std::string & uri, const Store::Params & params) | ||
-> std::shared_ptr<Store> | ||
{ | ||
if (std::string(uri, 0, 14) != "hashed-mirror+") | ||
return 0; | ||
auto store = std::make_shared<HashedMirrorStore>(params, uri); | ||
store->init(); | ||
return store; | ||
}); | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit for later: but
std::string_view
would be betterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
const std::string
isn't very useful. Maybe you intendedconst std::string &
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually i just meant the callee shouldn't modify ca -
const std::string &
would let you modify it. but it's confusing right next toconst StorePath &
.