2020-01-28 12:11:02 +00:00
|
|
|
#include "fetchers.hh"
|
|
|
|
#include "download.hh"
|
|
|
|
#include "globals.hh"
|
|
|
|
#include "parse.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
namespace nix::fetchers {
|
|
|
|
|
|
|
|
struct TarballInput : Input
|
|
|
|
{
|
|
|
|
ParsedURL url;
|
|
|
|
std::optional<Hash> hash;
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
TarballInput(const ParsedURL & url) : url(url)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
std::string type() const override { return "tarball"; }
|
|
|
|
|
2020-01-28 12:11:02 +00:00
|
|
|
bool operator ==(const Input & other) const override
|
|
|
|
{
|
|
|
|
auto other2 = dynamic_cast<const TarballInput *>(&other);
|
|
|
|
return
|
|
|
|
other2
|
|
|
|
&& to_string() == other2->to_string()
|
|
|
|
&& hash == other2->hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isImmutable() const override
|
|
|
|
{
|
2020-01-31 18:16:40 +00:00
|
|
|
return hash || narHash;
|
2020-01-28 12:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string to_string() const override
|
|
|
|
{
|
|
|
|
auto url2(url);
|
2020-01-31 18:16:40 +00:00
|
|
|
// NAR hashes are preferred over file hashes since tar/zip files
|
|
|
|
// don't have a canonical representation.
|
2020-01-28 12:11:02 +00:00
|
|
|
if (narHash)
|
|
|
|
url2.query.insert_or_assign("narHash", narHash->to_string(SRI));
|
2020-01-31 18:16:40 +00:00
|
|
|
else if (hash)
|
|
|
|
url2.query.insert_or_assign("hash", hash->to_string(SRI));
|
2020-01-28 12:11:02 +00:00
|
|
|
return url2.to_string();
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
Attrs toAttrsInternal() const override
|
|
|
|
{
|
|
|
|
Attrs attrs;
|
|
|
|
attrs.emplace("url", url.to_string());
|
|
|
|
if (narHash)
|
|
|
|
attrs.emplace("narHash", hash->to_string(SRI));
|
|
|
|
else if (hash)
|
|
|
|
attrs.emplace("hash", hash->to_string(SRI));
|
|
|
|
return attrs;
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:11:02 +00:00
|
|
|
std::pair<Tree, std::shared_ptr<const Input>> fetchTreeInternal(nix::ref<Store> store) const override
|
|
|
|
{
|
|
|
|
CachedDownloadRequest request(url.to_string());
|
|
|
|
request.unpack = true;
|
|
|
|
request.getLastModified = true;
|
|
|
|
request.name = "source";
|
|
|
|
|
|
|
|
auto res = getDownloader()->downloadCached(store, request);
|
|
|
|
|
|
|
|
auto input = std::make_shared<TarballInput>(*this);
|
|
|
|
|
|
|
|
auto storePath = store->parseStorePath(res.storePath);
|
|
|
|
|
|
|
|
input->narHash = store->queryPathInfo(storePath)->narHash;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Tree {
|
|
|
|
.actualPath = res.path,
|
|
|
|
.storePath = std::move(storePath),
|
2020-02-01 15:41:54 +00:00
|
|
|
.info = TreeInfo {
|
|
|
|
.lastModified = *res.lastModified,
|
|
|
|
},
|
2020-01-28 12:11:02 +00:00
|
|
|
},
|
|
|
|
input
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TarballInputScheme : InputScheme
|
|
|
|
{
|
|
|
|
std::unique_ptr<Input> inputFromURL(const ParsedURL & url) override
|
|
|
|
{
|
|
|
|
if (url.scheme != "file" && url.scheme != "http" && url.scheme != "https") return nullptr;
|
|
|
|
|
|
|
|
if (!hasSuffix(url.path, ".zip")
|
|
|
|
&& !hasSuffix(url.path, ".tar")
|
|
|
|
&& !hasSuffix(url.path, ".tar.gz")
|
|
|
|
&& !hasSuffix(url.path, ".tar.xz")
|
|
|
|
&& !hasSuffix(url.path, ".tar.bz2"))
|
|
|
|
return nullptr;
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
auto input = std::make_unique<TarballInput>(url);
|
|
|
|
|
|
|
|
auto hash = url.query.find("hash");
|
|
|
|
if (hash != url.query.end())
|
|
|
|
// FIXME: require SRI hash.
|
|
|
|
input->hash = Hash(hash->second);
|
2020-01-28 12:11:02 +00:00
|
|
|
|
|
|
|
auto narHash = url.query.find("narHash");
|
2020-01-31 18:16:40 +00:00
|
|
|
if (narHash != url.query.end())
|
2020-01-28 12:11:02 +00:00
|
|
|
// FIXME: require SRI hash.
|
|
|
|
input->narHash = Hash(narHash->second);
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
2020-01-31 18:16:40 +00:00
|
|
|
|
2020-03-17 19:54:36 +00:00
|
|
|
std::unique_ptr<Input> inputFromAttrs(const Attrs & attrs) override
|
2020-01-31 18:16:40 +00:00
|
|
|
{
|
|
|
|
if (maybeGetStrAttr(attrs, "type") != "tarball") return {};
|
2020-01-31 18:35:28 +00:00
|
|
|
|
|
|
|
for (auto & [name, value] : attrs)
|
|
|
|
if (name != "type" && name != "url" && name != "hash" && name != "narHash")
|
|
|
|
throw Error("unsupported tarball input attribute '%s'", name);
|
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
auto input = std::make_unique<TarballInput>(parseURL(getStrAttr(attrs, "url")));
|
|
|
|
if (auto hash = maybeGetStrAttr(attrs, "hash"))
|
|
|
|
// FIXME: require SRI hash.
|
|
|
|
input->hash = Hash(*hash);
|
2020-02-11 22:53:24 +00:00
|
|
|
|
2020-01-31 18:16:40 +00:00
|
|
|
return input;
|
|
|
|
}
|
2020-01-28 12:11:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static auto r1 = OnStartup([] { registerInputScheme(std::make_unique<TarballInputScheme>()); });
|
|
|
|
|
|
|
|
}
|