--- /dev/null
+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 16B52431FBF\r
+ for <notmuch@notmuchmail.org>; Tue, 1 Apr 2014 19:49:52 -0700 (PDT)\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 Oxu4OOMTlZ+0 for <notmuch@notmuchmail.org>;\r
+ Tue, 1 Apr 2014 19:49:44 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu\r
+ [18.9.25.12])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id A9B5C431FBD\r
+ for <notmuch@notmuchmail.org>; Tue, 1 Apr 2014 19:49:43 -0700 (PDT)\r
+X-AuditID: 1209190c-f794a6d000000c27-c9-533b7ac5e591\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id C8.93.03111.5CA7B335; Tue, 1 Apr 2014 22:49:41 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id s322nedP017188; \r
+ Tue, 1 Apr 2014 22:49:41 -0400\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.8/8.12.4) with ESMTP id s322ncZu028902\r
+ (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT);\r
+ Tue, 1 Apr 2014 22:49:40 -0400\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.80)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1WVBFC-0004Ib-M1; Tue, 01 Apr 2014 22:49:38 -0400\r
+Date: Tue, 1 Apr 2014 22:49:38 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: David Bremner <david@tethera.net>\r
+Subject: Re: [Patch v5 4/6] restore: transparently support gzipped input\r
+Message-ID: <20140402024938.GA25677@mit.edu>\r
+References: <1396401381-18128-1-git-send-email-david@tethera.net>\r
+ <1396401381-18128-5-git-send-email-david@tethera.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+Content-Disposition: inline\r
+In-Reply-To: <1396401381-18128-5-git-send-email-david@tethera.net>\r
+User-Agent: Mutt/1.5.21 (2010-09-15)\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFmpileLIzCtJLcpLzFFi42IR4hTV1j1aZR1sMOOzhsWN1m5Gi+s3ZzI7\r
+ MHk8W3WL2WPLoffMAUxRXDYpqTmZZalF+nYJXBmrTp1lLziqXjH7bWUD43W5LkZODgkBE4lv\r
+ e/6wQNhiEhfurWfrYuTiEBKYzSQx8+w2dghnA6NEy7WJrBDOKSaJZa+7GCGcJYwSmxf0MIH0\r
+ swioSHydtYsZxGYT0JDYtn85I4gtIqAqcXXbZDYQm1lAWuLb72awemEBD4lt25aD7eYV0JGY\r
+ t+s0UA0H0NBKiUcXZSHCghInZz5hgWjVkrjx7yUTSAnImOX/OEDCnAJOEudWrmUHsUWBLphy\r
+ chvbBEahWUi6ZyHpnoXQvYCReRWjbEpulW5uYmZOcWqybnFyYl5eapGuoV5uZoleakrpJkZw\r
+ UEvy7GB8c1DpEKMAB6MSD++B81bBQqyJZcWVuYcYJTmYlER5OYKtg4X4kvJTKjMSizPii0pz\r
+ UosPMUpwMCuJ8H73BcrxpiRWVqUW5cOkpDlYlMR531oDTRJITyxJzU5NLUgtgsnKcHAoSfBO\r
+ rQRqFCxKTU+tSMvMKUFIM3FwggznARqeC1LDW1yQmFucmQ6RP8WoKCXOuwQkIQCSyCjNg+uF\r
+ JZ1XjOJArwjzfgep4gEmLLjuV0CDmYAGc6+zAhlckoiQkmpgrGmMecNWrfjNPcHLviriXWJ5\r
+ 3JzCExKzZqpO+X7z8iFJxTOcQuteMxXEhDFdqshfZFOgq6Ptr7A7q8i222DvPrZXaZ1TG/R8\r
+ 9n9iz2qvqM38W6+S5pyWoib0kyU5+36QqOjKkrbImM2358rpT27ZU8nyNeLHsi9vD18+/OGQ\r
+ arKwftcujv1KLMUZiYZazEXFiQAImOYMFQMAAA==\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: Wed, 02 Apr 2014 02:49:52 -0000\r
+\r
+Quoth David Bremner on Apr 01 at 10:16 pm:\r
+> We rely completely on zlib to do the right thing in detecting gzipped\r
+> input. Since our dump format is chosen to be 7 bit ascii, this should\r
+> be fine.\r
+> ---\r
+> doc/man1/notmuch-restore.rst | 8 ++++++++\r
+> notmuch-restore.c | 37 +++++++++++++++++++++----------------\r
+> test/T240-dump-restore.sh | 14 ++++++++++++++\r
+> 3 files changed, 43 insertions(+), 16 deletions(-)\r
+> \r
+> diff --git a/doc/man1/notmuch-restore.rst b/doc/man1/notmuch-restore.rst\r
+> index d6cf19a..936b138 100644\r
+> --- a/doc/man1/notmuch-restore.rst\r
+> +++ b/doc/man1/notmuch-restore.rst\r
+> @@ -50,6 +50,14 @@ Supported options for **restore** include\r
+> format, this heuristic, based the fact that batch-tag format\r
+> contains no parentheses, should be accurate.\r
+> \r
+> +GZIPPED INPUT\r
+> +=============\r
+> +\r
+> +\ **notmuch restore** will detect if the input is compressed in\r
+> +**gzip(1)** format and automatically decompress it while reading. This\r
+> +detection does not depend on file naming and in particular works for\r
+> +standard input.\r
+> +\r
+> SEE ALSO\r
+> ========\r
+> \r
+> diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+> index c54d513..86bce20 100644\r
+> --- a/notmuch-restore.c\r
+> +++ b/notmuch-restore.c\r
+> @@ -22,6 +22,7 @@\r
+> #include "hex-escape.h"\r
+> #include "tag-util.h"\r
+> #include "string-util.h"\r
+> +#include "zlib-extra.h"\r
+> \r
+> static regex_t regex;\r
+> \r
+> @@ -128,7 +129,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+> tag_op_list_t *tag_ops;\r
+> \r
+> char *input_file_name = NULL;\r
+> - FILE *input = stdin;\r
+> + gzFile input;\r
+> char *line = NULL;\r
+> void *line_ctx = NULL;\r
+> size_t line_size;\r
+> @@ -163,13 +164,15 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+> if (! accumulate)\r
+> flags |= TAG_FLAG_REMOVE_ALL;\r
+> \r
+> - if (input_file_name) {\r
+> - input = fopen (input_file_name, "r");\r
+> - if (input == NULL) {\r
+> - fprintf (stderr, "Error opening %s for reading: %s\n",\r
+> - input_file_name, strerror (errno));\r
+> - return EXIT_FAILURE;\r
+> - }\r
+> + if (input_file_name)\r
+> + input = gzopen (input_file_name, "r");\r
+> + else\r
+> + input = gzdopen (fileno (stdin), "r");\r
+\r
+As for patch 2, we also need to gzclose input on all paths out of this\r
+function, which also means we probably need to dup stdin above.\r
+\r
+> +\r
+> + if (input == NULL) {\r
+> + fprintf (stderr, "Error opening %s for (gzip) reading: %s\n",\r
+> + input_file_name ? input_file_name : "stdin", strerror (errno));\r
+> + return EXIT_FAILURE;\r
+> }\r
+> \r
+> if (opt_index < argc) {\r
+> @@ -184,12 +187,17 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+> }\r
+> \r
+> do {\r
+> - line_len = getline (&line, &line_size, input);\r
+> + util_status_t status;\r
+> +\r
+> + status = gz_getline (line_ctx, &line, &line_size, &line_len, input);\r
+> \r
+> /* empty input file not considered an error */\r
+> - if (line_len < 0)\r
+> + if (status == UTIL_EOF)\r
+> return EXIT_SUCCESS;\r
+> \r
+> + if (status)\r
+> + return EXIT_FAILURE;\r
+> +\r
+> } while ((line_len == 0) ||\r
+> (line[0] == '#') ||\r
+> /* the cast is safe because we checked about for line_len < 0 */\r
+> @@ -254,7 +262,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+> if (ret)\r
+> break;\r
+> \r
+> - } while ((line_len = getline (&line, &line_size, input)) != -1);\r
+> + } while (gz_getline (line_ctx, &line, &line_size, &line_len, input) == UTIL_SUCCESS);\r
+> \r
+> if (line_ctx != NULL)\r
+> talloc_free (line_ctx);\r
+> @@ -262,13 +270,10 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
+> if (input_format == DUMP_FORMAT_SUP)\r
+> regfree (®ex);\r
+> \r
+> - if (line)\r
+> - free (line);\r
+> -\r
+> notmuch_database_destroy (notmuch);\r
+> \r
+> - if (input != stdin)\r
+> - fclose (input);\r
+> + if (input_file_name != NULL)\r
+> + gzclose_r (input);\r
+> \r
+> return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
+> }\r
+> diff --git a/test/T240-dump-restore.sh b/test/T240-dump-restore.sh\r
+> index d79aca8..50d4d48 100755\r
+> --- a/test/T240-dump-restore.sh\r
+> +++ b/test/T240-dump-restore.sh\r
+> @@ -80,6 +80,20 @@ notmuch dump --gzip --output=dump-gzip-outfile.gz\r
+> gunzip dump-gzip-outfile.gz\r
+> test_expect_equal_file dump.expected dump-gzip-outfile\r
+> \r
+> +test_begin_subtest "restoring gzipped stdin"\r
+> +notmuch dump --gzip --output=backup.gz\r
+> +notmuch tag +new_tag '*'\r
+> +notmuch restore < backup.gz\r
+> +notmuch dump --output=dump.actual\r
+> +test_expect_equal_file dump.expected dump.actual\r
+> +\r
+> +test_begin_subtest "restoring gzipped file"\r
+> +notmuch dump --gzip --output=backup.gz\r
+> +notmuch tag +new_tag '*'\r
+> +notmuch restore --input=backup.gz\r
+> +notmuch dump --output=dump.actual\r
+> +test_expect_equal_file dump.expected dump.actual\r
+> +\r
+> # Note, we assume all messages from cworth have a message-id\r
+> # containing cworth.org\r
+> \r