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"
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) );
54 gzprintf (output, " %s\n", buffer);
61 notmuch_config_list_destroy (list);
70 print_dump_header (gzFile output, int output_format, int include)
72 gzprintf (output, "#notmuch-dump %s:%d %s%s%s\n",
73 (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
75 (include & DUMP_INCLUDE_CONFIG) ? "config" : "",
76 (include & DUMP_INCLUDE_TAGS) && (include & DUMP_INCLUDE_CONFIG) ? "," : "",
77 (include & DUMP_INCLUDE_TAGS) ? "tags" : "");
81 dump_tags_message (void *ctx,
82 notmuch_message_t *message, int output_format,
84 char **buffer_p, size_t *size_p)
87 const char *message_id;
89 message_id = notmuch_message_get_message_id (message);
91 if (output_format == DUMP_FORMAT_BATCH_TAG &&
92 strchr (message_id, '\n')) {
93 /* This will produce a line break in the output, which
94 * would be difficult to handle in tools. However, it's
95 * also impossible to produce an email containing a line
96 * break in a message ID because of unfolding, so we can
97 * safely disallow it. */
98 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
102 if (output_format == DUMP_FORMAT_SUP) {
103 gzprintf (output, "%s (", message_id);
106 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
107 notmuch_tags_valid (tags);
108 notmuch_tags_move_to_next (tags)) {
109 const char *tag_str = notmuch_tags_get (tags);
112 gzputs (output, " ");
116 if (output_format == DUMP_FORMAT_SUP) {
117 gzputs (output, tag_str);
119 if (hex_encode (ctx, tag_str,
120 buffer_p, size_p) != HEX_SUCCESS) {
121 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
125 gzprintf (output, "+%s", *buffer_p);
129 if (output_format == DUMP_FORMAT_SUP) {
130 gzputs (output, ")\n");
132 if (make_boolean_term (ctx, "id", message_id,
134 fprintf (stderr, "Error quoting message id %s: %s\n",
135 message_id, strerror (errno));
138 gzprintf (output, " -- %s\n", *buffer_p);
144 database_dump_file (notmuch_database_t *notmuch, gzFile output,
145 const char *query_str, int output_format, int include)
147 notmuch_query_t *query;
148 notmuch_messages_t *messages;
149 notmuch_message_t *message;
150 notmuch_status_t status;
152 size_t buffer_size = 0;
154 print_dump_header (output, output_format, include);
156 if (include & DUMP_INCLUDE_CONFIG) {
157 if (print_status_database ("notmuch dump", notmuch,
158 database_dump_config(notmuch,output)))
162 if (! (include & DUMP_INCLUDE_TAGS))
168 query = notmuch_query_create (notmuch, query_str);
170 fprintf (stderr, "Out of memory\n");
173 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
174 * first results quickly at the expense of total time.
176 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
178 status = notmuch_query_search_messages_st (query, &messages);
179 if (print_status_query ("notmuch dump", query, status))
183 notmuch_messages_valid (messages);
184 notmuch_messages_move_to_next (messages)) {
186 message = notmuch_messages_get (messages);
188 if (dump_tags_message (notmuch, message, output_format, output,
189 &buffer, &buffer_size))
192 notmuch_message_destroy (message);
195 notmuch_query_destroy (query);
200 /* Dump database into output_file_name if it's non-NULL, stdout
204 notmuch_database_dump (notmuch_database_t *notmuch,
205 const char *output_file_name,
206 const char *query_str,
207 dump_format_t output_format,
208 dump_include_t include,
209 notmuch_bool_t gzip_output)
211 gzFile output = NULL;
212 const char *mode = gzip_output ? "w9" : "wT";
213 const char *name_for_error = output_file_name ? output_file_name : "stdout";
215 char *tempname = NULL;
220 if (output_file_name) {
221 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
222 outfd = mkstemp (tempname);
224 outfd = dup (STDOUT_FILENO);
228 fprintf (stderr, "Bad output file %s\n", name_for_error);
232 output = gzdopen (outfd, mode);
234 if (output == NULL) {
235 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
236 name_for_error, strerror (errno));
238 fprintf (stderr, "Error closing %s during shutdown: %s\n",
239 name_for_error, strerror (errno));
243 ret = database_dump_file (notmuch, output, query_str, output_format, include);
246 ret = gzflush (output, Z_FINISH);
248 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
252 if (output_file_name) {
255 fprintf (stderr, "Error syncing %s to disk: %s\n",
256 name_for_error, strerror (errno));
261 if (gzclose_w (output) != Z_OK) {
262 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
263 gzerror (output, NULL));
269 if (output_file_name) {
270 ret = rename (tempname, output_file_name);
272 fprintf (stderr, "Error renaming %s to %s: %s\n",
273 tempname, output_file_name, strerror (errno));
279 if (ret != EXIT_SUCCESS && output)
280 (void) gzclose_w (output);
282 if (ret != EXIT_SUCCESS && output_file_name)
283 (void) unlink (tempname);
289 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
291 notmuch_database_t *notmuch;
292 const char *query_str = NULL;
295 if (notmuch_database_open (notmuch_config_get_database_path (config),
296 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
299 notmuch_exit_if_unmatched_db_uuid (notmuch);
301 char *output_file_name = NULL;
304 int output_format = DUMP_FORMAT_BATCH_TAG;
306 notmuch_bool_t gzip_output = 0;
308 notmuch_opt_desc_t options[] = {
309 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
310 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
311 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
313 { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',
314 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
315 { "tags", DUMP_INCLUDE_TAGS} } },
316 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
317 { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
318 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
322 opt_index = parse_arguments (argc, argv, options, 1);
326 notmuch_process_shared_options (argv[0]);
329 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;
331 if (opt_index < argc) {
332 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
333 if (query_str == NULL) {
334 fprintf (stderr, "Out of memory.\n");
339 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
340 output_format, include, gzip_output);
342 notmuch_database_destroy (notmuch);