2015-07-20 05:30:16 +03:00
|
|
|
#include "builtins.hh"
|
2020-04-06 16:10:58 +03:00
|
|
|
#include "datatransfer.hh"
|
2015-10-30 12:27:47 +02:00
|
|
|
#include "store-api.hh"
|
|
|
|
#include "archive.hh"
|
2015-10-30 13:33:40 +02:00
|
|
|
#include "compression.hh"
|
2015-07-20 05:30:16 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2017-02-16 16:42:49 +02:00
|
|
|
void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
|
2015-07-20 05:30:16 +03:00
|
|
|
{
|
2017-02-16 16:42:49 +02: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 18:11:51 +03:00
|
|
|
auto getAttr = [&](const string & name) {
|
|
|
|
auto i = drv.env.find(name);
|
2017-07-30 14:27:57 +03:00
|
|
|
if (i == drv.env.end()) throw Error(format("attribute '%s' missing") % name);
|
2016-06-01 18:11:51 +03:00
|
|
|
return i->second;
|
|
|
|
};
|
|
|
|
|
2018-03-28 14:32:44 +03:00
|
|
|
Path storePath = getAttr("out");
|
|
|
|
auto mainUrl = getAttr("url");
|
2019-12-05 20:11:09 +02:00
|
|
|
bool unpack = get(drv.env, "unpack").value_or("") == "1";
|
2018-03-28 14:32:44 +03:00
|
|
|
|
2020-04-07 00:43:43 +03:00
|
|
|
/* Note: have to use a fresh fileTransfer here because we're in
|
2018-03-28 14:32:44 +03:00
|
|
|
a forked process. */
|
2020-04-07 00:43:43 +03:00
|
|
|
auto fileTransfer = makeFileTransfer();
|
2018-03-28 14:32:44 +03: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-07 00:43:43 +03:00
|
|
|
FileTransferRequest request(url);
|
2018-03-28 14:32:44 +03:00
|
|
|
request.verifyTLS = false;
|
|
|
|
request.decompress = false;
|
|
|
|
|
2018-08-06 16:40:29 +03:00
|
|
|
auto decompressor = makeDecompressionSink(
|
2018-09-05 22:22:37 +03:00
|
|
|
unpack && hasSuffix(mainUrl, ".xz") ? "xz" : "none", sink);
|
2020-04-07 00:43:43 +03:00
|
|
|
fileTransfer->download(std::move(request), *decompressor);
|
2018-08-06 16:40:29 +03:00
|
|
|
decompressor->finish();
|
2018-03-28 14:32:44 +03:00
|
|
|
});
|
|
|
|
|
2018-09-05 22:22:37 +03:00
|
|
|
if (unpack)
|
2018-08-06 16:40:29 +03:00
|
|
|
restorePath(storePath, *source);
|
|
|
|
else
|
|
|
|
writeFile(storePath, *source);
|
2018-03-28 14:32:44 +03: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 14:07:08 +03:00
|
|
|
if (getAttr("outputHashMode") == "flat")
|
|
|
|
for (auto hashedMirror : settings.hashedMirrors.get())
|
|
|
|
try {
|
|
|
|
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
|
2017-07-17 14:13:18 +03:00
|
|
|
auto ht = parseHashType(getAttr("outputHashAlgo"));
|
2019-06-01 16:27:43 +03:00
|
|
|
auto h = Hash(getAttr("outputHash"), ht);
|
|
|
|
fetch(hashedMirror + printHashType(h.type) + "/" + h.to_string(Base16, false));
|
2018-03-28 14:32:44 +03:00
|
|
|
return;
|
2017-07-17 14:07:08 +03:00
|
|
|
} catch (Error & e) {
|
|
|
|
debug(e.what());
|
|
|
|
}
|
2015-07-20 05:30:16 +03:00
|
|
|
|
2018-03-28 14:32:44 +03:00
|
|
|
/* Otherwise try the specified URL. */
|
|
|
|
fetch(mainUrl);
|
2015-07-20 05:30:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|