1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
4 * Copyright © 2009 Keith Packard
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see https://www.gnu.org/licenses/ .
19 * Author: Keith Packard <keithp@keithp.com>
22 #include "notmuch-client.h"
30 /* Return the number of files matching the query, or -1 for an error */
32 count_files (notmuch_query_t *query)
34 notmuch_messages_t *messages;
35 notmuch_message_t *message;
36 notmuch_filenames_t *filenames;
37 notmuch_status_t status;
40 status = notmuch_query_search_messages (query, &messages);
41 if (print_status_query ("notmuch count", query, status))
45 notmuch_messages_valid (messages);
46 notmuch_messages_move_to_next (messages)) {
47 message = notmuch_messages_get (messages);
48 filenames = notmuch_message_get_filenames (message);
51 notmuch_filenames_valid (filenames);
52 notmuch_filenames_move_to_next (filenames))
55 notmuch_filenames_destroy (filenames);
56 notmuch_message_destroy (message);
59 notmuch_messages_destroy (messages);
64 /* return 0 on success, -1 on failure */
66 print_count (notmuch_database_t *notmuch, const char *query_str,
67 const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
69 notmuch_query_t *query;
73 unsigned long revision;
76 notmuch_status_t status;
78 query = notmuch_query_create (notmuch, query_str);
80 fprintf (stderr, "Out of memory\n");
84 for (i = 0; i < exclude_tags_length; i++) {
85 status = notmuch_query_add_tag_exclude (query, exclude_tags[i]);
86 if (status && status != NOTMUCH_STATUS_IGNORED) {
87 print_status_query ("notmuch count", query, status);
94 status = notmuch_query_count_messages (query, &ucount);
95 if (print_status_query ("notmuch count", query, status))
97 printf ("%u", ucount);
100 status = notmuch_query_count_threads (query, &ucount);
101 if (print_status_query ("notmuch count", query, status))
103 printf ("%u", ucount);
106 count = count_files (query);
108 printf ("%d", count);
117 revision = notmuch_database_get_revision (notmuch, &uuid);
118 printf ("\t%s\t%lu\n", uuid, revision);
120 fputs ("\n", stdout);
124 notmuch_query_destroy (query);
130 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
131 size_t exclude_tags_length, int output, int print_lastmod)
138 while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
139 chomp_newline (line);
140 ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
141 output, print_lastmod);
151 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
153 notmuch_database_t *notmuch;
156 int output = OUTPUT_MESSAGES;
158 const char **search_exclude_tags = NULL;
159 size_t search_exclude_tags_length = 0;
161 bool print_lastmod = false;
163 const char *input_file_name = NULL;
166 notmuch_opt_desc_t options[] = {
167 { .opt_keyword = &output, .name = "output", .keywords =
168 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
169 { "messages", OUTPUT_MESSAGES },
170 { "files", OUTPUT_FILES },
172 { .opt_bool = &exclude, .name = "exclude" },
173 { .opt_bool = &print_lastmod, .name = "lastmod" },
174 { .opt_bool = &batch, .name = "batch" },
175 { .opt_string = &input_file_name, .name = "input" },
176 { .opt_inherit = notmuch_shared_options },
180 opt_index = parse_arguments (argc, argv, options, 1);
184 notmuch_process_shared_options (argv[0]);
186 if (input_file_name) {
188 input = fopen (input_file_name, "r");
190 fprintf (stderr, "Error opening %s for reading: %s\n",
191 input_file_name, strerror (errno));
196 if (batch && opt_index != argc) {
197 fprintf (stderr, "--batch and query string are not compatible\n");
203 if (notmuch_database_open (notmuch_config_get_database_path (config),
204 NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
207 notmuch_exit_if_unmatched_db_uuid (notmuch);
209 query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
210 if (query_str == NULL) {
211 fprintf (stderr, "Out of memory.\n");
216 search_exclude_tags = notmuch_config_get_search_exclude_tags
217 (config, &search_exclude_tags_length);
221 ret = count_file (notmuch, input, search_exclude_tags,
222 search_exclude_tags_length, output, print_lastmod);
224 ret = print_count (notmuch, query_str, search_exclude_tags,
225 search_exclude_tags_length, output, print_lastmod);
227 notmuch_database_destroy (notmuch);
232 return ret ? EXIT_FAILURE : EXIT_SUCCESS;