From 3a38d0f3565a02c034c29b264aceb0eb78dac005 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Thu, 6 Feb 2014 11:52:03 -0500 Subject: Add the nix-store --serve command This is essentially the substituter API operating on the local store, which will be used by the ssh substituter. It runs in a loop rather than just taking one command so that in the future nix will be able to keep one connection open for multiple instances of the substituter. Signed-off-by: Shea Levy --- src/nix-store/nix-store.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/nix-store/nix-store.cc') diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 69a98fe4726d..fb1d3f541dec 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -834,6 +834,24 @@ static void opClearFailedPaths(Strings opFlags, Strings opArgs) } +// Serve the nix store in a way usable by a restricted ssh user +static void opServe(Strings opFlags, Strings opArgs) +{ + if (!opArgs.empty()) + throw UsageError("no arguments expected"); + // Could eventually take a username argument? + bool sign; + foreach (Strings::iterator, i, opFlags) + if (*i == "--sign") sign = true; + else throw UsageError(format("unknown flag `%1%'") % *i); + + FdSource in(STDIN_FILENO); + FdSink out(STDOUT_FILENO); + + store->serve(in, out, sign); +} + + /* Scan the arguments; find the operation, set global flags, put all other flags in a list, and put all other arguments in another list. */ @@ -904,6 +922,8 @@ void run(Strings args) indirectRoot = true; else if (arg == "--no-output") noOutput = true; + else if (arg == "--serve") + op = opServe; else if (arg[0] == '-') { opFlags.push_back(arg); if (arg == "--max-freed" || arg == "--max-links" || arg == "--max-atime") { /* !!! hack */ -- cgit 1.4.1 From 73874629ef59dc3b237a2c316179e722f971bb5e Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 7 Feb 2014 16:17:52 -0500 Subject: nix-store --serve: Use dump instead of export Also remove signing support Signed-off-by: Shea Levy --- src/libstore/store-api.cc | 5 +++-- src/libstore/store-api.hh | 2 +- src/nix-store/nix-store.cc | 12 +++--------- 3 files changed, 7 insertions(+), 12 deletions(-) (limited to 'src/nix-store/nix-store.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index 082c2c4d9740..bd95590756de 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1,6 +1,7 @@ #include "store-api.hh" #include "globals.hh" #include "util.hh" +#include "archive.hh" #include @@ -258,7 +259,7 @@ string StoreAPI::makeValidityRegistration(const PathSet & paths, } -void StoreAPI::serve(Source & in, Sink & out, bool sign) +void StoreAPI::serve(Source & in, Sink & out) { string cmd = readString(in); if (cmd == "query") { @@ -284,7 +285,7 @@ void StoreAPI::serve(Source & in, Sink & out, bool sign) throw Error(format("Unknown serve query `%1%'") % cmd); } } else if (cmd == "substitute") - exportPath(readString(in), sign, out); + dumpPath(readString(in), out); else throw Error(format("Unknown serve command `%1%'") % cmd); } diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index 57cf5179485c..edab6ea5a2d9 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -251,7 +251,7 @@ public: /* Serve the store for ssh substituters by taking commands * from in and printing results to out */ - void serve(Source & in, Sink & out, bool sign); + void serve(Source & in, Sink & out); }; diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index fb1d3f541dec..e3f27820fffd 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -837,18 +837,12 @@ static void opClearFailedPaths(Strings opFlags, Strings opArgs) // Serve the nix store in a way usable by a restricted ssh user static void opServe(Strings opFlags, Strings opArgs) { - if (!opArgs.empty()) - throw UsageError("no arguments expected"); - // Could eventually take a username argument? - bool sign; - foreach (Strings::iterator, i, opFlags) - if (*i == "--sign") sign = true; - else throw UsageError(format("unknown flag `%1%'") % *i); - + if (!opArgs.empty() || !opFlags.empty()) + throw UsageError("no arguments or flags expected"); FdSource in(STDIN_FILENO); FdSink out(STDOUT_FILENO); - store->serve(in, out, sign); + store->serve(in, out); } -- cgit 1.4.1 From 38c3beac1a8ac9ddf4fdbbcafd400dabcf195076 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 10 Feb 2014 06:52:48 -0500 Subject: Move StoreApi::serve into opServe Signed-off-by: Shea Levy --- src/libstore/store-api.cc | 34 ---------------------------------- src/libstore/store-api.hh | 4 ---- src/nix-store/nix-store.cc | 30 +++++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 39 deletions(-) (limited to 'src/nix-store/nix-store.cc') diff --git a/src/libstore/store-api.cc b/src/libstore/store-api.cc index e4f180240992..0f250a3c7c04 100644 --- a/src/libstore/store-api.cc +++ b/src/libstore/store-api.cc @@ -1,7 +1,6 @@ #include "store-api.hh" #include "globals.hh" #include "util.hh" -#include "archive.hh" #include @@ -259,39 +258,6 @@ string StoreAPI::makeValidityRegistration(const PathSet & paths, } -void StoreAPI::serve(Source & in, BufferedSink & out) -{ - string cmd = readString(in); - if (cmd == "query") { - for (cmd = readString(in); !cmd.empty(); cmd = readString(in)) { - PathSet paths = readStrings(in); - if (cmd == "have") { - writeStrings(queryValidPaths(paths), out); - } else if (cmd == "info") { - // !!! Maybe we want a queryPathInfos? - foreach (PathSet::iterator, i, paths) { - if (!isValidPath(*i)) - continue; - ValidPathInfo info = queryPathInfo(*i); - writeString(info.path, out); - writeString(info.deriver, out); - writeStrings(info.references, out); - // !!! Maybe we want compression? - writeLongLong(info.narSize, out); // downloadSize - writeLongLong(info.narSize, out); - } - writeString("", out); - } else - throw Error(format("Unknown serve query `%1%'") % cmd); - out.flush(); - } - } else if (cmd == "substitute") - dumpPath(readString(in), out); - else - throw Error(format("Unknown serve command `%1%'") % cmd); -} - - ValidPathInfo decodeValidPathInfo(std::istream & str, bool hashGiven) { ValidPathInfo info; diff --git a/src/libstore/store-api.hh b/src/libstore/store-api.hh index f32824a3bb7c..a82fe3221639 100644 --- a/src/libstore/store-api.hh +++ b/src/libstore/store-api.hh @@ -248,10 +248,6 @@ public: `nix-store --register-validity'. */ string makeValidityRegistration(const PathSet & paths, bool showDerivers, bool showHash); - - /* Serve the store for ssh substituters by taking commands - * from in and printing results to out */ - void serve(Source & in, BufferedSink & out); }; diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index e3f27820fffd..68ad9026747f 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -839,10 +839,38 @@ static void opServe(Strings opFlags, Strings opArgs) { if (!opArgs.empty() || !opFlags.empty()) throw UsageError("no arguments or flags expected"); + FdSource in(STDIN_FILENO); FdSink out(STDOUT_FILENO); - store->serve(in, out); + string cmd = readString(in); + if (cmd == "query") { + for (cmd = readString(in); !cmd.empty(); cmd = readString(in)) { + PathSet paths = readStrings(in); + if (cmd == "have") { + writeStrings(store->queryValidPaths(paths), out); + } else if (cmd == "info") { + // !!! Maybe we want a queryPathInfos? + foreach (PathSet::iterator, i, paths) { + if (!store->isValidPath(*i)) + continue; + ValidPathInfo info = store->queryPathInfo(*i); + writeString(info.path, out); + writeString(info.deriver, out); + writeStrings(info.references, out); + // !!! Maybe we want compression? + writeLongLong(info.narSize, out); // downloadSize + writeLongLong(info.narSize, out); + } + writeString("", out); + } else + throw Error(format("Unknown serve query `%1%'") % cmd); + out.flush(); + } + } else if (cmd == "substitute") + dumpPath(readString(in), out); + else + throw Error(format("Unknown serve command `%1%'") % cmd); } -- cgit 1.4.1 From c89d6b9b63b629ff936a56855be5689523910c58 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Mon, 10 Feb 2014 07:43:13 -0500 Subject: nix-store --serve: Use a versioned protocol Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 28 ++++++++--- src/download-via-ssh/local.mk | 2 + src/nix-store/nix-store.cc | 82 +++++++++++++++++++++----------- src/nix-store/serve-protocol.hh | 24 ++++++++++ 4 files changed, 102 insertions(+), 34 deletions(-) create mode 100644 src/nix-store/serve-protocol.hh (limited to 'src/nix-store/nix-store.cc') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 003d7de2bf86..be70a374fe77 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -4,6 +4,7 @@ #include "archive.hh" #include "affinity.hh" #include "globals.hh" +#include "serve-protocol.hh" #include #include @@ -54,7 +55,7 @@ static pair connect(string conn) { } static void substitute(pair & pipes, Path storePath, Path destPath) { - writeString("substitute", pipes.first); + writeInt(cmdSubstitute, pipes.first); writeString(storePath, pipes.first); pipes.first.flush(); restorePath(destPath, pipes.second); @@ -63,20 +64,24 @@ static void substitute(pair & pipes, Path storePath, Path dest static void query(pair & pipes) { using std::cin; - writeString("query", pipes.first); + writeInt(cmdQuery, pipes.first); for (string line; getline(cin, line);) { Strings tokenized = tokenizeString(line); string cmd = tokenized.front(); - writeString(cmd, pipes.first); tokenized.pop_front(); - foreach (Strings::iterator, i, tokenized) - writeStrings(tokenized, pipes.first); - pipes.first.flush(); if (cmd == "have") { + writeInt(qCmdHave, pipes.first); + foreach (Strings::iterator, i, tokenized) + writeStrings(tokenized, pipes.first); + pipes.first.flush(); PathSet paths = readStrings(pipes.second); foreach (PathSet::iterator, i, paths) cout << *i << endl; } else if (cmd == "info") { + writeInt(qCmdInfo, pipes.first); + foreach (Strings::iterator, i, tokenized) + writeStrings(tokenized, pipes.first); + pipes.first.flush(); for (Path path = readString(pipes.second); !path.empty(); path = readString(pipes.second)) { cout << path << endl; cout << readString(pipes.second) << endl; @@ -91,7 +96,6 @@ static void query(pair & pipes) { throw Error(format("Unknown substituter query `%1%'") % cmd); cout << endl; } - writeString("", pipes.first); } void run(Strings args) @@ -106,6 +110,16 @@ void run(Strings args) pair pipes = connect(settings.sshSubstituterHosts.front()); + /* Exchange the greeting */ + writeInt(SERVE_MAGIC_1, pipes.first); + pipes.first.flush(); + unsigned int magic = readInt(pipes.second); + if (magic != SERVE_MAGIC_2) + throw Error("protocol mismatch"); + readInt(pipes.second); // Server version, unused for now + writeInt(SERVE_PROTOCOL_VERSION, pipes.first); + pipes.first.flush(); + Strings::iterator i = args.begin(); if (*i == "--query") query(pipes); diff --git a/src/download-via-ssh/local.mk b/src/download-via-ssh/local.mk index 92bf1159469d..80f4c385acb3 100644 --- a/src/download-via-ssh/local.mk +++ b/src/download-via-ssh/local.mk @@ -6,4 +6,6 @@ download-via-ssh_SOURCES := $(d)/download-via-ssh.cc download-via-ssh_INSTALL_DIR := $(libexecdir)/nix/substituters +download-via-ssh_CXXFLAGS = -Isrc/nix-store + download-via-ssh_LIBS = libmain libstore libutil libformat diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 68ad9026747f..638d24498821 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -6,6 +6,7 @@ #include "xmlgraph.hh" #include "local-store.hh" #include "util.hh" +#include "serve-protocol.hh" #include #include @@ -843,34 +844,61 @@ static void opServe(Strings opFlags, Strings opArgs) FdSource in(STDIN_FILENO); FdSink out(STDOUT_FILENO); - string cmd = readString(in); - if (cmd == "query") { - for (cmd = readString(in); !cmd.empty(); cmd = readString(in)) { - PathSet paths = readStrings(in); - if (cmd == "have") { - writeStrings(store->queryValidPaths(paths), out); - } else if (cmd == "info") { - // !!! Maybe we want a queryPathInfos? - foreach (PathSet::iterator, i, paths) { - if (!store->isValidPath(*i)) - continue; - ValidPathInfo info = store->queryPathInfo(*i); - writeString(info.path, out); - writeString(info.deriver, out); - writeStrings(info.references, out); - // !!! Maybe we want compression? - writeLongLong(info.narSize, out); // downloadSize - writeLongLong(info.narSize, out); + /* Exchange the greeting. */ + unsigned int magic = readInt(in); + if (magic != SERVE_MAGIC_1) throw Error("protocol mismatch"); + writeInt(SERVE_MAGIC_2, out); + writeInt(SERVE_PROTOCOL_VERSION, out); + out.flush(); + readInt(in); // Client version, unused for now + + ServeCommand cmd = (ServeCommand) readInt(in); + switch (cmd) { + case cmdQuery: + while (true) { + QueryCommand qCmd; + try { + qCmd = (QueryCommand) readInt(in); + } catch (EndOfFile & e) { + break; } - writeString("", out); - } else - throw Error(format("Unknown serve query `%1%'") % cmd); - out.flush(); - } - } else if (cmd == "substitute") - dumpPath(readString(in), out); - else - throw Error(format("Unknown serve command `%1%'") % cmd); + switch (qCmd) { + case qCmdHave: + { + PathSet paths = readStrings(in); + writeStrings(store->queryValidPaths(paths), out); + } + break; + case qCmdInfo: + { + PathSet paths = readStrings(in); + // !!! Maybe we want a queryPathInfos? + foreach (PathSet::iterator, i, paths) { + if (!store->isValidPath(*i)) + continue; + ValidPathInfo info = store->queryPathInfo(*i); + writeString(info.path, out); + writeString(info.deriver, out); + writeStrings(info.references, out); + // !!! Maybe we want compression? + writeLongLong(info.narSize, out); // downloadSize + writeLongLong(info.narSize, out); + } + writeString("", out); + } + break; + default: + throw Error(format("Unknown serve query `%1%'") % cmd); + } + out.flush(); + } + break; + case cmdSubstitute: + dumpPath(readString(in), out); + break; + default: + throw Error(format("Unknown serve command `%1%'") % cmd); + } } diff --git a/src/nix-store/serve-protocol.hh b/src/nix-store/serve-protocol.hh new file mode 100644 index 000000000000..69277bc1b99b --- /dev/null +++ b/src/nix-store/serve-protocol.hh @@ -0,0 +1,24 @@ +#pragma once + +namespace nix { + + +#define SERVE_MAGIC_1 0x390c9deb +#define SERVE_MAGIC_2 0x5452eecb + +#define SERVE_PROTOCOL_VERSION 0x101 +#define GET_PROTOCOL_MAJOR(x) ((x) & 0xff00) +#define GET_PROTOCOL_MINOR(x) ((x) & 0x00ff) + + +typedef enum { + cmdQuery = 0, + cmdSubstitute = 1, +} ServeCommand; + +typedef enum { + qCmdHave = 0, + qCmdInfo = 1, +} QueryCommand; + +} -- cgit 1.4.1 From 7438f0bc2bc4b92bddf7159744ab2923e34b7457 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 12 Feb 2014 07:26:35 -0500 Subject: error messages start in lowercase Signed-off-by: Shea Levy --- src/download-via-ssh/download-via-ssh.cc | 2 +- src/nix-store/nix-store.cc | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nix-store/nix-store.cc') diff --git a/src/download-via-ssh/download-via-ssh.cc b/src/download-via-ssh/download-via-ssh.cc index 5aa1e00ee38a..dee4fd78c9d0 100644 --- a/src/download-via-ssh/download-via-ssh.cc +++ b/src/download-via-ssh/download-via-ssh.cc @@ -89,7 +89,7 @@ static void query(std::pair & pipes) { std::cout << readLongLong(pipes.second) << std::endl; } } else - throw Error(format("Unknown substituter query `%1%'") % cmd); + throw Error(format("unknown substituter query `%1%'") % cmd); std::cout << std::endl; } } diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 638d24498821..038f099649b0 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -888,7 +888,7 @@ static void opServe(Strings opFlags, Strings opArgs) } break; default: - throw Error(format("Unknown serve query `%1%'") % cmd); + throw Error(format("unknown serve query `%1%'") % cmd); } out.flush(); } @@ -897,7 +897,7 @@ static void opServe(Strings opFlags, Strings opArgs) dumpPath(readString(in), out); break; default: - throw Error(format("Unknown serve command `%1%'") % cmd); + throw Error(format("unknown serve command `%1%'") % cmd); } } -- cgit 1.4.1 From f67f52751f21b2fe70b5a7352053f130eb6f0f59 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Wed, 12 Feb 2014 07:33:07 -0500 Subject: Indendation fix Signed-off-by: Shea Levy --- src/nix-store/nix-store.cc | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) (limited to 'src/nix-store/nix-store.cc') diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 038f099649b0..350a4ce0fa37 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -863,30 +863,28 @@ static void opServe(Strings opFlags, Strings opArgs) break; } switch (qCmd) { - case qCmdHave: - { - PathSet paths = readStrings(in); - writeStrings(store->queryValidPaths(paths), out); - } + case qCmdHave: { + PathSet paths = readStrings(in); + writeStrings(store->queryValidPaths(paths), out); break; - case qCmdInfo: - { - PathSet paths = readStrings(in); - // !!! Maybe we want a queryPathInfos? - foreach (PathSet::iterator, i, paths) { - if (!store->isValidPath(*i)) - continue; - ValidPathInfo info = store->queryPathInfo(*i); - writeString(info.path, out); - writeString(info.deriver, out); - writeStrings(info.references, out); - // !!! Maybe we want compression? - writeLongLong(info.narSize, out); // downloadSize - writeLongLong(info.narSize, out); - } - writeString("", out); + } + case qCmdInfo: { + PathSet paths = readStrings(in); + // !!! Maybe we want a queryPathInfos? + foreach (PathSet::iterator, i, paths) { + if (!store->isValidPath(*i)) + continue; + ValidPathInfo info = store->queryPathInfo(*i); + writeString(info.path, out); + writeString(info.deriver, out); + writeStrings(info.references, out); + // !!! Maybe we want compression? + writeLongLong(info.narSize, out); // downloadSize + writeLongLong(info.narSize, out); } + writeString("", out); break; + } default: throw Error(format("unknown serve query `%1%'") % cmd); } -- cgit 1.4.1