]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
test: Port atomicity test to Python
[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
52     for (messages = notmuch_query_search_messages (query);
53          notmuch_messages_valid (messages);
54          notmuch_messages_move_to_next (messages)) {
55         int first = 1;
56         const char *message_id;
57
58         message = notmuch_messages_get (messages);
59         message_id = notmuch_message_get_message_id (message);
60
61         if (output_format == DUMP_FORMAT_BATCH_TAG &&
62             strchr (message_id, '\n')) {
63             /* This will produce a line break in the output, which
64              * would be difficult to handle in tools.  However, it's
65              * also impossible to produce an email containing a line
66              * break in a message ID because of unfolding, so we can
67              * safely disallow it. */
68             fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
69             notmuch_message_destroy (message);
70             continue;
71         }
72
73         if (output_format == DUMP_FORMAT_SUP) {
74             gzprintf (output, "%s (", message_id);
75         }
76
77         for (tags = notmuch_message_get_tags (message);
78              notmuch_tags_valid (tags);
79              notmuch_tags_move_to_next (tags)) {
80             const char *tag_str = notmuch_tags_get (tags);
81
82             if (! first)
83                 gzputs (output, " ");
84
85             first = 0;
86
87             if (output_format == DUMP_FORMAT_SUP) {
88                 gzputs (output, tag_str);
89             } else {
90                 if (hex_encode (notmuch, tag_str,
91                                 &buffer, &buffer_size) != HEX_SUCCESS) {
92                     fprintf (stderr, "Error: failed to hex-encode tag %s\n",
93                              tag_str);
94                     return EXIT_FAILURE;
95                 }
96                 gzprintf (output, "+%s", buffer);
97             }
98         }
99
100         if (output_format == DUMP_FORMAT_SUP) {
101             gzputs (output, ")\n");
102         } else {
103             if (make_boolean_term (notmuch, "id", message_id,
104                                    &buffer, &buffer_size)) {
105                     fprintf (stderr, "Error quoting message id %s: %s\n",
106                              message_id, strerror (errno));
107                     return EXIT_FAILURE;
108             }
109             gzprintf (output, " -- %s\n", buffer);
110         }
111
112         notmuch_message_destroy (message);
113     }
114
115     notmuch_query_destroy (query);
116
117     return EXIT_SUCCESS;
118 }
119
120 /* Dump database into output_file_name if it's non-NULL, stdout
121  * otherwise.
122  */
123 int
124 notmuch_database_dump (notmuch_database_t *notmuch,
125                        const char *output_file_name,
126                        const char *query_str,
127                        dump_format_t output_format,
128                        notmuch_bool_t gzip_output)
129 {
130     gzFile output = NULL;
131     const char *mode = gzip_output ? "w9" : "wT";
132     const char *name_for_error = output_file_name ? output_file_name : "stdout";
133
134     char *tempname = NULL;
135     int outfd = -1;
136
137     int ret = -1;
138
139     if (output_file_name) {
140         tempname = talloc_asprintf (notmuch, "%s.XXXXXX", output_file_name);
141         outfd = mkstemp (tempname);
142     } else {
143         outfd = dup (STDOUT_FILENO);
144     }
145
146     if (outfd < 0) {
147         fprintf (stderr, "Bad output file %s\n", name_for_error);
148         goto DONE;
149     }
150
151     output = gzdopen (outfd, mode);
152
153     if (output == NULL) {
154         fprintf (stderr, "Error opening %s for (gzip) writing: %s\n",
155                  name_for_error, strerror (errno));
156         if (close (outfd))
157             fprintf (stderr, "Error closing %s during shutdown: %s\n",
158                  name_for_error, strerror (errno));
159         goto DONE;
160     }
161
162     ret = database_dump_file (notmuch, output, query_str, output_format);
163     if (ret) goto DONE;
164
165     ret = gzflush (output, Z_FINISH);
166     if (ret) {
167         fprintf (stderr, "Error flushing output: %s\n", gzerror (output, NULL));
168         goto DONE;
169     }
170
171     if (output_file_name) {
172         ret = fsync (outfd);
173         if (ret) {
174             fprintf (stderr, "Error syncing %s to disk: %s\n",
175                      name_for_error, strerror (errno));
176             goto DONE;
177         }
178     }
179
180     if (gzclose_w (output) != Z_OK) {
181         fprintf (stderr, "Error closing %s: %s\n", name_for_error,
182                  gzerror (output, NULL));
183         ret = EXIT_FAILURE;
184         output = NULL;
185         goto DONE;
186     }
187
188     if (output_file_name) {
189         ret = rename (tempname, output_file_name);
190         if (ret) {
191             fprintf (stderr, "Error renaming %s to %s: %s\n",
192                      tempname, output_file_name, strerror (errno));
193             goto DONE;
194         }
195
196     }
197  DONE:
198     if (ret != EXIT_SUCCESS && output)
199         (void) gzclose_w (output);
200
201     if (ret != EXIT_SUCCESS && output_file_name)
202         (void) unlink (tempname);
203
204     return ret;
205 }
206
207 int
208 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
209 {
210     notmuch_database_t *notmuch;
211     const char *query_str = NULL;
212     int ret;
213
214     if (notmuch_database_open (notmuch_config_get_database_path (config),
215                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
216         return EXIT_FAILURE;
217
218     char *output_file_name = NULL;
219     int opt_index;
220
221     int output_format = DUMP_FORMAT_BATCH_TAG;
222     notmuch_bool_t gzip_output = 0;
223
224     notmuch_opt_desc_t options[] = {
225         { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
226           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
227                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
228                                   { 0, 0 } } },
229         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
230         { NOTMUCH_OPT_BOOLEAN, &gzip_output, "gzip", 'z', 0 },
231         { 0, 0, 0, 0, 0 }
232     };
233
234     opt_index = parse_arguments (argc, argv, options, 1);
235     if (opt_index < 0)
236         return EXIT_FAILURE;
237
238     if (opt_index < argc) {
239         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
240         if (query_str == NULL) {
241             fprintf (stderr, "Out of memory.\n");
242             return EXIT_FAILURE;
243         }
244     }
245
246     ret = notmuch_database_dump (notmuch, output_file_name, query_str,
247                                  output_format, gzip_output);
248
249     notmuch_database_destroy (notmuch);
250
251     return ret;
252 }