Re: [PATCH v4 08/16] reorganize indexing of multipart/signed and multipart/encrypted
[notmuch-archives.git] / 2d / e8fd29800f68884cc4d5a78ab594ab2a1d04aa
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 782B8431FAF\r
6         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 09:41:38 -0800 (PST)\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 r+KOVW0xgucj for <notmuch@notmuchmail.org>;\r
16         Sat, 24 Nov 2012 09:41:37 -0800 (PST)\r
17 Received: from dmz-mailsec-scanner-6.mit.edu (DMZ-MAILSEC-SCANNER-6.MIT.EDU\r
18         [18.7.68.35])\r
19         by olra.theworths.org (Postfix) with ESMTP id C13E2431FAE\r
20         for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 09:41:37 -0800 (PST)\r
21 X-AuditID: 12074423-b7fab6d0000008f9-d6-50b106d19812\r
22 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
23         by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id 0A.F2.02297.1D601B05; Sat, 24 Nov 2012 12:41:37 -0500 (EST)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
26         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qAOHfaN0029112; \r
27         Sat, 24 Nov 2012 12:41:37 -0500\r
28 Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
29         (authenticated bits=0)\r
30         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
31         by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qAOHfYxm012063\r
32         (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
33         Sat, 24 Nov 2012 12:41:35 -0500 (EST)\r
34 Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
35         (envelope-from <amdragon@mit.edu>)\r
36         id 1TcJjS-0004gV-Fc; Sat, 24 Nov 2012 12:41:34 -0500\r
37 Date: Sat, 24 Nov 2012 12:41:34 -0500\r
38 From: Austin Clements <amdragon@MIT.EDU>\r
39 To: markwalters1009 <markwalters1009@gmail.com>\r
40 Subject: Re: [PATCH v2 1/7] cli: allow query to come from stdin\r
41 Message-ID: <20121124174134.GH4562@mit.edu>\r
42 References: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com>\r
43         <1353763256-32336-2-git-send-email-markwalters1009@gmail.com>\r
44 MIME-Version: 1.0\r
45 Content-Type: text/plain; charset=us-ascii\r
46 Content-Disposition: inline\r
47 In-Reply-To: <1353763256-32336-2-git-send-email-markwalters1009@gmail.com>\r
48 User-Agent: Mutt/1.5.21 (2010-09-15)\r
49 X-Brightmail-Tracker:\r
50  H4sIAAAAAAAAA+NgFmpmleLIzCtJLcpLzFFi42IRYrdT173ItjHA4NxxTovVc3ksrt+cyezA\r
51         5LFz1l12j2erbjEHMEVx2aSk5mSWpRbp2yVwZex5/Jy14IFMxeUdE1kaGOeLdjFycEgImEgc\r
52         WyDVxcgJZIpJXLi3nq2LkYtDSGAfo0TTha3MEM4GRom1R/ugnItMEnfn3mGHcJYwSvz6OJcd\r
53         pJ9FQFXixor1zCA2m4CGxLb9yxlBbBEBfYk9K26zgdjMAtIS3343M4HYwgL2Eq0f2llAbF4B\r
54         bYk9u3ZB7e5klOi+vZsJIiEocXLmExaIZi2JG/9eMoHcDTJo+T8OkDCngJfEuRO/wXaJCqhI\r
55         TDm5jW0Co9AsJN2zkHTPQuhewMi8ilE2JbdKNzcxM6c4NVm3ODkxLy+1SNdMLzezRC81pXQT\r
56         IzisXZR3MP45qHSIUYCDUYmHN+HM+gAh1sSy4srcQ4ySHExKorwlrBsDhPiS8lMqMxKLM+KL\r
57         SnNSiw8xSnAwK4nwPjuyIUCINyWxsiq1KB8mJc3BoiTOey3lpr+QQHpiSWp2ampBahFMVoaD\r
58         Q0mC9z/IUMGi1PTUirTMnBKENBMHJ8hwHqDhOSA1vMUFibnFmekQ+VOMilLivEdBEgIgiYzS\r
59         PLheWNp5xSgO9IowLw8wCQnxAFMWXPcroMFMQIOfzl4HMrgkESEl1cAoUd/dwqlaeKs372RC\r
60         dhLDvXsrzQ8+/LLOymvlO995B3huhLd/O/Lg4dKM+z+2Hz8UIq88e6XBQrll380O+C8XKeOW\r
61         CFj/VfXk+rLn351PR9T5zzPna1u9pi55jfKFHOcLuhMPFD63nfZ6su0nhrQr76IUHje6iWt1\r
62         mczb/teqv26tTcLXpv1hSizFGYmGWsxFxYkAhxI1AhYDAAA=\r
63 Cc: notmuch@notmuchmail.org\r
64 X-BeenThere: notmuch@notmuchmail.org\r
65 X-Mailman-Version: 2.1.13\r
66 Precedence: list\r
67 List-Id: "Use and development of the notmuch mail system."\r
68         <notmuch.notmuchmail.org>\r
69 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
71 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
72 List-Post: <mailto:notmuch@notmuchmail.org>\r
73 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
74 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
75         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
76 X-List-Received-Date: Sat, 24 Nov 2012 17:41:38 -0000\r
77 \r
78 Quoth markwalters1009 on Nov 24 at  1:20 pm:\r
79 > From: Mark Walters <markwalters1009@gmail.com>\r
80\r
81 > After this series there will be times when a caller will want to pass\r
82 > a very large query string to notmuch (eg a list of 10,000 message-ids)\r
83 > and this can exceed the size of ARG_MAX. Hence allow notmuch to take\r
84 > the query from stdin (if the query is -).\r
85 > ---\r
86 >  query-string.c |   41 +++++++++++++++++++++++++++++++++++++++++\r
87 >  1 files changed, 41 insertions(+), 0 deletions(-)\r
88\r
89 > diff --git a/query-string.c b/query-string.c\r
90 > index 6536512..b1fbdeb 100644\r
91 > --- a/query-string.c\r
92 > +++ b/query-string.c\r
93 > @@ -20,6 +20,44 @@\r
94 >  \r
95 >  #include "notmuch-client.h"\r
96 >  \r
97 > +/* Read a single query string from STDIN, using\r
98 > + * 'ctx' as the talloc owner for all allocations.\r
99 > + *\r
100 > + * This function returns NULL in case of insufficient memory or read\r
101 > + * errors.\r
102 > + */\r
103 > +static char *\r
104 > +query_string_from_stdin (void *ctx)\r
105 > +{\r
106 > +    char *query_string;\r
107 > +    char buf[4096];\r
108 > +    ssize_t remain;\r
109 > +\r
110 > +    query_string = talloc_strdup (ctx, "");\r
111 > +    if (query_string == NULL)\r
112 > +     return NULL;\r
113 > +\r
114 > +    for (;;) {\r
115 > +     remain = read (STDIN_FILENO, buf, sizeof(buf) - 1);\r
116 > +     if (remain == 0)\r
117 > +         break;\r
118 > +     if (remain < 0) {\r
119 > +         if (errno == EINTR)\r
120 > +             continue;\r
121 > +         fprintf (stderr, "Error: reading from standard input: %s\n",\r
122 > +                  strerror (errno));\r
123 \r
124 talloc_free (query_string) ?\r
125 \r
126 > +         return NULL;\r
127 > +     }\r
128 > +\r
129 > +     buf[remain] = '\0';\r
130 > +     query_string = talloc_strdup_append (query_string, buf);\r
131 \r
132 Eliminate the NUL in buf and instead\r
133  talloc_strndup_append (query_string, buf, remain) ?\r
134 \r
135 Should there be some (large) bound on the size of the query string to\r
136 prevent runaway?\r
137 \r
138 > +     if (query_string == NULL)\r
139 \r
140 Technically it would be good to talloc_free the old pointer here, too.\r
141 \r
142 > +         return NULL;\r
143 > +    }\r
144 > +\r
145 > +    return query_string;\r
146 > +}\r
147 > +\r
148 \r
149 This whole approach is O(n^2), which might actually matter for large\r
150 query strings.  How about (tested, but only a little):\r
151 \r
152 #define MAX_QUERY_STRING_LENGTH (16 * 1024 * 1024)\r
153 \r
154 /* Read a single query string from STDIN, using 'ctx' as the talloc\r
155  * owner for all allocations.\r
156  *\r
157  * This function returns NULL in case of insufficient memory or read\r
158  * errors.\r
159  */\r
160 static char *\r
161 query_string_from_stdin (void *ctx)\r
162 {\r
163     char *query_string = NULL, *new_qs;\r
164     size_t pos = 0, end = 0;\r
165     ssize_t got;\r
166 \r
167     for (;;) {\r
168         if (end - pos < 512) {\r
169             end = MAX(end * 2, 1024);\r
170             if (end >= MAX_QUERY_STRING_LENGTH) {\r
171                 fprintf (stderr, "Error: query too long\n");\r
172                 goto FAIL;\r
173             }\r
174             new_qs = talloc_realloc (ctx, query_string, char, end);\r
175             if (new_qs == NULL)\r
176                 goto FAIL;\r
177             query_string = new_qs;\r
178         }\r
179 \r
180         got = read (STDIN_FILENO, query_string + pos, end - pos - 1);\r
181         if (got == 0)\r
182             break;\r
183         if (got < 0) {\r
184            if (errno == EINTR)\r
185                continue;\r
186            fprintf (stderr, "Error: reading from standard input: %s\n",\r
187                     strerror (errno));\r
188            goto FAIL;\r
189         }\r
190         pos += got;\r
191     }\r
192 \r
193     query_string[pos] = '\0';\r
194     return query_string;\r
195 \r
196  FAIL:\r
197     talloc_free (query_string);\r
198     return NULL;\r
199 }\r
200 \r
201 >  /* Construct a single query string from the passed arguments, using\r
202 >   * 'ctx' as the talloc owner for all allocations.\r
203 >   *\r
204 > @@ -35,6 +73,9 @@ query_string_from_args (void *ctx, int argc, char *argv[])\r
205 >      char *query_string;\r
206 >      int i;\r
207 >  \r
208 > +    if ((argc == 1) && (strcmp ("-", argv[0]) == 0))\r
209 > +     return query_string_from_stdin (ctx);\r
210 > +\r
211 >      query_string = talloc_strdup (ctx, "");\r
212 >      if (query_string == NULL)\r
213 >       return NULL;\r