2003-03-13 18:28:32 +02:00
|
|
|
#include <iostream>
|
2003-03-14 18:43:14 +02:00
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
#include "globals.hh"
|
2005-01-19 18:39:47 +02:00
|
|
|
#include "build.hh"
|
2004-08-25 14:43:49 +03:00
|
|
|
#include "gc.hh"
|
2003-06-20 13:40:25 +03:00
|
|
|
#include "archive.hh"
|
2003-07-04 18:42:03 +03:00
|
|
|
#include "shared.hh"
|
2003-09-03 14:20:18 +03:00
|
|
|
#include "dotgraph.hh"
|
2003-11-18 14:06:07 +02:00
|
|
|
#include "help.txt.hh"
|
2003-03-24 13:50:20 +02:00
|
|
|
|
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
typedef void (* Operation) (Strings opFlags, Strings opArgs);
|
2003-04-02 18:34:05 +03:00
|
|
|
|
|
|
|
|
2003-12-01 17:55:05 +02:00
|
|
|
void printHelp()
|
2003-07-28 15:19:23 +03:00
|
|
|
{
|
2003-11-18 14:06:07 +02:00
|
|
|
cout << string((char *) helpText, sizeof helpText);
|
2003-07-28 15:19:23 +03:00
|
|
|
}
|
2003-06-20 17:11:31 +03:00
|
|
|
|
|
|
|
|
2005-02-01 14:36:25 +02:00
|
|
|
static Path gcRoot;
|
|
|
|
static int rootNr = 0;
|
2005-02-01 15:48:46 +02:00
|
|
|
static bool indirectRoot = false;
|
2005-02-01 14:36:25 +02:00
|
|
|
|
|
|
|
|
2005-02-07 16:32:44 +02:00
|
|
|
static Path fixPath(Path path)
|
2005-02-01 14:36:25 +02:00
|
|
|
{
|
2005-02-17 15:55:18 +02:00
|
|
|
SwitchToOriginalUser sw;
|
2005-02-01 15:48:46 +02:00
|
|
|
path = absPath(path);
|
2005-02-07 16:32:44 +02:00
|
|
|
while (!isInStore(path)) {
|
|
|
|
if (!isLink(path)) break;
|
2005-02-01 14:36:25 +02:00
|
|
|
string target = readLink(path);
|
2005-02-01 17:05:32 +02:00
|
|
|
path = absPath(target, dirOf(path));
|
2005-02-01 14:36:25 +02:00
|
|
|
}
|
2005-02-07 16:32:44 +02:00
|
|
|
return toStorePath(path);
|
2005-02-01 14:36:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-25 12:55:33 +02:00
|
|
|
/* Realisation the given path. For a derivation that means build it;
|
|
|
|
for other paths it means ensure their validity. */
|
|
|
|
static Path realisePath(const Path & path)
|
|
|
|
{
|
|
|
|
if (isDerivation(path)) {
|
|
|
|
PathSet paths;
|
|
|
|
paths.insert(path);
|
|
|
|
buildDerivations(paths);
|
2005-02-01 14:36:25 +02:00
|
|
|
Path outPath = findOutput(derivationFromPath(path), "out");
|
|
|
|
|
|
|
|
if (gcRoot == "")
|
|
|
|
printGCWarning();
|
|
|
|
else
|
2005-02-01 15:48:46 +02:00
|
|
|
outPath = addPermRoot(outPath,
|
|
|
|
makeRootName(gcRoot, rootNr),
|
|
|
|
indirectRoot);
|
2005-02-01 14:36:25 +02:00
|
|
|
|
|
|
|
return outPath;
|
2005-01-25 12:55:33 +02:00
|
|
|
} else {
|
|
|
|
ensurePath(path);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Realise the given paths. */
|
|
|
|
static void opRealise(Strings opFlags, Strings opArgs)
|
2003-03-14 18:43:14 +02:00
|
|
|
{
|
2003-06-18 00:12:58 +03:00
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
2003-03-13 18:28:32 +02:00
|
|
|
|
2005-02-01 14:36:25 +02:00
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
2005-02-07 16:32:44 +02:00
|
|
|
*i = fixPath(*i);
|
2005-02-01 14:36:25 +02:00
|
|
|
|
2005-01-25 12:55:33 +02:00
|
|
|
if (opArgs.size() > 1) {
|
|
|
|
PathSet drvPaths;
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
if (isDerivation(*i))
|
|
|
|
drvPaths.insert(*i);
|
|
|
|
buildDerivations(drvPaths);
|
|
|
|
}
|
2005-01-19 17:02:02 +02:00
|
|
|
|
2003-10-08 18:06:59 +03:00
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
2005-01-25 12:55:33 +02:00
|
|
|
cout << format("%1%\n") % realisePath(*i);
|
2003-04-02 18:34:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-19 13:16:11 +02:00
|
|
|
/* Add files to the Nix values directory and print the resulting
|
2003-07-08 13:00:46 +03:00
|
|
|
paths. */
|
2003-06-18 00:12:58 +03:00
|
|
|
static void opAdd(Strings opFlags, Strings opArgs)
|
2003-04-02 18:34:05 +03:00
|
|
|
{
|
2003-06-18 00:12:58 +03:00
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
2003-04-02 18:34:05 +03:00
|
|
|
|
2003-10-08 18:06:59 +03:00
|
|
|
for (Strings::iterator i = opArgs.begin(); i != opArgs.end(); i++)
|
|
|
|
cout << format("%1%\n") % addToStore(*i);
|
2003-03-13 18:28:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-25 23:28:25 +02:00
|
|
|
/* Place in `paths' the set of paths that are required to `realise'
|
|
|
|
the given store path, i.e., all paths necessary for valid
|
|
|
|
deployment of the path. For a derivation, this is the union of
|
|
|
|
requisites of the inputs, plus the derivation; for other store
|
|
|
|
paths, it is the set of paths in the FS closure of the path. If
|
|
|
|
`includeOutputs' is true, include the requisites of the output
|
|
|
|
paths of derivations as well.
|
|
|
|
|
|
|
|
Note that this function can be used to implement three different
|
|
|
|
deployment policies:
|
|
|
|
|
|
|
|
- Source deployment (when called on a derivation).
|
|
|
|
- Binary deployment (when called on an output path).
|
|
|
|
- Source/binary deployment (when called on a derivation with
|
|
|
|
`includeOutputs' set to true).
|
|
|
|
*/
|
|
|
|
static void storePathRequisites(const Path & storePath,
|
|
|
|
bool includeOutputs, PathSet & paths)
|
|
|
|
{
|
|
|
|
computeFSClosure(storePath, paths);
|
|
|
|
|
|
|
|
if (includeOutputs) {
|
|
|
|
for (PathSet::iterator i = paths.begin();
|
|
|
|
i != paths.end(); ++i)
|
|
|
|
if (isDerivation(*i)) {
|
|
|
|
Derivation drv = derivationFromPath(*i);
|
|
|
|
for (DerivationOutputs::iterator j = drv.outputs.begin();
|
|
|
|
j != drv.outputs.end(); ++j)
|
|
|
|
if (isValidPath(j->second.path))
|
|
|
|
computeFSClosure(j->second.path, paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-25 12:55:33 +02:00
|
|
|
static Path maybeUseOutput(const Path & storePath, bool useOutput, bool forceRealise)
|
2003-07-29 13:43:12 +03:00
|
|
|
{
|
2005-01-25 12:55:33 +02:00
|
|
|
if (forceRealise) realisePath(storePath);
|
2005-01-19 16:36:00 +02:00
|
|
|
if (useOutput && isDerivation(storePath)) {
|
|
|
|
Derivation drv = derivationFromPath(storePath);
|
|
|
|
return findOutput(drv, "out");
|
|
|
|
}
|
|
|
|
else return storePath;
|
2003-07-29 13:43:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-19 18:59:56 +02:00
|
|
|
static void printPathSet(const PathSet & paths)
|
|
|
|
{
|
|
|
|
for (PathSet::iterator i = paths.begin();
|
|
|
|
i != paths.end(); i++)
|
|
|
|
cout << format("%s\n") % *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-17 17:57:46 +02:00
|
|
|
/* Some code to print a tree representation of a derivation dependency
|
|
|
|
graph. Topological sorting is used to keep the tree relatively
|
|
|
|
flat. */
|
|
|
|
|
|
|
|
const string treeConn = "+---";
|
|
|
|
const string treeLine = "| ";
|
|
|
|
const string treeNull = " ";
|
|
|
|
|
|
|
|
|
|
|
|
static void dfsVisit(const PathSet & paths, const Path & path,
|
|
|
|
PathSet & visited, Paths & sorted)
|
|
|
|
{
|
|
|
|
if (visited.find(path) != visited.end()) return;
|
|
|
|
visited.insert(path);
|
|
|
|
|
|
|
|
PathSet closure;
|
|
|
|
computeFSClosure(path, closure);
|
|
|
|
|
|
|
|
for (PathSet::iterator i = closure.begin();
|
|
|
|
i != closure.end(); ++i)
|
|
|
|
if (*i != path && paths.find(*i) != paths.end())
|
|
|
|
dfsVisit(paths, *i, visited, sorted);
|
|
|
|
|
|
|
|
sorted.push_front(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Paths topoSort(const PathSet & paths)
|
|
|
|
{
|
|
|
|
Paths sorted;
|
|
|
|
PathSet visited;
|
|
|
|
for (PathSet::const_iterator i = paths.begin(); i != paths.end(); ++i)
|
|
|
|
dfsVisit(paths, *i, visited, sorted);
|
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void printDrvTree(const Path & drvPath,
|
|
|
|
const string & firstPad, const string & tailPad, PathSet & done)
|
|
|
|
{
|
|
|
|
if (done.find(drvPath) != done.end()) {
|
|
|
|
cout << format("%1%%2% [...]\n") % firstPad % drvPath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
done.insert(drvPath);
|
|
|
|
|
|
|
|
cout << format("%1%%2%\n") % firstPad % drvPath;
|
|
|
|
|
|
|
|
Derivation drv = derivationFromPath(drvPath);
|
|
|
|
|
|
|
|
for (PathSet::iterator i = drv.inputSrcs.begin();
|
|
|
|
i != drv.inputSrcs.end(); ++i)
|
|
|
|
cout << format("%1%%2%\n") % (tailPad + treeConn) % *i;
|
|
|
|
|
|
|
|
PathSet inputs;
|
|
|
|
for (DerivationInputs::iterator i = drv.inputDrvs.begin();
|
|
|
|
i != drv.inputDrvs.end(); ++i)
|
|
|
|
inputs.insert(i->first);
|
|
|
|
|
2005-02-22 17:23:24 +02:00
|
|
|
/* Topologically sort under the relation A < B iff A \in
|
2005-02-17 17:57:46 +02:00
|
|
|
closure(B). That is, if derivation A is an (possibly indirect)
|
|
|
|
input of B, then A is printed first. This has the effect of
|
|
|
|
flattening the tree, preventing deeply nested structures. */
|
|
|
|
Paths sorted = topoSort(inputs);
|
|
|
|
reverse(sorted.begin(), sorted.end());
|
|
|
|
|
|
|
|
for (Paths::iterator i = sorted.begin(); i != sorted.end(); ++i) {
|
|
|
|
Paths::iterator j = i; ++j;
|
|
|
|
printDrvTree(*i, tailPad + treeConn,
|
|
|
|
j == sorted.end() ? tailPad + treeNull : tailPad + treeLine,
|
|
|
|
done);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-08 16:22:08 +03:00
|
|
|
/* Perform various sorts of queries. */
|
|
|
|
static void opQuery(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2005-01-25 13:18:03 +02:00
|
|
|
enum { qOutputs, qRequisites, qReferences, qReferers,
|
2005-02-17 17:57:46 +02:00
|
|
|
qReferersClosure, qDeriver, qBinding, qTree, qGraph } query = qOutputs;
|
2005-01-19 16:36:00 +02:00
|
|
|
bool useOutput = false;
|
|
|
|
bool includeOutputs = false;
|
2005-01-25 12:55:33 +02:00
|
|
|
bool forceRealise = false;
|
2005-02-07 16:32:44 +02:00
|
|
|
string bindingName;
|
2003-07-21 17:46:01 +03:00
|
|
|
|
|
|
|
for (Strings::iterator i = opFlags.begin();
|
2005-02-07 16:32:44 +02:00
|
|
|
i != opFlags.end(); ++i)
|
2005-01-19 16:36:00 +02:00
|
|
|
if (*i == "--outputs") query = qOutputs;
|
2003-11-18 13:22:29 +02:00
|
|
|
else if (*i == "--requisites" || *i == "-R") query = qRequisites;
|
2005-01-19 18:59:56 +02:00
|
|
|
else if (*i == "--references") query = qReferences;
|
|
|
|
else if (*i == "--referers") query = qReferers;
|
2005-01-25 13:18:03 +02:00
|
|
|
else if (*i == "--referers-closure") query = qReferersClosure;
|
2005-02-07 15:40:40 +02:00
|
|
|
else if (*i == "--deriver" || *i == "-d") query = qDeriver;
|
2005-02-07 16:32:44 +02:00
|
|
|
else if (*i == "--binding" || *i == "-b") {
|
|
|
|
if (opArgs.size() == 0)
|
|
|
|
throw UsageError("expected binding name");
|
|
|
|
bindingName = opArgs.front();
|
|
|
|
opArgs.pop_front();
|
|
|
|
query = qBinding;
|
|
|
|
}
|
2005-02-17 17:57:46 +02:00
|
|
|
else if (*i == "--tree") query = qTree;
|
2003-07-28 15:19:23 +03:00
|
|
|
else if (*i == "--graph") query = qGraph;
|
2005-01-19 16:36:00 +02:00
|
|
|
else if (*i == "--use-output" || *i == "-u") useOutput = true;
|
2005-01-25 12:55:33 +02:00
|
|
|
else if (*i == "--force-realise" || *i == "-f") forceRealise = true;
|
2005-01-19 16:36:00 +02:00
|
|
|
else if (*i == "--include-outputs") includeOutputs = true;
|
2003-07-21 17:46:01 +03:00
|
|
|
else throw UsageError(format("unknown flag `%1%'") % *i);
|
|
|
|
|
|
|
|
switch (query) {
|
|
|
|
|
2005-01-19 16:36:00 +02:00
|
|
|
case qOutputs: {
|
2003-07-21 17:46:01 +03:00
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
{
|
2005-02-07 16:32:44 +02:00
|
|
|
*i = fixPath(*i);
|
2005-01-25 12:55:33 +02:00
|
|
|
if (forceRealise) realisePath(*i);
|
2005-01-19 16:36:00 +02:00
|
|
|
Derivation drv = derivationFromPath(*i);
|
|
|
|
cout << format("%1%\n") % findOutput(drv, "out");
|
2003-07-21 17:46:01 +03:00
|
|
|
}
|
2003-07-08 16:22:08 +03:00
|
|
|
break;
|
2003-07-10 16:41:28 +03:00
|
|
|
}
|
2003-07-08 16:22:08 +03:00
|
|
|
|
2005-01-19 18:59:56 +02:00
|
|
|
case qRequisites:
|
|
|
|
case qReferences:
|
2005-01-25 13:18:03 +02:00
|
|
|
case qReferers:
|
|
|
|
case qReferersClosure: {
|
2005-01-19 16:36:00 +02:00
|
|
|
PathSet paths;
|
2003-07-21 17:46:01 +03:00
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
{
|
2005-02-07 16:32:44 +02:00
|
|
|
*i = fixPath(*i);
|
2005-01-25 12:55:33 +02:00
|
|
|
Path path = maybeUseOutput(*i, useOutput, forceRealise);
|
2005-01-19 18:59:56 +02:00
|
|
|
if (query == qRequisites)
|
|
|
|
storePathRequisites(path, includeOutputs, paths);
|
2005-02-08 15:23:55 +02:00
|
|
|
else if (query == qReferences) queryReferences(noTxn, path, paths);
|
|
|
|
else if (query == qReferers) queryReferers(noTxn, path, paths);
|
2005-01-25 13:18:03 +02:00
|
|
|
else if (query == qReferersClosure) computeFSClosure(path, paths, true);
|
2003-07-21 17:46:01 +03:00
|
|
|
}
|
2005-01-19 18:59:56 +02:00
|
|
|
printPathSet(paths);
|
2003-07-21 17:46:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-02-07 15:40:40 +02:00
|
|
|
case qDeriver:
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
{
|
2005-02-07 16:32:44 +02:00
|
|
|
*i = fixPath(*i);
|
2005-02-07 15:40:40 +02:00
|
|
|
Path deriver = queryDeriver(noTxn, *i);
|
|
|
|
cout << format("%1%\n") %
|
|
|
|
(deriver == "" ? "unknown-deriver" : deriver);
|
|
|
|
}
|
|
|
|
break;
|
2005-02-07 16:32:44 +02:00
|
|
|
|
|
|
|
case qBinding:
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
{
|
|
|
|
*i = fixPath(*i);
|
|
|
|
Derivation drv = derivationFromPath(*i);
|
|
|
|
StringPairs::iterator j = drv.env.find(bindingName);
|
|
|
|
if (j == drv.env.end())
|
|
|
|
throw Error(format("derivation `%1%' has no environment binding named `%2%'")
|
|
|
|
% *i % bindingName);
|
|
|
|
cout << format("%1%\n") % j->second;
|
|
|
|
}
|
|
|
|
break;
|
2005-02-07 15:40:40 +02:00
|
|
|
|
2005-02-17 17:57:46 +02:00
|
|
|
case qTree: {
|
|
|
|
PathSet done;
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
|
|
|
printDrvTree(fixPath(*i), "", "", done);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-01-19 16:36:00 +02:00
|
|
|
#if 0
|
2003-07-28 15:19:23 +03:00
|
|
|
case qGraph: {
|
2003-10-08 18:06:59 +03:00
|
|
|
PathSet roots;
|
2003-07-28 15:19:23 +03:00
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); i++)
|
2004-06-20 22:17:54 +03:00
|
|
|
roots.insert(maybeNormalise(*i, normalise, realise));
|
2003-09-03 14:20:18 +03:00
|
|
|
printDotGraph(roots);
|
2003-07-28 15:19:23 +03:00
|
|
|
break;
|
|
|
|
}
|
2005-01-19 16:36:00 +02:00
|
|
|
#endif
|
2003-07-28 15:19:23 +03:00
|
|
|
|
2003-07-08 16:22:08 +03:00
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
2003-07-10 21:48:11 +03:00
|
|
|
|
|
|
|
|
2003-07-10 18:11:48 +03:00
|
|
|
static void opSubstitute(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
2004-06-20 22:17:54 +03:00
|
|
|
if (!opArgs.empty())
|
|
|
|
throw UsageError("no arguments expected");
|
|
|
|
|
2004-06-21 10:38:17 +03:00
|
|
|
Transaction txn;
|
|
|
|
createStoreTransaction(txn);
|
|
|
|
|
2004-06-20 22:17:54 +03:00
|
|
|
while (1) {
|
|
|
|
Path srcPath;
|
|
|
|
Substitute sub;
|
2005-01-25 19:08:52 +02:00
|
|
|
PathSet references;
|
2004-06-20 22:17:54 +03:00
|
|
|
getline(cin, srcPath);
|
|
|
|
if (cin.eof()) break;
|
2005-02-09 14:57:13 +02:00
|
|
|
getline(cin, sub.deriver);
|
2004-06-20 22:17:54 +03:00
|
|
|
getline(cin, sub.program);
|
|
|
|
string s;
|
|
|
|
int n;
|
2005-01-25 19:08:52 +02:00
|
|
|
getline(cin, s);
|
2004-09-10 16:32:08 +03:00
|
|
|
if (!string2Int(s, n)) throw Error("number expected");
|
2004-06-20 22:17:54 +03:00
|
|
|
while (n--) {
|
|
|
|
getline(cin, s);
|
|
|
|
sub.args.push_back(s);
|
|
|
|
}
|
2005-01-25 19:08:52 +02:00
|
|
|
getline(cin, s);
|
|
|
|
if (!string2Int(s, n)) throw Error("number expected");
|
|
|
|
while (n--) {
|
|
|
|
getline(cin, s);
|
|
|
|
references.insert(s);
|
|
|
|
}
|
2004-06-20 22:17:54 +03:00
|
|
|
if (!cin || cin.eof()) throw Error("missing input");
|
2005-01-25 22:27:40 +02:00
|
|
|
registerSubstitute(txn, srcPath, sub);
|
2005-01-25 19:08:52 +02:00
|
|
|
setReferences(txn, srcPath, references);
|
2003-07-10 18:11:48 +03:00
|
|
|
}
|
2004-06-21 10:38:17 +03:00
|
|
|
|
|
|
|
txn.commit();
|
2003-07-10 18:11:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-20 15:43:32 +02:00
|
|
|
static void opClearSubstitutes(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
if (!opArgs.empty())
|
|
|
|
throw UsageError("no arguments expected");
|
|
|
|
|
|
|
|
clearSubstitutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-14 23:44:18 +02:00
|
|
|
static void opValidPath(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
|
|
|
|
Transaction txn;
|
|
|
|
createStoreTransaction(txn);
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); ++i)
|
2005-02-07 15:40:40 +02:00
|
|
|
registerValidPath(txn, *i, hashPath(htSHA256, *i), PathSet(), "");
|
2004-02-14 23:44:18 +02:00
|
|
|
txn.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void opIsValid(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
|
|
|
|
for (Strings::iterator i = opArgs.begin();
|
|
|
|
i != opArgs.end(); ++i)
|
|
|
|
if (!isValidPath(*i))
|
|
|
|
throw Error(format("path `%1%' is not valid") % *i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-25 14:43:49 +03:00
|
|
|
static void opGC(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2005-02-01 17:05:32 +02:00
|
|
|
GCAction action = gcDeleteDead;
|
2005-01-27 17:21:29 +02:00
|
|
|
|
2004-08-25 14:43:49 +03:00
|
|
|
/* Do what? */
|
2004-08-25 19:54:08 +03:00
|
|
|
for (Strings::iterator i = opFlags.begin();
|
|
|
|
i != opFlags.end(); ++i)
|
2005-02-01 17:05:32 +02:00
|
|
|
if (*i == "--print-roots") action = gcReturnRoots;
|
|
|
|
else if (*i == "--print-live") action = gcReturnLive;
|
2005-01-27 17:21:29 +02:00
|
|
|
else if (*i == "--print-dead") action = gcReturnDead;
|
|
|
|
else if (*i == "--delete") action = gcDeleteDead;
|
2004-08-25 19:54:08 +03:00
|
|
|
else throw UsageError(format("bad sub-operation `%1%' in GC") % *i);
|
2005-01-27 17:21:29 +02:00
|
|
|
|
|
|
|
PathSet result;
|
2005-02-01 17:05:32 +02:00
|
|
|
collectGarbage(action, result);
|
2004-08-25 14:43:49 +03:00
|
|
|
|
2005-01-27 17:21:29 +02:00
|
|
|
if (action != gcDeleteDead) {
|
|
|
|
for (PathSet::iterator i = result.begin(); i != result.end(); ++i)
|
2004-08-25 14:43:49 +03:00
|
|
|
cout << *i << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-18 17:34:43 +03:00
|
|
|
/* A sink that writes dump output to stdout. */
|
|
|
|
struct StdoutSink : DumpSink
|
|
|
|
{
|
|
|
|
virtual void operator ()
|
|
|
|
(const unsigned char * data, unsigned int len)
|
|
|
|
{
|
2003-07-21 00:11:43 +03:00
|
|
|
writeFull(STDOUT_FILENO, data, len);
|
2003-06-18 17:34:43 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-08 13:00:46 +03:00
|
|
|
/* Dump a path as a Nix archive. The archive is written to standard
|
2003-06-23 17:08:34 +03:00
|
|
|
output. */
|
2003-06-18 17:34:43 +03:00
|
|
|
static void opDump(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
if (opArgs.size() != 1) throw UsageError("only one argument allowed");
|
|
|
|
|
|
|
|
StdoutSink sink;
|
2003-10-08 18:06:59 +03:00
|
|
|
string path = *opArgs.begin();
|
2003-06-23 17:08:34 +03:00
|
|
|
dumpPath(path, sink);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* A source that read restore intput to stdin. */
|
|
|
|
struct StdinSource : RestoreSource
|
|
|
|
{
|
2003-07-21 00:11:43 +03:00
|
|
|
virtual void operator () (unsigned char * data, unsigned int len)
|
2003-06-23 17:08:34 +03:00
|
|
|
{
|
2003-07-21 00:11:43 +03:00
|
|
|
readFull(STDIN_FILENO, data, len);
|
2003-06-23 17:08:34 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Restore a value from a Nix archive. The archive is written to
|
|
|
|
standard input. */
|
|
|
|
static void opRestore(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
if (opArgs.size() != 1) throw UsageError("only one argument allowed");
|
|
|
|
|
|
|
|
StdinSource source;
|
|
|
|
restorePath(*opArgs.begin(), source);
|
2003-06-18 17:34:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
/* Initialise the Nix databases. */
|
|
|
|
static void opInit(Strings opFlags, Strings opArgs)
|
2003-05-26 12:44:18 +03:00
|
|
|
{
|
2003-06-18 00:12:58 +03:00
|
|
|
if (!opFlags.empty()) throw UsageError("unknown flag");
|
|
|
|
if (!opArgs.empty())
|
2004-06-20 22:17:54 +03:00
|
|
|
throw UsageError("no arguments expected");
|
2003-06-18 00:12:58 +03:00
|
|
|
initDB();
|
2003-03-21 17:53:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-17 15:27:55 +03:00
|
|
|
/* Verify the consistency of the Nix environment. */
|
|
|
|
static void opVerify(Strings opFlags, Strings opArgs)
|
|
|
|
{
|
2005-02-08 15:48:53 +02:00
|
|
|
if (!opArgs.empty())
|
|
|
|
throw UsageError("no arguments expected");
|
|
|
|
|
|
|
|
bool checkContents = false;
|
|
|
|
|
|
|
|
for (Strings::iterator i = opFlags.begin();
|
|
|
|
i != opFlags.end(); ++i)
|
|
|
|
if (*i == "--check-contents") checkContents = true;
|
|
|
|
else throw UsageError(format("unknown flag `%1%'") % *i);
|
|
|
|
|
|
|
|
verifyStore(checkContents);
|
2003-07-17 15:27:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-04 18:42:03 +03:00
|
|
|
/* Scan the arguments; find the operation, set global flags, put all
|
|
|
|
other flags in a list, and put all other arguments in another
|
|
|
|
list. */
|
|
|
|
void run(Strings args)
|
2003-03-24 19:49:56 +02:00
|
|
|
{
|
2003-06-20 17:11:31 +03:00
|
|
|
Strings opFlags, opArgs;
|
|
|
|
Operation op = 0;
|
|
|
|
|
2005-02-01 14:36:25 +02:00
|
|
|
for (Strings::iterator i = args.begin(); i != args.end(); ) {
|
|
|
|
string arg = *i++;
|
2003-03-24 19:49:56 +02:00
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
Operation oldOp = op;
|
2003-03-24 19:49:56 +02:00
|
|
|
|
2005-01-25 12:55:33 +02:00
|
|
|
if (arg == "--realise" || arg == "-r")
|
|
|
|
op = opRealise;
|
2003-07-08 16:22:08 +03:00
|
|
|
else if (arg == "--add" || arg == "-A")
|
2003-06-18 00:12:58 +03:00
|
|
|
op = opAdd;
|
2005-01-19 16:36:00 +02:00
|
|
|
else if (arg == "--query" || arg == "-q")
|
|
|
|
op = opQuery;
|
2003-07-10 18:11:48 +03:00
|
|
|
else if (arg == "--substitute")
|
|
|
|
op = opSubstitute;
|
2004-12-20 15:43:32 +02:00
|
|
|
else if (arg == "--clear-substitutes")
|
|
|
|
op = opClearSubstitutes;
|
2004-02-14 23:44:18 +02:00
|
|
|
else if (arg == "--validpath")
|
|
|
|
op = opValidPath;
|
|
|
|
else if (arg == "--isvalid")
|
|
|
|
op = opIsValid;
|
2004-08-25 14:43:49 +03:00
|
|
|
else if (arg == "--gc")
|
|
|
|
op = opGC;
|
2003-06-18 17:34:43 +03:00
|
|
|
else if (arg == "--dump")
|
|
|
|
op = opDump;
|
2003-06-23 17:08:34 +03:00
|
|
|
else if (arg == "--restore")
|
|
|
|
op = opRestore;
|
2003-06-18 00:12:58 +03:00
|
|
|
else if (arg == "--init")
|
|
|
|
op = opInit;
|
2003-07-17 15:27:55 +03:00
|
|
|
else if (arg == "--verify")
|
|
|
|
op = opVerify;
|
2005-02-01 14:36:25 +02:00
|
|
|
else if (arg == "--add-root") {
|
|
|
|
if (i == args.end())
|
|
|
|
throw UsageError("`--add-root requires an argument");
|
2005-02-01 15:48:46 +02:00
|
|
|
gcRoot = absPath(*i++);
|
2005-02-01 14:36:25 +02:00
|
|
|
}
|
2005-02-01 15:48:46 +02:00
|
|
|
else if (arg == "--indirect")
|
|
|
|
indirectRoot = true;
|
2003-06-18 00:12:58 +03:00
|
|
|
else if (arg[0] == '-')
|
|
|
|
opFlags.push_back(arg);
|
|
|
|
else
|
|
|
|
opArgs.push_back(arg);
|
2003-05-26 01:42:19 +03:00
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
if (oldOp && oldOp != op)
|
|
|
|
throw UsageError("only one operation may be specified");
|
2003-05-26 01:42:19 +03:00
|
|
|
}
|
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
if (!op) throw UsageError("no operation specified");
|
2003-03-20 18:53:00 +02:00
|
|
|
|
2004-02-14 23:44:18 +02:00
|
|
|
if (op != opDump && op != opRestore) /* !!! hack */
|
|
|
|
openDB();
|
2003-10-14 18:33:00 +03:00
|
|
|
|
2003-06-18 00:12:58 +03:00
|
|
|
op(opFlags, opArgs);
|
2003-03-20 18:53:00 +02:00
|
|
|
}
|
2003-03-14 18:43:14 +02:00
|
|
|
|
2003-03-24 19:49:56 +02:00
|
|
|
|
2003-11-18 14:06:07 +02:00
|
|
|
string programId = "nix-store";
|