2015-07-20 02:30:16 +00:00
|
|
|
#include "builtins.hh"
|
2020-04-06 13:10:58 +00:00
|
|
|
#include "datatransfer.hh"
|
2015-10-30 10:27:47 +00:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "archive.hh"
|
2015-10-30 11:33:40 +00:00
|
|
|
#include "compression.hh"
|
2015-07-20 02:30:16 +00:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-02-16 14:42:49 +00:00
|
|
|
void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
|
2015-07-20 02:30:16 +00:00
|
|
|
{
|
2017-02-16 14:42:49 +00:00
|
|
|
/* Make the host's netrc data available. Too bad curl requires
|
|
|
|
this to be stored in a file. It would be nice if we could just
|
|
|
|
pass a pointer to the data. */
|
|
|
|
if (netrcData != "") {
|
|
|
|
settings.netrcFile = "netrc";
|
|
|
|
writeFile(settings.netrcFile, netrcData, 0600);
|
|
|
|
}
|
|
|
|
|
2016-06-01 15:11:51 +00:00
|
|
|
auto getAttr = [&](const string & name) {
|
|
|
|
auto i = drv.env.find(name);
|
2017-07-30 11:27:57 +00:00
|
|
|
if (i == drv.env.end()) throw Error(format("attribute '%s' missing") % name);
|
2016-06-01 15:11:51 +00:00
|
|
|
return i->second;
|
|
|
|
};
|
|
|
|
|
2018-03-28 11:32:44 +00:00
|
|
|
Path storePath = getAttr("out");
|
|
|
|
auto mainUrl = getAttr("url");
|
2019-12-05 18:11:09 +00:00
|
|
|
bool unpack = get(drv.env, "unpack").value_or("") == "1";
|
2018-03-28 11:32:44 +00:00
|
|
|
|
2020-04-06 21:43:43 +00:00
|
|
|
/* Note: have to use a fresh fileTransfer here because we're in
|
2018-03-28 11:32:44 +00:00
|
|
|
a forked process. */
|
2020-04-06 21:43:43 +00:00
|
|
|
auto fileTransfer = makeFileTransfer();
|
2018-03-28 11:32:44 +00:00
|
|
|
|
|
|
|
auto fetch = [&](const std::string & url) {
|
|
|
|
|
|
|
|
auto source = sinkToSource([&](Sink & sink) {
|
|
|
|
|
|
|
|
/* No need to do TLS verification, because we check the hash of
|
|
|
|
the result anyway. */
|
2020-04-06 21:43:43 +00:00
|
|
|
FileTransferRequest request(url);
|
2018-03-28 11:32:44 +00:00
|
|
|
request.verifyTLS = false;
|
|
|
|
request.decompress = false;
|
|
|
|
|
2018-08-06 13:40:29 +00:00
|
|
|
auto decompressor = makeDecompressionSink(
|
2018-09-05 19:22:37 +00:00
|
|
|
unpack && hasSuffix(mainUrl, ".xz") ? "xz" : "none", sink);
|
2020-04-06 21:43:43 +00:00
|
|
|
fileTransfer->download(std::move(request), *decompressor);
|
2018-08-06 13:40:29 +00:00
|
|
|
decompressor->finish();
|
2018-03-28 11:32:44 +00:00
|
|
|
});
|
|
|
|
|
2018-09-05 19:22:37 +00:00
|
|
|
if (unpack)
|
2018-08-06 13:40:29 +00:00
|
|
|
restorePath(storePath, *source);
|
|
|
|
else
|
|
|
|
writeFile(storePath, *source);
|
2018-03-28 11:32:44 +00:00
|
|
|
|
|
|
|
auto executable = drv.env.find("executable");
|
|
|
|
if (executable != drv.env.end() && executable->second == "1") {
|
|
|
|
if (chmod(storePath.c_str(), 0755) == -1)
|
|
|
|
throw SysError(format("making '%1%' executable") % storePath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Try the hashed mirrors first. */
|
2017-07-17 11:07:08 +00:00
|
|
|
if (getAttr("outputHashMode") == "flat")
|
|
|
|
for (auto hashedMirror : settings.hashedMirrors.get())
|
|
|
|
try {
|
|
|
|
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
|
2017-07-17 11:13:18 +00:00
|
|
|
auto ht = parseHashType(getAttr("outputHashAlgo"));
|
2019-06-01 13:27:43 +00:00
|
|
|
auto h = Hash(getAttr("outputHash"), ht);
|
|
|
|
fetch(hashedMirror + printHashType(h.type) + "/" + h.to_string(Base16, false));
|
2018-03-28 11:32:44 +00:00
|
|
|
return;
|
2017-07-17 11:07:08 +00:00
|
|
|
} catch (Error & e) {
|
|
|
|
debug(e.what());
|
|
|
|
}
|
2015-07-20 02:30:16 +00:00
|
|
|
|
2018-03-28 11:32:44 +00:00
|
|
|
/* Otherwise try the specified URL. */
|
|
|
|
fetch(mainUrl);
|
2015-07-20 02:30:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|