[RFC PATCH 3/5] util: add a homebrew scandir implementation
[notmuch-archives.git] / 2b / 4e33aac3d68e4f63c305d0b7e60af1911cd7d6
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 227EA431FC9\r
6         for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:08:02 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id 0plqUxhpOOAu for <notmuch@notmuchmail.org>;\r
17         Tue, 25 Mar 2014 10:07:58 -0700 (PDT)\r
18 Received: from mail-ee0-f45.google.com (mail-ee0-f45.google.com\r
19  [74.125.83.45])        (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
20  certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
21  29213431FBF    for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:07:58 -0700\r
22  (PDT)\r
23 Received: by mail-ee0-f45.google.com with SMTP id d17so702040eek.32\r
24         for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:07:56 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=1e100.net; s=20130820;\r
27         h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
28         :references:in-reply-to:references;\r
29         bh=4zU2tMXnmmXLILHXOyigMU63aCMrSQ3mmyKDPtIPR0s=;\r
30         b=l5Z2J/ga/RoKA7O99T3WXaUm3kpIf5sMuxelA/PzcRCOt+lDHNOg+c3kgNM50rme8m\r
31         hOrozcDafPn00cb5CYtHRFTS/ptj98b/G81XOSUTLxTZ3Eo+YRDQDnIBN2pQk2O7FM1e\r
32         r6Rbg80r5iOgF4cqnnivVOnYVYUaFBQVsByfczePi17UNjcS8MM4Toxmauu0tQX7eqpf\r
33         tiq3LDzJRU60NGqNsQiEi3pu2kaQoj1k0Sp5EaQ+mzbltPQvqpbbW13HnEFStIKczB8u\r
34         Da966slyrM5ey9jApIrSsDD8DyaoFpnZ1fxX7j71+/Ze1k+MyRlPeeHD9AJBngqiDyNK\r
35         x8Ig==\r
36 X-Gm-Message-State:\r
37  ALoCoQkMBe7m86ljBFITE4rnR2dK5EyObnF8mLXCZaiZjcsuDxmuX7Y4bKRCgaB55JUprKHTHNbj\r
38 X-Received: by 10.14.220.130 with SMTP id o2mr15815574eep.42.1395767275613;\r
39         Tue, 25 Mar 2014 10:07:55 -0700 (PDT)\r
40 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
41         [88.195.111.91])\r
42         by mx.google.com with ESMTPSA id x3sm41360271eep.17.2014.03.25.10.07.54\r
43         for <multiple recipients>\r
44         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
45         Tue, 25 Mar 2014 10:07:54 -0700 (PDT)\r
46 From: Jani Nikula <jani@nikula.org>\r
47 To: notmuch@notmuchmail.org\r
48 Subject: [PATCH 1/2] cli: abstract database dumping from the dump command\r
49 Date: Tue, 25 Mar 2014 19:07:49 +0200\r
50 Message-Id:\r
51  <8fce111b04768fda5594a0c1883d780183d19cda.1395767165.git.jani@nikula.org>\r
52 X-Mailer: git-send-email 1.9.0\r
53 In-Reply-To: <cover.1395767165.git.jani@nikula.org>\r
54 References: <cover.1395767165.git.jani@nikula.org>\r
55 In-Reply-To: <cover.1395767165.git.jani@nikula.org>\r
56 References: <cover.1395767165.git.jani@nikula.org>\r
57 X-BeenThere: notmuch@notmuchmail.org\r
58 X-Mailman-Version: 2.1.13\r
59 Precedence: list\r
60 List-Id: "Use and development of the notmuch mail system."\r
61         <notmuch.notmuchmail.org>\r
62 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
63         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
64 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
65 List-Post: <mailto:notmuch@notmuchmail.org>\r
66 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
67 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
68         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
69 X-List-Received-Date: Tue, 25 Mar 2014 17:08:02 -0000\r
70 \r
71 No functional changes, except for slight improvement in error\r
72 handling.\r
73 ---\r
74  notmuch-dump.c | 107 ++++++++++++++++++++++++++++++++-------------------------\r
75  1 file changed, 61 insertions(+), 46 deletions(-)\r
76 \r
77 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
78 index 158142f55b49..179e2e97bf61 100644\r
79 --- a/notmuch-dump.c\r
80 +++ b/notmuch-dump.c\r
81 @@ -22,56 +22,17 @@\r
82  #include "dump-restore-private.h"\r
83  #include "string-util.h"\r
84  \r
85 -int\r
86 -notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
87 +static int\r
88 +database_dump_file (notmuch_database_t *notmuch, FILE *output,\r
89 +                   const char *query_str, int output_format)\r
90  {\r
91 -    notmuch_database_t *notmuch;\r
92      notmuch_query_t *query;\r
93 -    FILE *output = stdout;\r
94      notmuch_messages_t *messages;\r
95      notmuch_message_t *message;\r
96      notmuch_tags_t *tags;\r
97 -    const char *query_str = "";\r
98 -\r
99 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
100 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
101 -       return EXIT_FAILURE;\r
102 -\r
103 -    char *output_file_name = NULL;\r
104 -    int opt_index;\r
105 -\r
106 -    int output_format = DUMP_FORMAT_BATCH_TAG;\r
107  \r
108 -    notmuch_opt_desc_t options[] = {\r
109 -       { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',\r
110 -         (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
111 -                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
112 -                                 { 0, 0 } } },\r
113 -       { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
114 -       { 0, 0, 0, 0, 0 }\r
115 -    };\r
116 -\r
117 -    opt_index = parse_arguments (argc, argv, options, 1);\r
118 -    if (opt_index < 0)\r
119 -       return EXIT_FAILURE;\r
120 -\r
121 -    if (output_file_name) {\r
122 -       output = fopen (output_file_name, "w");\r
123 -       if (output == NULL) {\r
124 -           fprintf (stderr, "Error opening %s for writing: %s\n",\r
125 -                    output_file_name, strerror (errno));\r
126 -           return EXIT_FAILURE;\r
127 -       }\r
128 -    }\r
129 -\r
130 -\r
131 -    if (opt_index < argc) {\r
132 -       query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
133 -       if (query_str == NULL) {\r
134 -           fprintf (stderr, "Out of memory.\n");\r
135 -           return EXIT_FAILURE;\r
136 -       }\r
137 -    }\r
138 +    if (! query_str)\r
139 +       query_str = "";\r
140  \r
141      query = notmuch_query_create (notmuch, query_str);\r
142      if (query == NULL) {\r
143 @@ -149,11 +110,65 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
144         notmuch_message_destroy (message);\r
145      }\r
146  \r
147 +    notmuch_query_destroy (query);\r
148 +\r
149 +    return EXIT_SUCCESS;\r
150 +}\r
151 +\r
152 +int\r
153 +notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
154 +{\r
155 +    notmuch_database_t *notmuch;\r
156 +    FILE *output = stdout;\r
157 +    const char *query_str = NULL;\r
158 +    int ret;\r
159 +\r
160 +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
161 +                              NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
162 +       return EXIT_FAILURE;\r
163 +\r
164 +    char *output_file_name = NULL;\r
165 +    int opt_index;\r
166 +\r
167 +    int output_format = DUMP_FORMAT_BATCH_TAG;\r
168 +\r
169 +    notmuch_opt_desc_t options[] = {\r
170 +       { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',\r
171 +         (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
172 +                                 { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
173 +                                 { 0, 0 } } },\r
174 +       { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
175 +       { 0, 0, 0, 0, 0 }\r
176 +    };\r
177 +\r
178 +    opt_index = parse_arguments (argc, argv, options, 1);\r
179 +    if (opt_index < 0)\r
180 +       return EXIT_FAILURE;\r
181 +\r
182 +    if (output_file_name) {\r
183 +       output = fopen (output_file_name, "w");\r
184 +       if (output == NULL) {\r
185 +           fprintf (stderr, "Error opening %s for writing: %s\n",\r
186 +                    output_file_name, strerror (errno));\r
187 +           return EXIT_FAILURE;\r
188 +       }\r
189 +    }\r
190 +\r
191 +\r
192 +    if (opt_index < argc) {\r
193 +       query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
194 +       if (query_str == NULL) {\r
195 +           fprintf (stderr, "Out of memory.\n");\r
196 +           return EXIT_FAILURE;\r
197 +       }\r
198 +    }\r
199 +\r
200 +    ret = database_dump_file (notmuch, output, query_str, output_format);\r
201 +\r
202      if (output != stdout)\r
203         fclose (output);\r
204  \r
205 -    notmuch_query_destroy (query);\r
206      notmuch_database_destroy (notmuch);\r
207  \r
208 -    return EXIT_SUCCESS;\r
209 +    return ret;\r
210  }\r
211 -- \r
212 1.9.0\r
213 \r