]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
test: regression tests for n_d_status_string and n_d_get_path
[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     query = notmuch_query_create (notmuch, query_str);
236     if (query == NULL) {
237         fprintf (stderr, "Out of memory\n");
238         return EXIT_FAILURE;
239     }
240     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
241      * first results quickly at the expense of total time.
242      */
243     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
244
245     status = notmuch_query_search_messages (query, &messages);
246     if (print_status_query ("notmuch dump", query, status))
247         return EXIT_FAILURE;
248
249     for (;
250          notmuch_messages_valid (messages);
251          notmuch_messages_move_to_next (messages)) {
252
253         message = notmuch_messages_get (messages);
254
255         if ((include & DUMP_INCLUDE_TAGS) &&
256             dump_tags_message (notmuch, message, output_format, output,
257                                &buffer, &buffer_size))
258             return EXIT_FAILURE;
259
260         if ((include & DUMP_INCLUDE_PROPERTIES) &&
261             dump_properties_message (notmuch, message, output,
262                                      &buffer, &buffer_size))
263             return EXIT_FAILURE;
264
265         notmuch_message_destroy (message);
266     }
267
268     notmuch_query_destroy (query);
269
270     return EXIT_SUCCESS;
271 }
272
273 /* Dump database into output_file_name if it's non-NULL, stdout
274  * otherwise.
275  */
276 int
277 notmuch_database_dump (notmuch_database_t *notmuch,
278                        const char *output_file_name,
279                        const char *query_str,
280                        dump_format_t output_format,
281                        dump_include_t include,
282                        bool gzip_output)
283 {
284     gzFile output = NULL;
285     const char *mode = gzip_output ? "w9" : "wT";
286     const char *name_for_error = output_file_name ? output_file_name : "stdout";
287
288     char *tempname = NULL;
289     int outfd = -1;
290
291     int ret = -1;
292
293     if (output_file_name) {
294         tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
295         outfd = mkstemp (tempname);
296     } else {
297         outfd = dup (STDOUT_FILENO);
298     }
299
300     if (outfd < 0) {
301         fprintf (stderr, "Bad output file %s\n", name_for_error);
302         goto DONE;
303     }
304
305     output = gzdopen (outfd, mode);
306
307     if (output == NULL) {
308         fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
309                  name_for_error, strerror (errno));
310         if (close (outfd))
311             fprintf (stderr, "Error closing %s during shutdown: %s\n",
312                      name_for_error, strerror (errno));
313         goto DONE;
314     }
315
316     ret = database_dump_file (notmuch, output, query_str, output_format, include);
317     if (ret) goto DONE;
318
319     ret = gzflush (output, Z_FINISH);
320     if (ret) {
321         fprintf (stderr, "Error flushing output: %s\n", gzerror_str (output));
322         goto DONE;
323     }
324
325     if (output_file_name) {
326         ret = fsync (outfd);
327         if (ret) {
328             fprintf (stderr, "Error syncing %s to disk: %s\n",
329                      name_for_error, strerror (errno));
330             goto DONE;
331         }
332     }
333
334     ret = gzclose_w (output);
335     if (ret) {
336         fprintf (stderr, "Error closing %s: %s\n", name_for_error,
337                  gzerror_str (output));
338         ret = EXIT_FAILURE;
339         output = NULL;
340         goto DONE;
341     } else
342         output = NULL;
343
344     if (output_file_name) {
345         ret = rename (tempname, output_file_name);
346         if (ret) {
347             fprintf (stderr, "Error renaming %s to %s: %s\n",
348                      tempname, output_file_name, strerror (errno));
349             goto DONE;
350         }
351
352     }
353   DONE:
354     if (ret != EXIT_SUCCESS && output)
355         (void) gzclose_w (output);
356
357     if (ret != EXIT_SUCCESS && output_file_name)
358         (void) unlink (tempname);
359
360     return ret;
361 }
362
363 int
364 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
365 {
366     notmuch_database_t *notmuch;
367     const char *query_str = NULL;
368     int ret;
369
370     if (notmuch_database_open (notmuch_config_get_database_path (config),
371                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
372         return EXIT_FAILURE;
373
374     notmuch_exit_if_unmatched_db_uuid (notmuch);
375
376     const char *output_file_name = NULL;
377     int opt_index;
378
379     int output_format = DUMP_FORMAT_BATCH_TAG;
380     int include = 0;
381     bool gzip_output = 0;
382
383     notmuch_opt_desc_t options[] = {
384         { .opt_keyword = &output_format, .name = "format", .keywords =
385               (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
386                                       { "batch-tag", DUMP_FORMAT_BATCH_TAG },
387                                       { 0, 0 } } },
388         { .opt_flags = &include, .name = "include", .keywords =
389               (notmuch_keyword_t []){ { "config", DUMP_INCLUDE_CONFIG },
390                                       { "properties", DUMP_INCLUDE_PROPERTIES },
391                                       { "tags", DUMP_INCLUDE_TAGS } } },
392         { .opt_string = &output_file_name, .name = "output" },
393         { .opt_bool = &gzip_output, .name = "gzip" },
394         { .opt_inherit = notmuch_shared_options },
395         { }
396     };
397
398     opt_index = parse_arguments (argc, argv, options, 1);
399     if (opt_index < 0)
400         return EXIT_FAILURE;
401
402     notmuch_process_shared_options (argv[0]);
403
404     if (include == 0)
405         include = DUMP_INCLUDE_CONFIG | DUMP_INCLUDE_TAGS | DUMP_INCLUDE_PROPERTIES;
406
407     if (opt_index < argc) {
408         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
409         if (query_str == NULL) {
410             fprintf (stderr, "Out of memory.\n");
411             return EXIT_FAILURE;
412         }
413     }
414
415     ret = notmuch_database_dump (notmuch, output_file_name, query_str,
416                                  output_format, include, gzip_output);
417
418     notmuch_database_destroy (notmuch);
419
420     return ret;
421 }