Re: Hi all
[notmuch-archives.git] / 49 / 897b3847c02d5e3ca038a999b6a049664b0c9a
1 Return-Path: <amdragon@mit.edu>\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 8BD82431FBF\r
6         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 20:26:56 -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.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 arQb2BlrWfO3 for <notmuch@notmuchmail.org>;\r
16         Tue,  1 Apr 2014 20:26:48 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-2.mit.edu (dmz-mailsec-scanner-2.mit.edu\r
18         [18.9.25.13])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 86B08431FBD\r
22         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 20:26:48 -0700 (PDT)\r
23 X-AuditID: 1209190d-f79776d000000ce9-3a-533b83778d2e\r
24 Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
25         (using TLS with cipher AES256-SHA (256/256 bits))\r
26         (Client did not present a certificate)\r
27         by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP\r
28         id F4.61.03305.7738B335; Tue,  1 Apr 2014 23:26:47 -0400 (EDT)\r
29 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
30         by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id s323QkkI020348; \r
31         Tue, 1 Apr 2014 23:26:47 -0400\r
32 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
33         (authenticated bits=0)\r
34         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
35         by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s323Qi9D007890\r
36         (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
37         Tue, 1 Apr 2014 23:26:46 -0400\r
38 Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
39         (envelope-from <amdragon@mit.edu>)\r
40         id 1WVBp6-0004YY-MZ; Tue, 01 Apr 2014 23:26:44 -0400\r
41 Date: Tue, 1 Apr 2014 23:26:44 -0400\r
42 From: Austin Clements <amdragon@MIT.EDU>\r
43 To: David Bremner <david@tethera.net>\r
44 Subject: Re: [Patch v5 3/6] util: add gz_readline\r
45 Message-ID: <20140402032644.GB25677@mit.edu>\r
46 References: <1396401381-18128-1-git-send-email-david@tethera.net>\r
47         <1396401381-18128-4-git-send-email-david@tethera.net>\r
48 MIME-Version: 1.0\r
49 Content-Type: text/plain; charset=us-ascii\r
50 Content-Disposition: inline\r
51 In-Reply-To: <1396401381-18128-4-git-send-email-david@tethera.net>\r
52 User-Agent: Mutt/1.5.21 (2010-09-15)\r
53 X-Brightmail-Tracker:\r
54  H4sIAAAAAAAAA+NgFuplleLIzCtJLcpLzFFi42IR4hTV1i1vtg42uDRL2+JGazejxfWbM5kd\r
55         mDyerbrF7LHl0HvmAKYoLpuU1JzMstQifbsEroye3RfYCuZrVJxqdWlg/CPXxcjJISFgIjH/\r
56         7EM2CFtM4sK99UA2F4eQwGwmiZ6mzUwQzgZGiUfHlzGDVAkJnGKS2LTKDMJewihx7LINiM0i\r
57         oCLRvb6ZBcRmE9CQ2LZ/OSOILSKgKnF122SwDcwC0hLffjczgdjCAoYS/5c9A6vnFdCROLJh\r
58         NwvEzEqJnvWLmCDighInZz5hgejVkrjx7yVQnANszvJ/HCBhTgEniTentoOViAKdMOXkNrYJ\r
59         jEKzkHTPQtI9C6F7ASPzKkbZlNwq3dzEzJzi1GTd4uTEvLzUIl0jvdzMEr3UlNJNjOCQluTd\r
60         wfjuoNIhRgEORiUe3gPnrYKFWBPLiitzDzFKcjApifJyBFsHC/El5adUZiQWZ8QXleakFh9i\r
61         lOBgVhLh/e4LlONNSaysSi3Kh0lJc7AoifO+tQaaJJCeWJKanZpakFoEk5Xh4FCS4PVuAmoU\r
62         LEpNT61Iy8wpQUgzcXCCDOcBGi4HUsNbXJCYW5yZDpE/xajL8WnDmkYmIZa8/LxUKXFeJZAi\r
63         AZCijNI8uDmwVPSKURzoLWFefZAqHmAag5v0CmgJE9AS7nVWIEtKEhFSUg2Mgs4FZ5daeLgt\r
64         dOeolJQ5M2PjL955fR9qWCLXLJ5ofZx5h+eVsLIbBZ5OhW1hLtuWrwg6f7W/fPVfpROf2cv3\r
65         K2289PP58n2f0o9NKD9r1vJ0A1v6vE59pXfWq7rYsy/PNtC/IJYv4yGUOsPI7Ue8s6AIn0ty\r
66         fOKHbvPcczKZB5mMb/C8lVdSYinOSDTUYi4qTgQARaRAISADAAA=\r
67 Cc: notmuch@notmuchmail.org\r
68 X-BeenThere: notmuch@notmuchmail.org\r
69 X-Mailman-Version: 2.1.13\r
70 Precedence: list\r
71 List-Id: "Use and development of the notmuch mail system."\r
72         <notmuch.notmuchmail.org>\r
73 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
74         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
75 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
76 List-Post: <mailto:notmuch@notmuchmail.org>\r
77 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
78 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
79         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
80 X-List-Received-Date: Wed, 02 Apr 2014 03:26:56 -0000\r
81 \r
82 Quoth David Bremner on Apr 01 at 10:16 pm:\r
83 > The idea is to provide a more or less drop in replacement for readline\r
84 > to read from zlib/gzip streams.  Take the opportunity to replace\r
85 > malloc with talloc.\r
86 > ---\r
87 >  util/Makefile.local |  2 +-\r
88 >  util/util.h         | 12 +++++++++\r
89 >  util/zlib-extra.c   | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
90 >  util/zlib-extra.h   | 11 ++++++++\r
91 >  4 files changed, 100 insertions(+), 1 deletion(-)\r
92 >  create mode 100644 util/util.h\r
93 >  create mode 100644 util/zlib-extra.c\r
94 >  create mode 100644 util/zlib-extra.h\r
95\r
96 > diff --git a/util/Makefile.local b/util/Makefile.local\r
97 > index 29c0ce6..e2a5b65 100644\r
98 > --- a/util/Makefile.local\r
99 > +++ b/util/Makefile.local\r
100 > @@ -4,7 +4,7 @@ dir := util\r
101 >  extra_cflags += -I$(srcdir)/$(dir)\r
102 >  \r
103 >  libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
104 > -               $(dir)/string-util.c $(dir)/talloc-extra.c\r
105 > +               $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c\r
106 >  \r
107 >  libutil_modules := $(libutil_c_srcs:.c=.o)\r
108 >  \r
109 > diff --git a/util/util.h b/util/util.h\r
110 > new file mode 100644\r
111 > index 0000000..8663cfc\r
112 > --- /dev/null\r
113 > +++ b/util/util.h\r
114 > @@ -0,0 +1,12 @@\r
115 > +#ifndef _UTIL_H\r
116 > +#define _UTIL_H\r
117 > +\r
118 > +typedef enum util_status {\r
119 > +    UTIL_SUCCESS = 0,\r
120 > +    UTIL_ERROR = 1,\r
121 > +    UTIL_OUT_OF_MEMORY,\r
122 > +    UTIL_EOF,\r
123 > +    UTIL_FILE,\r
124 > +} util_status_t;\r
125 > +\r
126 > +#endif\r
127 > diff --git a/util/zlib-extra.c b/util/zlib-extra.c\r
128 > new file mode 100644\r
129 > index 0000000..cb1eba0\r
130 > --- /dev/null\r
131 > +++ b/util/zlib-extra.c\r
132 > @@ -0,0 +1,76 @@\r
133 > +/* zlib-extra.c -  Extra or enhanced routines for compressed I/O.\r
134 > + *\r
135 > + * Copyright (c) 2014 David Bremner\r
136 > + *\r
137 > + * This program is free software: you can redistribute it and/or modify\r
138 > + * it under the terms of the GNU General Public License as published by\r
139 > + * the Free Software Foundation, either version 3 of the License, or\r
140 > + * (at your option) any later version.\r
141 > + *\r
142 > + * This program is distributed in the hope that it will be useful,\r
143 > + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
144 > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
145 > + * GNU General Public License for more details.\r
146 > + *\r
147 > + * You should have received a copy of the GNU General Public License\r
148 > + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
149 > + *\r
150 > + * Author: David Bremner <david@tethera.net>\r
151 > + */\r
152 > +\r
153 > +#include "zlib-extra.h"\r
154 > +#include <talloc.h>\r
155 > +#include <stdio.h>\r
156 > +#include <string.h>\r
157 > +\r
158 > +/* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */\r
159 > +util_status_t\r
160 > +gz_getline (void *talloc_ctx, char **bufptr, size_t *bufsiz, ssize_t *bytes_read,\r
161 \r
162 Talloc chunks know their size, so rather than taking bufsize, use\r
163 talloc_get_size (or talloc_array_length if you switch to talloc array\r
164 functions below).\r
165 \r
166 > +         gzFile stream)\r
167 > +{\r
168 > +    size_t len = *bufsiz;\r
169 > +    char *buf = *bufptr;\r
170 > +    size_t offset = 0;\r
171 > +\r
172 > +    if (len == 0 || buf == NULL) {\r
173 > +     /* same as getdelim from gnulib */\r
174 > +     len = 120;\r
175 \r
176 This is presumably because glibc's malloc has an 8 byte header.  Fun\r
177 fact: talloc has a 104 byte header (on 64-bit and including the malloc\r
178 header).\r
179 \r
180 > +     buf = talloc_size (talloc_ctx, len);\r
181 > +     if (buf == NULL)\r
182 > +         return UTIL_OUT_OF_MEMORY;\r
183 > +    }\r
184 > +\r
185 > +    while (1) {\r
186 > +     if (! gzgets (stream, buf + offset, len - offset)) {\r
187 > +         int zlib_status = 0;\r
188 > +         (void) gzerror (stream, &zlib_status);\r
189 > +         switch (zlib_status) {\r
190 > +         case Z_OK:\r
191 > +             /* follow getline behaviour */\r
192 > +             *bytes_read = -1;\r
193 \r
194 Is this really what getline does when the last line of a file isn't\r
195 \n-terminated?\r
196 \r
197 > +             return UTIL_EOF;\r
198 > +             break;\r
199 > +         case Z_ERRNO:\r
200 > +             return UTIL_FILE;\r
201 > +             break;\r
202 > +         default:\r
203 > +             return UTIL_ERROR;\r
204 > +         }\r
205 > +     }\r
206 > +\r
207 > +     offset += strlen (buf + offset);\r
208 > +\r
209 > +     if ( buf[offset - 1] == '\n' )\r
210 \r
211 Too many spaces!\r
212 \r
213 > +         break;\r
214 > +\r
215 > +     len *= 2;\r
216 > +     buf = talloc_realloc (talloc_ctx, buf, char, len);\r
217 \r
218 Or talloc_realloc_size, to match the initial talloc_size.\r
219 Alternatively, the initial talloc_size could be a talloc_array.\r
220 \r
221 > +     if (buf == NULL)\r
222 > +         return UTIL_OUT_OF_MEMORY;\r
223 > +    }\r
224 > +\r
225 > +    *bufptr = buf;\r
226 > +    *bufsiz = len;\r
227 > +    *bytes_read = offset;\r
228 > +    return UTIL_SUCCESS;\r
229 > +}\r
230 > diff --git a/util/zlib-extra.h b/util/zlib-extra.h\r
231 > new file mode 100644\r
232 > index 0000000..ed46ac1\r
233 > --- /dev/null\r
234 > +++ b/util/zlib-extra.h\r
235 > @@ -0,0 +1,11 @@\r
236 > +#ifndef _ZLIB_EXTRA_H\r
237 > +#define _ZLIB_EXTRA_H\r
238 > +\r
239 > +#include <zlib.h>\r
240 > +#include "util.h"\r
241 \r
242 I'd put "util.h" first so we're more likely to catch missing header\r
243 dependencies (obviously util.h doesn't have any right now, but in the\r
244 future).\r
245 \r
246 Also, I'd put a blank line after the #includes.\r
247 \r
248 > +/* Like getline, but read from a gzFile. Allocation is with talloc */\r
249 > +util_status_t\r
250 > +gz_getline (void *ctx, char **lineptr, size_t *line_size, ssize_t *bytes_read,\r
251 > +         gzFile stream);\r
252 > +\r
253 > +#endif\r