]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
NEWS: note some Emacs UI changes for 0.21
[notmuch] / notmuch-dump.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
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.
9  *
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.
14  *
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/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "hex-escape.h"
23 #include "string-util.h"
24 #include <zlib.h>
25
26
27 static int
28 database_dump_file (notmuch_database_t *notmuch, gzFile output,
29                     const char *query_str, int output_format)
30 {
31     notmuch_query_t *query;
32     notmuch_messages_t *messages;
33     notmuch_message_t *message;
34     notmuch_tags_t *tags;
35
36     if (! query_str)
37         query_str = "";
38
39     query = notmuch_query_create (notmuch, query_str);
40     if (query == NULL) {
41         fprintf (stderr, "Out of memory\n");
42         return EXIT_FAILURE;
43     }
44     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
45      * first results quickly at the expense of total time.
46      */
47     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
48
49     char *buffer = NULL;
50     size_t buffer_size = 0;
51     notmuch_status_t status;
52
53     status = notmuch_query_search_messages_st (query, &messages);
54     if (print_status_query ("notmuch dump", query, status))
55         return EXIT_FAILURE;
56
57     for (;
58          notmuch_messages_valid (messages);
59          notmuch_messages_move_to_next (messages)) {
60         int first = 1;
61         const char *message_id;
62
63         message = notmuch_messages_get (messages);
64         message_id = notmuch_message_get_message_id (message);
65
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);
75             continue;
76         }
77
78         if (output_format == DUMP_FORMAT_SUP) {
79             gzprintf (output, "%s (", message_id);
80         }
81
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);
86
87             if (! first)
88                 gzputs (output, " ");
89
90             first = 0;
91
92             if (output_format == DUMP_FORMAT_SUP) {
93                 gzputs (output, tag_str);
94             } else {
95                 if (hex_encode (notmuch, tag_str,
96                                 &buffer, &buffer_size) != HEX_SUCCESS) {
97                     fprintf (stderr, "Error: failed to hex-encode tag %s\n",
98                              tag_str);
99                     return EXIT_FAILURE;
100                 }
101                 gzprintf (output, "+%s", buffer);
102             }
103         }
104
105         if (output_format == DUMP_FORMAT_SUP) {
106             gzputs (output, ")\n");
107         } else {
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));
112                     return EXIT_FAILURE;
113             }
114             gzprintf (output, " -- %s\n", buffer);
115         }
116
117         notmuch_message_destroy (message);
118     }
119
120     notmuch_query_destroy (query);
121
122     return EXIT_SUCCESS;
123 }
124
125 /* Dump database into output_file_name if it's non-NULL, stdout
126  * otherwise.
127  */
128 int
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)
134 {
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";
138
139     char *tempname = NULL;
140     int outfd = -1;
141
142     int ret = -1;
143
144     if (output_file_name) {
145         tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
146         outfd = mkstemp (tempname);
147     } else {
148         outfd = dup (STDOUT_FILENO);
149     }
150
151     if (outfd < 0) {
152         fprintf (stderr, "Bad output file %s\n", name_for_error);
153         goto DONE;
154     }
155
156     output = gzdopen (outfd, mode);
157
158     if (output == NULL) {
159         fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
160                  name_for_error, strerror (errno));
161         if (close (outfd))
162             fprintf (stderr, "Error closing %s during shutdown: %s\n",
163                  name_for_error, strerror (errno));
164         goto DONE;
165     }
166
167     ret = database_dump_file (notmuch, output, query_str, output_format);
168     if (ret) goto DONE;
169
170     ret = gzflush (output, Z_FINISH);
171     if (ret) {
172         fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
173         goto DONE;
174     }
175
176     if (output_file_name) {
177         ret = fsync (outfd);
178         if (ret) {
179             fprintf (stderr, "Error syncing %s to disk: %s\n",
180                      name_for_error, strerror (errno));
181             goto DONE;
182         }
183     }
184
185     if (gzclose_w (output) != Z_OK) {
186         fprintf (stderr, "Error closing %s: %s\n", name_for_error,
187                  gzerror (output, NULL));
188         ret = EXIT_FAILURE;
189         output = NULL;
190         goto DONE;
191     }
192
193     if (output_file_name) {
194         ret = rename (tempname, output_file_name);
195         if (ret) {
196             fprintf (stderr, "Error renaming %s to %s: %s\n",
197                      tempname, output_file_name, strerror (errno));
198             goto DONE;
199         }
200
201     }
202  DONE:
203     if (ret != EXIT_SUCCESS && output)
204         (void) gzclose_w (output);
205
206     if (ret != EXIT_SUCCESS && output_file_name)
207         (void) unlink (tempname);
208
209     return ret;
210 }
211
212 int
213 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
214 {
215     notmuch_database_t *notmuch;
216     const char *query_str = NULL;
217     int ret;
218
219     if (notmuch_database_open (notmuch_config_get_database_path (config),
220                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
221         return EXIT_FAILURE;
222
223     notmuch_exit_if_unmatched_db_uuid (notmuch);
224
225     char *output_file_name = NULL;
226     int opt_index;
227
228     int output_format = DUMP_FORMAT_BATCH_TAG;
229     notmuch_bool_t gzip_output = 0;
230
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 },
235                                   { 0, 0 } } },
236         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
237         { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
238         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
239         { 0, 0, 0, 0, 0 }
240     };
241
242     opt_index = parse_arguments (argc, argv, options, 1);
243     if (opt_index < 0)
244         return EXIT_FAILURE;
245
246     notmuch_process_shared_options (argv[0]);
247
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");
252             return EXIT_FAILURE;
253         }
254     }
255
256     ret = notmuch_database_dump (notmuch, output_file_name, query_str,
257                                  output_format, gzip_output);
258
259     notmuch_database_destroy (notmuch);
260
261     return ret;
262 }