builtins.fetch{url,tarball}: Allow name attribute

This commit is contained in:
Shea Levy 2016-08-15 07:37:11 -04:00
parent eff80419c7
commit d52d391164
3 changed files with 13 additions and 9 deletions

View file

@ -1682,6 +1682,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
{ {
string url; string url;
Hash expectedHash; Hash expectedHash;
string name;
state.forceValue(*args[0]); state.forceValue(*args[0]);
@ -1690,11 +1691,13 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
state.forceAttrs(*args[0], pos); state.forceAttrs(*args[0], pos);
for (auto & attr : *args[0]->attrs) { for (auto & attr : *args[0]->attrs) {
string name(attr.name); string n(attr.name);
if (name == "url") if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos); url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (name == "sha256") else if (n == "sha256")
expectedHash = parseHash16or32(htSHA256, state.forceStringNoCtx(*attr.value, *attr.pos)); expectedHash = parseHash16or32(htSHA256, state.forceStringNoCtx(*attr.value, *attr.pos));
else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else else
throw EvalError(format("unsupported argument %1% to %2%, at %3%") % attr.name % who % attr.pos); throw EvalError(format("unsupported argument %1% to %2%, at %3%") % attr.name % who % attr.pos);
} }
@ -1708,7 +1711,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
if (state.restricted && !expectedHash) if (state.restricted && !expectedHash)
throw Error(format("%1% is not allowed in restricted mode") % who); throw Error(format("%1% is not allowed in restricted mode") % who);
Path res = makeDownloader()->downloadCached(state.store, url, unpack, expectedHash); Path res = makeDownloader()->downloadCached(state.store, url, unpack, name, expectedHash);
mkString(v, res, PathSet({res})); mkString(v, res, PathSet({res}));
} }

View file

@ -243,13 +243,14 @@ ref<Downloader> makeDownloader()
return make_ref<CurlDownloader>(); return make_ref<CurlDownloader>();
} }
Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, const Hash & expectedHash) Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, string name, const Hash & expectedHash)
{ {
auto url = resolveUri(url_); auto url = resolveUri(url_);
string name; if (name == "") {
auto p = url.rfind('/'); auto p = url.rfind('/');
if (p != string::npos) name = string(url, p + 1); if (p != string::npos) name = string(url, p + 1);
}
Path expectedStorePath; Path expectedStorePath;
if (expectedHash) { if (expectedHash) {

View file

@ -29,7 +29,7 @@ struct Downloader
{ {
virtual DownloadResult download(string url, const DownloadOptions & options) = 0; virtual DownloadResult download(string url, const DownloadOptions & options) = 0;
Path downloadCached(ref<Store> store, const string & url, bool unpack, Path downloadCached(ref<Store> store, const string & url, bool unpack, string name = "",
const Hash & expectedHash = Hash()); const Hash & expectedHash = Hash());
enum Error { NotFound, Forbidden, Misc, Transient }; enum Error { NotFound, Forbidden, Misc, Transient };