2015-10-01 14:47:43 +00:00
|
|
|
|
#include "hash.hh"
|
|
|
|
|
#include "shared.hh"
|
|
|
|
|
#include "download.hh"
|
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
#include "eval.hh"
|
|
|
|
|
#include "eval-inline.hh"
|
|
|
|
|
#include "common-opts.hh"
|
2015-10-01 16:07:56 +00:00
|
|
|
|
#include "attr-path.hh"
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
|
|
|
|
|
|
2016-11-25 23:37:43 +00:00
|
|
|
|
/* If ‘uri’ starts with ‘mirror://’, then resolve it using the list of
|
2015-10-01 14:47:43 +00:00
|
|
|
|
mirrors defined in Nixpkgs. */
|
|
|
|
|
string resolveMirrorUri(EvalState & state, string uri)
|
|
|
|
|
{
|
|
|
|
|
if (string(uri, 0, 9) != "mirror://") return uri;
|
|
|
|
|
|
|
|
|
|
string s(uri, 9);
|
|
|
|
|
auto p = s.find('/');
|
|
|
|
|
if (p == string::npos) throw Error("invalid mirror URI");
|
|
|
|
|
string mirrorName(s, 0, p);
|
|
|
|
|
|
|
|
|
|
Value vMirrors;
|
|
|
|
|
state.eval(state.parseExprFromString("import <nixpkgs/pkgs/build-support/fetchurl/mirrors.nix>", "."), vMirrors);
|
|
|
|
|
state.forceAttrs(vMirrors);
|
|
|
|
|
|
|
|
|
|
auto mirrorList = vMirrors.attrs->find(state.symbols.create(mirrorName));
|
|
|
|
|
if (mirrorList == vMirrors.attrs->end())
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("unknown mirror name ‘%1%’") % mirrorName);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
state.forceList(*mirrorList->value);
|
|
|
|
|
|
|
|
|
|
if (mirrorList->value->listSize() < 1)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("mirror URI ‘%1%’ did not expand to anything") % uri);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
string mirror = state.forceString(*mirrorList->value->listElems()[0]);
|
|
|
|
|
return mirror + (hasSuffix(mirror, "/") ? "" : "/") + string(s, p + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
|
{
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
|
initNix();
|
|
|
|
|
initGC();
|
|
|
|
|
|
|
|
|
|
HashType ht = htSHA256;
|
|
|
|
|
std::vector<string> args;
|
|
|
|
|
Strings searchPath;
|
2015-10-01 14:53:07 +00:00
|
|
|
|
bool printPath = getEnv("PRINT_PATH") != "";
|
2015-10-01 16:07:56 +00:00
|
|
|
|
bool fromExpr = false;
|
|
|
|
|
string attrPath;
|
|
|
|
|
std::map<string, string> autoArgs_;
|
2015-10-07 12:40:10 +00:00
|
|
|
|
bool unpack = false;
|
2015-10-07 12:47:39 +00:00
|
|
|
|
string name;
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
|
|
|
|
|
if (*arg == "--help")
|
|
|
|
|
showManPage("nix-prefetch-url");
|
|
|
|
|
else if (*arg == "--version")
|
|
|
|
|
printVersion("nix-prefetch-url");
|
|
|
|
|
else if (*arg == "--type") {
|
|
|
|
|
string s = getArg(*arg, arg, end);
|
|
|
|
|
ht = parseHashType(s);
|
|
|
|
|
if (ht == htUnknown)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw UsageError(format("unknown hash type ‘%1%’") % s);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
}
|
2015-10-01 14:53:07 +00:00
|
|
|
|
else if (*arg == "--print-path")
|
|
|
|
|
printPath = true;
|
2015-10-01 16:07:56 +00:00
|
|
|
|
else if (*arg == "--attr" || *arg == "-A") {
|
|
|
|
|
fromExpr = true;
|
|
|
|
|
attrPath = getArg(*arg, arg, end);
|
|
|
|
|
}
|
2015-10-07 12:40:10 +00:00
|
|
|
|
else if (*arg == "--unpack")
|
|
|
|
|
unpack = true;
|
2015-10-07 12:55:33 +00:00
|
|
|
|
else if (*arg == "--name")
|
|
|
|
|
name = getArg(*arg, arg, end);
|
2015-10-01 16:07:56 +00:00
|
|
|
|
else if (parseAutoArgs(arg, end, autoArgs_))
|
|
|
|
|
;
|
2015-10-01 14:47:43 +00:00
|
|
|
|
else if (parseSearchPathArg(arg, end, searchPath))
|
|
|
|
|
;
|
|
|
|
|
else if (*arg != "" && arg->at(0) == '-')
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
args.push_back(*arg);
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-01 16:07:56 +00:00
|
|
|
|
if (args.size() > 2)
|
|
|
|
|
throw UsageError("too many arguments");
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
|
auto store = openStore();
|
|
|
|
|
EvalState state(searchPath, store);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
2015-10-01 16:07:56 +00:00
|
|
|
|
Bindings & autoArgs(*evalAutoArgs(state, autoArgs_));
|
|
|
|
|
|
|
|
|
|
/* If -A is given, get the URI from the specified Nix
|
|
|
|
|
expression. */
|
|
|
|
|
string uri;
|
|
|
|
|
if (!fromExpr) {
|
|
|
|
|
if (args.empty())
|
|
|
|
|
throw UsageError("you must specify a URI");
|
|
|
|
|
uri = args[0];
|
|
|
|
|
} else {
|
|
|
|
|
Path path = resolveExprPath(lookupFileArg(state, args.empty() ? "." : args[0]));
|
|
|
|
|
Value vRoot;
|
|
|
|
|
state.evalFile(path, vRoot);
|
|
|
|
|
Value & v(*findAlongAttrPath(state, attrPath, autoArgs, vRoot));
|
|
|
|
|
state.forceAttrs(v);
|
2015-10-07 12:40:10 +00:00
|
|
|
|
|
|
|
|
|
/* Extract the URI. */
|
|
|
|
|
auto attr = v.attrs->find(state.symbols.create("urls"));
|
|
|
|
|
if (attr == v.attrs->end())
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error("attribute set does not contain a ‘urls’ attribute");
|
2015-10-07 12:40:10 +00:00
|
|
|
|
state.forceList(*attr->value);
|
|
|
|
|
if (attr->value->listSize() < 1)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error("‘urls’ list is empty");
|
2015-10-07 12:40:10 +00:00
|
|
|
|
uri = state.forceString(*attr->value->listElems()[0]);
|
|
|
|
|
|
|
|
|
|
/* Extract the hash mode. */
|
|
|
|
|
attr = v.attrs->find(state.symbols.create("outputHashMode"));
|
|
|
|
|
if (attr == v.attrs->end())
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printInfo("warning: this does not look like a fetchurl call");
|
2015-10-07 12:40:10 +00:00
|
|
|
|
else
|
|
|
|
|
unpack = state.forceString(*attr->value) == "recursive";
|
2015-10-07 12:47:39 +00:00
|
|
|
|
|
|
|
|
|
/* Extract the name. */
|
2015-10-07 12:55:33 +00:00
|
|
|
|
if (name.empty()) {
|
|
|
|
|
attr = v.attrs->find(state.symbols.create("name"));
|
|
|
|
|
if (attr != v.attrs->end())
|
|
|
|
|
name = state.forceString(*attr->value);
|
|
|
|
|
}
|
2015-10-01 16:07:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 14:47:43 +00:00
|
|
|
|
/* Figure out a name in the Nix store. */
|
2015-10-07 12:47:39 +00:00
|
|
|
|
if (name.empty())
|
|
|
|
|
name = baseNameOf(uri);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
if (name.empty())
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("cannot figure out file name for ‘%1%’") % uri);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
/* If an expected hash is given, the file may already exist in
|
|
|
|
|
the store. */
|
|
|
|
|
Hash hash, expectedHash(ht);
|
|
|
|
|
Path storePath;
|
|
|
|
|
if (args.size() == 2) {
|
|
|
|
|
expectedHash = parseHash16or32(ht, args[1]);
|
2016-07-26 19:25:52 +00:00
|
|
|
|
storePath = store->makeFixedOutputPath(unpack, expectedHash, name);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
if (store->isValidPath(storePath))
|
|
|
|
|
hash = expectedHash;
|
|
|
|
|
else
|
|
|
|
|
storePath.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (storePath.empty()) {
|
|
|
|
|
|
|
|
|
|
auto actualUri = resolveMirrorUri(state, uri);
|
|
|
|
|
|
|
|
|
|
/* Download the file. */
|
2016-09-14 14:00:40 +00:00
|
|
|
|
auto result = getDownloader()->download(DownloadRequest(actualUri));
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
AutoDelete tmpDir(createTempDir(), true);
|
|
|
|
|
Path tmpFile = (Path) tmpDir + "/tmp";
|
2016-04-15 13:11:34 +00:00
|
|
|
|
writeFile(tmpFile, *result.data);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
2015-10-07 12:40:10 +00:00
|
|
|
|
/* Optionally unpack the file. */
|
|
|
|
|
if (unpack) {
|
2016-09-21 14:11:01 +00:00
|
|
|
|
printInfo("unpacking...");
|
2015-10-07 12:40:10 +00:00
|
|
|
|
Path unpacked = (Path) tmpDir + "/unpacked";
|
|
|
|
|
createDirs(unpacked);
|
|
|
|
|
if (hasSuffix(baseNameOf(uri), ".zip"))
|
2017-03-15 13:40:47 +00:00
|
|
|
|
runProgram("unzip", true, {"-qq", tmpFile, "-d", unpacked});
|
2015-10-07 12:40:10 +00:00
|
|
|
|
else
|
|
|
|
|
// FIXME: this requires GNU tar for decompression.
|
2017-03-15 13:40:47 +00:00
|
|
|
|
runProgram("tar", true, {"xf", tmpFile, "-C", unpacked});
|
2015-10-07 12:40:10 +00:00
|
|
|
|
|
|
|
|
|
/* If the archive unpacks to a single file/directory, then use
|
|
|
|
|
that as the top-level. */
|
|
|
|
|
auto entries = readDirectory(unpacked);
|
|
|
|
|
if (entries.size() == 1)
|
|
|
|
|
tmpFile = unpacked + "/" + entries[0].name;
|
|
|
|
|
else
|
|
|
|
|
tmpFile = unpacked;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 14:47:43 +00:00
|
|
|
|
/* FIXME: inefficient; addToStore() will also hash
|
|
|
|
|
this. */
|
2016-04-15 13:11:34 +00:00
|
|
|
|
hash = unpack ? hashPath(ht, tmpFile).first : hashString(ht, *result.data);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
if (expectedHash != Hash(ht) && expectedHash != hash)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
throw Error(format("hash mismatch for ‘%1%’") % uri);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
2015-10-07 12:40:10 +00:00
|
|
|
|
/* Copy the file to the Nix store. FIXME: if RemoteStore
|
|
|
|
|
implemented addToStoreFromDump() and downloadFile()
|
|
|
|
|
supported a sink, we could stream the download directly
|
|
|
|
|
into the Nix store. */
|
|
|
|
|
storePath = store->addToStore(name, tmpFile, unpack, ht);
|
|
|
|
|
|
2016-07-26 19:25:52 +00:00
|
|
|
|
assert(storePath == store->makeFixedOutputPath(unpack, hash, name));
|
2015-10-01 14:47:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-01 14:53:07 +00:00
|
|
|
|
if (!printPath)
|
2016-11-25 23:37:43 +00:00
|
|
|
|
printInfo(format("path is ‘%1%’") % storePath);
|
2015-10-01 14:47:43 +00:00
|
|
|
|
|
|
|
|
|
std::cout << printHash16or32(hash) << std::endl;
|
2015-10-01 14:53:07 +00:00
|
|
|
|
if (printPath)
|
2015-10-01 14:47:43 +00:00
|
|
|
|
std::cout << storePath << std::endl;
|
|
|
|
|
});
|
|
|
|
|
}
|