2006-09-05 00:06:23 +03:00
|
|
|
#include "misc.hh"
|
2006-11-30 19:43:04 +02:00
|
|
|
#include "store-api.hh"
|
2008-08-02 15:54:35 +03:00
|
|
|
#include "local-store.hh"
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
|
|
|
namespace nix {
|
2004-06-18 21:09:32 +03:00
|
|
|
|
|
|
|
|
2011-09-01 00:11:50 +03:00
|
|
|
Derivation derivationFromPath(StoreAPI & store, const Path & drvPath)
|
2004-06-18 21:09:32 +03:00
|
|
|
{
|
2005-01-19 13:16:11 +02:00
|
|
|
assertStorePath(drvPath);
|
2011-09-01 00:11:50 +03:00
|
|
|
store.ensurePath(drvPath);
|
2010-04-19 16:46:58 +03:00
|
|
|
return parseDerivation(readFile(drvPath));
|
2004-06-18 21:09:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-01 00:11:50 +03:00
|
|
|
void computeFSClosure(StoreAPI & store, const Path & storePath,
|
2010-01-25 19:18:44 +02:00
|
|
|
PathSet & paths, bool flipDirection, bool includeOutputs)
|
2005-01-19 13:16:11 +02:00
|
|
|
{
|
|
|
|
if (paths.find(storePath) != paths.end()) return;
|
|
|
|
paths.insert(storePath);
|
|
|
|
|
|
|
|
PathSet references;
|
2005-01-25 13:18:03 +02:00
|
|
|
if (flipDirection)
|
2011-09-01 00:11:50 +03:00
|
|
|
store.queryReferrers(storePath, references);
|
2005-01-25 13:18:03 +02:00
|
|
|
else
|
2011-09-01 00:11:50 +03:00
|
|
|
store.queryReferences(storePath, references);
|
2005-01-19 13:16:11 +02:00
|
|
|
|
2010-01-25 19:18:44 +02:00
|
|
|
if (includeOutputs && isDerivation(storePath)) {
|
2011-09-01 00:11:50 +03:00
|
|
|
PathSet outputs = store.queryDerivationOutputs(storePath);
|
2010-02-22 14:44:36 +02:00
|
|
|
foreach (PathSet::iterator, i, outputs)
|
2011-09-01 00:11:50 +03:00
|
|
|
if (store.isValidPath(*i))
|
|
|
|
computeFSClosure(store, *i, paths, flipDirection, true);
|
2010-01-25 19:18:44 +02:00
|
|
|
}
|
|
|
|
|
2009-04-21 14:52:16 +03:00
|
|
|
foreach (PathSet::iterator, i, references)
|
2011-09-01 00:11:50 +03:00
|
|
|
computeFSClosure(store, *i, paths, flipDirection, includeOutputs);
|
2005-01-19 13:16:11 +02:00
|
|
|
}
|
2005-02-14 19:35:10 +02:00
|
|
|
|
|
|
|
|
2006-03-06 13:21:15 +02:00
|
|
|
Path findOutput(const Derivation & drv, string id)
|
2005-02-14 19:35:10 +02:00
|
|
|
{
|
2009-04-21 14:52:16 +03:00
|
|
|
foreach (DerivationOutputs::const_iterator, i, drv.outputs)
|
2005-02-14 19:35:10 +02:00
|
|
|
if (i->first == id) return i->second.path;
|
|
|
|
throw Error(format("derivation has no output `%1%'") % id);
|
|
|
|
}
|
2006-03-06 13:21:15 +02:00
|
|
|
|
|
|
|
|
2011-09-01 00:11:50 +03:00
|
|
|
void queryMissing(StoreAPI & store, const PathSet & targets,
|
2008-08-04 15:29:04 +03:00
|
|
|
PathSet & willBuild, PathSet & willSubstitute, PathSet & unknown,
|
2010-11-17 16:31:42 +02:00
|
|
|
unsigned long long & downloadSize, unsigned long long & narSize)
|
2006-03-06 13:21:15 +02:00
|
|
|
{
|
2010-11-17 16:31:42 +02:00
|
|
|
downloadSize = narSize = 0;
|
2008-08-04 15:29:04 +03:00
|
|
|
|
2006-03-06 13:21:15 +02:00
|
|
|
PathSet todo(targets.begin(), targets.end()), done;
|
|
|
|
|
|
|
|
while (!todo.empty()) {
|
|
|
|
Path p = *(todo.begin());
|
|
|
|
todo.erase(p);
|
|
|
|
if (done.find(p) != done.end()) continue;
|
|
|
|
done.insert(p);
|
|
|
|
|
|
|
|
if (isDerivation(p)) {
|
2011-09-01 00:11:50 +03:00
|
|
|
if (!store.isValidPath(p)) {
|
2008-08-04 14:44:50 +03:00
|
|
|
unknown.insert(p);
|
|
|
|
continue;
|
|
|
|
}
|
2011-09-01 00:11:50 +03:00
|
|
|
Derivation drv = derivationFromPath(store, p);
|
2006-03-06 13:21:15 +02:00
|
|
|
|
|
|
|
bool mustBuild = false;
|
2009-04-21 14:52:16 +03:00
|
|
|
foreach (DerivationOutputs::iterator, i, drv.outputs)
|
2011-09-01 00:11:50 +03:00
|
|
|
if (!store.isValidPath(i->second.path) && !store.hasSubstitutes(i->second.path))
|
2006-03-06 13:21:15 +02:00
|
|
|
mustBuild = true;
|
|
|
|
|
|
|
|
if (mustBuild) {
|
|
|
|
willBuild.insert(p);
|
|
|
|
todo.insert(drv.inputSrcs.begin(), drv.inputSrcs.end());
|
2009-04-21 14:52:16 +03:00
|
|
|
foreach (DerivationInputs::iterator, i, drv.inputDrvs)
|
2006-03-06 13:21:15 +02:00
|
|
|
todo.insert(i->first);
|
|
|
|
} else
|
2009-04-21 14:52:16 +03:00
|
|
|
foreach (DerivationOutputs::iterator, i, drv.outputs)
|
2006-03-06 13:21:15 +02:00
|
|
|
todo.insert(i->second.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2011-09-01 00:11:50 +03:00
|
|
|
if (store.isValidPath(p)) continue;
|
2008-08-02 15:54:35 +03:00
|
|
|
SubstitutablePathInfo info;
|
2011-09-01 00:11:50 +03:00
|
|
|
if (store.querySubstitutablePathInfo(p, info)) {
|
2006-03-06 13:21:15 +02:00
|
|
|
willSubstitute.insert(p);
|
2008-08-04 15:29:04 +03:00
|
|
|
downloadSize += info.downloadSize;
|
2010-11-17 16:31:42 +02:00
|
|
|
narSize += info.narSize;
|
2008-08-02 15:54:35 +03:00
|
|
|
todo.insert(info.references.begin(), info.references.end());
|
2008-08-04 14:44:50 +03:00
|
|
|
} else
|
|
|
|
unknown.insert(p);
|
2006-03-06 13:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-09-05 00:06:23 +03:00
|
|
|
|
|
|
|
|
2011-12-30 17:02:50 +02:00
|
|
|
static void dfsVisit(StoreAPI & store, const PathSet & paths,
|
|
|
|
const Path & path, PathSet & visited, Paths & sorted,
|
|
|
|
PathSet & parents)
|
|
|
|
{
|
|
|
|
if (parents.find(path) != parents.end())
|
|
|
|
throw BuildError(format("cycle detected in the references of `%1%'") % path);
|
|
|
|
|
|
|
|
if (visited.find(path) != visited.end()) return;
|
|
|
|
visited.insert(path);
|
2011-12-30 19:13:25 +02:00
|
|
|
parents.insert(path);
|
2011-12-30 17:02:50 +02:00
|
|
|
|
|
|
|
PathSet references;
|
|
|
|
if (store.isValidPath(path))
|
|
|
|
store.queryReferences(path, references);
|
|
|
|
|
|
|
|
foreach (PathSet::iterator, i, references)
|
|
|
|
/* Don't traverse into paths that don't exist. That can
|
|
|
|
happen due to substitutes for non-existent paths. */
|
|
|
|
if (*i != path && paths.find(*i) != paths.end())
|
|
|
|
dfsVisit(store, paths, *i, visited, sorted, parents);
|
|
|
|
|
|
|
|
sorted.push_front(path);
|
|
|
|
parents.erase(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
|
|
|
|
{
|
|
|
|
Paths sorted;
|
|
|
|
PathSet visited, parents;
|
|
|
|
foreach (PathSet::const_iterator, i, paths)
|
|
|
|
dfsVisit(store, paths, *i, visited, sorted, parents);
|
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|