2015-07-20 04:30:16 +02:00
|
|
|
|
#include "builtins.hh"
|
|
|
|
|
#include "download.hh"
|
2015-10-30 11:27:47 +01:00
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "archive.hh"
|
2015-10-30 12:33:40 +01:00
|
|
|
|
#include "compression.hh"
|
2015-07-20 04:30:16 +02:00
|
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
|
|
void builtinFetchurl(const BasicDerivation & drv)
|
|
|
|
|
{
|
2016-06-01 17:11:51 +02:00
|
|
|
|
auto getAttr = [&](const string & name) {
|
|
|
|
|
auto i = drv.env.find(name);
|
|
|
|
|
if (i == drv.env.end()) throw Error(format("attribute ‘%s’ missing") % name);
|
|
|
|
|
return i->second;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto fetch = [&](const string & url) {
|
|
|
|
|
/* No need to do TLS verification, because we check the hash of
|
|
|
|
|
the result anyway. */
|
|
|
|
|
DownloadOptions options;
|
|
|
|
|
options.verifyTLS = false;
|
|
|
|
|
|
|
|
|
|
/* Show a progress indicator, even though stderr is not a tty. */
|
|
|
|
|
options.showProgress = DownloadOptions::yes;
|
|
|
|
|
|
|
|
|
|
auto data = makeDownloader()->download(url, options);
|
|
|
|
|
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 04:30:16 +02:00
|
|
|
|
|
2016-06-01 17:11:51 +02:00
|
|
|
|
if (!data) data = fetch(getAttr("url"));
|
2015-10-30 11:27:47 +01:00
|
|
|
|
|
2016-06-01 17:11:51 +02:00
|
|
|
|
Path storePath = getAttr("out");
|
2015-10-30 11:27:47 +01:00
|
|
|
|
|
|
|
|
|
auto unpack = drv.env.find("unpack");
|
|
|
|
|
if (unpack != drv.env.end() && unpack->second == "1") {
|
2016-06-01 17:11:51 +02:00
|
|
|
|
if (string(*data, 0, 6) == string("\xfd" "7zXZ\0", 6))
|
|
|
|
|
data = decompress("xz", *data);
|
|
|
|
|
StringSource source(*data);
|
2015-10-30 11:27:47 +01:00
|
|
|
|
restorePath(storePath, source);
|
|
|
|
|
} else
|
2016-06-01 17:11:51 +02:00
|
|
|
|
writeFile(storePath, *data);
|
2015-07-20 04:30:16 +02:00
|
|
|
|
|
2015-07-23 22:25:04 +02:00
|
|
|
|
auto executable = drv.env.find("executable");
|
2015-07-20 04:30:16 +02:00
|
|
|
|
if (executable != drv.env.end() && executable->second == "1") {
|
2015-10-30 11:27:47 +01:00
|
|
|
|
if (chmod(storePath.c_str(), 0755) == -1)
|
|
|
|
|
throw SysError(format("making ‘%1%’ executable") % storePath);
|
2015-07-20 04:30:16 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|