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;
51 notmuch_status_t status;
53 status = notmuch_query_search_messages_st (query, &messages);
54 if (print_status_query ("notmuch dump", query, status))
58 notmuch_messages_valid (messages);
59 notmuch_messages_move_to_next (messages)) {
61 const char *message_id;
63 message = notmuch_messages_get (messages);
64 message_id = notmuch_message_get_message_id (message);
66 if (output_format == DUMP_FORMAT_BATCH_TAG &&
67 strchr (message_id, '\n')) {
68 /* This will produce a line break in the output, which
69 * would be difficult to handle in tools. However, it's
70 * also impossible to produce an email containing a line
71 * break in a message ID because of unfolding, so we can
72 * safely disallow it. */
73 fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
74 notmuch_message_destroy (message);
78 if (output_format == DUMP_FORMAT_SUP) {
79 gzprintf (output, "%s (", message_id);
82 for (tags = notmuch_message_get_tags (message);
83 notmuch_tags_valid (tags);
84 notmuch_tags_move_to_next (tags)) {
85 const char *tag_str = notmuch_tags_get (tags);
92 if (output_format == DUMP_FORMAT_SUP) {
93 gzputs (output, tag_str);
95 if (hex_encode (notmuch, tag_str,
96 &buffer, &buffer_size) != HEX_SUCCESS) {
97 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
101 gzprintf (output, "+%s", buffer);
105 if (output_format == DUMP_FORMAT_SUP) {
106 gzputs (output, ")\n");
108 if (make_boolean_term (notmuch, "id", message_id,
109 &buffer, &buffer_size)) {
110 fprintf (stderr, "Error quoting message id %s: %s\n",
111 message_id, strerror (errno));
114 gzprintf (output, " -- %s\n", buffer);
117 notmuch_message_destroy (message);
120 notmuch_query_destroy (query);
125 /* Dump database into output_file_name if it's non-NULL, stdout
129 notmuch_database_dump (notmuch_database_t *notmuch,
130 const char *output_file_name,
131 const char *query_str,
132 dump_format_t output_format,
133 notmuch_bool_t gzip_output)
135 gzFile output = NULL;
136 const char *mode = gzip_output ? "w9" : "wT";
137 const char *name_for_error = output_file_name ? output_file_name : "stdout";
139 char *tempname = NULL;
144 if (output_file_name) {
145 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
146 outfd = mkstemp (tempname);
148 outfd = dup (STDOUT_FILENO);
152 fprintf (stderr, "Bad output file %s\n", name_for_error);
156 output = gzdopen (outfd, mode);
158 if (output == NULL) {
159 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
160 name_for_error, strerror (errno));
162 fprintf (stderr, "Error closing %s during shutdown: %s\n",
163 name_for_error, strerror (errno));
167 ret = database_dump_file (notmuch, output, query_str, output_format);
170 ret = gzflush (output, Z_FINISH);
172 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
176 if (output_file_name) {
179 fprintf (stderr, "Error syncing %s to disk: %s\n",
180 name_for_error, strerror (errno));
185 if (gzclose_w (output) != Z_OK) {
186 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
187 gzerror (output, NULL));
193 if (output_file_name) {
194 ret = rename (tempname, output_file_name);
196 fprintf (stderr, "Error renaming %s to %s: %s\n",
197 tempname, output_file_name, strerror (errno));
203 if (ret != EXIT_SUCCESS && output)
204 (void) gzclose_w (output);
206 if (ret != EXIT_SUCCESS && output_file_name)
207 (void) unlink (tempname);
213 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
215 notmuch_database_t *notmuch;
216 const char *query_str = NULL;
219 if (notmuch_database_open (notmuch_config_get_database_path (config),
220 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
223 notmuch_exit_if_unmatched_db_uuid (notmuch);
225 char *output_file_name = NULL;
228 int output_format = DUMP_FORMAT_BATCH_TAG;
229 notmuch_bool_t gzip_output = 0;
231 notmuch_opt_desc_t options[] = {
232 { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
233 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
234 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
236 { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0 },
237 { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
238 { NOTMUCH_OPT_INHERIT, (void *) ¬much_shared_options, NULL, 0, 0 },
242 opt_index = parse_arguments (argc, argv, options, 1);
246 notmuch_process_shared_options (argv[0]);
248 if (opt_index < argc) {
249 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
250 if (query_str == NULL) {
251 fprintf (stderr, "Out of memory.\n");
256 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
257 output_format, gzip_output);
259 notmuch_database_destroy (notmuch);