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)
74 gzprintf (output, "#notmuch-dump %s:%d ",
75 (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
76 NOTMUCH_DUMP_VERSION);
78 if (include & DUMP_INCLUDE_CONFIG) {
79 gzputs (output, "config");
82 if (include & DUMP_INCLUDE_PROPERTIES) {
83 gzprintf (output, "%sproperties", sep);
86 if (include & DUMP_INCLUDE_TAGS) {
87 gzprintf (output, "%sproperties", sep);
89 gzputs (output, "\n");
93 dump_properties_message (void *ctx,
94 notmuch_message_t *message,
96 char **buffer_p, size_t *size_p)
98 const char *message_id;
99 notmuch_message_properties_t *list;
100 notmuch_bool_t first = TRUE;
102 message_id = notmuch_message_get_message_id (message);
104 if (strchr (message_id, '\n')) {
105 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
109 for (list = notmuch_message_get_properties (message, "", FALSE);
110 notmuch_message_properties_valid (list); notmuch_message_properties_move_to_next (list)) {
111 const char *key, *val;
114 if (hex_encode (ctx, message_id, buffer_p, size_p) != HEX_SUCCESS) {
115 fprintf (stderr, "Error: failed to hex-encode message-id %s\n", message_id);
118 gzprintf (output, "#= %s", *buffer_p);
122 key = notmuch_message_properties_key (list);
123 val = notmuch_message_properties_value (list);
125 if (hex_encode (ctx, key, buffer_p, size_p) != HEX_SUCCESS) {
126 fprintf (stderr, "Error: failed to hex-encode key %s\n", key);
129 gzprintf (output, " %s", *buffer_p);
131 if (hex_encode (ctx, val, buffer_p, size_p) != HEX_SUCCESS) {
132 fprintf (stderr, "Error: failed to hex-encode value %s\n", val);
135 gzprintf (output, "=%s", *buffer_p);
137 notmuch_message_properties_destroy (list);
140 gzprintf (output, "\n", *buffer_p);
146 dump_tags_message (void *ctx,
147 notmuch_message_t *message, int output_format,
149 char **buffer_p, size_t *size_p)
152 const char *message_id;
154 message_id = notmuch_message_get_message_id (message);
156 if (output_format == DUMP_FORMAT_BATCH_TAG &&
157 strchr (message_id, '\n')) {
158 /* This will produce a line break in the output, which
159 * would be difficult to handle in tools. However, it's
160 * also impossible to produce an email containing a line
161 * break in a message ID because of unfolding, so we can
162 * safely disallow it. */
163 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
167 if (output_format == DUMP_FORMAT_SUP) {
168 gzprintf (output, "%s (", message_id);
171 for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
172 notmuch_tags_valid (tags);
173 notmuch_tags_move_to_next (tags)) {
174 const char *tag_str = notmuch_tags_get (tags);
177 gzputs (output, " ");
181 if (output_format == DUMP_FORMAT_SUP) {
182 gzputs (output, tag_str);
184 if (hex_encode (ctx, tag_str,
185 buffer_p, size_p) != HEX_SUCCESS) {
186 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
190 gzprintf (output, "+%s", *buffer_p);
194 if (output_format == DUMP_FORMAT_SUP) {
195 gzputs (output, ")\n");
197 if (make_boolean_term (ctx, "id", message_id,
199 fprintf (stderr, "Error quoting message id %s: %s\n",
200 message_id, strerror (errno));
203 gzprintf (output, " -- %s\n", *buffer_p);
209 database_dump_file (notmuch_database_t *notmuch, gzFile output,
210 const char *query_str, int output_format, int include)
212 notmuch_query_t *query;
213 notmuch_messages_t *messages;
214 notmuch_message_t *message;
215 notmuch_status_t status;
217 size_t buffer_size = 0;
219 print_dump_header (output, output_format, include);
221 if (include & DUMP_INCLUDE_CONFIG) {
222 if (print_status_database ("notmuch dump", notmuch,
223 database_dump_config(notmuch,output)))
227 if (! (include & (DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES)))
233 query = notmuch_query_create (notmuch, query_str);
235 fprintf (stderr, "Out of memory\n");
238 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
239 * first results quickly at the expense of total time.
241 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
243 status = notmuch_query_search_messages_st (query, &messages);
244 if (print_status_query ("notmuch dump", query, status))
248 notmuch_messages_valid (messages);
249 notmuch_messages_move_to_next (messages)) {
251 message = notmuch_messages_get (messages);
253 if (dump_tags_message (notmuch, message, output_format, output,
254 &buffer, &buffer_size))
257 if ((include & DUMP_INCLUDE_PROPERTIES) &&
258 dump_properties_message (notmuch, message, output,
259 &buffer, &buffer_size))
262 notmuch_message_destroy (message);
265 notmuch_query_destroy (query);
270 /* Dump database into output_file_name if it's non-NULL, stdout
274 notmuch_database_dump (notmuch_database_t *notmuch,
275 const char *output_file_name,
276 const char *query_str,
277 dump_format_t output_format,
278 dump_include_t include,
279 notmuch_bool_t gzip_output)
281 gzFile output = NULL;
282 const char *mode = gzip_output ? "w9" : "wT";
283 const char *name_for_error = output_file_name ? output_file_name : "stdout";
285 char *tempname = NULL;
290 if (output_file_name) {
291 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
292 outfd = mkstemp (tempname);
294 outfd = dup (STDOUT_FILENO);
298 fprintf (stderr, "Bad output file %s\n", name_for_error);
302 output = gzdopen (outfd, mode);
304 if (output == NULL) {
305 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
306 name_for_error, strerror (errno));
308 fprintf (stderr, "Error closing %s during shutdown: %s\n",
309 name_for_error, strerror (errno));
313 ret = database_dump_file (notmuch, output, query_str, output_format, include);
316 ret = gzflush (output, Z_FINISH);
318 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
322 if (output_file_name) {
325 fprintf (stderr, "Error syncing %s to disk: %s\n",
326 name_for_error, strerror (errno));
331 if (gzclose_w (output) != Z_OK) {
332 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
333 gzerror (output, NULL));
339 if (output_file_name) {
340 ret = rename (tempname, output_file_name);
342 fprintf (stderr, "Error renaming %s to %s: %s\n",
343 tempname, output_file_name, strerror (errno));
349 if (ret != EXIT_SUCCESS && output)
350 (void) gzclose_w (output);
352 if (ret != EXIT_SUCCESS && output_file_name)
353 (void) unlink (tempname);
359 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
361 notmuch_database_t *notmuch;
362 const char *query_str = NULL;
365 if (notmuch_database_open (notmuch_config_get_database_path (config),
366 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
369 notmuch_exit_if_unmatched_db_uuid (notmuch);
371 char *output_file_name = NULL;
374 int output_format = DUMP_FORMAT_BATCH_TAG;
376 notmuch_bool_t gzip_output = 0;
378 notmuch_opt_desc_t options[] = {
379 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
380 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
381 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
383 { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',
384 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
385 { "properties", DUMP_INCLUDE_PROPERTIES },
386 { "tags", DUMP_INCLUDE_TAGS} } },
387 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
388 { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
389 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
393 opt_index = parse_arguments (argc, argv, options, 1);
397 notmuch_process_shared_options (argv[0]);
400 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
402 if (opt_index < argc) {
403 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
404 if (query_str == NULL) {
405 fprintf (stderr, "Out of memory.\n");
410 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
411 output_format, include, gzip_output);
413 notmuch_database_destroy (notmuch);