1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2016 Daniel Kahn Gillmor
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
21 #include "notmuch-client.h"
22 #include "string-util.h"
24 static volatile sig_atomic_t interrupted;
27 handle_sigint (unused (int sig))
29 static const char msg[] = "Stopping... \n";
31 /* This write is "opportunistic", so it's okay to ignore the
32 * result. It is not required for correctness, and if it does
33 * fail or produce a short write, we want to get out of the signal
34 * handler as quickly as possible, not retry it. */
35 IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
39 /* reindex all messages matching 'query_string' using the passed-in indexopts
42 reindex_query (notmuch_database_t *notmuch, const char *query_string,
43 notmuch_indexopts_t *indexopts)
45 notmuch_query_t *query;
46 notmuch_messages_t *messages;
47 notmuch_message_t *message;
48 notmuch_status_t status;
50 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
52 status = notmuch_query_create_with_syntax (notmuch, query_string,
53 shared_option_query_syntax (),
55 if (print_status_database ("notmuch reindex", notmuch, status))
58 /* reindexing is not interested in any special sort order */
59 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
61 status = notmuch_query_search_messages (query, &messages);
62 if (print_status_query ("notmuch reindex", query, status))
65 ret = notmuch_database_begin_atomic (notmuch);
67 notmuch_messages_valid (messages) && ! interrupted;
68 notmuch_messages_move_to_next (messages)) {
69 message = notmuch_messages_get (messages);
71 ret = notmuch_message_reindex (message, indexopts);
72 if (ret != NOTMUCH_STATUS_SUCCESS)
74 notmuch_message_destroy (message);
78 ret = notmuch_database_end_atomic (notmuch);
80 notmuch_query_destroy (query);
82 return ret || interrupted;
86 notmuch_reindex_command (notmuch_database_t *notmuch, int argc, char *argv[])
88 char *query_string = NULL;
89 struct sigaction action;
92 notmuch_status_t status;
93 notmuch_indexopts_t *indexopts = notmuch_database_get_default_indexopts (notmuch);
95 /* Set up our handler for SIGINT */
96 memset (&action, 0, sizeof (struct sigaction));
97 action.sa_handler = handle_sigint;
98 sigemptyset (&action.sa_mask);
99 action.sa_flags = SA_RESTART;
100 sigaction (SIGINT, &action, NULL);
102 notmuch_opt_desc_t options[] = {
103 { .opt_inherit = notmuch_shared_indexing_options },
104 { .opt_inherit = notmuch_shared_options },
108 opt_index = parse_arguments (argc, argv, options, 1);
112 notmuch_process_shared_options (notmuch, argv[0]);
114 status = notmuch_process_shared_indexing_options (indexopts);
115 if (status != NOTMUCH_STATUS_SUCCESS) {
116 fprintf (stderr, "Error: Failed to process index options. (%s)\n",
117 notmuch_status_to_string (status));
121 query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
122 if (query_string == NULL) {
123 fprintf (stderr, "Out of memory\n");
127 if (*query_string == '\0') {
128 fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
132 ret = reindex_query (notmuch, query_string, indexopts);
134 notmuch_database_destroy (notmuch);
136 return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;