]> git.notmuchmail.org Git - notmuch/blob - notmuch-reindex.c
Merge branch 'release'
[notmuch] / notmuch-reindex.c
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
27 handle_sigint (unused (int sig))
28 {
29     static 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
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     query = notmuch_query_create (notmuch, query_string);
53     if (query == NULL) {
54         fprintf (stderr, "Out of memory.\n");
55         return 1;
56     }
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
86 notmuch_reindex_command (notmuch_config_t *config, int argc, char *argv[])
87 {
88     char *query_string = NULL;
89     notmuch_database_t *notmuch;
90     struct sigaction action;
91     int opt_index;
92     int ret;
93     notmuch_status_t status;
94
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);
101
102     notmuch_opt_desc_t options[] = {
103         { .opt_inherit = notmuch_shared_indexing_options },
104         { .opt_inherit = notmuch_shared_options },
105         { }
106     };
107
108     opt_index = parse_arguments (argc, argv, options, 1);
109     if (opt_index < 0)
110         return EXIT_FAILURE;
111
112     notmuch_process_shared_options (argv[0]);
113
114     if (notmuch_database_open (notmuch_config_get_database_path (config),
115                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
116         return EXIT_FAILURE;
117
118     notmuch_exit_if_unmatched_db_uuid (notmuch);
119
120     status = notmuch_process_shared_indexing_options (notmuch);
121     if (status != NOTMUCH_STATUS_SUCCESS) {
122         fprintf (stderr, "Error: Failed to process index options. (%s)\n",
123                  notmuch_status_to_string (status));
124         return EXIT_FAILURE;
125     }
126
127     query_string = query_string_from_args (config, argc - opt_index, argv + opt_index);
128     if (query_string == NULL) {
129         fprintf (stderr, "Out of memory\n");
130         return EXIT_FAILURE;
131     }
132
133     if (*query_string == '\0') {
134         fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
135         return EXIT_FAILURE;
136     }
137
138     ret = reindex_query (notmuch, query_string, indexing_cli_choices.opts);
139
140     notmuch_database_destroy (notmuch);
141
142     return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
143 }