diff options
author | David Mazieres <dm@uun.org> | 2014-03-28 21:38:18 -0700 |
---|---|---|
committer | David Mazieres <dm@uun.org> | 2014-03-28 21:38:18 -0700 |
commit | 8d6993c66395c3139ea7a0ee5da85282fd1fc9de (patch) | |
tree | 091c119eb74f967424f25ab472ab043f8ae5d202 | |
parent | 48700416d1c77729f4dac2407049e93957a5a904 (diff) |
fold cmd_iofds into muchsync.cc to kill spawn.cc
-rw-r--r-- | Makefile.am | 5 | ||||
-rw-r--r-- | muchsync.cc | 79 | ||||
-rw-r--r-- | muchsync.h | 17 | ||||
-rw-r--r-- | protocol.cc | 8 | ||||
-rw-r--r-- | spawn.cc | 97 |
5 files changed, 69 insertions, 137 deletions
diff --git a/Makefile.am b/Makefile.am index 9e75717..e6bc865 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,9 +5,8 @@ LDADD = $(sqlite3_LIBS) $(libcrypto_LIBS) $(xapian_LIBS) -lnotmuch bin_PROGRAMS = muchsync muchsync_SOURCES = infinibuf.cc misc.cc muchsync.cc notmuch_db.cc \ - protocol.cc spawn.cc sqlstmt.cc sql_db.cc xapian_sync.cc \ - cleanup.h misc.h muchsync.h infinibuf.h notmuch_db.h \ - sqlstmt.h sql_db.h + protocol.cc sqlstmt.cc sql_db.cc xapian_sync.cc cleanup.h \ + misc.h muchsync.h infinibuf.h notmuch_db.h sqlstmt.h sql_db.h CLEANFILES = *~ maintainer-clean-local: diff --git a/muchsync.cc b/muchsync.cc index 478c5fb..5696ca1 100644 --- a/muchsync.cc +++ b/muchsync.cc @@ -1,30 +1,36 @@ #include <cstring> -#include <istream> -#include <iostream> #include <iomanip> +#include <iostream> #include <sstream> -#include <vector> -#include <stdlib.h> -#include <unistd.h> -#include <getopt.h> #include <fcntl.h> +#include <getopt.h> +#include <unistd.h> #include <sys/stat.h> #include <sys/types.h> -#include <sys/wait.h> -#include <notmuch.h> + #include "misc.h" #include "muchsync.h" #include "infinibuf.h" using namespace std; +#if 1 +struct whattocatch_t { + const char *what() noexcept { return "no such exception"; } +}; +#else +using whattocatch_t = const exception; +#endif + #define MUCHSYNC_DEFDIR "/.notmuch/muchsync" const char muchsync_defdir[] = MUCHSYNC_DEFDIR; const char muchsync_dbpath[] = MUCHSYNC_DEFDIR "/state.db"; const char muchsync_trashdir[] = MUCHSYNC_DEFDIR "/trash"; const char muchsync_tmpdir[] = MUCHSYNC_DEFDIR "/tmp"; +constexpr char shell[] = "/bin/sh"; + bool opt_fullscan; bool opt_noscan; bool opt_init; @@ -39,15 +45,7 @@ string opt_remote_muchsync_path = "muchsync"; string opt_notmuch_config; string opt_init_dest; -#if 1 -struct whattocatch_t { - const char *what() noexcept { return "no such exception"; } -}; -#else -using whattocatch_t = const exception; -#endif - -bool +static bool muchsync_init (const string &maildir, bool create = false) { string trashbase = maildir + muchsync_trashdir + "/"; @@ -165,6 +163,53 @@ server() } static void +cmd_iofds (int fds[2], const string &cmd) +{ + int ifds[2], ofds[2]; + if (pipe (ifds)) + throw runtime_error (string ("pipe: ") + strerror (errno)); + if (pipe (ofds)) { + close (ifds[0]); + close (ifds[1]); + throw runtime_error (string ("pipe: ") + strerror (errno)); + } + + pid_t pid = fork(); + switch (pid) { + case -1: + close (ifds[0]); + close (ifds[1]); + close (ofds[0]); + close (ofds[1]); + throw runtime_error (string ("fork: ") + strerror (errno)); + break; + case 0: + close (ifds[0]); + close (ofds[1]); + if (ofds[0] != 0) { + dup2 (ofds[0], 0); + close (ofds[0]); + } + if (ifds[1] != 1) { + dup2 (ifds[1], 1); + close (ifds[1]); + } + execl (shell, shell, "-c", cmd.c_str(), nullptr); + cerr << shell << ": " << strerror (errno) << '\n'; + _exit (1); + break; + default: + close (ifds[1]); + close (ofds[0]); + fcntl (ifds[0], F_SETFD, 1); + fcntl (ofds[1], F_SETFD, 1); + fds[0] = ifds[0]; + fds[1] = ofds[1]; + break; + } +} + +static void create_config(istream &in, ostream &out, string &maildir) { if (!maildir.size() || !maildir.front()) @@ -13,11 +13,10 @@ using std::string; /* protocol.cc */ -void muchsync_server (sqlite3 *db, notmuch_db &nm); -void muchsync_client (sqlite3 *db, notmuch_db &nm, - std::istream &in, std::ostream &out); -std::istream &get_response (std::istream &in, string &line); - +void muchsync_server(sqlite3 *db, notmuch_db &nm); +void muchsync_client(sqlite3 *db, notmuch_db &nm, + std::istream &in, std::ostream &out); +std::istream &get_response(std::istream &in, string &line); /* muchsync.cc */ extern bool opt_fullscan; @@ -32,10 +31,4 @@ extern const char muchsync_trashdir[]; extern const char muchsync_tmpdir[]; /* xapian_sync.cc */ -void xapian_scan (sqlite3 *sqldb, writestamp ws, string maildir); -void sync_local_data (sqlite3 *sqldb, const string &maildir); - -/* spawn.cc */ -string cmd_output (const string &cmd); -void cmd_iofds (int fds[2], const string &cmd); - +void sync_local_data(sqlite3 *sqldb, const string &maildir); diff --git a/protocol.cc b/protocol.cc index 3f3b576..5a88b3d 100644 --- a/protocol.cc +++ b/protocol.cc @@ -663,14 +663,6 @@ muchsync_server(sqlite3 *db, notmuch_db &nm) cout << "200 goodbye\n"; return; } - else if (cmd == "conf") { - string conf = cmd_output("notmuch config list"); - if (conf.length()) - cout << "221-" << conf.length() << '\n' - << conf << "221 ok\n"; - else - cout << "410 cannot find configuration\n"; - } else if (cmd == "conffile") { ifstream is (opt_notmuch_config); ostringstream os; diff --git a/spawn.cc b/spawn.cc deleted file mode 100644 index 8ff13e0..0000000 --- a/spawn.cc +++ /dev/null @@ -1,97 +0,0 @@ - -#include <cstring> -#include <iostream> -#include <iterator> -#include <queue> -#include <sstream> -#include <fcntl.h> -#include <unistd.h> -#include <sys/wait.h> -#include <sys/socket.h> -#include <poll.h> - -#include "muchsync.h" -#include "infinibuf.h" - -using namespace std; - -constexpr char shell[] = "/bin/sh"; - -void -cmd_iofds (int fds[2], const string &cmd) -{ - int ifds[2], ofds[2]; - if (pipe (ifds)) - throw runtime_error (string ("pipe: ") + strerror (errno)); - if (pipe (ofds)) { - close (ifds[0]); - close (ifds[1]); - throw runtime_error (string ("pipe: ") + strerror (errno)); - } - - pid_t pid = fork(); - switch (pid) { - case -1: - close (ifds[0]); - close (ifds[1]); - close (ofds[0]); - close (ofds[1]); - throw runtime_error (string ("fork: ") + strerror (errno)); - break; - case 0: - close (ifds[0]); - close (ofds[1]); - if (ofds[0] != 0) { - dup2 (ofds[0], 0); - close (ofds[0]); - } - if (ifds[1] != 1) { - dup2 (ifds[1], 1); - close (ifds[1]); - } - execl (shell, shell, "-c", cmd.c_str(), nullptr); - cerr << shell << ": " << strerror (errno) << '\n'; - _exit (1); - break; - default: - close (ifds[1]); - close (ofds[0]); - fcntl (ifds[0], F_SETFD, 1); - fcntl (ofds[1], F_SETFD, 1); - fds[0] = ifds[0]; - fds[1] = ofds[1]; - break; - } -} - -string -cmd_output (const string &cmd) -{ - int fds[2]; - if (pipe (fds)) - throw runtime_error (string ("pipe: ") + strerror (errno)); - fcntl (fds[0], F_SETFD, 1); - - pid_t pid = fork(); - switch (pid) { - case -1: - throw runtime_error (string ("fork: ") + strerror (errno)); - break; - case 0: - if (fds[1] != 1) { - dup2 (fds[1], 1); - close (fds[1]); - } - execl (shell, shell, "-c", cmd.c_str(), nullptr); - cerr << shell << ": " << strerror (errno) << '\n'; - _exit (1); - break; - default: - close (fds[1]); - ifdstream stream{fds[0]}; - ostringstream os; - os << stream.rdbuf(); - waitpid (pid, NULL, 0); - return os.str(); - } -} |