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