Re: [PATCH v4 02/16] Move crypto.c into libutil
[notmuch-archives.git] / 15 / 1f8452d515db2aae39ed28ca21da18b617a00d
1 Return-Path: <bremner@tethera.net>\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 BCD24431FC9\r
6         for <notmuch@notmuchmail.org>; Sat, 29 Mar 2014 11:16:59 -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 NX8JPB18EuIG for <notmuch@notmuchmail.org>;\r
16         Sat, 29 Mar 2014 11:16:52 -0700 (PDT)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 37183431FBF\r
21         for <notmuch@notmuchmail.org>; Sat, 29 Mar 2014 11:16:49 -0700 (PDT)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tethera.net>)\r
24         id 1WTxoG-0007wj-V8; Sat, 29 Mar 2014 15:16:48 -0300\r
25 Received: (nullmailer pid 29820 invoked by uid 1000); Sat, 29 Mar 2014\r
26         18:16:38 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v2 3/3] restore: transparently support gzipped input\r
30 Date: Sat, 29 Mar 2014 15:16:32 -0300\r
31 Message-Id: <1396116992-29640-4-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 1.9.0\r
33 In-Reply-To: <1396116992-29640-1-git-send-email-david@tethera.net>\r
34 References: <1396100785-8744-1-git-send-email-david@tethera.net>\r
35         <1396116992-29640-1-git-send-email-david@tethera.net>\r
36 X-BeenThere: notmuch@notmuchmail.org\r
37 X-Mailman-Version: 2.1.13\r
38 Precedence: list\r
39 List-Id: "Use and development of the notmuch mail system."\r
40         <notmuch.notmuchmail.org>\r
41 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
42         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
43 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
44 List-Post: <mailto:notmuch@notmuchmail.org>\r
45 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
46 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
47         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
48 X-List-Received-Date: Sat, 29 Mar 2014 18:17:00 -0000\r
49 \r
50 We rely completely on zlib to do the right thing in detecting gzipped\r
51 input. Since our dump format is chosen to be 7 bit ascii, this should\r
52 be fine.\r
53 ---\r
54  notmuch-restore.c         | 30 +++++++++++++++---------------\r
55  test/T240-dump-restore.sh | 14 ++++++++++++++\r
56  2 files changed, 29 insertions(+), 15 deletions(-)\r
57 \r
58 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
59 index c54d513..50da480 100644\r
60 --- a/notmuch-restore.c\r
61 +++ b/notmuch-restore.c\r
62 @@ -22,6 +22,7 @@\r
63  #include "hex-escape.h"\r
64  #include "tag-util.h"\r
65  #include "string-util.h"\r
66 +#include "zlib-extra.h"\r
67  \r
68  static regex_t regex;\r
69  \r
70 @@ -128,7 +129,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
71      tag_op_list_t *tag_ops;\r
72  \r
73      char *input_file_name = NULL;\r
74 -    FILE *input = stdin;\r
75 +    gzFile input;\r
76      char *line = NULL;\r
77      void *line_ctx = NULL;\r
78      size_t line_size;\r
79 @@ -163,13 +164,15 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
80      if (! accumulate)\r
81         flags |= TAG_FLAG_REMOVE_ALL;\r
82  \r
83 -    if (input_file_name) {\r
84 -       input = fopen (input_file_name, "r");\r
85 -       if (input == NULL) {\r
86 -           fprintf (stderr, "Error opening %s for reading: %s\n",\r
87 -                    input_file_name, strerror (errno));\r
88 -           return EXIT_FAILURE;\r
89 -       }\r
90 +    if (input_file_name)\r
91 +       input = gzopen (input_file_name, "r");\r
92 +    else\r
93 +       input = gzdopen (fileno (stdin), "r");\r
94 +\r
95 +    if (input == NULL) {\r
96 +       fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",\r
97 +                input_file_name ? input_file_name : "stdin", strerror (errno));\r
98 +       return EXIT_FAILURE;\r
99      }\r
100  \r
101      if (opt_index < argc) {\r
102 @@ -184,7 +187,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
103      }\r
104  \r
105      do {\r
106 -       line_len = getline (&line, &line_size, input);\r
107 +       line_len = gzgetline (line_ctx, &line, &line_size, input);\r
108  \r
109         /* empty input file not considered an error */\r
110         if (line_len < 0)\r
111 @@ -254,7 +257,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
112         if (ret)\r
113             break;\r
114  \r
115 -    }  while ((line_len = getline (&line, &line_size, input)) != -1);\r
116 +    }  while ((line_len = gzgetline (line_ctx, &line, &line_size, input)) != -1);\r
117  \r
118      if (line_ctx != NULL)\r
119         talloc_free (line_ctx);\r
120 @@ -262,13 +265,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
121      if (input_format == DUMP_FORMAT_SUP)\r
122         regfree (&regex);\r
123  \r
124 -    if (line)\r
125 -       free (line);\r
126 -\r
127      notmuch_database_destroy (notmuch);\r
128  \r
129 -    if (input != stdin)\r
130 -       fclose (input);\r
131 +    if (input_file_name != NULL)\r
132 +       gzclose_r (input);\r
133  \r
134      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
135  }\r
136 diff --git a/test/T240-dump-restore.sh b/test/T240-dump-restore.sh\r
137 index d79aca8..50d4d48 100755\r
138 --- a/test/T240-dump-restore.sh\r
139 +++ b/test/T240-dump-restore.sh\r
140 @@ -80,6 +80,20 @@ notmuch dump --gzip --output=dump-gzip-outfile.gz\r
141  gunzip dump-gzip-outfile.gz\r
142  test_expect_equal_file dump.expected dump-gzip-outfile\r
143  \r
144 +test_begin_subtest "restoring gzipped stdin"\r
145 +notmuch dump --gzip --output=backup.gz\r
146 +notmuch tag +new_tag '*'\r
147 +notmuch restore < backup.gz\r
148 +notmuch dump --output=dump.actual\r
149 +test_expect_equal_file dump.expected dump.actual\r
150 +\r
151 +test_begin_subtest "restoring gzipped file"\r
152 +notmuch dump --gzip --output=backup.gz\r
153 +notmuch tag +new_tag '*'\r
154 +notmuch restore --input=backup.gz\r
155 +notmuch dump --output=dump.actual\r
156 +test_expect_equal_file dump.expected dump.actual\r
157 +\r
158  # Note, we assume all messages from cworth have a message-id\r
159  # containing cworth.org\r
160  \r
161 -- \r
162 1.9.0\r
163 \r