2020-03-30 17:04:18 +03:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
std::unique_ptr<std::vector<std::unique_ptr<InputScheme>>> inputSchemes = nullptr;
|
|
|
|
|
|
|
|
void registerInputScheme(std::unique_ptr<InputScheme> && inputScheme)
|
|
|
|
{
|
|
|
|
if (!inputSchemes) inputSchemes = std::make_unique<std::vector<std::unique_ptr<InputScheme>>>();
|
|
|
|
inputSchemes->push_back(std::move(inputScheme));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Input> inputFromURL(const ParsedURL & url)
|
|
|
|
{
|
|
|
|
for (auto & inputScheme : *inputSchemes) {
|
|
|
|
auto res = inputScheme->inputFromURL(url);
|
|
|
|
if (res) return res;
|
|
|
|
}
|
|
|
|
throw Error("input '%s' is unsupported", url.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Input> inputFromURL(const std::string & url)
|
|
|
|
{
|
|
|
|
return inputFromURL(parseURL(url));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs)
|
|
|
|
{
|
2020-04-02 20:04:33 +03:00
|
|
|
auto attrs2(attrs);
|
|
|
|
attrs2.erase("narHash");
|
2020-03-30 17:04:18 +03:00
|
|
|
for (auto & inputScheme : *inputSchemes) {
|
2020-04-02 20:04:33 +03:00
|
|
|
auto res = inputScheme->inputFromAttrs(attrs2);
|
2020-03-30 17:04:18 +03:00
|
|
|
if (res) {
|
2020-06-09 19:10:54 +03:00
|
|
|
if (auto narHash = maybeGetStrAttr(attrs, "narHash")) {
|
|
|
|
if (narHash->empty()) {
|
|
|
|
res->narHash = Hash(htUnknown);
|
|
|
|
printError("warning: found empty hash, assuming you wanted '%s'", res->narHash->to_string());
|
|
|
|
} else
|
|
|
|
// FIXME: require SRI hash.
|
|
|
|
res->narHash = Hash(*narHash);
|
|
|
|
}
|
2020-03-30 17:04:18 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw Error("input '%s' is unsupported", attrsToJson(attrs));
|
|
|
|
}
|
|
|
|
|
|
|
|
Attrs Input::toAttrs() const
|
|
|
|
{
|
|
|
|
auto attrs = toAttrsInternal();
|
|
|
|
if (narHash)
|
|
|
|
attrs.emplace("narHash", narHash->to_string(SRI));
|
|
|
|
attrs.emplace("type", type());
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<Tree, std::shared_ptr<const Input>> Input::fetchTree(ref<Store> store) const
|
|
|
|
{
|
|
|
|
auto [tree, input] = fetchTreeInternal(store);
|
|
|
|
|
|
|
|
if (tree.actualPath == "")
|
|
|
|
tree.actualPath = store->toRealPath(tree.storePath);
|
|
|
|
|
|
|
|
if (!tree.info.narHash)
|
|
|
|
tree.info.narHash = store->queryPathInfo(tree.storePath)->narHash;
|
|
|
|
|
|
|
|
if (input->narHash)
|
|
|
|
assert(input->narHash == tree.info.narHash);
|
|
|
|
|
|
|
|
if (narHash && narHash != input->narHash)
|
|
|
|
throw Error("NAR hash mismatch in input '%s' (%s), expected '%s', got '%s'",
|
|
|
|
to_string(), tree.actualPath, narHash->to_string(SRI), input->narHash->to_string(SRI));
|
|
|
|
|
|
|
|
return {std::move(tree), input};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|