2015-07-20 05:30:16 +03:00
|
|
|
#include "builtins.hh"
|
|
|
|
#include "download.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 {
|
|
|
|
|
|
|
|
void builtinFetchurl(const BasicDerivation & drv)
|
|
|
|
{
|
2016-06-01 18:11:51 +03:00
|
|
|
auto getAttr = [&](const string & name) {
|
|
|
|
auto i = drv.env.find(name);
|
2016-11-25 16:48:27 +02:00
|
|
|
if (i == drv.env.end()) throw Error(format("attribute '%s' missing") % name);
|
2016-06-01 18:11:51 +03: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 17:00:40 +03:00
|
|
|
DownloadRequest request(url);
|
|
|
|
request.verifyTLS = false;
|
2016-06-01 18:11:51 +03:00
|
|
|
|
|
|
|
/* Show a progress indicator, even though stderr is not a tty. */
|
2016-09-14 17:00:40 +03:00
|
|
|
request.showProgress = DownloadRequest::yes;
|
2016-06-01 18:11:51 +03:00
|
|
|
|
2016-09-14 17:00:40 +03:00
|
|
|
/* Note: have to use a fresh downloader here because we're in
|
|
|
|
a forked process. */
|
|
|
|
auto data = makeDownloader()->download(request);
|
2016-06-01 18:11:51 +03: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 05:30:16 +03:00
|
|
|
|
2016-06-01 18:11:51 +03:00
|
|
|
if (!data) data = fetch(getAttr("url"));
|
2015-10-30 12:27:47 +02:00
|
|
|
|
2016-06-01 18:11:51 +03:00
|
|
|
Path storePath = getAttr("out");
|
2015-10-30 12:27:47 +02:00
|
|
|
|
|
|
|
auto unpack = drv.env.find("unpack");
|
|
|
|
if (unpack != drv.env.end() && unpack->second == "1") {
|
2016-06-01 18:11:51 +03:00
|
|
|
if (string(*data, 0, 6) == string("\xfd" "7zXZ\0", 6))
|
|
|
|
data = decompress("xz", *data);
|
|
|
|
StringSource source(*data);
|
2015-10-30 12:27:47 +02:00
|
|
|
restorePath(storePath, source);
|
|
|
|
} else
|
2016-06-01 18:11:51 +03:00
|
|
|
writeFile(storePath, *data);
|
2015-07-20 05:30:16 +03:00
|
|
|
|
2015-07-23 23:25:04 +03:00
|
|
|
auto executable = drv.env.find("executable");
|
2015-07-20 05:30:16 +03:00
|
|
|
if (executable != drv.env.end() && executable->second == "1") {
|
2015-10-30 12:27:47 +02:00
|
|
|
if (chmod(storePath.c_str(), 0755) == -1)
|
2016-11-25 16:48:27 +02:00
|
|
|
throw SysError(format("making '%1%' executable") % storePath);
|
2015-07-20 05:30:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|