2017-06-04 14:32:35 +02:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2016 Daniel Kahn Gillmor
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-04-29 10:35:30 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2017-06-04 14:32:35 +02:00
|
|
|
*
|
|
|
|
* Author: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
|
|
|
#include "string-util.h"
|
|
|
|
|
|
|
|
static volatile sig_atomic_t interrupted;
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigint (unused (int sig))
|
|
|
|
{
|
2021-05-13 01:40:23 +02:00
|
|
|
static const char msg[] = "Stopping... \n";
|
2017-06-04 14:32:35 +02:00
|
|
|
|
|
|
|
/* This write is "opportunistic", so it's okay to ignore the
|
|
|
|
* result. It is not required for correctness, and if it does
|
|
|
|
* fail or produce a short write, we want to get out of the signal
|
|
|
|
* handler as quickly as possible, not retry it. */
|
|
|
|
IGNORE_RESULT (write (2, msg, sizeof (msg) - 1));
|
|
|
|
interrupted = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reindex all messages matching 'query_string' using the passed-in indexopts
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
reindex_query (notmuch_database_t *notmuch, const char *query_string,
|
reindex: drop notmuch_param_t, use notmuch_indexopts_t instead
There are at least three places in notmuch that can trigger an
indexing action:
* notmuch new
* notmuch insert
* notmuch reindex
I have plans to add some indexing options (e.g. indexing the cleartext
of encrypted parts, external filters, automated property injection)
that should properly be available in all places where indexing
happens.
I also want those indexing options to be exposed by (and constrained
by) the libnotmuch C API.
This isn't yet an API break because we've never made a release with
notmuch_param_t.
These indexing options are relevant in the listed places (and in the
libnotmuch analogues), but they aren't relevant in the other kinds of
functionality that notmuch offers (e.g. dump/restore, tagging, search,
show, reply).
So i think a generic "param" object isn't well-suited for this case.
In particular:
* a param object sounds like it could contain parameters for some
other (non-indexing) operation. This sounds confusing -- why would
i pass non-indexing parameters to a function that only does
indexing?
* bremner suggests online a generic param object would actually be
passed as a list of param objects, argv-style. In this case (at
least in the obvious argv implementation), the params might be some
sort of generic string. This introduces a problem where the API of
the library doesn't grow as new options are added, which means that
when code outside the library tries to use a feature, it first has
to test for it, and have code to handle it not being available.
The indexopts approach proposed here instead makes it clear at
compile time and at dynamic link time that there is an explicit
dependency on that feature, which allows automated tools to keep
track of what's needed and keeps the actual code simple.
My proposal adds the notmuch_indexopts_t as an opaque struct, so that
we can extend the list of options without causing ABI breakage.
The cost of this proposal appears to be that the "boilerplate" API
increases a little bit, with a generic constructor and destructor
function for the indexopts struct.
More patches will follow that make use of this indexopts approach.
2017-08-18 01:14:26 +02:00
|
|
|
notmuch_indexopts_t *indexopts)
|
2017-06-04 14:32:35 +02:00
|
|
|
{
|
|
|
|
notmuch_query_t *query;
|
|
|
|
notmuch_messages_t *messages;
|
|
|
|
notmuch_message_t *message;
|
|
|
|
notmuch_status_t status;
|
|
|
|
|
|
|
|
notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
|
|
|
|
|
|
|
|
query = notmuch_query_create (notmuch, query_string);
|
|
|
|
if (query == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* reindexing is not interested in any special sort order */
|
|
|
|
notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
|
|
|
|
|
|
|
|
status = notmuch_query_search_messages (query, &messages);
|
|
|
|
if (print_status_query ("notmuch reindex", query, status))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
ret = notmuch_database_begin_atomic (notmuch);
|
|
|
|
for (;
|
|
|
|
notmuch_messages_valid (messages) && ! interrupted;
|
|
|
|
notmuch_messages_move_to_next (messages)) {
|
|
|
|
message = notmuch_messages_get (messages);
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
ret = notmuch_message_reindex (message, indexopts);
|
2017-06-04 14:32:35 +02:00
|
|
|
if (ret != NOTMUCH_STATUS_SUCCESS)
|
|
|
|
break;
|
2019-04-16 03:46:15 +02:00
|
|
|
notmuch_message_destroy (message);
|
2017-06-04 14:32:35 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 12:31:01 +02:00
|
|
|
if (! ret)
|
2017-06-04 14:32:35 +02:00
|
|
|
ret = notmuch_database_end_atomic (notmuch);
|
|
|
|
|
|
|
|
notmuch_query_destroy (query);
|
|
|
|
|
|
|
|
return ret || interrupted;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-02-27 15:07:45 +01:00
|
|
|
notmuch_reindex_command (notmuch_database_t *notmuch, int argc, char *argv[])
|
2017-06-04 14:32:35 +02:00
|
|
|
{
|
|
|
|
char *query_string = NULL;
|
|
|
|
struct sigaction action;
|
|
|
|
int opt_index;
|
|
|
|
int ret;
|
2017-10-21 04:25:48 +02:00
|
|
|
notmuch_status_t status;
|
2017-06-04 14:32:35 +02:00
|
|
|
|
|
|
|
/* Set up our handler for SIGINT */
|
|
|
|
memset (&action, 0, sizeof (struct sigaction));
|
|
|
|
action.sa_handler = handle_sigint;
|
|
|
|
sigemptyset (&action.sa_mask);
|
|
|
|
action.sa_flags = SA_RESTART;
|
|
|
|
sigaction (SIGINT, &action, NULL);
|
|
|
|
|
|
|
|
notmuch_opt_desc_t options[] = {
|
2017-10-21 04:25:48 +02:00
|
|
|
{ .opt_inherit = notmuch_shared_indexing_options },
|
cli: use designated initializers for opt desc
Several changes at once, just to not have to change the same lines
several times over:
- Use designated initializers to initialize opt desc arrays.
- Only initialize the needed fields.
- Remove arg_id (short options) as unused.
- Replace opt_type and output_var with several type safe output
variables, where the output variable being non-NULL determines the
type. Introduce checks to ensure only one is set. The downside is
some waste of const space per argument; this could be saved by
retaining opt_type and using a union, but that's still pretty
verbose.
- Fix some variables due to the type safety. Mostly a good thing, but
leads to some enums being changed to ints. This is pedantically
correct, but somewhat annoying. We could also cast, but that defeats
the purpose a bit.
- Terminate the opt desc arrays using {}.
The output variable type safety and the ability to add new fields for
just some output types or arguments are the big wins. For example, if
we wanted to add a variable to set when the argument is present, we
could do so for just the arguments that need it.
Beauty is in the eye of the beholder, but I think this looks nice when
defining the arguments, and reduces some of the verbosity we have
there.
2017-10-01 22:53:11 +02:00
|
|
|
{ .opt_inherit = notmuch_shared_options },
|
|
|
|
{ }
|
2017-06-04 14:32:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
|
|
|
if (opt_index < 0)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
notmuch_process_shared_options (argv[0]);
|
|
|
|
|
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch);
|
|
|
|
|
2019-05-02 15:19:41 +02:00
|
|
|
status = notmuch_process_shared_indexing_options (notmuch);
|
2017-10-21 04:25:48 +02:00
|
|
|
if (status != NOTMUCH_STATUS_SUCCESS) {
|
|
|
|
fprintf (stderr, "Error: Failed to process index options. (%s)\n",
|
|
|
|
notmuch_status_to_string (status));
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-10-06 01:35:26 +02:00
|
|
|
query_string = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
|
2017-06-04 14:32:35 +02:00
|
|
|
if (query_string == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*query_string == '\0') {
|
|
|
|
fprintf (stderr, "Error: notmuch reindex requires at least one search term.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-06-13 12:31:01 +02:00
|
|
|
|
2017-10-21 04:25:48 +02:00
|
|
|
ret = reindex_query (notmuch, query_string, indexing_cli_choices.opts);
|
2017-06-04 14:32:35 +02:00
|
|
|
|
|
|
|
notmuch_database_destroy (notmuch);
|
|
|
|
|
|
|
|
return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|