]> git.notmuchmail.org Git - notmuch/blob - notmuch-count.c
crypto: rename notmuch_crypto_t to _notmuch_crypto_t
[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 (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         status = notmuch_query_add_tag_exclude (query, exclude_tags[i]);
92         if (status && status != NOTMUCH_STATUS_IGNORED) {
93             print_status_query ("notmuch count", query, status);
94             return -1;
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
136 count_file (notmuch_database_t *notmuch, FILE *input, const char **exclude_tags,
137             size_t exclude_tags_length, 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, exclude_tags_length,
147                            output, print_lastmod);
148     }
149
150     if (line)
151         free (line);
152
153     return ret;
154 }
155
156 int
157 notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
158 {
159     notmuch_database_t *notmuch;
160     char *query_str;
161     int opt_index;
162     int output = OUTPUT_MESSAGES;
163     int exclude = EXCLUDE_true;
164     const char **search_exclude_tags = NULL;
165     size_t search_exclude_tags_length = 0;
166     bool batch = false;
167     bool print_lastmod = false;
168     FILE *input = stdin;
169     const char *input_file_name = NULL;
170     int ret;
171
172     notmuch_opt_desc_t options[] = {
173         { .opt_keyword = &output, .name = "output", .keywords =
174           (notmuch_keyword_t []){ { "threads", OUTPUT_THREADS },
175                                   { "messages", OUTPUT_MESSAGES },
176                                   { "files", OUTPUT_FILES },
177                                   { 0, 0 } } },
178         { .opt_keyword = &exclude, .name = "exclude", .keywords =
179           (notmuch_keyword_t []){ { "true", EXCLUDE_true },
180                                   { "false", EXCLUDE_false },
181                                   { 0, 0 } } },
182         { .opt_bool = &print_lastmod, .name = "lastmod" },
183         { .opt_bool = &batch, .name = "batch" },
184         { .opt_string = &input_file_name, .name = "input" },
185         { .opt_inherit = notmuch_shared_options },
186         { }
187     };
188
189     opt_index = parse_arguments (argc, argv, options, 1);
190     if (opt_index < 0)
191         return EXIT_FAILURE;
192
193     notmuch_process_shared_options (argv[0]);
194
195     if (input_file_name) {
196         batch = true;
197         input = fopen (input_file_name, "r");
198         if (input == NULL) {
199             fprintf (stderr, "Error opening %s for reading: %s\n",
200                      input_file_name, strerror (errno));
201             return EXIT_FAILURE;
202         }
203     }
204
205     if (batch && opt_index != argc) {
206         fprintf (stderr, "--batch and query string are not compatible\n");
207         if (input)
208             fclose (input);
209         return EXIT_FAILURE;
210     }
211
212     if (notmuch_database_open (notmuch_config_get_database_path (config),
213                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
214         return EXIT_FAILURE;
215
216     notmuch_exit_if_unmatched_db_uuid (notmuch);
217
218     query_str = query_string_from_args (config, argc - opt_index, argv + opt_index);
219     if (query_str == NULL) {
220         fprintf (stderr, "Out of memory.\n");
221         return EXIT_FAILURE;
222     }
223
224     if (exclude == EXCLUDE_true) {
225         search_exclude_tags = notmuch_config_get_search_exclude_tags
226             (config, &search_exclude_tags_length);
227     }
228
229     if (batch)
230         ret = count_file (notmuch, input, search_exclude_tags,
231                           search_exclude_tags_length, output, print_lastmod);
232     else
233         ret = print_count (notmuch, query_str, search_exclude_tags,
234                            search_exclude_tags_length, output, print_lastmod);
235
236     notmuch_database_destroy (notmuch);
237
238     if (input != stdin)
239         fclose (input);
240
241     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
242 }