]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
CLI: refactor dumping of tags.
[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     gzprintf (output, "#notmuch-dump %s:%d %s%s%s\n",
73               (output_format == DUMP_FORMAT_SUP) ? "sup" : "batch-tag",
74               NOTMUCH_DUMP_VERSION,
75               (include & DUMP_INCLUDE_CONFIG) ? "config" : "",
76               (include & DUMP_INCLUDE_TAGS) && (include & DUMP_INCLUDE_CONFIG) ? "," : "",
77               (include & DUMP_INCLUDE_TAGS) ? "tags" : "");
78 }
79
80 static int
81 dump_tags_message (void *ctx,
82                    notmuch_message_t *message, int output_format,
83                    gzFile output,
84                    char **buffer_p, size_t *size_p)
85 {
86     int first = 1;
87     const char *message_id;
88
89     message_id = notmuch_message_get_message_id (message);
90
91     if (output_format == DUMP_FORMAT_BATCH_TAG &&
92         strchr (message_id, '\n')) {
93         /* This will produce a line break in the output, which
94          * would be difficult to handle in tools.  However, it's
95          * also impossible to produce an email containing a line
96          * break in a message ID because of unfolding, so we can
97          * safely disallow it. */
98         fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
99         return EXIT_SUCCESS;
100     }
101
102     if (output_format == DUMP_FORMAT_SUP) {
103         gzprintf (output, "%s (", message_id);
104     }
105
106     for (notmuch_tags_t *tags = notmuch_message_get_tags (message);
107          notmuch_tags_valid (tags);
108          notmuch_tags_move_to_next (tags)) {
109         const char *tag_str = notmuch_tags_get (tags);
110
111         if (! first)
112             gzputs (output, " ");
113
114         first = 0;
115
116         if (output_format == DUMP_FORMAT_SUP) {
117             gzputs (output, tag_str);
118         } else {
119             if (hex_encode (ctx, tag_str,
120                             buffer_p, size_p) != HEX_SUCCESS) {
121                 fprintf (stderr, "Error: failed to hex-encode tag %s\n",
122                          tag_str);
123                 return EXIT_FAILURE;
124             }
125             gzprintf (output, "+%s", *buffer_p);
126         }
127     }
128
129     if (output_format == DUMP_FORMAT_SUP) {
130         gzputs (output, ")\n");
131     } else {
132         if (make_boolean_term (ctx, "id", message_id,
133                                buffer_p, size_p)) {
134             fprintf (stderr, "Error quoting message id %s: %s\n",
135                      message_id, strerror (errno));
136             return EXIT_FAILURE;
137         }
138         gzprintf (output, " -- %s\n", *buffer_p);
139     }
140     return EXIT_SUCCESS;
141 }
142
143 static int
144 database_dump_file (notmuch_database_t *notmuch, gzFile output,
145                     const char *query_str, int output_format, int include)
146 {
147     notmuch_query_t *query;
148     notmuch_messages_t *messages;
149     notmuch_message_t *message;
150     notmuch_status_t status;
151     char *buffer = NULL;
152     size_t buffer_size = 0;
153
154     print_dump_header (output, output_format, include);
155
156     if (include & DUMP_INCLUDE_CONFIG) {
157         if (print_status_database ("notmuch dump", notmuch,
158                                    database_dump_config(notmuch,output)))
159             return EXIT_FAILURE;
160     }
161
162     if (! (include & DUMP_INCLUDE_TAGS))
163         return EXIT_SUCCESS;
164
165     if (! query_str)
166         query_str = "";
167
168     query = notmuch_query_create (notmuch, query_str);
169     if (query == NULL) {
170         fprintf (stderr, "Out of memory\n");
171         return EXIT_FAILURE;
172     }
173     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
174      * first results quickly at the expense of total time.
175      */
176     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
177
178     status = notmuch_query_search_messages_st (query, &messages);
179     if (print_status_query ("notmuch dump", query, status))
180         return EXIT_FAILURE;
181
182     for (;
183          notmuch_messages_valid (messages);
184          notmuch_messages_move_to_next (messages)) {
185
186         message = notmuch_messages_get (messages);
187
188         if (dump_tags_message (notmuch, message, output_format, output,
189                                &buffer, &buffer_size))
190             return EXIT_FAILURE;
191
192         notmuch_message_destroy (message);
193     }
194
195     notmuch_query_destroy (query);
196
197     return EXIT_SUCCESS;
198 }
199
200 /* Dump database into output_file_name if it's non-NULL, stdout
201  * otherwise.
202  */
203 int
204 notmuch_database_dump (notmuch_database_t *notmuch,
205                        const char *output_file_name,
206                        const char *query_str,
207                        dump_format_t output_format,
208                        dump_include_t include,
209                        notmuch_bool_t gzip_output)
210 {
211     gzFile output = NULL;
212     const char *mode = gzip_output ? "w9" : "wT";
213     const char *name_for_error = output_file_name ? output_file_name : "stdout";
214
215     char *tempname = NULL;
216     int outfd = -1;
217
218     int ret = -1;
219
220     if (output_file_name) {
221         tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
222         outfd = mkstemp (tempname);
223     } else {
224         outfd = dup (STDOUT_FILENO);
225     }
226
227     if (outfd < 0) {
228         fprintf (stderr, "Bad output file %s\n", name_for_error);
229         goto DONE;
230     }
231
232     output = gzdopen (outfd, mode);
233
234     if (output == NULL) {
235         fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
236                  name_for_error, strerror (errno));
237         if (close (outfd))
238             fprintf (stderr, "Error closing %s during shutdown: %s\n",
239                  name_for_error, strerror (errno));
240         goto DONE;
241     }
242
243     ret = database_dump_file (notmuch, output, query_str, output_format, include);
244     if (ret) goto DONE;
245
246     ret = gzflush (output, Z_FINISH);
247     if (ret) {
248         fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
249         goto DONE;
250     }
251
252     if (output_file_name) {
253         ret = fsync (outfd);
254         if (ret) {
255             fprintf (stderr, "Error syncing %s to disk: %s\n",
256                      name_for_error, strerror (errno));
257             goto DONE;
258         }
259     }
260
261     if (gzclose_w (output) != Z_OK) {
262         fprintf (stderr, "Error closing %s: %s\n", name_for_error,
263                  gzerror (output, NULL));
264         ret = EXIT_FAILURE;
265         output = NULL;
266         goto DONE;
267     }
268
269     if (output_file_name) {
270         ret = rename (tempname, output_file_name);
271         if (ret) {
272             fprintf (stderr, "Error renaming %s to %s: %s\n",
273                      tempname, output_file_name, strerror (errno));
274             goto DONE;
275         }
276
277     }
278  DONE:
279     if (ret != EXIT_SUCCESS && output)
280         (void) gzclose_w (output);
281
282     if (ret != EXIT_SUCCESS && output_file_name)
283         (void) unlink (tempname);
284
285     return ret;
286 }
287
288 int
289 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
290 {
291     notmuch_database_t *notmuch;
292     const char *query_str = NULL;
293     int ret;
294
295     if (notmuch_database_open (notmuch_config_get_database_path (config),
296                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
297         return EXIT_FAILURE;
298
299     notmuch_exit_if_unmatched_db_uuid (notmuch);
300
301     char *output_file_name = NULL;
302     int opt_index;
303
304     int output_format = DUMP_FORMAT_BATCH_TAG;
305     int include = 0;
306     notmuch_bool_t gzip_output = 0;
307
308     notmuch_opt_desc_t options[] = {
309         { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
310           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
311                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
312                                   { 0, 0 } } },
313         { NOTMUCH_OPT_KEYWORD_FLAGS, &include, "include", 'I',
314           (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
315                                   { "tags", DUMP_INCLUDE_TAGS} } },
316         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
317         { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
318         { NOTMUCH_OPT_INHERIT, (void *) &notmuch_shared_options, NULL, 0, 0 },
319         { 0, 0, 0, 0, 0 }
320     };
321
322     opt_index = parse_arguments (argc, argv, options, 1);
323     if (opt_index < 0)
324         return EXIT_FAILURE;
325
326     notmuch_process_shared_options (argv[0]);
327
328     if (include == 0)
329         include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS;
330
331     if (opt_index < argc) {
332         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
333         if (query_str == NULL) {
334             fprintf (stderr, "Out of memory.\n");
335             return EXIT_FAILURE;
336         }
337     }
338
339     ret = notmuch_database_dump (notmuch, output_file_name, query_str,
340                                  output_format, include, gzip_output);
341
342     notmuch_database_destroy (notmuch);
343
344     return ret;
345 }