forked from lix-project/lix
* `nix --restore' command.
This commit is contained in:
parent
5f5cab0ac7
commit
c0cbaef4be
44
src/nix.cc
44
src/nix.cc
|
@ -26,9 +26,13 @@ static ArgType argType = atpUnknown;
|
||||||
--delete / -d: delete values
|
--delete / -d: delete values
|
||||||
--query / -q: query stored values
|
--query / -q: query stored values
|
||||||
--add: add values
|
--add: add values
|
||||||
--verify: verify Nix structures
|
|
||||||
--dump: dump a file or value
|
--dump: dump a value as a Nix archive
|
||||||
|
--restore: restore a value from a Nix archive
|
||||||
|
|
||||||
--init: initialise the Nix database
|
--init: initialise the Nix database
|
||||||
|
--verify: verify Nix structures
|
||||||
|
|
||||||
--version: output version information
|
--version: output version information
|
||||||
--help: display help
|
--help: display help
|
||||||
|
|
||||||
|
@ -132,14 +136,14 @@ struct StdoutSink : DumpSink
|
||||||
virtual void operator ()
|
virtual void operator ()
|
||||||
(const unsigned char * data, unsigned int len)
|
(const unsigned char * data, unsigned int len)
|
||||||
{
|
{
|
||||||
/* Don't use cout, it's slow as hell! */
|
if (write(STDOUT_FILENO, (char *) data, len) != (ssize_t) len)
|
||||||
if (write(STDOUT_FILENO, (char *) data, len) != len)
|
|
||||||
throw SysError("writing to stdout");
|
throw SysError("writing to stdout");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/* Dump a value to standard output */
|
/* Dump a value as a Nix archive. The archive is written to standard
|
||||||
|
output. */
|
||||||
static void opDump(Strings opFlags, Strings opArgs)
|
static void opDump(Strings opFlags, Strings opArgs)
|
||||||
{
|
{
|
||||||
getArgType(opFlags);
|
getArgType(opFlags);
|
||||||
|
@ -157,7 +161,33 @@ static void opDump(Strings opFlags, Strings opArgs)
|
||||||
else if (argType == atpPath)
|
else if (argType == atpPath)
|
||||||
path = arg;
|
path = arg;
|
||||||
|
|
||||||
dumpPath(*opArgs.begin(), sink);
|
dumpPath(path, sink);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* A source that read restore intput to stdin. */
|
||||||
|
struct StdinSource : RestoreSource
|
||||||
|
{
|
||||||
|
virtual void operator () (const unsigned char * data, unsigned int len)
|
||||||
|
{
|
||||||
|
ssize_t res = read(STDIN_FILENO, (char *) data, len);
|
||||||
|
if (res == -1)
|
||||||
|
throw SysError("reading from stdin");
|
||||||
|
if (res != (ssize_t) len)
|
||||||
|
throw Error("not enough data available on stdin");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/* 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -219,6 +249,8 @@ void run(int argc, char * * argv)
|
||||||
op = opAdd;
|
op = opAdd;
|
||||||
else if (arg == "--dump")
|
else if (arg == "--dump")
|
||||||
op = opDump;
|
op = opDump;
|
||||||
|
else if (arg == "--restore")
|
||||||
|
op = opRestore;
|
||||||
else if (arg == "--init")
|
else if (arg == "--init")
|
||||||
op = opInit;
|
op = opInit;
|
||||||
else if (arg[0] == '-')
|
else if (arg[0] == '-')
|
||||||
|
|
Loading…
Reference in a new issue