]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
Make notmuch-mutt script more portable
[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 https://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 static int
27 database_dump_config (notmuch_database_t *notmuch, gzFile output)
28 {
29     notmuch_config_list_t *list;
30     int ret = EXIT_FAILURE;
31     char *buffer = NULL;
32     size_t buffer_size = 0;
33
34     if (print_status_database ("notmuch dump", notmuch,
35                                notmuch_database_get_config_list (notmuch, NULL, &list)))
36         goto DONE;
37
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));
43             goto DONE;
44         }
45         gzprintf (output, "#@ %s", buffer);
46
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) );
51             goto DONE;
52         }
53
54         gzprintf (output, " %s\n", buffer);
55     }
56
57     ret = EXIT_SUCCESS;
58
59   DONE:
60     if (list)
61         notmuch_config_list_destroy (list);
62
63     if (buffer)
64         talloc_free (buffer);
65
66     return ret;
67 }
68
69 static void
70 print_dump_header (gzFile output, int output_format, int include)
71 {
72     const char *sep = "";
73
74     gzprintf (output, "#notmuch-dump %s:%d ",
75               (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
76               NOTMUCH_DUMP_VERSION);
77
78     if (include & DUMP_INCLUDE_CONFIG) {
79         gzputs (output, "config");
80         sep = ",";
81     }
82     if (include & DUMP_INCLUDE_PROPERTIES) {
83         gzprintf (output, "%sproperties", sep);
84         sep = ",";
85     }
86     if (include & DUMP_INCLUDE_TAGS) {
87         gzprintf (output, "%stags", sep);
88     }
89     gzputs (output, "\n");
90 }
91
92 static int
93 dump_properties_message (void *ctx,
94                          notmuch_message_t *message,
95                          gzFile output,
96                          char **buffer_p, size_t *size_p)
97 {
98     const char *message_id;
99     notmuch_message_properties_t *list;
100     bool first = true;
101
102     message_id = notmuch_message_get_message_id (message);
103
104     if (strchr (message_id, '\n')) {
105         fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
106         return 0;
107     }
108
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;
112
113         if (first) {
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);
116                 return 1;
117             }
118             gzprintf (output, "#= %s", *buffer_p);
119             first = false;
120         }
121
122         key = notmuch_message_properties_key (list);
123         val = notmuch_message_properties_value (list);
124
125         if (hex_encode (ctx, key, buffer_p, size_p) != HEX_SUCCESS) {
126             fprintf (stderr, "Error: failed to hex-encode key %s\n", key);
127             return 1;
128         }
129         gzprintf (output, " %s", *buffer_p);
130
131         if (hex_encode (ctx, val, buffer_p, size_p) != HEX_SUCCESS) {
132             fprintf (stderr, "Error: failed to hex-encode value %s\n", val);
133             return 1;
134         }
135         gzprintf (output, "=%s", *buffer_p);
136     }
137     notmuch_message_properties_destroy (list);
138
139     if (! first)
140         gzprintf (output, "\n", *buffer_p);
141
142     return 0;
143 }
144
145 static int
146 dump_tags_message (void *ctx,
147                    notmuch_message_t *message, int output_format,
148                    gzFile output,
149                    char **buffer_p, size_t *size_p)
150 {
151     int first = 1;
152     const char *message_id;
153
154     message_id = notmuch_message_get_message_id (message);
155
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);
164         return EXIT_SUCCESS;
165     }
166
167     if (output_format == DUMP_FORMAT_SUP) {
168         gzprintf (output, "%s (", message_id);
169     }
170
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);
175
176         if (! first)
177             gzputs (output, " ");
178
179         first = 0;
180
181         if (output_format == DUMP_FORMAT_SUP) {
182             gzputs (output, tag_str);
183         } else {
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",
187                          tag_str);
188                 return EXIT_FAILURE;
189             }
190             gzprintf (output, "+%s", *buffer_p);
191         }
192     }
193
194     if (output_format == DUMP_FORMAT_SUP) {
195         gzputs (output, ")\n");
196     } else {
197         if (make_boolean_term (ctx, "id", message_id,
198                                buffer_p, size_p)) {
199             fprintf (stderr, "Error quoting message id %s: %s\n",
200                      message_id, strerror (errno));
201             return EXIT_FAILURE;
202         }
203         gzprintf (output, " -- %s\n", *buffer_p);
204     }
205     return EXIT_SUCCESS;
206 }
207
208 static int
209 database_dump_file (notmuch_database_t *notmuch, gzFile output,
210                     const char *query_str, int output_format, int include)
211 {
212     notmuch_query_t *query;
213     notmuch_messages_t *messages;
214     notmuch_message_t *message;
215     notmuch_status_t status;
216     char *buffer = NULL;
217     size_t buffer_size = 0;
218
219     print_dump_header (output, output_format, include);
220
221     if (include & DUMP_INCLUDE_CONFIG) {
222         if (print_status_database ("notmuch dump", notmuch,
223                                    database_dump_config (notmuch, output)))
224             return EXIT_FAILURE;
225     }
226
227     if (! (include & (DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES)))
228         return EXIT_SUCCESS;
229
230     if (! query_str)
231         query_str = "";
232
233     query = notmuch_query_create (notmuch, query_str);
234     if (query == NULL) {
235         fprintf (stderr, "Out of memory\n");
236         return EXIT_FAILURE;
237     }
238     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
239      * first results quickly at the expense of total time.
240      */
241     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
242
243     status = notmuch_query_search_messages (query, &messages);
244     if (print_status_query ("notmuch dump", query, status))
245         return EXIT_FAILURE;
246
247     for (;
248          notmuch_messages_valid (messages);
249          notmuch_messages_move_to_next (messages)) {
250
251         message = notmuch_messages_get (messages);
252
253         if ((include & DUMP_INCLUDE_TAGS) &&
254             dump_tags_message (notmuch, message, output_format, output,
255                                &buffer, &buffer_size))
256             return EXIT_FAILURE;
257
258         if ((include & DUMP_INCLUDE_PROPERTIES) &&
259             dump_properties_message (notmuch, message, output,
260                                      &buffer, &buffer_size))
261             return EXIT_FAILURE;
262
263         notmuch_message_destroy (message);
264     }
265
266     notmuch_query_destroy (query);
267
268     return EXIT_SUCCESS;
269 }
270
271 /* Dump database into output_file_name if it's non-NULL, stdout
272  * otherwise.
273  */
274 int
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,
280                        bool gzip_output)
281 {
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";
285
286     char *tempname = NULL;
287     int outfd = -1;
288
289     int ret = -1;
290
291     if (output_file_name) {
292         tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
293         outfd = mkstemp (tempname);
294     } else {
295         outfd = dup (STDOUT_FILENO);
296     }
297
298     if (outfd < 0) {
299         fprintf (stderr, "Bad output file %s\n", name_for_error);
300         goto DONE;
301     }
302
303     output = gzdopen (outfd, mode);
304
305     if (output == NULL) {
306         fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
307                  name_for_error, strerror (errno));
308         if (close (outfd))
309             fprintf (stderr, "Error closing %s during shutdown: %s\n",
310                      name_for_error, strerror (errno));
311         goto DONE;
312     }
313
314     ret = database_dump_file (notmuch, output, query_str, output_format, include);
315     if (ret) goto DONE;
316
317     ret = gzflush (output, Z_FINISH);
318     if (ret) {
319         fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
320         goto DONE;
321     }
322
323     if (output_file_name) {
324         ret = fsync (outfd);
325         if (ret) {
326             fprintf (stderr, "Error syncing %s to disk: %s\n",
327                      name_for_error, strerror (errno));
328             goto DONE;
329         }
330     }
331
332     ret = gzclose_w (output);
333     if (ret) {
334         fprintf (stderr, "Error closing %s: %s\n", name_for_error,
335                  gzerror (output, NULL));
336         ret = EXIT_FAILURE;
337         output = NULL;
338         goto DONE;
339     } else
340         output = NULL;
341
342     if (output_file_name) {
343         ret = rename (tempname, output_file_name);
344         if (ret) {
345             fprintf (stderr, "Error renaming %s to %s: %s\n",
346                      tempname, output_file_name, strerror (errno));
347             goto DONE;
348         }
349
350     }
351   DONE:
352     if (ret != EXIT_SUCCESS && output)
353         (void) gzclose_w (output);
354
355     if (ret != EXIT_SUCCESS && output_file_name)
356         (void) unlink (tempname);
357
358     return ret;
359 }
360
361 int
362 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
363 {
364     notmuch_database_t *notmuch;
365     const char *query_str = NULL;
366     int ret;
367
368     if (notmuch_database_open (notmuch_config_get_database_path (config),
369                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
370         return EXIT_FAILURE;
371
372     notmuch_exit_if_unmatched_db_uuid (notmuch);
373
374     const char *output_file_name = NULL;
375     int opt_index;
376
377     int output_format = DUMP_FORMAT_BATCH_TAG;
378     int include = 0;
379     bool gzip_output = 0;
380
381     notmuch_opt_desc_t options[] = {
382         { .opt_keyword = &output_format, .name = "format", .keywords =
383               (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
384                                       { "batch-tag", DUMP_FORMAT_BATCH_TAG },
385                                       { 0, 0 } } },
386         { .opt_flags = &include, .name = "include", .keywords =
387               (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
388                                       { "properties", DUMP_INCLUDE_PROPERTIES },
389                                       { "tags", DUMP_INCLUDE_TAGS } } },
390         { .opt_string = &output_file_name, .name = "output" },
391         { .opt_bool = &gzip_output, .name = "gzip" },
392         { .opt_inherit = notmuch_shared_options },
393         { }
394     };
395
396     opt_index = parse_arguments (argc, argv, options, 1);
397     if (opt_index < 0)
398         return EXIT_FAILURE;
399
400     notmuch_process_shared_options (argv[0]);
401
402     if (include == 0)
403         include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
404
405     if (opt_index < argc) {
406         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
407         if (query_str == NULL) {
408             fprintf (stderr, "Out of memory.\n");
409             return EXIT_FAILURE;
410         }
411     }
412
413     ret = notmuch_database_dump (notmuch, output_file_name, query_str,
414                                  output_format, include, gzip_output);
415
416     notmuch_database_destroy (notmuch);
417
418     return ret;
419 }