2015-07-20 05:30:16 +03:00
|
|
|
#include "builtins.hh"
|
2020-04-07 00:57:28 +03:00
|
|
|
#include "filetransfer.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);
|
|
|
|
}
|
|
|
|
|
2022-02-25 17:00:00 +02:00
|
|
|
auto getAttr = [&](const std::string & name) {
|
2016-06-01 18:11:51 +03:00
|
|
|
auto i = drv.env.find(name);
|
2020-04-22 02:07:07 +03:00
|
|
|
if (i == drv.env.end()) throw Error("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");
|
2022-05-04 08:44:32 +03:00
|
|
|
bool unpack = getOr(drv.env, "unpack", "") == "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)
|
2020-04-22 02:07:07 +03:00
|
|
|
throw SysError("making '%1%' executable", storePath);
|
2018-03-28 14:32:44 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-07 02:13:14 +03:00
|
|
|
/* Try the hashed mirrors first. */
|
|
|
|
if (getAttr("outputHashMode") == "flat")
|
|
|
|
for (auto hashedMirror : settings.hashedMirrors.get())
|
|
|
|
try {
|
|
|
|
if (!hasSuffix(hashedMirror, "/")) hashedMirror += '/';
|
2020-08-07 03:00:33 +03:00
|
|
|
std::optional<HashType> ht = parseHashTypeOpt(getAttr("outputHashAlgo"));
|
|
|
|
Hash h = newHashAllowEmpty(getAttr("outputHash"), ht);
|
2023-10-13 04:48:15 +03:00
|
|
|
fetch(hashedMirror + printHashType(h.type) + "/" + h.to_string(HashFormat::Base16, false));
|
2020-08-07 02:13:14 +03:00
|
|
|
return;
|
|
|
|
} catch (Error & e) {
|
|
|
|
debug(e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise try the specified URL. */
|
2018-03-28 14:32:44 +03:00
|
|
|
fetch(mainUrl);
|
2015-07-20 05:30:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|