Re: [PATCH v2 1/7] cli: allow query to come from stdin
authorAustin Clements <amdragon@MIT.EDU>
Sat, 24 Nov 2012 17:41:34 +0000 (12:41 +1900)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:50 +0000 (09:50 -0800)
2d/e8fd29800f68884cc4d5a78ab594ab2a1d04aa [new file with mode: 0644]

diff --git a/2d/e8fd29800f68884cc4d5a78ab594ab2a1d04aa b/2d/e8fd29800f68884cc4d5a78ab594ab2a1d04aa
new file mode 100644 (file)
index 0000000..a168c3d
--- /dev/null
@@ -0,0 +1,213 @@
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 782B8431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 09:41:38 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id r+KOVW0xgucj for <notmuch@notmuchmail.org>;\r
+       Sat, 24 Nov 2012 09:41:37 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (DMZ-MAILSEC-SCANNER-6.MIT.EDU\r
+       [18.7.68.35])\r
+       by olra.theworths.org (Postfix) with ESMTP id C13E2431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 09:41:37 -0800 (PST)\r
+X-AuditID: 12074423-b7fab6d0000008f9-d6-50b106d19812\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 0A.F2.02297.1D601B05; Sat, 24 Nov 2012 12:41:37 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+       by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id qAOHfaN0029112; \r
+       Sat, 24 Nov 2012 12:41:37 -0500\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+       (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id qAOHfYxm012063\r
+       (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+       Sat, 24 Nov 2012 12:41:35 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1TcJjS-0004gV-Fc; Sat, 24 Nov 2012 12:41:34 -0500\r
+Date: Sat, 24 Nov 2012 12:41:34 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: markwalters1009 <markwalters1009@gmail.com>\r
+Subject: Re: [PATCH v2 1/7] cli: allow query to come from stdin\r
+Message-ID: <20121124174134.GH4562@mit.edu>\r
+References: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com>\r
+       <1353763256-32336-2-git-send-email-markwalters1009@gmail.com>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1353763256-32336-2-git-send-email-markwalters1009@gmail.com>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmpmleLIzCtJLcpLzFFi42IRYrdT173ItjHA4NxxTovVc3ksrt+cyezA\r
+       5LFz1l12j2erbjEHMEVx2aSk5mSWpRbp2yVwZex5/Jy14IFMxeUdE1kaGOeLdjFycEgImEgc\r
+       WyDVxcgJZIpJXLi3nq2LkYtDSGAfo0TTha3MEM4GRom1R/ugnItMEnfn3mGHcJYwSvz6OJcd\r
+       pJ9FQFXixor1zCA2m4CGxLb9yxlBbBEBfYk9K26zgdjMAtIS3343M4HYwgL2Eq0f2llAbF4B\r
+       bYk9u3ZB7e5klOi+vZsJIiEocXLmExaIZi2JG/9eMoHcDTJo+T8OkDCngJfEuRO/wXaJCqhI\r
+       TDm5jW0Co9AsJN2zkHTPQuhewMi8ilE2JbdKNzcxM6c4NVm3ODkxLy+1SNdMLzezRC81pXQT\r
+       IzisXZR3MP45qHSIUYCDUYmHN+HM+gAh1sSy4srcQ4ySHExKorwlrBsDhPiS8lMqMxKLM+KL\r
+       SnNSiw8xSnAwK4nwPjuyIUCINyWxsiq1KB8mJc3BoiTOey3lpr+QQHpiSWp2ampBahFMVoaD\r
+       Q0mC9z/IUMGi1PTUirTMnBKENBMHJ8hwHqDhOSA1vMUFibnFmekQ+VOMilLivEdBEgIgiYzS\r
+       PLheWNp5xSgO9IowLw8wCQnxAFMWXPcroMFMQIOfzl4HMrgkESEl1cAoUd/dwqlaeKs372RC\r
+       dhLDvXsrzQ8+/LLOymvlO995B3huhLd/O/Lg4dKM+z+2Hz8UIq88e6XBQrll380O+C8XKeOW\r
+       CFj/VfXk+rLn351PR9T5zzPna1u9pi55jfKFHOcLuhMPFD63nfZ6su0nhrQr76IUHje6iWt1\r
+       mczb/teqv26tTcLXpv1hSizFGYmGWsxFxYkAhxI1AhYDAAA=\r
+Cc: notmuch@notmuchmail.org\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 24 Nov 2012 17:41:38 -0000\r
+\r
+Quoth markwalters1009 on Nov 24 at  1:20 pm:\r
+> From: Mark Walters <markwalters1009@gmail.com>\r
+> \r
+> After this series there will be times when a caller will want to pass\r
+> a very large query string to notmuch (eg a list of 10,000 message-ids)\r
+> and this can exceed the size of ARG_MAX. Hence allow notmuch to take\r
+> the query from stdin (if the query is -).\r
+> ---\r
+>  query-string.c |   41 +++++++++++++++++++++++++++++++++++++++++\r
+>  1 files changed, 41 insertions(+), 0 deletions(-)\r
+> \r
+> diff --git a/query-string.c b/query-string.c\r
+> index 6536512..b1fbdeb 100644\r
+> --- a/query-string.c\r
+> +++ b/query-string.c\r
+> @@ -20,6 +20,44 @@\r
+>  \r
+>  #include "notmuch-client.h"\r
+>  \r
+> +/* Read a single query string from STDIN, using\r
+> + * 'ctx' as the talloc owner for all allocations.\r
+> + *\r
+> + * This function returns NULL in case of insufficient memory or read\r
+> + * errors.\r
+> + */\r
+> +static char *\r
+> +query_string_from_stdin (void *ctx)\r
+> +{\r
+> +    char *query_string;\r
+> +    char buf[4096];\r
+> +    ssize_t remain;\r
+> +\r
+> +    query_string = talloc_strdup (ctx, "");\r
+> +    if (query_string == NULL)\r
+> +    return NULL;\r
+> +\r
+> +    for (;;) {\r
+> +    remain = read (STDIN_FILENO, buf, sizeof(buf) - 1);\r
+> +    if (remain == 0)\r
+> +        break;\r
+> +    if (remain < 0) {\r
+> +        if (errno == EINTR)\r
+> +            continue;\r
+> +        fprintf (stderr, "Error: reading from standard input: %s\n",\r
+> +                 strerror (errno));\r
+\r
+talloc_free (query_string) ?\r
+\r
+> +        return NULL;\r
+> +    }\r
+> +\r
+> +    buf[remain] = '\0';\r
+> +    query_string = talloc_strdup_append (query_string, buf);\r
+\r
+Eliminate the NUL in buf and instead\r
+ talloc_strndup_append (query_string, buf, remain) ?\r
+\r
+Should there be some (large) bound on the size of the query string to\r
+prevent runaway?\r
+\r
+> +    if (query_string == NULL)\r
+\r
+Technically it would be good to talloc_free the old pointer here, too.\r
+\r
+> +        return NULL;\r
+> +    }\r
+> +\r
+> +    return query_string;\r
+> +}\r
+> +\r
+\r
+This whole approach is O(n^2), which might actually matter for large\r
+query strings.  How about (tested, but only a little):\r
+\r
+#define MAX_QUERY_STRING_LENGTH (16 * 1024 * 1024)\r
+\r
+/* Read a single query string from STDIN, using 'ctx' as the talloc\r
+ * owner for all allocations.\r
+ *\r
+ * This function returns NULL in case of insufficient memory or read\r
+ * errors.\r
+ */\r
+static char *\r
+query_string_from_stdin (void *ctx)\r
+{\r
+    char *query_string = NULL, *new_qs;\r
+    size_t pos = 0, end = 0;\r
+    ssize_t got;\r
+\r
+    for (;;) {\r
+       if (end - pos < 512) {\r
+           end = MAX(end * 2, 1024);\r
+           if (end >= MAX_QUERY_STRING_LENGTH) {\r
+               fprintf (stderr, "Error: query too long\n");\r
+               goto FAIL;\r
+           }\r
+           new_qs = talloc_realloc (ctx, query_string, char, end);\r
+           if (new_qs == NULL)\r
+               goto FAIL;\r
+           query_string = new_qs;\r
+       }\r
+\r
+       got = read (STDIN_FILENO, query_string + pos, end - pos - 1);\r
+       if (got == 0)\r
+           break;\r
+       if (got < 0) {\r
+          if (errno == EINTR)\r
+              continue;\r
+          fprintf (stderr, "Error: reading from standard input: %s\n",\r
+                   strerror (errno));\r
+          goto FAIL;\r
+       }\r
+       pos += got;\r
+    }\r
+\r
+    query_string[pos] = '\0';\r
+    return query_string;\r
+\r
+ FAIL:\r
+    talloc_free (query_string);\r
+    return NULL;\r
+}\r
+\r
+>  /* Construct a single query string from the passed arguments, using\r
+>   * 'ctx' as the talloc owner for all allocations.\r
+>   *\r
+> @@ -35,6 +73,9 @@ query_string_from_args (void *ctx, int argc, char *argv[])\r
+>      char *query_string;\r
+>      int i;\r
+>  \r
+> +    if ((argc == 1) && (strcmp ("-", argv[0]) == 0))\r
+> +    return query_string_from_stdin (ctx);\r
+> +\r
+>      query_string = talloc_strdup (ctx, "");\r
+>      if (query_string == NULL)\r
+>      return NULL;\r