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, "%stags", 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;
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 (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 ((include & DUMP_INCLUDE_TAGS) &&
254 dump_tags_message (notmuch, message, output_format, output,
255 &buffer, &buffer_size))
258 if ((include & DUMP_INCLUDE_PROPERTIES) &&
259 dump_properties_message (notmuch, message, output,
260 &buffer, &buffer_size))
263 notmuch_message_destroy (message);
266 notmuch_query_destroy (query);
271 /* Dump database into output_file_name if it's non-NULL, stdout
275 notmuch_database_dump (notmuch_database_t *notmuch,
276 const char *output_file_name,
277 const char *query_str,
278 dump_format_t output_format,
279 dump_include_t include,
282 gzFile output = NULL;
283 const char *mode = gzip_output ? "w9" : "wT";
284 const char *name_for_error = output_file_name ? output_file_name : "stdout";
286 char *tempname = NULL;
291 if (output_file_name) {
292 tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
293 outfd = mkstemp (tempname);
295 outfd = dup (STDOUT_FILENO);
299 fprintf (stderr, "Bad output file %s\n", name_for_error);
303 output = gzdopen (outfd, mode);
305 if (output == NULL) {
306 fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
307 name_for_error, strerror (errno));
309 fprintf (stderr, "Error closing %s during shutdown: %s\n",
310 name_for_error, strerror (errno));
314 ret = database_dump_file (notmuch, output, query_str, output_format, include);
317 ret = gzflush (output, Z_FINISH);
319 fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
323 if (output_file_name) {
326 fprintf (stderr, "Error syncing %s to disk: %s\n",
327 name_for_error, strerror (errno));
332 if (gzclose_w (output) != Z_OK) {
333 fprintf (stderr, "Error closing %s: %s\n", name_for_error,
334 gzerror (output, NULL));
340 if (output_file_name) {
341 ret = rename (tempname, output_file_name);
343 fprintf (stderr, "Error renaming %s to %s: %s\n",
344 tempname, output_file_name, strerror (errno));
350 if (ret != EXIT_SUCCESS && output)
351 (void) gzclose_w (output);
353 if (ret != EXIT_SUCCESS && output_file_name)
354 (void) unlink (tempname);
360 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
362 notmuch_database_t *notmuch;
363 const char *query_str = NULL;
366 if (notmuch_database_open (notmuch_config_get_database_path (config),
367 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
370 notmuch_exit_if_unmatched_db_uuid (notmuch);
372 const char *output_file_name = NULL;
375 int output_format = DUMP_FORMAT_BATCH_TAG;
377 bool gzip_output = 0;
379 notmuch_opt_desc_t options[] = {
380 { .opt_keyword = &output_format, .name = "format", .keywords =
381 (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
382 { "batch-tag", DUMP_FORMAT_BATCH_TAG },
384 { .opt_flags = &include, .name = "include", .keywords =
385 (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
386 { "properties", DUMP_INCLUDE_PROPERTIES },
387 { "tags", DUMP_INCLUDE_TAGS} } },
388 { .opt_string = &output_file_name, .name = "output" },
389 { .opt_bool = &gzip_output, .name = "gzip" },
390 { .opt_inherit = notmuch_shared_options },
394 opt_index = parse_arguments (argc, argv, options, 1);
398 notmuch_process_shared_options (argv[0]);
401 include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
403 if (opt_index < argc) {
404 query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
405 if (query_str == NULL) {
406 fprintf (stderr, "Out of memory.\n");
411 ret = notmuch_database_dump (notmuch, output_file_name, query_str,
412 output_format, include, gzip_output);
414 notmuch_database_destroy (notmuch);