]> git.notmuchmail.org Git - notmuch/blob - notmuch-reindex.c
index: explicitly follow GObject conventions
[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     }
75
76     if (!ret)
77         ret = notmuch_database_end_atomic (notmuch);
78
79     notmuch_query_destroy (query);
80
81     return ret || interrupted;
82 }
83
84 int
85 notmuch_reindex_command (notmuch_config_t *config, int argc, char *argv[])
86 {
87     char *query_string = NULL;
88     notmuch_database_t *notmuch;
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 (argv[0]);
112
113     if (notmuch_database_open (notmuch_config_get_database_path (config),
114                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
115         return EXIT_FAILURE;
116
117     notmuch_exit_if_unmatched_db_uuid (notmuch);
118
119     status = notmuch_process_shared_indexing_options (notmuch, config);
120     if (status != NOTMUCH_STATUS_SUCCESS) {
121         fprintf (stderr, "Error: Failed to process index options. (%s)\n",
122                  notmuch_status_to_string (status));
123         return EXIT_FAILURE;
124     }
125
126     query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
127     if (query_string == NULL) {
128         fprintf (stderr, "Out of memory\n");
129         return EXIT_FAILURE;
130     }
131
132     if (*query_string == '\0') {
133         fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
134         return EXIT_FAILURE;
135     }
136     
137     ret = reindex_query (notmuch, query_string, indexing_cli_choices.opts);
138
139     notmuch_database_destroy (notmuch);
140
141     return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
142 }