2009-11-21 08:15:07 +01:00
|
|
|
/* notmuch - Not much of an email program, (just index and search)
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Carl Worth
|
|
|
|
* Copyright © 2009 Keith Packard
|
|
|
|
*
|
|
|
|
* 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
|
2016-06-02 18:26:14 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2009-11-21 08:15:07 +01:00
|
|
|
*
|
|
|
|
* Author: Keith Packard <keithp@keithp.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-client.h"
|
|
|
|
|
2012-01-14 00:07:02 +01:00
|
|
|
enum {
|
|
|
|
OUTPUT_THREADS,
|
|
|
|
OUTPUT_MESSAGES,
|
2013-08-17 14:11:29 +02:00
|
|
|
OUTPUT_FILES,
|
2012-01-14 00:07:02 +01:00
|
|
|
};
|
|
|
|
|
2012-04-07 18:10:04 +02:00
|
|
|
/* The following is to allow future options to be added more easily */
|
|
|
|
enum {
|
|
|
|
EXCLUDE_TRUE,
|
|
|
|
EXCLUDE_FALSE,
|
|
|
|
};
|
|
|
|
|
2015-09-06 15:15:46 +02:00
|
|
|
/* Return the number of files matching the query, or -1 for an error */
|
|
|
|
static int
|
2013-08-17 14:11:29 +02:00
|
|
|
count_files (notmuch_query_t *query)
|
|
|
|
{
|
|
|
|
notmuch_messages_t *messages;
|
|
|
|
notmuch_message_t *message;
|
|
|
|
notmuch_filenames_t *filenames;
|
2015-09-06 15:15:46 +02:00
|
|
|
notmuch_status_t status;
|
|
|
|
int count = 0;
|
2013-08-17 14:11:29 +02:00
|
|
|
|
2017-02-26 22:21:32 +01:00
|
|
|
status = notmuch_query_search_messages (query, &messages);
|
2015-09-06 15:15:46 +02:00
|
|
|
if (print_status_query ("notmuch count", query, status))
|
|
|
|
return -1;
|
2013-08-17 14:11:29 +02:00
|
|
|
|
|
|
|
for (;
|
|
|
|
notmuch_messages_valid (messages);
|
|
|
|
notmuch_messages_move_to_next (messages)) {
|
|
|
|
message = notmuch_messages_get (messages);
|
|
|
|
filenames = notmuch_message_get_filenames (message);
|
|
|
|
|
|
|
|
for (;
|
|
|
|
notmuch_filenames_valid (filenames);
|
|
|
|
notmuch_filenames_move_to_next (filenames))
|
|
|
|
count++;
|
|
|
|
|
|
|
|
notmuch_filenames_destroy (filenames);
|
|
|
|
notmuch_message_destroy (message);
|
|
|
|
}
|
|
|
|
|
|
|
|
notmuch_messages_destroy (messages);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:31:56 +02:00
|
|
|
/* return 0 on success, -1 on failure */
|
2013-03-31 11:45:59 +02:00
|
|
|
static int
|
|
|
|
print_count (notmuch_database_t *notmuch, const char *query_str,
|
2015-06-05 08:16:17 +02:00
|
|
|
const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
|
2013-03-31 11:45:59 +02:00
|
|
|
{
|
|
|
|
notmuch_query_t *query;
|
|
|
|
size_t i;
|
2015-09-06 15:15:46 +02:00
|
|
|
int count;
|
2015-09-27 17:31:58 +02:00
|
|
|
unsigned int ucount;
|
2015-06-05 08:16:17 +02:00
|
|
|
unsigned long revision;
|
|
|
|
const char *uuid;
|
|
|
|
int ret = 0;
|
2015-09-27 17:31:58 +02:00
|
|
|
notmuch_status_t status;
|
2013-03-31 11:45:59 +02:00
|
|
|
|
|
|
|
query = notmuch_query_create (notmuch, query_str);
|
|
|
|
if (query == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory\n");
|
2015-09-27 17:31:56 +02:00
|
|
|
return -1;
|
2013-03-31 11:45:59 +02:00
|
|
|
}
|
|
|
|
|
2017-02-18 16:08:04 +01:00
|
|
|
for (i = 0; i < exclude_tags_length; i++) {
|
|
|
|
status = notmuch_query_add_tag_exclude (query, exclude_tags[i]);
|
|
|
|
if (status && status != NOTMUCH_STATUS_IGNORED) {
|
|
|
|
print_status_query ("notmuch count", query, status);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-03-31 11:45:59 +02:00
|
|
|
|
|
|
|
switch (output) {
|
|
|
|
case OUTPUT_MESSAGES:
|
2017-02-26 22:21:34 +01:00
|
|
|
status = notmuch_query_count_messages (query, &ucount);
|
2015-09-27 17:31:58 +02:00
|
|
|
if (print_status_query ("notmuch count", query, status))
|
|
|
|
return -1;
|
|
|
|
printf ("%u", ucount);
|
2013-03-31 11:45:59 +02:00
|
|
|
break;
|
|
|
|
case OUTPUT_THREADS:
|
2017-02-26 22:21:35 +01:00
|
|
|
status = notmuch_query_count_threads (query, &ucount);
|
2015-09-27 17:31:58 +02:00
|
|
|
if (print_status_query ("notmuch count", query, status))
|
|
|
|
return -1;
|
|
|
|
printf ("%u", ucount);
|
2013-03-31 11:45:59 +02:00
|
|
|
break;
|
2013-08-17 14:11:29 +02:00
|
|
|
case OUTPUT_FILES:
|
2015-09-06 15:15:46 +02:00
|
|
|
count = count_files (query);
|
|
|
|
if (count >= 0) {
|
2017-06-17 00:50:25 +02:00
|
|
|
printf ("%d", count);
|
2015-09-06 15:15:46 +02:00
|
|
|
} else {
|
|
|
|
ret = -1;
|
|
|
|
goto DONE;
|
|
|
|
}
|
2013-08-17 14:11:29 +02:00
|
|
|
break;
|
2013-03-31 11:45:59 +02:00
|
|
|
}
|
|
|
|
|
2015-06-05 08:16:17 +02:00
|
|
|
if (print_lastmod) {
|
|
|
|
revision = notmuch_database_get_revision (notmuch, &uuid);
|
|
|
|
printf ("\t%s\t%lu\n", uuid, revision);
|
|
|
|
} else {
|
|
|
|
fputs ("\n", stdout);
|
|
|
|
}
|
|
|
|
|
2015-09-06 15:15:46 +02:00
|
|
|
DONE:
|
2013-03-31 11:45:59 +02:00
|
|
|
notmuch_query_destroy (query);
|
|
|
|
|
2015-06-05 08:16:17 +02:00
|
|
|
return ret;
|
2013-03-31 11:45:59 +02:00
|
|
|
}
|
|
|
|
|
2013-03-31 11:46:00 +02:00
|
|
|
static int
|
|
|
|
count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
|
2015-06-05 08:16:17 +02:00
|
|
|
size_t exclude_tags_length, int output, int print_lastmod)
|
2013-03-31 11:46:00 +02:00
|
|
|
{
|
|
|
|
char *line = NULL;
|
|
|
|
ssize_t line_len;
|
|
|
|
size_t line_size;
|
|
|
|
int ret = 0;
|
|
|
|
|
2015-09-07 15:29:14 +02:00
|
|
|
while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
|
2013-03-31 11:46:00 +02:00
|
|
|
chomp_newline (line);
|
|
|
|
ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
|
2015-06-05 08:16:17 +02:00
|
|
|
output, print_lastmod);
|
2013-03-31 11:46:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (line)
|
|
|
|
free (line);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-11-21 08:15:07 +01:00
|
|
|
int
|
2013-03-03 22:55:09 +01:00
|
|
|
notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
|
2009-11-21 08:15:07 +01:00
|
|
|
{
|
|
|
|
notmuch_database_t *notmuch;
|
|
|
|
char *query_str;
|
2012-01-14 00:07:02 +01:00
|
|
|
int opt_index;
|
|
|
|
int output = OUTPUT_MESSAGES;
|
2012-04-07 18:10:04 +02:00
|
|
|
int exclude = EXCLUDE_TRUE;
|
2013-03-31 11:45:59 +02:00
|
|
|
const char **search_exclude_tags = NULL;
|
|
|
|
size_t search_exclude_tags_length = 0;
|
2013-03-31 11:46:00 +02:00
|
|
|
notmuch_bool_t batch = FALSE;
|
2015-06-05 08:16:17 +02:00
|
|
|
notmuch_bool_t print_lastmod = FALSE;
|
2013-03-31 11:46:00 +02:00
|
|
|
FILE *input = stdin;
|
|
|
|
char *input_file_name = NULL;
|
2013-03-31 11:45:59 +02:00
|
|
|
int ret;
|
2009-11-21 08:15:07 +01:00
|
|
|
|
2012-01-14 00:07:02 +01:00
|
|
|
notmuch_opt_desc_t options[] = {
|
|
|
|
{ NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
|
|
|
|
(notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
|
|
|
|
{ "messages", OUTPUT_MESSAGES },
|
2013-08-17 14:11:29 +02:00
|
|
|
{ "files", OUTPUT_FILES },
|
2012-01-14 00:07:02 +01:00
|
|
|
{ 0, 0 } } },
|
2012-04-07 18:10:04 +02:00
|
|
|
{ NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
|
|
|
|
(notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
|
|
|
|
{ "false", EXCLUDE_FALSE },
|
|
|
|
{ 0, 0 } } },
|
2015-06-05 08:16:17 +02:00
|
|
|
{ NOTMUCH_OPT_BOOLEAN, &print_lastmod, "lastmod", 'l', 0 },
|
2013-03-31 11:46:00 +02:00
|
|
|
{ NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
|
|
|
|
{ NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
|
2015-04-05 15:13:03 +02:00
|
|
|
{ NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
|
2012-01-14 00:07:02 +01:00
|
|
|
{ 0, 0, 0, 0, 0 }
|
|
|
|
};
|
2011-10-21 14:19:17 +02:00
|
|
|
|
2012-01-14 00:07:02 +01:00
|
|
|
opt_index = parse_arguments (argc, argv, options, 1);
|
2014-01-10 22:28:53 +01:00
|
|
|
if (opt_index < 0)
|
|
|
|
return EXIT_FAILURE;
|
2009-11-21 08:15:07 +01:00
|
|
|
|
2015-04-05 15:13:03 +02:00
|
|
|
notmuch_process_shared_options (argv[0]);
|
|
|
|
|
2013-03-31 11:46:00 +02:00
|
|
|
if (input_file_name) {
|
|
|
|
batch = TRUE;
|
|
|
|
input = fopen (input_file_name, "r");
|
|
|
|
if (input == NULL) {
|
|
|
|
fprintf (stderr, "Error opening %s for reading: %s\n",
|
|
|
|
input_file_name, strerror (errno));
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-03-31 11:46:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (batch && opt_index != argc) {
|
|
|
|
fprintf (stderr, "--batch and query string are not compatible\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2013-03-31 11:46:00 +02:00
|
|
|
}
|
|
|
|
|
2012-04-30 18:25:33 +02:00
|
|
|
if (notmuch_database_open (notmuch_config_get_database_path (config),
|
|
|
|
NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-11-21 08:15:07 +01:00
|
|
|
|
2015-04-06 00:39:55 +02:00
|
|
|
notmuch_exit_if_unmatched_db_uuid (notmuch);
|
|
|
|
|
2015-09-07 15:29:14 +02:00
|
|
|
query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
|
2009-11-21 08:15:07 +01:00
|
|
|
if (query_str == NULL) {
|
|
|
|
fprintf (stderr, "Out of memory.\n");
|
2014-01-10 22:28:53 +01:00
|
|
|
return EXIT_FAILURE;
|
2009-11-21 08:15:07 +01:00
|
|
|
}
|
2010-04-10 01:49:58 +02:00
|
|
|
|
2012-04-07 18:10:04 +02:00
|
|
|
if (exclude == EXCLUDE_TRUE) {
|
2012-03-01 23:30:33 +01:00
|
|
|
search_exclude_tags = notmuch_config_get_search_exclude_tags
|
|
|
|
(config, &search_exclude_tags_length);
|
|
|
|
}
|
2012-01-15 01:17:34 +01:00
|
|
|
|
2013-03-31 11:46:00 +02:00
|
|
|
if (batch)
|
|
|
|
ret = count_file (notmuch, input, search_exclude_tags,
|
2015-06-05 08:16:17 +02:00
|
|
|
search_exclude_tags_length, output, print_lastmod);
|
2013-03-31 11:46:00 +02:00
|
|
|
else
|
|
|
|
ret = print_count (notmuch, query_str, search_exclude_tags,
|
2015-06-05 08:16:17 +02:00
|
|
|
search_exclude_tags_length, output, print_lastmod);
|
2009-11-21 08:15:07 +01:00
|
|
|
|
2012-04-22 14:07:53 +02:00
|
|
|
notmuch_database_destroy (notmuch);
|
2009-11-21 08:15:07 +01:00
|
|
|
|
2013-03-31 11:46:00 +02:00
|
|
|
if (input != stdin)
|
|
|
|
fclose (input);
|
|
|
|
|
2014-01-10 22:28:53 +01:00
|
|
|
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
|
2009-11-21 08:15:07 +01:00
|
|
|
}
|