Skip to content

Commit

Permalink
builtins.fetch{url,tarball}: Allow name attribute
Browse files Browse the repository at this point in the history
(cherry picked from commit d52d391)
  • Loading branch information
shlevy committed Aug 15, 2016
1 parent 66151dc commit 7bb4d02
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
if (state.restricted) throw Error(format("‘%1%’ is not allowed in restricted mode") % who);

string url;
string name;

state.forceValue(*args[0]);

Expand All @@ -1665,9 +1666,11 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
state.forceAttrs(*args[0], pos);

for (auto & attr : *args[0]->attrs) {
string name(attr.name);
if (name == "url")
string n(attr.name);
if (n == "url")
url = state.forceStringNoCtx(*attr.value, *attr.pos);
else if (n == "name")
name = state.forceStringNoCtx(*attr.value, *attr.pos);
else
throw EvalError(format("unsupported argument ‘%1%’ to ‘%2%’, at %3%") % attr.name % who % attr.pos);
}
Expand All @@ -1678,7 +1681,7 @@ void fetch(EvalState & state, const Pos & pos, Value * * args, Value & v,
} else
url = state.forceStringNoCtx(*args[0], pos);

Path res = downloadFileCached(url, unpack);
Path res = downloadFileCached(url, unpack, name);
mkString(v, res, PathSet({res}));
}

Expand Down
9 changes: 5 additions & 4 deletions src/libstore/download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ DownloadResult downloadFile(string url, const DownloadOptions & options)
}


Path downloadFileCached(const string & url, bool unpack)
Path downloadFileCached(const string & url, bool unpack, string name)
{
Path cacheDir = getEnv("XDG_CACHE_HOME", getEnv("HOME", "") + "/.cache") + "/nix/tarballs";
createDirs(cacheDir);
Expand Down Expand Up @@ -223,9 +223,10 @@ Path downloadFileCached(const string & url, bool unpack)
storePath = "";
}

string name;
auto p = url.rfind('/');
if (p != string::npos) name = string(url, p + 1);
if (name == "") {
auto p = url.rfind('/');
if (p != string::npos) name = string(url, p + 1);
}

if (!skip) {

Expand Down
2 changes: 1 addition & 1 deletion src/libstore/download.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct DownloadResult

DownloadResult downloadFile(string url, const DownloadOptions & options);

Path downloadFileCached(const string & url, bool unpack);
Path downloadFileCached(const string & url, bool unpack, string name = "");

MakeError(DownloadError, Error)

Expand Down

0 comments on commit 7bb4d02

Please sign in to comment.