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 database_dump_file (notmuch_database_t *notmuch, gzFile output,
82 const char *query_str, int output_format, int include)
84 notmuch_query_t *query;
85 notmuch_messages_t *messages;
86 notmuch_message_t *message;
89 print_dump_header (output, output_format, include);
91 if (include & DUMP_INCLUDE_CONFIG) {
92 if (print_status_database ("notmuch dump", notmuch,
93 database_dump_config(notmuch,output)))
97 if (! (include & DUMP_INCLUDE_TAGS))
103 query = notmuch_query_create (notmuch, query_str);
105 fprintf (stderr, "Out of memory\n");
108 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
109 * first results quickly at the expense of total time.
111 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
114 size_t buffer_size = 0;
115 notmuch_status_t status;
117 status = notmuch_query_search_messages_st (query, &messages);
118 if (print_status_query ("notmuch dump", query, status))
122 notmuch_messages_valid (messages);
123 notmuch_messages_move_to_next (messages)) {
125 const char *message_id;
127 message = notmuch_messages_get (messages);
128 message_id = notmuch_message_get_message_id (message);
130 if (output_format == DUMP_FORMAT_BATCH_TAG &&
131 strchr (message_id, '\n')) {
132 /* This will produce a line break in the output, which
133 * would be difficult to handle in tools. However, it's
134 * also impossible to produce an email containing a line
135 * break in a message ID because of unfolding, so we can
136 * safely disallow it. */
137 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
138 notmuch_message_destroy (message);
142 if (output_format == DUMP_FORMAT_SUP) {
143 gzprintf (output, "%s (", message_id);
146 for (tags = notmuch_message_get_tags (message);
147 notmuch_tags_valid (tags);
148 notmuch_tags_move_to_next (tags)) {
149 const char *tag_str = notmuch_tags_get (tags);
152 gzputs (output, " ");
156 if (output_format == DUMP_FORMAT_SUP) {
157 gzputs (output, tag_str);
159 if (hex_encode (notmuch, tag_str,
160 &buffer, &buffer_size) != HEX_SUCCESS) {
161 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
165 gzprintf (output, "+%s", buffer);
169 if (output_format == DUMP_FORMAT_SUP) {
170 gzputs (output, ")\n");
172 if (make_boolean_term (notmuch, "id", message_id,
173 &buffer, &buffer_size)) {
174 fprintf (stderr, "Error quoting message id %s: %s\n",
175 message_id, strerror (errno));
178 gzprintf (output, " -- %s\n", buffer);
181 notmuch_message_destroy (message);
184 notmuch_query_destroy (query);
189 /* Dump database into output_file_name if it's non-NULL, stdout
193 notmuch_database_dump (notmuch_database_t *notmuch,
194 const char *output_file_name,
195 const char *query_str,
196 dump_format_t output_format,
197 dump_include_t include,
198 notmuch_bool_t gzip_output)
200 gzFile output = NULL;
201 const char *mode = gzip_output ? "w9" : "wT";
202 const char *name_for_error = output_file_name ? output_file_name : "stdout";
204 char *tempname = NULL;
209 if (output_file_name) {
210 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
211 outfd = mkstemp (tempname);
213 outfd = dup (STDOUT_FILENO);
217 fprintf (stderr, "Bad output file %s\n", name_for_error);
221 output = gzdopen (outfd, mode);
223 if (output == NULL) {
224 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
225 name_for_error, strerror (errno));
227 fprintf (stderr, "Error closing %s during shutdown: %s\n",
228 name_for_error, strerror (errno));
232 ret = database_dump_file (notmuch, output, query_str, output_format, include);
235 ret = gzflush (output, Z_FINISH);
237 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
241 if (output_file_name) {
244 fprintf (stderr, "Error syncing %s to disk: %s\n",
245 name_for_error, strerror (errno));
250 if (gzclose_w (output) != Z_OK) {
251 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
252 gzerror (output, NULL));
258 if (output_file_name) {
259 ret = rename (tempname, output_file_name);
261 fprintf (stderr, "Error renaming %s to %s: %s\n",
262 tempname, output_file_name, strerror (errno));
268 if (ret != EXIT_SUCCESS && output)
269 (void) gzclose_w (output);
271 if (ret != EXIT_SUCCESS && output_file_name)
272 (void) unlink (tempname);
278 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
280 notmuch_database_t *notmuch;
281 const char *query_str = NULL;
284 if (notmuch_database_open (notmuch_config_get_database_path (config),
285 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
288 notmuch_exit_if_unmatched_db_uuid (notmuch);
290 char *output_file_name = NULL;
293 int output_format = DUMP_FORMAT_BATCH_TAG;
295 notmuch_bool_t gzip_output = 0;
297 notmuch_opt_desc_t options[] = {
298 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
299 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
300 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
302 { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',
303 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
304 { "tags", DUMP_INCLUDE_TAGS} } },
305 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
306 { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
307 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
311 opt_index = parse_arguments (argc, argv, options, 1);
315 notmuch_process_shared_options (argv[0]);
318 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;
320 if (opt_index < argc) {
321 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
322 if (query_str == NULL) {
323 fprintf (stderr, "Out of memory.\n");
328 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
329 output_format, include, gzip_output);
331 notmuch_database_destroy (notmuch);