Re: [PATCH v4 09/16] index encrypted parts when asked.
[notmuch-archives.git] / 59 / 5d4e9baf406b071b619b3dcf3d884dc43f9b64
1 Return-Path: <bremner@tesseract.cs.unb.ca>\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 D9475431FAF\r
6         for <notmuch@notmuchmail.org>; Sat,  5 Apr 2014 08:44:39 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id YKXB9MuOeEk8 for <notmuch@notmuchmail.org>;\r
16         Sat,  5 Apr 2014 08:44:33 -0700 (PDT)\r
17 Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
18         [87.98.215.224]) (using TLSv1 with cipher AES256-SHA (256/256 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id A93AD431FAE\r
21         for <notmuch@notmuchmail.org>; Sat,  5 Apr 2014 08:44:33 -0700 (PDT)\r
22 Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
23         4.72) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
24         id 1WWSlS-00082G-IP\r
25         for notmuch@notmuchmail.org; Sat, 05 Apr 2014 15:44:14 +0000\r
26 Received: (nullmailer pid 10909 invoked by uid 1000); Sat, 05 Apr 2014\r
27         15:43:58 -0000\r
28 From: David Bremner <david@tethera.net>\r
29 To: notmuch@notmuchmail.org\r
30 Subject: v7 gzip dump restore patches\r
31 Date: Sat,  5 Apr 2014 12:43:50 -0300\r
32 Message-Id: <1396712636-10640-1-git-send-email-david@tethera.net>\r
33 X-Mailer: git-send-email 1.9.0\r
34 X-BeenThere: notmuch@notmuchmail.org\r
35 X-Mailman-Version: 2.1.13\r
36 Precedence: list\r
37 List-Id: "Use and development of the notmuch mail system."\r
38         <notmuch.notmuchmail.org>\r
39 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
40         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
41 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
42 List-Post: <mailto:notmuch@notmuchmail.org>\r
43 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
44 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
45         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
46 X-List-Received-Date: Sat, 05 Apr 2014 15:44:40 -0000\r
47 \r
48 Supercedes \r
49 \r
50   id:1396554083-3892-2-git-send-email-david@tethera.net\r
51 \r
52 - adds new analogues of strerror\r
53   - util_error_string\r
54   - gz_error_string\r
55 \r
56 Interdiff:\r
57 \r
58 diff --git a/configure b/configure\r
59 index 1d624f7..83b4af7 100755\r
60 --- a/configure\r
61 +++ b/configure\r
62 @@ -509,7 +509,7 @@ EOF\r
63         echo "  http://xapian.org/"\r
64      fi\r
65      if [ $have_zlib -eq 0 ]; then\r
66 -       echo "  zlib library (including development files such as headers)"\r
67 +       echo "  zlib library (>= version 1.2.5.2, including development files such as headers)"\r
68         echo "  http://zlib.net/"\r
69         echo\r
70      fi\r
71 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
72 index 2a7252a..2849eab 100644\r
73 --- a/notmuch-dump.c\r
74 +++ b/notmuch-dump.c\r
75 @@ -127,7 +127,7 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
76                        dump_format_t output_format,\r
77                        notmuch_bool_t gzip_output)\r
78  {\r
79 -    gzFile output;\r
80 +    gzFile output = NULL;\r
81      const char *mode = gzip_output ? "w9" : "wT";\r
82      const char *name_for_error = output_file_name ? output_file_name : "stdout";\r
83  \r
84 @@ -178,7 +178,10 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
85      }\r
86  \r
87      if (gzclose_w (output) != Z_OK) {\r
88 +       fprintf (stderr, "Error closing %s: %s\n", name_for_error,\r
89 +                gzerror (output, NULL));\r
90         ret = EXIT_FAILURE;\r
91 +       output = NULL;\r
92         goto DONE;\r
93      }\r
94  \r
95 @@ -192,6 +195,9 @@ notmuch_database_dump (notmuch_database_t *notmuch,\r
96  \r
97      }\r
98   DONE:\r
99 +    if (ret != EXIT_SUCCESS && output)\r
100 +       (void) gzclose_w (output);\r
101 +\r
102      if (ret != EXIT_SUCCESS && output_file_name)\r
103         (void) unlink (tempname);\r
104  \r
105 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
106 index eb5b7b2..7abee0a 100644\r
107 --- a/notmuch-restore.c\r
108 +++ b/notmuch-restore.c\r
109 @@ -129,7 +129,8 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
110      tag_op_list_t *tag_ops;\r
111  \r
112      char *input_file_name = NULL;\r
113 -    gzFile input;\r
114 +    const char *name_for_error = NULL;\r
115 +    gzFile input = NULL;\r
116      char *line = NULL;\r
117      void *line_ctx = NULL;\r
118      ssize_t line_len;\r
119 @@ -157,19 +158,26 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
120      };\r
121  \r
122      opt_index = parse_arguments (argc, argv, options, 1);\r
123 -    if (opt_index < 0)\r
124 -       return EXIT_FAILURE;\r
125 +    if (opt_index < 0) {\r
126 +       ret = EXIT_FAILURE;\r
127 +       goto DONE;\r
128 +    }\r
129 +\r
130 +    name_for_error = input_file_name ? input_file_name : "stdin";\r
131  \r
132      if (! accumulate)\r
133         flags |= TAG_FLAG_REMOVE_ALL;\r
134  \r
135 +    errno = 0;\r
136      if (input_file_name)\r
137         input = gzopen (input_file_name, "r");\r
138      else {\r
139         int infd = dup (STDIN_FILENO);\r
140         if (infd < 0) {\r
141 -           fprintf (stderr, "Error duping stdin\n");\r
142 -           return EXIT_FAILURE;\r
143 +           fprintf (stderr, "Error duping stdin: %s\n",\r
144 +                    strerror (errno));\r
145 +           ret = EXIT_FAILURE;\r
146 +           goto DONE;\r
147         }\r
148         input = gzdopen (infd, "r");\r
149         if (! input)\r
150 @@ -178,19 +186,22 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
151  \r
152      if (input == NULL) {\r
153         fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",\r
154 -                input_file_name ? input_file_name : "stdin", strerror (errno));\r
155 -       return EXIT_FAILURE;\r
156 +                name_for_error, strerror (errno));\r
157 +       ret = EXIT_FAILURE;\r
158 +       goto DONE;\r
159      }\r
160  \r
161      if (opt_index < argc) {\r
162         fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]);\r
163 -       return EXIT_FAILURE;\r
164 +       ret = EXIT_FAILURE;\r
165 +       goto DONE;\r
166      }\r
167  \r
168      tag_ops = tag_op_list_create (config);\r
169      if (tag_ops == NULL) {\r
170         fprintf (stderr, "Out of memory.\n");\r
171 -       return EXIT_FAILURE;\r
172 +       ret = EXIT_FAILURE;\r
173 +       goto DONE;\r
174      }\r
175  \r
176      do {\r
177 @@ -199,12 +210,17 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
178         status = gz_getline (line_ctx, &line, &line_len, input);\r
179  \r
180         /* empty input file not considered an error */\r
181 -       if (status == UTIL_EOF)\r
182 -           return EXIT_SUCCESS;\r
183 -\r
184 -       if (status)\r
185 -           return EXIT_FAILURE;\r
186 +       if (status == UTIL_EOF) {\r
187 +           ret = EXIT_SUCCESS;\r
188 +           goto DONE;\r
189 +       }\r
190  \r
191 +       if (status) {\r
192 +           fprintf (stderr, "Error reading (gzipped) input: %s\n",\r
193 +                    gz_error_string(status, input));\r
194 +           ret = EXIT_FAILURE;\r
195 +           goto DONE;\r
196 +       }\r
197      } while ((line_len == 0) ||\r
198              (line[0] == '#') ||\r
199              /* the cast is safe because we checked about for line_len < 0 */\r
200 @@ -269,17 +285,37 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
201         if (ret)\r
202             break;\r
203  \r
204 -    }  while (gz_getline (line_ctx, &line, &line_len, input) == UTIL_SUCCESS);\r
205 +    }  while (! (ret = gz_getline (line_ctx, &line, &line_len, input)));\r
206 +    \r
207  \r
208 -    if (line_ctx != NULL)\r
209 -       talloc_free (line_ctx);\r
210 +    /* EOF is normal loop termination condition, UTIL_SUCCESS is\r
211 +     * impossible here */\r
212 +    if (ret == UTIL_EOF) {\r
213 +       ret = UTIL_SUCCESS;\r
214 +    } else {\r
215 +       fprintf (stderr, "Error reading (gzipped) input: %s\n",\r
216 +                gz_error_string (ret, input));\r
217 +    }\r
218 +\r
219 +    /* currently this should not be after DONE: since we don't \r
220 +     * know if the xregcomp was reached\r
221 +     */\r
222  \r
223      if (input_format == DUMP_FORMAT_SUP)\r
224         regfree (&regex);\r
225  \r
226 -    notmuch_database_destroy (notmuch);\r
227 + DONE:\r
228 +    if (line_ctx != NULL)\r
229 +       talloc_free (line_ctx);\r
230  \r
231 -    gzclose_r (input);\r
232 +    if (notmuch)\r
233 +       notmuch_database_destroy (notmuch);\r
234 +\r
235 +    if (input && gzclose_r (input)) {\r
236 +       fprintf (stderr, "Error closing %s: %s\n",\r
237 +                name_for_error, gzerror (input, NULL));\r
238 +       ret = EXIT_FAILURE;\r
239 +    }\r
240  \r
241      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
242  }\r
243 diff --git a/util/Makefile.local b/util/Makefile.local\r
244 index e2a5b65..905f237 100644\r
245 --- a/util/Makefile.local\r
246 +++ b/util/Makefile.local\r
247 @@ -4,7 +4,8 @@ dir := util\r
248  extra_cflags += -I$(srcdir)/$(dir)\r
249  \r
250  libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
251 -                 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c\r
252 +                 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \\r
253 +               $(dir)/util.c\r
254  \r
255  libutil_modules := $(libutil_c_srcs:.c=.o)\r
256  \r
257 diff --git a/util/util.c b/util/util.c\r
258 new file mode 100644\r
259 index 0000000..3bd305d\r
260 --- /dev/null\r
261 +++ b/util/util.c\r
262 @@ -0,0 +1,24 @@\r
263 +#include "util.h"\r
264 +#include "error_util.h"\r
265 +#include <string.h>\r
266 +#include <errno.h>\r
267 +\r
268 +const char *\r
269 +util_error_string (util_status_t errnum)\r
270 +{\r
271 +    switch (errnum) {\r
272 +    case UTIL_SUCCESS:\r
273 +       return "none";\r
274 +    case UTIL_OUT_OF_MEMORY:\r
275 +       return "out of memory";\r
276 +    case UTIL_EOF:\r
277 +       return "end of file";\r
278 +    case UTIL_ERRNO:\r
279 +       return strerror (errno);\r
280 +    case UTIL_GZERROR:\r
281 +       /* we lack context to be more informative here */\r
282 +       return "zlib error";\r
283 +    default:\r
284 +       INTERNAL_ERROR("unexpected error status %d", errnum);\r
285 +    }\r
286 +}\r
287 diff --git a/util/util.h b/util/util.h\r
288 index 8663cfc..d12fadb 100644\r
289 --- a/util/util.h\r
290 +++ b/util/util.h\r
291 @@ -2,11 +2,28 @@\r
292  #define _UTIL_H\r
293  \r
294  typedef enum util_status {\r
295 +    /**\r
296 +     * No error occurred.\r
297 +     */\r
298      UTIL_SUCCESS = 0,\r
299 -    UTIL_ERROR = 1,\r
300 +    /**\r
301 +     * Out of memory.\r
302 +     */\r
303      UTIL_OUT_OF_MEMORY,\r
304 +    /**\r
305 +     * End of stream reached while attempting to read.\r
306 +     */\r
307      UTIL_EOF,\r
308 -    UTIL_FILE,\r
309 +    /**\r
310 +     * Low level error occured, consult errno.\r
311 +     */\r
312 +    UTIL_ERRNO,\r
313 +    /**\r
314 +     * Zlib error occured, call gzerror for details.\r
315 +     */\r
316 +    UTIL_GZERROR\r
317  } util_status_t;\r
318  \r
319 +const char *\r
320 +util_error_string (util_status_t status);\r
321  #endif\r
322 diff --git a/util/zlib-extra.c b/util/zlib-extra.c\r
323 index 922ab52..cb34845 100644\r
324 --- a/util/zlib-extra.c\r
325 +++ b/util/zlib-extra.c\r
326 @@ -54,9 +54,9 @@ gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)\r
327                 else\r
328                     goto SUCCESS;\r
329             case Z_ERRNO:\r
330 -               return UTIL_FILE;\r
331 +               return UTIL_ERRNO;\r
332             default:\r
333 -               return UTIL_ERROR;\r
334 +               return UTIL_GZERROR;\r
335             }\r
336         }\r
337  \r
338 @@ -75,3 +75,11 @@ gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)\r
339      *bytes_read = offset;\r
340      return UTIL_SUCCESS;\r
341  }\r
342 +\r
343 +const char *gz_error_string (util_status_t status, gzFile file) \r
344 +{\r
345 +    if (status == UTIL_GZERROR)\r
346 +       return gzerror (file, NULL);\r
347 +    else\r
348 +       return util_error_string (status);\r
349 +}\r
350 diff --git a/util/zlib-extra.h b/util/zlib-extra.h\r
351 index ed67115..dbaa0b9 100644\r
352 --- a/util/zlib-extra.h\r
353 +++ b/util/zlib-extra.h\r
354 @@ -4,8 +4,22 @@\r
355  #include "util.h"\r
356  #include <zlib.h>\r
357  \r
358 -/* Like getline, but read from a gzFile. Allocation is with talloc */\r
359 +/* Like getline, but read from a gzFile. Allocation is with talloc.\r
360 + * Returns:\r
361 + *\r
362 + *   UTIL_SUCCESS, UTIL_OUT_OF_MEMORY, UTIL_ERRNO, UTIL_GZERROR\r
363 + *                     Consult util.h for description\r
364 + *\r
365 + *   UTIL_EOF          End of file encountered before \r
366 + *                     any characters read\r
367 + */\r
368  util_status_t\r
369  gz_getline (void *ctx, char **lineptr, ssize_t *bytes_read, gzFile stream);\r
370  \r
371 +/* return a suitable error string based on the return status\r
372 + *  from gz_readline\r
373 + */\r
374 +\r
375 +const char *\r
376 +gz_error_string (util_status_t status, gzFile stream);\r
377  #endif\r