]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
cli: abstract database dumping from the dump command
[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 "dump-restore-private.h"
23 #include "string-util.h"
24
25 static int
26 database_dump_file (notmuch_database_t *notmuch, FILE *output,
27                     const char *query_str, int output_format)
28 {
29     notmuch_query_t *query;
30     notmuch_messages_t *messages;
31     notmuch_message_t *message;
32     notmuch_tags_t *tags;
33
34     if (! query_str)
35         query_str = "";
36
37     query = notmuch_query_create (notmuch, query_str);
38     if (query == NULL) {
39         fprintf (stderr, "Out of memory\n");
40         return EXIT_FAILURE;
41     }
42     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
43      * first results quickly at the expense of total time.
44      */
45     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
46
47     char *buffer = NULL;
48     size_t buffer_size = 0;
49
50     for (messages = notmuch_query_search_messages (query);
51          notmuch_messages_valid (messages);
52          notmuch_messages_move_to_next (messages)) {
53         int first = 1;
54         const char *message_id;
55
56         message = notmuch_messages_get (messages);
57         message_id = notmuch_message_get_message_id (message);
58
59         if (output_format == DUMP_FORMAT_BATCH_TAG &&
60             strchr (message_id, '\n')) {
61             /* This will produce a line break in the output, which
62              * would be difficult to handle in tools.  However, it's
63              * also impossible to produce an email containing a line
64              * break in a message ID because of unfolding, so we can
65              * safely disallow it. */
66             fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id);
67             notmuch_message_destroy (message);
68             continue;
69         }
70
71         if (output_format == DUMP_FORMAT_SUP) {
72             fprintf (output, "%s (", message_id);
73         }
74
75         for (tags = notmuch_message_get_tags (message);
76              notmuch_tags_valid (tags);
77              notmuch_tags_move_to_next (tags)) {
78             const char *tag_str = notmuch_tags_get (tags);
79
80             if (! first)
81                 fputs (" ", output);
82
83             first = 0;
84
85             if (output_format == DUMP_FORMAT_SUP) {
86                 fputs (tag_str, output);
87             } else {
88                 if (hex_encode (notmuch, tag_str,
89                                 &buffer, &buffer_size) != HEX_SUCCESS) {
90                     fprintf (stderr, "Error: failed to hex-encode tag %s\n",
91                              tag_str);
92                     return EXIT_FAILURE;
93                 }
94                 fprintf (output, "+%s", buffer);
95             }
96         }
97
98         if (output_format == DUMP_FORMAT_SUP) {
99             fputs (")\n", output);
100         } else {
101             if (make_boolean_term (notmuch, "id", message_id,
102                                    &buffer, &buffer_size)) {
103                     fprintf (stderr, "Error quoting message id %s: %s\n",
104                              message_id, strerror (errno));
105                     return EXIT_FAILURE;
106             }
107             fprintf (output, " -- %s\n", buffer);
108         }
109
110         notmuch_message_destroy (message);
111     }
112
113     notmuch_query_destroy (query);
114
115     return EXIT_SUCCESS;
116 }
117
118 int
119 notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
120 {
121     notmuch_database_t *notmuch;
122     FILE *output = stdout;
123     const char *query_str = NULL;
124     int ret;
125
126     if (notmuch_database_open (notmuch_config_get_database_path (config),
127                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
128         return EXIT_FAILURE;
129
130     char *output_file_name = NULL;
131     int opt_index;
132
133     int output_format = DUMP_FORMAT_BATCH_TAG;
134
135     notmuch_opt_desc_t options[] = {
136         { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
137           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
138                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
139                                   { 0, 0 } } },
140         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
141         { 0, 0, 0, 0, 0 }
142     };
143
144     opt_index = parse_arguments (argc, argv, options, 1);
145     if (opt_index < 0)
146         return EXIT_FAILURE;
147
148     if (output_file_name) {
149         output = fopen (output_file_name, "w");
150         if (output == NULL) {
151             fprintf (stderr, "Error opening %s for writing: %s\n",
152                      output_file_name, strerror (errno));
153             return EXIT_FAILURE;
154         }
155     }
156
157
158     if (opt_index < argc) {
159         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
160         if (query_str == NULL) {
161             fprintf (stderr, "Out of memory.\n");
162             return EXIT_FAILURE;
163         }
164     }
165
166     ret = database_dump_file (notmuch, output, query_str, output_format);
167
168     if (output != stdout)
169         fclose (output);
170
171     notmuch_database_destroy (notmuch);
172
173     return ret;
174 }