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 {
|
|
|
|
|
2017-02-16 15:42:49 +01:00
|
|
|
void builtinFetchurl(const BasicDerivation & drv, const std::string & netrcData)
|
2015-07-20 04:30:16 +02:00
|
|
|
{
|
2017-02-16 15:42:49 +01: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 17:11:51 +02:00
|
|
|
auto getAttr = [&](const string & name) {
|
|
|
|
auto i = drv.env.find(name);
|
2017-07-30 13:27:57 +02:00
|
|
|
if (i == drv.env.end()) throw Error(format("attribute '%s' missing") % name);
|
2016-06-01 17:11:51 +02: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 16:00:40 +02:00
|
|
|
DownloadRequest request(url);
|
|
|
|
request.verifyTLS = false;
|
2016-06-01 17:11:51 +02:00
|
|
|
|
2016-09-14 16:00:40 +02:00
|
|
|
/* Note: have to use a fresh downloader here because we're in
|
|
|
|
a forked process. */
|
|
|
|
auto data = makeDownloader()->download(request);
|
2016-06-01 17:11:51 +02:00
|
|
|
assert(data.data);
|
|
|
|
|
|
|
|
return data.data;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> data;
|
|
|
|
|
2017-07-17 13:07:08 +02:00
|
|
|
if (getAttr("outputHashMode") == "flat")
|
|
|
|
for (auto hashedMirror : settings.hashedMirrors.get())
|
|
|
|
try {
|
|
|
|
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
|
2017-07-17 13:13:18 +02:00
|
|
|
auto ht = parseHashType(getAttr("outputHashAlgo"));
|
|
|
|
data = fetch(hashedMirror + printHashType(ht) + "/" + Hash(getAttr("outputHash"), ht).to_string(Base16, false));
|
2017-07-17 13:07:08 +02:00
|
|
|
break;
|
|
|
|
} 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)
|
2017-07-30 13:27:57 +02:00
|
|
|
throw SysError(format("making '%1%' executable") % storePath);
|
2015-07-20 04:30:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|