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 http://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"
28 database_dump_file (notmuch_database_t *notmuch, gzFile output,
29 const char *query_str, int output_format)
31 notmuch_query_t *query;
32 notmuch_messages_t *messages;
33 notmuch_message_t *message;
39 query = notmuch_query_create (notmuch, query_str);
41 fprintf (stderr, "Out of memory\n");
44 /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
45 * first results quickly at the expense of total time.
47 notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
50 size_t buffer_size = 0;
52 for (messages = notmuch_query_search_messages (query);
53 notmuch_messages_valid (messages);
54 notmuch_messages_move_to_next (messages)) {
56 const char *message_id;
58 message = notmuch_messages_get (messages);
59 message_id = notmuch_message_get_message_id (message);
61 if (output_format == DUMP_FORMAT_BATCH_TAG &&
62 strchr (message_id, '\n')) {
63 /* This will produce a line break in the output, which
64 * would be difficult to handle in tools. However, it's
65 * also impossible to produce an email containing a line
66 * break in a message ID because of unfolding, so we can
67 * safely disallow it. */
68 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
69 notmuch_message_destroy (message);
73 if (output_format == DUMP_FORMAT_SUP) {
74 gzprintf (output, "%s (", message_id);
77 for (tags = notmuch_message_get_tags (message);
78 notmuch_tags_valid (tags);
79 notmuch_tags_move_to_next (tags)) {
80 const char *tag_str = notmuch_tags_get (tags);
87 if (output_format == DUMP_FORMAT_SUP) {
88 gzputs (output, tag_str);
90 if (hex_encode (notmuch, tag_str,
91 &buffer, &buffer_size) != HEX_SUCCESS) {
92 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
96 gzprintf (output, "+%s", buffer);
100 if (output_format == DUMP_FORMAT_SUP) {
101 gzputs (output, ")\n");
103 if (make_boolean_term (notmuch, "id", message_id,
104 &buffer, &buffer_size)) {
105 fprintf (stderr, "Error quoting message id %s: %s\n",
106 message_id, strerror (errno));
109 gzprintf (output, " -- %s\n", buffer);
112 notmuch_message_destroy (message);
115 notmuch_query_destroy (query);
120 /* Dump database into output_file_name if it's non-NULL, stdout
124 notmuch_database_dump (notmuch_database_t *notmuch,
125 const char *output_file_name,
126 const char *query_str,
127 dump_format_t output_format,
128 notmuch_bool_t gzip_output)
130 gzFile output = NULL;
131 const char *mode = gzip_output ? "w9" : "wT";
132 const char *name_for_error = output_file_name ? output_file_name : "stdout";
134 char *tempname = NULL;
139 if (output_file_name) {
140 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
141 outfd = mkstemp (tempname);
143 outfd = dup (STDOUT_FILENO);
147 fprintf (stderr, "Bad output file %s\n", name_for_error);
151 output = gzdopen (outfd, mode);
153 if (output == NULL) {
154 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
155 name_for_error, strerror (errno));
157 fprintf (stderr, "Error closing %s during shutdown: %s\n",
158 name_for_error, strerror (errno));
162 ret = database_dump_file (notmuch, output, query_str, output_format);
165 ret = gzflush (output, Z_FINISH);
167 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
171 if (output_file_name) {
174 fprintf (stderr, "Error syncing %s to disk: %s\n",
175 name_for_error, strerror (errno));
180 if (gzclose_w (output) != Z_OK) {
181 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
182 gzerror (output, NULL));
188 if (output_file_name) {
189 ret = rename (tempname, output_file_name);
191 fprintf (stderr, "Error renaming %s to %s: %s\n",
192 tempname, output_file_name, strerror (errno));
198 if (ret != EXIT_SUCCESS && output)
199 (void) gzclose_w (output);
201 if (ret != EXIT_SUCCESS && output_file_name)
202 (void) unlink (tempname);
208 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
210 notmuch_database_t *notmuch;
211 const char *query_str = NULL;
214 if (notmuch_database_open (notmuch_config_get_database_path (config),
215 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
218 char *output_file_name = NULL;
221 int output_format = DUMP_FORMAT_BATCH_TAG;
222 notmuch_bool_t gzip_output = 0;
224 notmuch_opt_desc_t options[] = {
225 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
226 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
227 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
229 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
230 { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
234 opt_index = parse_arguments (argc, argv, options, 1);
238 if (opt_index < argc) {
239 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
240 if (query_str == NULL) {
241 fprintf (stderr, "Out of memory.\n");
246 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
247 output_format, gzip_output);
249 notmuch_database_destroy (notmuch);