]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
version: bump to 0.24.1
[notmuch] / notmuch-count.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  * Copyright © 2009 Keith Packard
5  *
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.
10  *
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.
15  *
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/ .
18  *
19  * Author: Keith Packard <keithp@keithp.com>
20  */
21
22 #include "notmuch-client.h"
23
24 enum {
25     OUTPUT_THREADS,
26     OUTPUT_MESSAGES,
27     OUTPUT_FILES,
28 };
29
30 /* The following is to allow future options to be added more easily */
31 enum {
32     EXCLUDE_TRUE,
33     EXCLUDE_FALSE,
34 };
35
36 /* Return the number of files matching the query, or -1 for an error */
37 static int
38 count_files (notmuch_query_t *query)
39 {
40     notmuch_messages_t *messages;
41     notmuch_message_t *message;
42     notmuch_filenames_t *filenames;
43     notmuch_status_t status;
44     int count = 0;
45
46     status = notmuch_query_search_messages_st (query, &messages);
47     if (print_status_query ("notmuch count", query, status))
48         return -1;
49
50     for (;
51          notmuch_messages_valid (messages);
52          notmuch_messages_move_to_next (messages)) {
53         message = notmuch_messages_get (messages);
54         filenames = notmuch_message_get_filenames (message);
55
56         for (;
57              notmuch_filenames_valid (filenames);
58              notmuch_filenames_move_to_next (filenames))
59             count++;
60
61         notmuch_filenames_destroy (filenames);
62         notmuch_message_destroy (message);
63     }
64
65     notmuch_messages_destroy (messages);
66
67     return count;
68 }
69
70 /* return 0 on success, -1 on failure */
71 static int
72 print_count (notmuch_database_t *notmuch, const char *query_str,
73              const char **exclude_tags, size_t exclude_tags_length, int output, int print_lastmod)
74 {
75     notmuch_query_t *query;
76     size_t i;
77     int count;
78     unsigned int ucount;
79     unsigned long revision;
80     const char *uuid;
81     int ret = 0;
82     notmuch_status_t status;
83
84     query = notmuch_query_create (notmuch, query_str);
85     if (query == NULL) {
86         fprintf (stderr, "Out of memory\n");
87         return -1;
88     }
89
90     for (i = 0; i < exclude_tags_length; i++)
91         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
92
93     switch (output) {
94     case OUTPUT_MESSAGES:
95         status = notmuch_query_count_messages_st (query, &ucount);
96         if (print_status_query ("notmuch count", query, status))
97             return -1;
98         printf ("%u", ucount);
99         break;
100     case OUTPUT_THREADS:
101         status = notmuch_query_count_threads_st (query, &ucount);
102         if (print_status_query ("notmuch count", query, status))
103             return -1;
104         printf ("%u", ucount);
105         break;
106     case OUTPUT_FILES:
107         count = count_files (query);
108         if (count >= 0) {
109             printf ("%u", count);
110         } else {
111             ret = -1;
112             goto DONE;
113         }
114         break;
115     }
116
117     if (print_lastmod) {
118         revision = notmuch_database_get_revision (notmuch, &uuid);
119         printf ("\t%s\t%lu\n", uuid, revision);
120     } else {
121         fputs ("\n", stdout);
122     }
123
124   DONE:
125     notmuch_query_destroy (query);
126
127     return ret;
128 }
129
130 static int
131 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
132             size_t exclude_tags_length, int output, int print_lastmod)
133 {
134     char *line = NULL;
135     ssize_t line_len;
136     size_t line_size;
137     int ret = 0;
138
139     while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
140         chomp_newline (line);
141         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
142                            output, print_lastmod);
143     }
144
145     if (line)
146         free (line);
147
148     return ret;
149 }
150
151 int
152 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
153 {
154     notmuch_database_t *notmuch;
155     char *query_str;
156     int opt_index;
157     int output = OUTPUT_MESSAGES;
158     int exclude = EXCLUDE_TRUE;
159     const char **search_exclude_tags = NULL;
160     size_t search_exclude_tags_length = 0;
161     notmuch_bool_t batch = FALSE;
162     notmuch_bool_t print_lastmod = FALSE;
163     FILE *input = stdin;
164     char *input_file_name = NULL;
165     int ret;
166
167     notmuch_opt_desc_t options[] = {
168         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
169           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
170                                   { "messages", OUTPUT_MESSAGES },
171                                   { "files", OUTPUT_FILES },
172                                   { 0, 0 } } },
173         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
174           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
175                                   { "false", EXCLUDE_FALSE },
176                                   { 0, 0 } } },
177         { NOTMUCH_OPT_BOOLEAN, &print_lastmod, "lastmod", 'l', 0 },
178         { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
179         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
180         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
181         { 0, 0, 0, 0, 0 }
182     };
183
184     opt_index = parse_arguments (argc, argv, options, 1);
185     if (opt_index < 0)
186         return EXIT_FAILURE;
187
188     notmuch_process_shared_options (argv[0]);
189
190     if (input_file_name) {
191         batch = TRUE;
192         input = fopen (input_file_name, "r");
193         if (input == NULL) {
194             fprintf (stderr, "Error opening %s for reading: %s\n",
195                      input_file_name, strerror (errno));
196             return EXIT_FAILURE;
197         }
198     }
199
200     if (batch && opt_index != argc) {
201         fprintf (stderr, "--batch and query string are not compatible\n");
202         return EXIT_FAILURE;
203     }
204
205     if (notmuch_database_open (notmuch_config_get_database_path (config),
206                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
207         return EXIT_FAILURE;
208
209     notmuch_exit_if_unmatched_db_uuid (notmuch);
210
211     query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
212     if (query_str == NULL) {
213         fprintf (stderr, "Out of memory.\n");
214         return EXIT_FAILURE;
215     }
216
217     if (exclude == EXCLUDE_TRUE) {
218         search_exclude_tags = notmuch_config_get_search_exclude_tags
219             (config, &search_exclude_tags_length);
220     }
221
222     if (batch)
223         ret = count_file (notmuch, input, search_exclude_tags,
224                           search_exclude_tags_length, output, print_lastmod);
225     else
226         ret = print_count (notmuch, query_str, search_exclude_tags,
227                            search_exclude_tags_length, output, print_lastmod);
228
229     notmuch_database_destroy (notmuch);
230
231     if (input != stdin)
232         fclose (input);
233
234     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
235 }