Synchronize notmuch mail across machines
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notmuch_db.cc 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <cassert>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <stdexcept>
  5. #include <fcntl.h>
  6. #include <signal.h>
  7. #include <stdarg.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <sys/stat.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include "cleanup.h"
  14. #include "infinibuf.h"
  15. #include "notmuch_db.h"
  16. using namespace std;
  17. static unordered_set<string>
  18. lines(const string &s)
  19. {
  20. istringstream is (s);
  21. string line;
  22. unordered_set<string> ret;
  23. while (getline(is, line))
  24. ret.insert(line);
  25. return ret;
  26. }
  27. static string
  28. chomp(string s)
  29. {
  30. while (s.length() && (s.back() == '\n' || s.back() == '\r'))
  31. s.resize(s.length() - 1);
  32. return s;
  33. }
  34. static bool
  35. conf_to_bool(string s)
  36. {
  37. s = chomp(s);
  38. if (s.empty() || s == "false" || s == "0")
  39. return false;
  40. return true;
  41. }
  42. notmuch_db::message_t
  43. notmuch_db::get_message(const char *msgid)
  44. {
  45. notmuch_message_t *message;
  46. nmtry("notmuch_database_find_message",
  47. notmuch_database_find_message (notmuch(), msgid, &message));
  48. return message_t (message);
  49. }
  50. notmuch_db::message_t
  51. notmuch_db::add_message(const string &path, const tags_t *newtags,
  52. bool *was_new)
  53. {
  54. notmuch_status_t err;
  55. notmuch_message_t *message;
  56. #if LIBNOTMUCH_CHECK_VERSION(5,1,0)
  57. err = notmuch_database_index_file(notmuch(), path.c_str(), nullptr, &message);
  58. #else // libnotmuch < 5.1.0
  59. err = notmuch_database_add_message(notmuch(), path.c_str(), &message);
  60. #endif // libnotmuch < 5.1.0
  61. if (err != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
  62. nmtry("notmuch_database_add_message", err);
  63. set_tags(message, newtags ? *newtags : new_tags);
  64. }
  65. if (was_new)
  66. *was_new = err != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
  67. return message_t (message);
  68. }
  69. void
  70. notmuch_db::remove_message(const string &path)
  71. {
  72. notmuch_status_t err =
  73. notmuch_database_remove_message(notmuch(), path.c_str());
  74. if (err != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
  75. nmtry("notmuch_database_remove_message", err);
  76. }
  77. void
  78. notmuch_db::set_tags(notmuch_message_t *msg, const tags_t &tags)
  79. {
  80. // Deliberately don't unthaw message if we throw exception
  81. nmtry("notmuch_message_freeze", notmuch_message_freeze(msg));
  82. nmtry("notmuch_message_remove_all_tags",
  83. notmuch_message_remove_all_tags(msg));
  84. for (auto tag : tags)
  85. nmtry("notmuch_message_add_tag", notmuch_message_add_tag(msg, tag.c_str()));
  86. if (sync_flags)
  87. nmtry("notmuch_message_maildir_flags_to_tags",
  88. notmuch_message_tags_to_maildir_flags(msg));
  89. nmtry("notmuch_message_thaw", notmuch_message_thaw(msg));
  90. }
  91. string
  92. notmuch_db::default_notmuch_config()
  93. {
  94. char *p = getenv("NOTMUCH_CONFIG");
  95. if (p && *p)
  96. return p;
  97. p = getenv("HOME");
  98. if (p && *p)
  99. return string(p) + "/.notmuch-config";
  100. throw runtime_error ("Cannot find HOME directory\n");
  101. }
  102. string
  103. notmuch_db::get_config(const char *config, int *err)
  104. {
  105. const char *av[] { "notmuch", "config", "get", config, nullptr };
  106. return run_notmuch(av, nullptr, err);
  107. }
  108. void
  109. notmuch_db::set_config(const char *config, ...)
  110. {
  111. va_list ap;
  112. va_start(ap, config);
  113. vector<const char *> av { "notmuch", "config", "set", config };
  114. const char *a;
  115. do {
  116. a = va_arg(ap, const char *);
  117. av.push_back(a);
  118. } while (a);
  119. run_notmuch(av.data(), "[notmuch] ");
  120. }
  121. notmuch_db::notmuch_db(string config, bool create)
  122. : notmuch_config (config),
  123. maildir (chomp(get_config("database.path"))),
  124. new_tags (lines(get_config("new.tags"))),
  125. and_tags (make_and_tags()),
  126. sync_flags (conf_to_bool(get_config("maildir.synchronize_flags")))
  127. {
  128. if (maildir.empty())
  129. throw runtime_error(notmuch_config + ": no database.path in config file");
  130. if (create) {
  131. struct stat sb;
  132. string nmdir = maildir + "/.notmuch";
  133. int err = stat(nmdir.c_str(), &sb);
  134. if (!err && S_ISDIR(sb.st_mode))
  135. return;
  136. if (!err || errno != ENOENT)
  137. throw runtime_error(nmdir + ": cannot access directory");
  138. mkdir(maildir.c_str(), 0777);
  139. nmtry("notmuch_database_create",
  140. notmuch_database_create(maildir.c_str(), &notmuch_));
  141. }
  142. }
  143. notmuch_db::~notmuch_db()
  144. {
  145. close();
  146. }
  147. notmuch_db::tags_t
  148. notmuch_db::make_and_tags()
  149. {
  150. int err;
  151. string s = get_config("muchsync.and_tags", &err);
  152. return err ? new_tags : lines(s);
  153. }
  154. string
  155. notmuch_db::run_notmuch(const char *const *av, const char *errprefix,
  156. int *exit_value)
  157. {
  158. int fds[2];
  159. if (pipe(fds) != 0)
  160. throw runtime_error (string("pipe: ") + strerror(errno));
  161. pid_t pid = fork();
  162. switch (pid) {
  163. case -1:
  164. {
  165. string err = string("fork: ") + strerror(errno);
  166. ::close(fds[0]);
  167. ::close(fds[1]);
  168. throw runtime_error (err);
  169. }
  170. case 0:
  171. ::close(fds[0]);
  172. if (errprefix && fds[1] != 2)
  173. dup2(fds[1], 2);
  174. if (fds[1] != 1) {
  175. dup2(fds[1], 1);
  176. if (errprefix && fds[1] != 2)
  177. ::close(fds[1]);
  178. }
  179. setenv("NOTMUCH_CONFIG", notmuch_config.c_str(), 1);
  180. int err = -1;
  181. if (exit_value) {
  182. // Since the caller is looking at exit value, suppress chatter
  183. err = dup(2);
  184. ::close(2);
  185. fcntl(err, F_SETFD, 1);
  186. ::open("/dev/null", O_WRONLY);
  187. }
  188. execvp("notmuch", const_cast<char *const*> (av));
  189. if (err != -1)
  190. dup2(err, 2);
  191. cerr << "notmuch: " << strerror(errno) << endl;
  192. // Use SIGINT as hacky way to convey that exec failed
  193. raise(SIGINT);
  194. _exit(127);
  195. }
  196. ::close(fds[1]);
  197. ifdstream in (fds[0]);
  198. ostringstream os;
  199. if (errprefix) {
  200. string line;
  201. while (getline(in, line))
  202. cerr << errprefix << line << '\n';
  203. }
  204. else
  205. os << in.rdbuf();
  206. int status;
  207. if (waitpid(pid, &status, 0) != pid)
  208. assert(!"waitpid failed waiting for notmuch");
  209. else if (!WIFEXITED(status)) {
  210. if (WIFSIGNALED(status)) {
  211. if (WTERMSIG(status) == SIGINT)
  212. throw runtime_error ("could not run notmuch");
  213. else
  214. throw runtime_error ("notmuch exited with signal "
  215. + std::to_string(WTERMSIG(status)));
  216. }
  217. else
  218. throw runtime_error ("notmuch exit status " + std::to_string(status));
  219. }
  220. if (exit_value)
  221. *exit_value = WEXITSTATUS(status);
  222. return os.str();
  223. }
  224. Xapian::docid
  225. notmuch_db::get_dir_docid(const char *path)
  226. {
  227. unique_obj<notmuch_directory_t, notmuch_directory_destroy> dir;
  228. nmtry("notmuch_database_get_directory",
  229. notmuch_database_get_directory(notmuch(), path, &dir.get()));
  230. if (!dir)
  231. throw range_error (path + string (": directory not found in notmuch"));
  232. /* XXX -- evil evil */
  233. struct fake_directory {
  234. notmuch_database_t *notmuch;
  235. Xapian::docid doc_id;
  236. };
  237. return reinterpret_cast<const fake_directory *>(dir.get())->doc_id;
  238. }
  239. notmuch_database_t *
  240. notmuch_db::notmuch ()
  241. {
  242. if (!notmuch_) {
  243. notmuch_status_t err =
  244. notmuch_database_open (maildir.c_str(),
  245. NOTMUCH_DATABASE_MODE_READ_WRITE,
  246. &notmuch_);
  247. if (err)
  248. throw runtime_error (maildir + ": "
  249. + notmuch_status_to_string(err));
  250. }
  251. return notmuch_;
  252. }
  253. void
  254. notmuch_db::close()
  255. {
  256. if (notmuch_)
  257. notmuch_database_destroy (notmuch_);
  258. notmuch_ = nullptr;
  259. }
  260. void
  261. notmuch_db::run_new(const char *prefix)
  262. {
  263. const char *av[] = { "notmuch", "new", nullptr };
  264. close();
  265. run_notmuch(av, prefix);
  266. }