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 query = notmuch_query_create (notmuch, query_string);
54 fprintf (stderr, "Out of memory.\n");
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;
94 /* Set up our handler for SIGINT */
95 memset (&action, 0, sizeof (struct sigaction));
96 action.sa_handler = handle_sigint;
97 sigemptyset (&action.sa_mask);
98 action.sa_flags = SA_RESTART;
99 sigaction (SIGINT, &action, NULL);
101 notmuch_opt_desc_t options[] = {
102 { .opt_inherit = notmuch_shared_indexing_options },
103 { .opt_inherit = notmuch_shared_options },
107 opt_index = parse_arguments (argc, argv, options, 1);
111 notmuch_process_shared_options (argv[0]);
113 notmuch_exit_if_unmatched_db_uuid (notmuch);
115 status = notmuch_process_shared_indexing_options (notmuch);
116 if (status != NOTMUCH_STATUS_SUCCESS) {
117 fprintf (stderr, "Error: Failed to process index options. (%s)\n",
118 notmuch_status_to_string (status));
122 query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
123 if (query_string == NULL) {
124 fprintf (stderr, "Out of memory\n");
128 if (*query_string == '\0') {
129 fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
133 ret = reindex_query (notmuch, query_string, indexing_cli_choices.opts);
135 notmuch_database_destroy (notmuch);
137 return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;