2015-07-20 02:30:16 +00:00
|
|
|
|
#include "builtins.hh"
|
|
|
|
|
#include "download.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);
|
2016-11-25 23:37:43 +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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto fetch = [&](const string & url) {
|
|
|
|
|
/* No need to do TLS verification, because we check the hash of
|
|
|
|
|
the result anyway. */
|
2016-09-14 14:00:40 +00:00
|
|
|
|
DownloadRequest request(url);
|
|
|
|
|
request.verifyTLS = false;
|
2016-06-01 15:11:51 +00:00
|
|
|
|
|
|
|
|
|
/* Show a progress indicator, even though stderr is not a tty. */
|
2016-09-14 14:00:40 +00:00
|
|
|
|
request.showProgress = DownloadRequest::yes;
|
2016-06-01 15:11:51 +00:00
|
|
|
|
|
2016-09-14 14:00:40 +00:00
|
|
|
|
/* Note: have to use a fresh downloader here because we're in
|
|
|
|
|
a forked process. */
|
|
|
|
|
auto data = makeDownloader()->download(request);
|
2016-06-01 15:11:51 +00:00
|
|
|
|
assert(data.data);
|
|
|
|
|
|
|
|
|
|
return data.data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> data;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (getAttr("outputHashMode") == "flat")
|
|
|
|
|
data = fetch("http://tarballs.nixos.org/" + getAttr("outputHashAlgo") + "/" + getAttr("outputHash"));
|
|
|
|
|
} catch (Error & e) {
|
|
|
|
|
debug(e.what());
|
|
|
|
|
}
|
2015-07-20 02:30:16 +00:00
|
|
|
|
|
2016-06-01 15:11:51 +00:00
|
|
|
|
if (!data) data = fetch(getAttr("url"));
|
2015-10-30 10:27:47 +00:00
|
|
|
|
|
2016-06-01 15:11:51 +00:00
|
|
|
|
Path storePath = getAttr("out");
|
2015-10-30 10:27:47 +00:00
|
|
|
|
|
|
|
|
|
auto unpack = drv.env.find("unpack");
|
|
|
|
|
if (unpack != drv.env.end() && unpack->second == "1") {
|
2016-06-01 15:11:51 +00:00
|
|
|
|
if (string(*data, 0, 6) == string("\xfd" "7zXZ\0", 6))
|
|
|
|
|
data = decompress("xz", *data);
|
|
|
|
|
StringSource source(*data);
|
2015-10-30 10:27:47 +00:00
|
|
|
|
restorePath(storePath, source);
|
|
|
|
|
} else
|
2016-06-01 15:11:51 +00:00
|
|
|
|
writeFile(storePath, *data);
|
2015-07-20 02:30:16 +00:00
|
|
|
|
|
2015-07-23 20:25:04 +00:00
|
|
|
|
auto executable = drv.env.find("executable");
|
2015-07-20 02:30:16 +00:00
|
|
|
|
if (executable != drv.env.end() && executable->second == "1") {
|
2015-10-30 10:27:47 +00:00
|
|
|
|
if (chmod(storePath.c_str(), 0755) == -1)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw SysError(format("making ‘%1%’ executable") % storePath);
|
2015-07-20 02:30:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|