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 query = notmuch_query_create (notmuch, query_str);
237 fprintf (stderr, "Out of memory\n");
240 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
241 * first results quickly at the expense of total time.
243 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
245 status = notmuch_query_search_messages (query, &messages);
246 if (print_status_query ("notmuch dump", query, status))
250 notmuch_messages_valid (messages);
251 notmuch_messages_move_to_next (messages)) {
253 message = notmuch_messages_get (messages);
255 if ((include & DUMP_INCLUDE_TAGS) &&
256 dump_tags_message (notmuch, message, output_format, output,
257 &buffer, &buffer_size))
260 if ((include & DUMP_INCLUDE_PROPERTIES) &&
261 dump_properties_message (notmuch, message, output,
262 &buffer, &buffer_size))
265 notmuch_message_destroy (message);
268 notmuch_query_destroy (query);
273 /* Dump database into output_file_name if it's non-NULL, stdout
277 notmuch_database_dump (notmuch_database_t *notmuch,
278 const char *output_file_name,
279 const char *query_str,
280 dump_format_t output_format,
281 dump_include_t include,
284 gzFile output = NULL;
285 const char *mode = gzip_output ? "w9" : "wT";
286 const char *name_for_error = output_file_name ? output_file_name : "stdout";
288 char *tempname = NULL;
293 if (output_file_name) {
294 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
295 outfd = mkstemp (tempname);
297 outfd = dup (STDOUT_FILENO);
301 fprintf (stderr, "Bad output file %s\n", name_for_error);
305 output = gzdopen (outfd, mode);
307 if (output == NULL) {
308 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
309 name_for_error, strerror (errno));
311 fprintf (stderr, "Error closing %s during shutdown: %s\n",
312 name_for_error, strerror (errno));
316 ret = database_dump_file (notmuch, output, query_str, output_format, include);
319 ret = gzflush (output, Z_FINISH);
321 fprintf (stderr, "Error flushing output: %s\n", gzerror_str (output));
325 if (output_file_name) {
328 fprintf (stderr, "Error syncing %s to disk: %s\n",
329 name_for_error, strerror (errno));
334 ret = gzclose_w (output);
336 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
337 gzerror_str (output));
344 if (output_file_name) {
345 ret = rename (tempname, output_file_name);
347 fprintf (stderr, "Error renaming %s to %s: %s\n",
348 tempname, output_file_name, strerror (errno));
354 if (ret != EXIT_SUCCESS && output)
355 (void) gzclose_w (output);
357 if (ret != EXIT_SUCCESS && output_file_name)
358 (void) unlink (tempname);
364 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
366 notmuch_database_t *notmuch;
367 const char *query_str = NULL;
370 if (notmuch_database_open (notmuch_config_get_database_path (config),
371 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
374 notmuch_exit_if_unmatched_db_uuid (notmuch);
376 const char *output_file_name = NULL;
379 int output_format = DUMP_FORMAT_BATCH_TAG;
381 bool gzip_output = 0;
383 notmuch_opt_desc_t options[] = {
384 { .opt_keyword = &output_format, .name = "format", .keywords =
385 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
386 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
388 { .opt_flags = &include, .name = "include", .keywords =
389 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
390 { "properties", DUMP_INCLUDE_PROPERTIES },
391 { "tags", DUMP_INCLUDE_TAGS } } },
392 { .opt_string = &output_file_name, .name = "output" },
393 { .opt_bool = &gzip_output, .name = "gzip" },
394 { .opt_inherit = notmuch_shared_options },
398 opt_index = parse_arguments (argc, argv, options, 1);
402 notmuch_process_shared_options (argv[0]);
405 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
407 if (opt_index < argc) {
408 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
409 if (query_str == NULL) {
410 fprintf (stderr, "Out of memory.\n");
415 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
416 output_format, include, gzip_output);
418 notmuch_database_destroy (notmuch);