]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
lib: add versions of n_q_count_{message,threads} with status return
[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 http://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 long revision;
79     const char *uuid;
80     int ret = 0;
81
82     query = notmuch_query_create (notmuch, query_str);
83     if (query == NULL) {
84         fprintf (stderr, "Out of memory\n");
85         return -1;
86     }
87
88     for (i = 0; i < exclude_tags_length; i++)
89         notmuch_query_add_tag_exclude (query, exclude_tags[i]);
90
91     switch (output) {
92     case OUTPUT_MESSAGES:
93         printf ("%u", notmuch_query_count_messages (query));
94         break;
95     case OUTPUT_THREADS:
96         printf ("%u", notmuch_query_count_threads (query));
97         break;
98     case OUTPUT_FILES:
99         count = count_files (query);
100         if (count >= 0) {
101             printf ("%u", count);
102         } else {
103             ret = -1;
104             goto DONE;
105         }
106         break;
107     }
108
109     if (print_lastmod) {
110         revision = notmuch_database_get_revision (notmuch, &uuid);
111         printf ("\t%s\t%lu\n", uuid, revision);
112     } else {
113         fputs ("\n", stdout);
114     }
115
116   DONE:
117     notmuch_query_destroy (query);
118
119     return ret;
120 }
121
122 static int
123 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
124             size_t exclude_tags_length, int output, int print_lastmod)
125 {
126     char *line = NULL;
127     ssize_t line_len;
128     size_t line_size;
129     int ret = 0;
130
131     while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
132         chomp_newline (line);
133         ret = print_count (notmuch, line, exclude_tags, exclude_tags_length,
134                            output, print_lastmod);
135     }
136
137     if (line)
138         free (line);
139
140     return ret;
141 }
142
143 int
144 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
145 {
146     notmuch_database_t *notmuch;
147     char *query_str;
148     int opt_index;
149     int output = OUTPUT_MESSAGES;
150     int exclude = EXCLUDE_TRUE;
151     const char **search_exclude_tags = NULL;
152     size_t search_exclude_tags_length = 0;
153     notmuch_bool_t batch = FALSE;
154     notmuch_bool_t print_lastmod = FALSE;
155     FILE *input = stdin;
156     char *input_file_name = NULL;
157     int ret;
158
159     notmuch_opt_desc_t options[] = {
160         { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
161           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
162                                   { "messages", OUTPUT_MESSAGES },
163                                   { "files", OUTPUT_FILES },
164                                   { 0, 0 } } },
165         { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
166           (notmuch_keyword_t []){ { "true", EXCLUDE_TRUE },
167                                   { "false", EXCLUDE_FALSE },
168                                   { 0, 0 } } },
169         { NOTMUCH_OPT_BOOLEAN, &print_lastmod, "lastmod", 'l', 0 },
170         { NOTMUCH_OPT_BOOLEAN, &batch, "batch", 0, 0 },
171         { NOTMUCH_OPT_STRING, &input_file_name, "input", 'i', 0 },
172         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
173         { 0, 0, 0, 0, 0 }
174     };
175
176     opt_index = parse_arguments (argc, argv, options, 1);
177     if (opt_index < 0)
178         return EXIT_FAILURE;
179
180     notmuch_process_shared_options (argv[0]);
181
182     if (input_file_name) {
183         batch = TRUE;
184         input = fopen (input_file_name, "r");
185         if (input == NULL) {
186             fprintf (stderr, "Error opening %s for reading: %s\n",
187                      input_file_name, strerror (errno));
188             return EXIT_FAILURE;
189         }
190     }
191
192     if (batch && opt_index != argc) {
193         fprintf (stderr, "--batch and query string are not compatible\n");
194         return EXIT_FAILURE;
195     }
196
197     if (notmuch_database_open (notmuch_config_get_database_path (config),
198                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
199         return EXIT_FAILURE;
200
201     notmuch_exit_if_unmatched_db_uuid (notmuch);
202
203     query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
204     if (query_str == NULL) {
205         fprintf (stderr, "Out of memory.\n");
206         return EXIT_FAILURE;
207     }
208
209     if (exclude == EXCLUDE_TRUE) {
210         search_exclude_tags = notmuch_config_get_search_exclude_tags
211             (config, &search_exclude_tags_length);
212     }
213
214     if (batch)
215         ret = count_file (notmuch, input, search_exclude_tags,
216                           search_exclude_tags_length, output, print_lastmod);
217     else
218         ret = print_count (notmuch, query_str, search_exclude_tags,
219                            search_exclude_tags_length, output, print_lastmod);
220
221     notmuch_database_destroy (notmuch);
222
223     if (input != stdin)
224         fclose (input);
225
226     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
227 }