1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
22 #include "hex-escape.h"
23 #include "string-util.h"
24 #include "zlib-extra.h"
27 database_dump_config (notmuch_database_t *notmuch, gzFile output)
29 notmuch_config_list_t *list;
30 int ret = EXIT_FAILURE;
32 size_t buffer_size = 0;
34 if (print_status_database ("notmuch dump", notmuch,
35 notmuch_database_get_config_list (notmuch, NULL, &list)))
38 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
39 if (hex_encode (notmuch, notmuch_config_list_key (list),
40 &buffer, &buffer_size) != HEX_SUCCESS) {
41 fprintf (stderr, "Error: failed to hex-encode config key %s\n",
42 notmuch_config_list_key (list));
45 GZPRINTF (output, "#@ %s", buffer);
47 if (hex_encode (notmuch, notmuch_config_list_value (list),
48 &buffer, &buffer_size) != HEX_SUCCESS) {
49 fprintf (stderr, "Error: failed to hex-encode config value %s\n",
50 notmuch_config_list_value (list) );
55 GZPUTS (output, buffer);
56 GZPUTS (output, "\n");
63 notmuch_config_list_destroy (list);
72 print_dump_header (gzFile output, int output_format, int include)
76 GZPRINTF (output, "#notmuch-dump %s:%d ",
77 (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
78 NOTMUCH_DUMP_VERSION);
80 if (include & DUMP_INCLUDE_CONFIG) {
81 GZPUTS (output, "config");
84 if (include & DUMP_INCLUDE_PROPERTIES) {
85 GZPRINTF (output, "%sproperties", sep);
88 if (include & DUMP_INCLUDE_TAGS) {
89 GZPRINTF (output, "%stags", sep);
91 GZPUTS (output, "\n");
95 dump_properties_message (void *ctx,
96 notmuch_message_t *message,
98 char **buffer_p, size_t *size_p)
100 const char *message_id;
101 notmuch_message_properties_t *list;
104 message_id = notmuch_message_get_message_id (message);
106 if (strchr (message_id, '\n')) {
107 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
111 for (list = notmuch_message_get_properties (message, "", false);
112 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
113 const char *key, *val;
116 if (hex_encode (ctx, message_id, buffer_p, size_p) != HEX_SUCCESS) {
117 fprintf (stderr, "Error: failed to hex-encode message-id %s\n", message_id);
120 GZPRINTF (output, "#= %s", *buffer_p);
124 key = notmuch_message_properties_key (list);
125 val = notmuch_message_properties_value (list);
127 if (hex_encode (ctx, key, buffer_p, size_p) != HEX_SUCCESS) {
128 fprintf (stderr, "Error: failed to hex-encode key %s\n", key);
131 GZPRINTF (output, " %s", *buffer_p);
133 if (hex_encode (ctx, val, buffer_p, size_p) != HEX_SUCCESS) {
134 fprintf (stderr, "Error: failed to hex-encode value %s\n", val);
137 GZPRINTF (output, "=%s", *buffer_p);
139 notmuch_message_properties_destroy (list);
142 GZPRINTF (output, "\n", *buffer_p);
148 dump_tags_message (void *ctx,
149 notmuch_message_t *message, int output_format,
151 char **buffer_p, size_t *size_p)
154 const char *message_id;
156 message_id = notmuch_message_get_message_id (message);
158 if (output_format == DUMP_FORMAT_BATCH_TAG &&
159 strchr (message_id, '\n')) {
160 /* This will produce a line break in the output, which
161 * would be difficult to handle in tools. However, it's
162 * also impossible to produce an email containing a line
163 * break in a message ID because of unfolding, so we can
164 * safely disallow it. */
165 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
169 if (output_format == DUMP_FORMAT_SUP) {
170 GZPRINTF (output, "%s (", message_id);
173 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
174 notmuch_tags_valid (tags);
175 notmuch_tags_move_to_next (tags)) {
176 const char *tag_str = notmuch_tags_get (tags);
179 GZPUTS (output, " ");
183 if (output_format == DUMP_FORMAT_SUP) {
184 GZPUTS (output, tag_str);
186 if (hex_encode (ctx, tag_str,
187 buffer_p, size_p) != HEX_SUCCESS) {
188 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
192 GZPRINTF (output, "+%s", *buffer_p);
196 if (output_format == DUMP_FORMAT_SUP) {
197 GZPUTS (output, ")\n");
199 if (make_boolean_term (ctx, "id", message_id,
201 fprintf (stderr, "Error quoting message id %s: %s\n",
202 message_id, strerror (errno));
205 GZPRINTF (output, " -- %s\n", *buffer_p);
211 database_dump_file (notmuch_database_t *notmuch, gzFile output,
212 const char *query_str, int output_format, int include)
214 notmuch_query_t *query;
215 notmuch_messages_t *messages;
216 notmuch_message_t *message;
217 notmuch_status_t status;
219 size_t buffer_size = 0;
221 print_dump_header (output, output_format, include);
223 if (include & DUMP_INCLUDE_CONFIG) {
224 if (print_status_database ("notmuch dump", notmuch,
225 database_dump_config (notmuch, output)))
229 if (! (include & (DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES)))
235 status = notmuch_query_create_with_syntax (notmuch, query_str,
236 shared_option_query_syntax (),
238 if (print_status_database ("notmuch dump", notmuch, status))
241 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
242 * first results quickly at the expense of total time.
244 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
246 status = notmuch_query_search_messages (query, &messages);
247 if (print_status_query ("notmuch dump", query, status))
251 notmuch_messages_valid (messages);
252 notmuch_messages_move_to_next (messages)) {
254 message = notmuch_messages_get (messages);
256 if ((include & DUMP_INCLUDE_TAGS) &&
257 dump_tags_message (notmuch, message, output_format, output,
258 &buffer, &buffer_size))
261 if ((include & DUMP_INCLUDE_PROPERTIES) &&
262 dump_properties_message (notmuch, message, output,
263 &buffer, &buffer_size))
266 notmuch_message_destroy (message);
269 notmuch_query_destroy (query);
274 /* Dump database into output_file_name if it's non-NULL, stdout
278 notmuch_database_dump (notmuch_database_t *notmuch,
279 const char *output_file_name,
280 const char *query_str,
281 dump_format_t output_format,
282 dump_include_t include,
285 gzFile output = NULL;
286 const char *mode = gzip_output ? "w9" : "wT";
287 const char *name_for_error = output_file_name ? output_file_name : "stdout";
289 char *tempname = NULL;
294 if (output_file_name) {
295 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
296 outfd = mkstemp (tempname);
298 outfd = dup (STDOUT_FILENO);
302 fprintf (stderr, "Bad output file %s\n", name_for_error);
306 output = gzdopen (outfd, mode);
308 if (output == NULL) {
309 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
310 name_for_error, strerror (errno));
312 fprintf (stderr, "Error closing %s during shutdown: %s\n",
313 name_for_error, strerror (errno));
317 ret = database_dump_file (notmuch, output, query_str, output_format, include);
320 ret = gzflush (output, Z_FINISH);
322 fprintf (stderr, "Error flushing output: %s\n", gzerror_str (output));
326 if (output_file_name) {
329 fprintf (stderr, "Error syncing %s to disk: %s\n",
330 name_for_error, strerror (errno));
335 ret = gzclose_w (output);
337 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
338 gzerror_str (output));
345 if (output_file_name) {
346 ret = rename (tempname, output_file_name);
348 fprintf (stderr, "Error renaming %s to %s: %s\n",
349 tempname, output_file_name, strerror (errno));
355 if (ret != EXIT_SUCCESS && output)
356 (void) gzclose_w (output);
358 if (ret != EXIT_SUCCESS && output_file_name)
359 (void) unlink (tempname);
365 notmuch_dump_command (notmuch_database_t *notmuch, int argc, char *argv[])
367 const char *query_str = NULL;
370 const char *output_file_name = NULL;
373 int output_format = DUMP_FORMAT_BATCH_TAG;
375 bool gzip_output = 0;
377 notmuch_opt_desc_t options[] = {
378 { .opt_keyword = &output_format, .name = "format", .keywords =
379 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
380 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
382 { .opt_flags = &include, .name = "include", .keywords =
383 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
384 { "properties", DUMP_INCLUDE_PROPERTIES },
385 { "tags", DUMP_INCLUDE_TAGS } } },
386 { .opt_string = &output_file_name, .name = "output" },
387 { .opt_bool = &gzip_output, .name = "gzip" },
388 { .opt_inherit = notmuch_shared_options },
392 opt_index = parse_arguments (argc, argv, options, 1);
396 notmuch_process_shared_options (notmuch, argv[0]);
399 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
401 if (opt_index < argc) {
402 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
403 if (query_str == NULL) {
404 fprintf (stderr, "Out of memory.\n");
409 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
410 output_format, include, gzip_output);
412 notmuch_database_destroy (notmuch);