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 /* Return the number of files matching the query, or -1 for an error */
31 static int
count_files(notmuch_query_t * query)32 count_files (notmuch_query_t *query)
33 {
34 notmuch_messages_t *messages;
35 notmuch_message_t *message;
36 notmuch_filenames_t *filenames;
37 notmuch_status_t status;
38 int count = 0;
39
40 status = notmuch_query_search_messages (query, &messages);
41 if (print_status_query ("notmuch count", query, status))
42 return -1;
43
44 for (;
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);
49
50 for (;
51 notmuch_filenames_valid (filenames);
52 notmuch_filenames_move_to_next (filenames))
53 count++;
54
55 notmuch_filenames_destroy (filenames);
56 notmuch_message_destroy (message);
57 }
58
59 notmuch_messages_destroy (messages);
60
61 return count;
62 }
63
64 /* return 0 on success, -1 on failure */
65 static int
print_count(notmuch_database_t * notmuch,const char * query_str,notmuch_config_values_t * exclude_tags,int output,int print_lastmod)66 print_count (notmuch_database_t *notmuch, const char *query_str,
67 notmuch_config_values_t *exclude_tags, int output, int print_lastmod)
68 {
69 notmuch_query_t *query;
70 int count;
71 unsigned int ucount;
72 unsigned long revision;
73 const char *uuid;
74 int ret = 0;
75 notmuch_status_t status;
76
77 status = notmuch_query_create_with_syntax (notmuch, query_str,
78 shared_option_query_syntax (),
79 &query);
80 if (print_status_database ("notmuch count", notmuch, status)) {
81 ret = -1;
82 goto DONE;
83 }
84
85 for (notmuch_config_values_start (exclude_tags);
86 notmuch_config_values_valid (exclude_tags);
87 notmuch_config_values_move_to_next (exclude_tags)) {
88
89 status = notmuch_query_add_tag_exclude (query,
90 notmuch_config_values_get (exclude_tags));
91 if (status && status != NOTMUCH_STATUS_IGNORED) {
92 print_status_query ("notmuch count", query, status);
93 ret = -1;
94 goto DONE;
95 }
96 }
97
98 switch (output) {
99 case OUTPUT_MESSAGES:
100 status = notmuch_query_count_messages (query, &ucount);
101 if (print_status_query ("notmuch count", query, status))
102 return -1;
103 printf ("%u", ucount);
104 break;
105 case OUTPUT_THREADS:
106 status = notmuch_query_count_threads (query, &ucount);
107 if (print_status_query ("notmuch count", query, status))
108 return -1;
109 printf ("%u", ucount);
110 break;
111 case OUTPUT_FILES:
112 count = count_files (query);
113 if (count >= 0) {
114 printf ("%d", count);
115 } else {
116 ret = -1;
117 goto DONE;
118 }
119 break;
120 }
121
122 if (print_lastmod) {
123 revision = notmuch_database_get_revision (notmuch, &uuid);
124 printf ("\t%s\t%lu\n", uuid, revision);
125 } else {
126 fputs ("\n", stdout);
127 }
128
129 DONE:
130 notmuch_query_destroy (query);
131
132 return ret;
133 }
134
135 static int
count_file(notmuch_database_t * notmuch,FILE * input,notmuch_config_values_t * exclude_tags,int output,int print_lastmod)136 count_file (notmuch_database_t *notmuch, FILE *input, notmuch_config_values_t *exclude_tags,
137 int output, int print_lastmod)
138 {
139 char *line = NULL;
140 ssize_t line_len;
141 size_t line_size;
142 int ret = 0;
143
144 while (! ret && (line_len = getline (&line, &line_size, input)) != -1) {
145 chomp_newline (line);
146 ret = print_count (notmuch, line, exclude_tags, output, print_lastmod);
147 }
148
149 if (line)
150 free (line);
151
152 return ret;
153 }
154
155 int
notmuch_count_command(notmuch_database_t * notmuch,int argc,char * argv[])156 notmuch_count_command (notmuch_database_t *notmuch, int argc, char *argv[])
157 {
158 char *query_str;
159 int opt_index;
160 int output = OUTPUT_MESSAGES;
161 bool exclude = true;
162 notmuch_config_values_t *exclude_tags = NULL;
163 bool batch = false;
164 bool print_lastmod = false;
165 FILE *input = stdin;
166 const char *input_file_name = NULL;
167 int ret;
168
169 notmuch_opt_desc_t options[] = {
170 { .opt_keyword = &output, .name = "output", .keywords =
171 (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
172 { "messages", OUTPUT_MESSAGES },
173 { "files", OUTPUT_FILES },
174 { 0, 0 } } },
175 { .opt_bool = &exclude, .name = "exclude" },
176 { .opt_bool = &print_lastmod, .name = "lastmod" },
177 { .opt_bool = &batch, .name = "batch" },
178 { .opt_string = &input_file_name, .name = "input" },
179 { .opt_inherit = notmuch_shared_options },
180 { }
181 };
182
183 opt_index = parse_arguments (argc, argv, options, 1);
184 if (opt_index < 0)
185 return EXIT_FAILURE;
186
187 notmuch_process_shared_options (notmuch, argv[0]);
188
189 if (input_file_name) {
190 batch = true;
191 input = fopen (input_file_name, "r");
192 if (input == NULL) {
193 fprintf (stderr, "Error opening %s for reading: %s\n",
194 input_file_name, strerror (errno));
195 return EXIT_FAILURE;
196 }
197 }
198
199 if (batch && opt_index != argc) {
200 fprintf (stderr, "--batch and query string are not compatible\n");
201 if (input)
202 fclose (input);
203 return EXIT_FAILURE;
204 }
205
206 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
207 if (query_str == NULL) {
208 fprintf (stderr, "Out of memory.\n");
209 return EXIT_FAILURE;
210 }
211
212 if (exclude) {
213 exclude_tags = notmuch_config_get_values (notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS);
214 }
215
216 if (batch)
217 ret = count_file (notmuch, input, exclude_tags, output, print_lastmod);
218 else
219 ret = print_count (notmuch, query_str, exclude_tags, 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 }
228