]> git.notmuchmail.org Git - notmuch/blob - notmuch-dump.c
perf-test: remove redundant "initial notmuch new"
[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
24 int
25 notmuch_dump_command (unused (void *ctx), int argc, char *argv[])
26 {
27     notmuch_config_t *config;
28     notmuch_database_t *notmuch;
29     notmuch_query_t *query;
30     FILE *output = stdout;
31     notmuch_messages_t *messages;
32     notmuch_message_t *message;
33     notmuch_tags_t *tags;
34     const char *query_str = "";
35
36     config = notmuch_config_open (ctx, NULL, NULL);
37     if (config == NULL)
38         return 1;
39
40     if (notmuch_database_open (notmuch_config_get_database_path (config),
41                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
42         return 1;
43
44     char *output_file_name = NULL;
45     int opt_index;
46
47     int output_format = DUMP_FORMAT_SUP;
48
49     notmuch_opt_desc_t options[] = {
50         { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',
51           (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },
52                                   { "batch-tag", DUMP_FORMAT_BATCH_TAG },
53                                   { 0, 0 } } },
54         { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },
55         { 0, 0, 0, 0, 0 }
56     };
57
58     opt_index = parse_arguments (argc, argv, options, 1);
59
60     if (opt_index < 0) {
61         /* diagnostics already printed */
62         return 1;
63     }
64
65     if (output_file_name) {
66         output = fopen (output_file_name, "w");
67         if (output == NULL) {
68             fprintf (stderr, "Error opening %s for writing: %s\n",
69                      output_file_name, strerror (errno));
70             return 1;
71         }
72     }
73
74
75     if (opt_index < argc) {
76         query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
77         if (query_str == NULL) {
78             fprintf (stderr, "Out of memory.\n");
79             return 1;
80         }
81     }
82
83     query = notmuch_query_create (notmuch, query_str);
84     if (query == NULL) {
85         fprintf (stderr, "Out of memory\n");
86         return 1;
87     }
88     /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
89      * first results quickly at the expense of total time.
90      */
91     notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED);
92
93     char *buffer = NULL;
94     size_t buffer_size = 0;
95
96     for (messages = notmuch_query_search_messages (query);
97          notmuch_messages_valid (messages);
98          notmuch_messages_move_to_next (messages)) {
99         int first = 1;
100         const char *message_id;
101
102         message = notmuch_messages_get (messages);
103         message_id = notmuch_message_get_message_id (message);
104
105         if (output_format == DUMP_FORMAT_SUP) {
106             fprintf (output, "%s (", message_id);
107         }
108
109         for (tags = notmuch_message_get_tags (message);
110              notmuch_tags_valid (tags);
111              notmuch_tags_move_to_next (tags)) {
112             const char *tag_str = notmuch_tags_get (tags);
113
114             if (! first)
115                 fputs (" ", output);
116
117             first = 0;
118
119             if (output_format == DUMP_FORMAT_SUP) {
120                 fputs (tag_str, output);
121             } else {
122                 if (hex_encode (notmuch, tag_str,
123                                 &buffer, &buffer_size) != HEX_SUCCESS) {
124                     fprintf (stderr, "Error: failed to hex-encode tag %s\n",
125                              tag_str);
126                     return 1;
127                 }
128                 fprintf (output, "+%s", buffer);
129             }
130         }
131
132         if (output_format == DUMP_FORMAT_SUP) {
133             fputs (")\n", output);
134         } else {
135             if (hex_encode (notmuch, message_id,
136                             &buffer, &buffer_size) != HEX_SUCCESS) {
137                     fprintf (stderr, "Error: failed to hex-encode msg-id %s\n",
138                              message_id);
139                     return 1;
140             }
141             fprintf (output, " -- id:%s\n", buffer);
142         }
143
144         notmuch_message_destroy (message);
145     }
146
147     if (output != stdout)
148         fclose (output);
149
150     notmuch_query_destroy (query);
151     notmuch_database_destroy (notmuch);
152
153     return 0;
154 }