[PATCH v2 11/14] cli/reply: return internet address list from get header funcs
[notmuch-archives.git] / 77 / 1f4f20731d4917e4e76081a1b8921f6cfb05e8
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 F3A9E431FCF\r
6         for <notmuch@notmuchmail.org>; Thu,  3 Apr 2014 19:43:32 -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 Cx2eCdI3zupG for <notmuch@notmuchmail.org>;\r
16         Thu,  3 Apr 2014 19:43:27 -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 87296431FCB\r
21         for <notmuch@notmuchmail.org>; Thu,  3 Apr 2014 19:43:27 -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 1WVtWq-000174-1D; Fri, 04 Apr 2014 02:06:48 +0000\r
25 Received: (nullmailer pid 4018 invoked by uid 1000); Thu, 03 Apr 2014\r
26         19:41:39 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v6 4/6] restore: transparently support gzipped input\r
30 Date: Thu,  3 Apr 2014 16:41:21 -0300\r
31 Message-Id: <1396554083-3892-5-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 1.9.0\r
33 In-Reply-To: <1396554083-3892-1-git-send-email-david@tethera.net>\r
34 References: <1396554083-3892-1-git-send-email-david@tethera.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Fri, 04 Apr 2014 02:43:33 -0000\r
48 \r
49 We rely completely on zlib to do the right thing in detecting gzipped\r
50 input. Since our dump format is chosen to be 7 bit ascii, this should\r
51 be fine.\r
52 ---\r
53  doc/man1/notmuch-restore.rst |  8 ++++++++\r
54  notmuch-restore.c            | 41 ++++++++++++++++++++++++++---------------\r
55  test/T240-dump-restore.sh    | 14 ++++++++++++++\r
56  3 files changed, 48 insertions(+), 15 deletions(-)\r
57 \r
58 diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
59 index d6cf19a..936b138 100644\r
60 --- a/doc/man1/notmuch-restore.rst\r
61 +++ b/doc/man1/notmuch-restore.rst\r
62 @@ -50,6 +50,14 @@ Supported options for **restore** include\r
63              format, this heuristic, based the fact that batch-tag format\r
64              contains no parentheses, should be accurate.\r
65  \r
66 +GZIPPED INPUT\r
67 +=============\r
68 +\r
69 +\ **notmuch restore** will detect if the input is compressed in\r
70 +**gzip(1)** format and automatically decompress it while reading. This\r
71 +detection does not depend on file naming and in particular works for\r
72 +standard input.\r
73 +\r
74  SEE ALSO\r
75  ========\r
76  \r
77 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
78 index c54d513..eb5b7b2 100644\r
79 --- a/notmuch-restore.c\r
80 +++ b/notmuch-restore.c\r
81 @@ -22,6 +22,7 @@\r
82  #include "hex-escape.h"\r
83  #include "tag-util.h"\r
84  #include "string-util.h"\r
85 +#include "zlib-extra.h"\r
86  \r
87  static regex_t regex;\r
88  \r
89 @@ -128,10 +129,9 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
90      tag_op_list_t *tag_ops;\r
91  \r
92      char *input_file_name = NULL;\r
93 -    FILE *input = stdin;\r
94 +    gzFile input;\r
95      char *line = NULL;\r
96      void *line_ctx = NULL;\r
97 -    size_t line_size;\r
98      ssize_t line_len;\r
99  \r
100      int ret = 0;\r
101 @@ -163,13 +163,23 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
102      if (! accumulate)\r
103         flags |= TAG_FLAG_REMOVE_ALL;\r
104  \r
105 -    if (input_file_name) {\r
106 -       input = fopen (input_file_name, "r");\r
107 -       if (input == NULL) {\r
108 -           fprintf (stderr, "Error opening %s for reading: %s\n",\r
109 -                    input_file_name, strerror (errno));\r
110 +    if (input_file_name)\r
111 +       input = gzopen (input_file_name, "r");\r
112 +    else {\r
113 +       int infd = dup (STDIN_FILENO);\r
114 +       if (infd < 0) {\r
115 +           fprintf (stderr, "Error duping stdin\n");\r
116             return EXIT_FAILURE;\r
117         }\r
118 +       input = gzdopen (infd, "r");\r
119 +       if (! input)\r
120 +           close (infd);\r
121 +    }\r
122 +\r
123 +    if (input == NULL) {\r
124 +       fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",\r
125 +                input_file_name ? input_file_name : "stdin", strerror (errno));\r
126 +       return EXIT_FAILURE;\r
127      }\r
128  \r
129      if (opt_index < argc) {\r
130 @@ -184,12 +194,17 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
131      }\r
132  \r
133      do {\r
134 -       line_len = getline (&line, &line_size, input);\r
135 +       util_status_t status;\r
136 +\r
137 +       status = gz_getline (line_ctx, &line, &line_len, input);\r
138  \r
139         /* empty input file not considered an error */\r
140 -       if (line_len < 0)\r
141 +       if (status == UTIL_EOF)\r
142             return EXIT_SUCCESS;\r
143  \r
144 +       if (status)\r
145 +           return EXIT_FAILURE;\r
146 +\r
147      } while ((line_len == 0) ||\r
148              (line[0] == '#') ||\r
149              /* the cast is safe because we checked about for line_len < 0 */\r
150 @@ -254,7 +269,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
151         if (ret)\r
152             break;\r
153  \r
154 -    }  while ((line_len = getline (&line, &line_size, input)) != -1);\r
155 +    }  while (gz_getline (line_ctx, &line, &line_len, input) == UTIL_SUCCESS);\r
156  \r
157      if (line_ctx != NULL)\r
158         talloc_free (line_ctx);\r
159 @@ -262,13 +277,9 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
160      if (input_format == DUMP_FORMAT_SUP)\r
161         regfree (&regex);\r
162  \r
163 -    if (line)\r
164 -       free (line);\r
165 -\r
166      notmuch_database_destroy (notmuch);\r
167  \r
168 -    if (input != stdin)\r
169 -       fclose (input);\r
170 +    gzclose_r (input);\r
171  \r
172      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
173  }\r
174 diff --git a/test/T240-dump-restore.sh b/test/T240-dump-restore.sh\r
175 index b6d8602..efe463e 100755\r
176 --- a/test/T240-dump-restore.sh\r
177 +++ b/test/T240-dump-restore.sh\r
178 @@ -80,6 +80,20 @@ notmuch dump --gzip --output=dump-gzip-outfile.gz\r
179  gunzip dump-gzip-outfile.gz\r
180  test_expect_equal_file dump.expected dump-gzip-outfile\r
181  \r
182 +test_begin_subtest "restoring gzipped stdin"\r
183 +notmuch dump --gzip --output=backup.gz\r
184 +notmuch tag +new_tag '*'\r
185 +notmuch restore < backup.gz\r
186 +notmuch dump --output=dump.actual\r
187 +test_expect_equal_file dump.expected dump.actual\r
188 +\r
189 +test_begin_subtest "restoring gzipped file"\r
190 +notmuch dump --gzip --output=backup.gz\r
191 +notmuch tag +new_tag '*'\r
192 +notmuch restore --input=backup.gz\r
193 +notmuch dump --output=dump.actual\r
194 +test_expect_equal_file dump.expected dump.actual\r
195 +\r
196  # Note, we assume all messages from cworth have a message-id\r
197  # containing cworth.org\r
198  \r
199 -- \r
200 1.9.0\r
201 \r