2020-01-21 15:27:53 +00:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs)
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
2020-04-02 17:04:33 +00:00
|
|
|
auto attrs2(attrs);
|
|
|
|
attrs2.erase("narHash");
|
2020-01-31 18:16:40 +00:00
|
|
|
for (auto & inputScheme : *inputSchemes) {
|
2020-04-02 17:04:33 +00:00
|
|
|
auto res = inputScheme->inputFromAttrs(attrs2);
|
2020-02-11 22:53:24 +00:00
|
|
|
if (res) {
|
|
|
|
if (auto narHash = maybeGetStrAttr(attrs, "narHash"))
|
|
|
|
// FIXME: require SRI hash.
|
|
|
|
res->narHash = Hash(*narHash);
|
|
|
|
return res;
|
|
|
|
}
|
2020-01-31 18:16:40 +00:00
|
|
|
}
|
|
|
|
throw Error("input '%s' is unsupported", attrsToJson(attrs));
|
|
|
|
}
|
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
Attrs Input::toAttrs() const
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
|
|
|
auto attrs = toAttrsInternal();
|
|
|
|
if (narHash)
|
|
|
|
attrs.emplace("narHash", narHash->to_string(SRI));
|
|
|
|
attrs.emplace("type", type());
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
std::pair<Tree, std::shared_ptr<const Input>> Input::fetchTree(ref<Store> store) const
|
|
|
|
{
|
|
|
|
auto [tree, input] = fetchTreeInternal(store);
|
|
|
|
|
|
|
|
if (tree.actualPath == "")
|
2020-02-02 11:28:56 +00:00
|
|
|
tree.actualPath = store->toRealPath(tree.storePath);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
2020-02-01 15:41:54 +00:00
|
|
|
if (!tree.info.narHash)
|
|
|
|
tree.info.narHash = store->queryPathInfo(tree.storePath)->narHash;
|
2020-01-21 15:27:53 +00:00
|
|
|
|
|
|
|
if (input->narHash)
|
2020-02-01 15:41:54 +00:00
|
|
|
assert(input->narHash == tree.info.narHash);
|
2020-01-21 15:27:53 +00:00
|
|
|
|
2020-01-28 12:11:02 +00:00
|
|
|
if (narHash && narHash != input->narHash)
|
2020-02-11 22:53:24 +00:00
|
|
|
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));
|
2020-01-28 12:11:02 +00:00
|
|
|
|
2020-01-21 15:27:53 +00:00
|
|
|
return {std::move(tree), input};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<const Input> Input::applyOverrides(
|
|
|
|
std::optional<std::string> ref,
|
|
|
|
std::optional<Hash> rev) const
|
|
|
|
{
|
|
|
|
if (ref)
|
|
|
|
throw Error("don't know how to apply '%s' to '%s'", *ref, to_string());
|
|
|
|
if (rev)
|
|
|
|
throw Error("don't know how to apply '%s' to '%s'", rev->to_string(Base16, false), to_string());
|
|
|
|
return shared_from_this();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|