1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2016 Daniel Kahn Gillmor
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
19  */
20 
21 #include "notmuch-client.h"
22 #include "string-util.h"
23 
24 static volatile sig_atomic_t interrupted;
25 
26 static void
handle_sigint(unused (int sig))27 handle_sigint (unused (int sig))
28 {
29     static const char msg[] = "Stopping...         \n";
30 
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));
36     interrupted = 1;
37 }
38 
39 /* reindex all messages matching 'query_string' using the passed-in indexopts
40  */
41 static int
reindex_query(notmuch_database_t * notmuch,const char * query_string,notmuch_indexopts_t * indexopts)42 reindex_query (notmuch_database_t *notmuch, const char *query_string,
43 	       notmuch_indexopts_t *indexopts)
44 {
45     notmuch_query_t *query;
46     notmuch_messages_t *messages;
47     notmuch_message_t *message;
48     notmuch_status_t status;
49 
50     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
51 
52     status = notmuch_query_create_with_syntax (notmuch, query_string,
53 					       shared_option_query_syntax (),
54 					       &query);
55     if (print_status_database ("notmuch reindex", notmuch, status))
56 	return 1;
57 
58     /* reindexing is not interested in any special sort order */
59     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
60 
61     status = notmuch_query_search_messages (query, &messages);
62     if (print_status_query ("notmuch reindex", query, status))
63 	return status;
64 
65     ret = notmuch_database_begin_atomic (notmuch);
66     for (;
67 	 notmuch_messages_valid (messages) && ! interrupted;
68 	 notmuch_messages_move_to_next (messages)) {
69 	message = notmuch_messages_get (messages);
70 
71 	ret = notmuch_message_reindex (message, indexopts);
72 	if (ret != NOTMUCH_STATUS_SUCCESS)
73 	    break;
74 	notmuch_message_destroy (message);
75     }
76 
77     if (! ret)
78 	ret = notmuch_database_end_atomic (notmuch);
79 
80     notmuch_query_destroy (query);
81 
82     return ret || interrupted;
83 }
84 
85 int
notmuch_reindex_command(notmuch_database_t * notmuch,int argc,char * argv[])86 notmuch_reindex_command (notmuch_database_t *notmuch, int argc, char *argv[])
87 {
88     char *query_string = NULL;
89     struct sigaction action;
90     int opt_index;
91     int ret;
92     notmuch_status_t status;
93 
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);
100 
101     notmuch_opt_desc_t options[] = {
102 	{ .opt_inherit = notmuch_shared_indexing_options },
103 	{ .opt_inherit = notmuch_shared_options },
104 	{ }
105     };
106 
107     opt_index = parse_arguments (argc, argv, options, 1);
108     if (opt_index < 0)
109 	return EXIT_FAILURE;
110 
111     notmuch_process_shared_options (notmuch, argv[0]);
112 
113     status = notmuch_process_shared_indexing_options (notmuch);
114     if (status != NOTMUCH_STATUS_SUCCESS) {
115 	fprintf (stderr, "Error: Failed to process index options. (%s)\n",
116 		 notmuch_status_to_string (status));
117 	return EXIT_FAILURE;
118     }
119 
120     query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
121     if (query_string == NULL) {
122 	fprintf (stderr, "Out of memory\n");
123 	return EXIT_FAILURE;
124     }
125 
126     if (*query_string == '\0') {
127 	fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
128 	return EXIT_FAILURE;
129     }
130 
131     ret = reindex_query (notmuch, query_string, indexing_cli_choices.opts);
132 
133     notmuch_database_destroy (notmuch);
134 
135     return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
136 }
137