database error
[notmuch-archives.git] / 9b / 01efc1e5085320e22bc06a28f1a7b917d9211d
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 3830F431FAF\r
6         for <notmuch@notmuchmail.org>; Thu,  3 Apr 2014 19:44:22 -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 hgIiWuhI4QKI for <notmuch@notmuchmail.org>;\r
16         Thu,  3 Apr 2014 19:44:16 -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 8A786431FBF\r
21         for <notmuch@notmuchmail.org>; Thu,  3 Apr 2014 19:44:16 -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 1WVtY7-00017W-GA; Fri, 04 Apr 2014 02:08:07 +0000\r
25 Received: (nullmailer pid 4014 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 2/6] util: add gz_readline\r
30 Date: Thu,  3 Apr 2014 16:41:19 -0300\r
31 Message-Id: <1396554083-3892-3-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:44:22 -0000\r
48 \r
49 The idea is to provide a more or less drop in replacement for readline\r
50 to read from zlib/gzip streams.  Take the opportunity to replace\r
51 malloc with talloc.\r
52 ---\r
53  util/Makefile.local |  2 +-\r
54  util/util.h         | 12 +++++++++\r
55  util/zlib-extra.c   | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
56  util/zlib-extra.h   | 11 ++++++++\r
57  4 files changed, 101 insertions(+), 1 deletion(-)\r
58  create mode 100644 util/util.h\r
59  create mode 100644 util/zlib-extra.c\r
60  create mode 100644 util/zlib-extra.h\r
61 \r
62 diff --git a/util/Makefile.local b/util/Makefile.local\r
63 index 29c0ce6..e2a5b65 100644\r
64 --- a/util/Makefile.local\r
65 +++ b/util/Makefile.local\r
66 @@ -4,7 +4,7 @@ dir := util\r
67  extra_cflags += -I$(srcdir)/$(dir)\r
68  \r
69  libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
70 -                 $(dir)/string-util.c $(dir)/talloc-extra.c\r
71 +                 $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c\r
72  \r
73  libutil_modules := $(libutil_c_srcs:.c=.o)\r
74  \r
75 diff --git a/util/util.h b/util/util.h\r
76 new file mode 100644\r
77 index 0000000..8663cfc\r
78 --- /dev/null\r
79 +++ b/util/util.h\r
80 @@ -0,0 +1,12 @@\r
81 +#ifndef _UTIL_H\r
82 +#define _UTIL_H\r
83 +\r
84 +typedef enum util_status {\r
85 +    UTIL_SUCCESS = 0,\r
86 +    UTIL_ERROR = 1,\r
87 +    UTIL_OUT_OF_MEMORY,\r
88 +    UTIL_EOF,\r
89 +    UTIL_FILE,\r
90 +} util_status_t;\r
91 +\r
92 +#endif\r
93 diff --git a/util/zlib-extra.c b/util/zlib-extra.c\r
94 new file mode 100644\r
95 index 0000000..922ab52\r
96 --- /dev/null\r
97 +++ b/util/zlib-extra.c\r
98 @@ -0,0 +1,77 @@\r
99 +/* zlib-extra.c -  Extra or enhanced routines for compressed I/O.\r
100 + *\r
101 + * Copyright (c) 2014 David Bremner\r
102 + *\r
103 + * This program is free software: you can redistribute it and/or modify\r
104 + * it under the terms of the GNU General Public License as published by\r
105 + * the Free Software Foundation, either version 3 of the License, or\r
106 + * (at your option) any later version.\r
107 + *\r
108 + * This program is distributed in the hope that it will be useful,\r
109 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
110 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
111 + * GNU General Public License for more details.\r
112 + *\r
113 + * You should have received a copy of the GNU General Public License\r
114 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
115 + *\r
116 + * Author: David Bremner <david@tethera.net>\r
117 + */\r
118 +\r
119 +#include "zlib-extra.h"\r
120 +#include <talloc.h>\r
121 +#include <stdio.h>\r
122 +#include <string.h>\r
123 +\r
124 +/* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */\r
125 +util_status_t\r
126 +gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)\r
127 +{\r
128 +    char *buf = *bufptr;\r
129 +    unsigned int len;\r
130 +    size_t offset = 0;\r
131 +\r
132 +    if (buf) {\r
133 +       len = talloc_array_length (buf);\r
134 +    } else {\r
135 +       /* same as getdelim from gnulib */\r
136 +       len = 120;\r
137 +       buf = talloc_array (talloc_ctx, char, len);\r
138 +       if (buf == NULL)\r
139 +           return UTIL_OUT_OF_MEMORY;\r
140 +    }\r
141 +\r
142 +    while (1) {\r
143 +       if (! gzgets (stream, buf + offset, len - offset)) {\r
144 +           /* Null indicates EOF or error */\r
145 +           int zlib_status = 0;\r
146 +           (void) gzerror (stream, &zlib_status);\r
147 +           switch (zlib_status) {\r
148 +           case Z_OK:\r
149 +               /* no data read before EOF */\r
150 +               if (offset == 0)\r
151 +                   return UTIL_EOF;\r
152 +               else\r
153 +                   goto SUCCESS;\r
154 +           case Z_ERRNO:\r
155 +               return UTIL_FILE;\r
156 +           default:\r
157 +               return UTIL_ERROR;\r
158 +           }\r
159 +       }\r
160 +\r
161 +       offset += strlen (buf + offset);\r
162 +\r
163 +       if (buf[offset - 1] == '\n')\r
164 +           goto SUCCESS;\r
165 +\r
166 +       len *= 2;\r
167 +       buf = talloc_realloc (talloc_ctx, buf, char, len);\r
168 +       if (buf == NULL)\r
169 +           return UTIL_OUT_OF_MEMORY;\r
170 +    }\r
171 + SUCCESS:\r
172 +    *bufptr = buf;\r
173 +    *bytes_read = offset;\r
174 +    return UTIL_SUCCESS;\r
175 +}\r
176 diff --git a/util/zlib-extra.h b/util/zlib-extra.h\r
177 new file mode 100644\r
178 index 0000000..ed67115\r
179 --- /dev/null\r
180 +++ b/util/zlib-extra.h\r
181 @@ -0,0 +1,11 @@\r
182 +#ifndef _ZLIB_EXTRA_H\r
183 +#define _ZLIB_EXTRA_H\r
184 +\r
185 +#include "util.h"\r
186 +#include <zlib.h>\r
187 +\r
188 +/* Like getline, but read from a gzFile. Allocation is with talloc */\r
189 +util_status_t\r
190 +gz_getline (void *ctx, char **lineptr, ssize_t *bytes_read, gzFile stream);\r
191 +\r
192 +#endif\r
193 -- \r
194 1.9.0\r
195 \r