[PATCH 1/2] cli: abstract database dumping from the dump command
authorJani Nikula <jani@nikula.org>
Tue, 25 Mar 2014 17:07:49 +0000 (19:07 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:00:58 +0000 (10:00 -0800)
2b/4e33aac3d68e4f63c305d0b7e60af1911cd7d6 [new file with mode: 0644]

diff --git a/2b/4e33aac3d68e4f63c305d0b7e60af1911cd7d6 b/2b/4e33aac3d68e4f63c305d0b7e60af1911cd7d6
new file mode 100644 (file)
index 0000000..b3b3142
--- /dev/null
@@ -0,0 +1,213 @@
+Return-Path: <jani@nikula.org>\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 227EA431FC9\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:08:02 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\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 0plqUxhpOOAu for <notmuch@notmuchmail.org>;\r
+       Tue, 25 Mar 2014 10:07:58 -0700 (PDT)\r
+Received: from mail-ee0-f45.google.com (mail-ee0-f45.google.com\r
+ [74.125.83.45])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 29213431FBF   for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:07:58 -0700\r
+ (PDT)\r
+Received: by mail-ee0-f45.google.com with SMTP id d17so702040eek.32\r
+       for <notmuch@notmuchmail.org>; Tue, 25 Mar 2014 10:07:56 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=1e100.net; s=20130820;\r
+       h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+       :references:in-reply-to:references;\r
+       bh=4zU2tMXnmmXLILHXOyigMU63aCMrSQ3mmyKDPtIPR0s=;\r
+       b=l5Z2J/ga/RoKA7O99T3WXaUm3kpIf5sMuxelA/PzcRCOt+lDHNOg+c3kgNM50rme8m\r
+       hOrozcDafPn00cb5CYtHRFTS/ptj98b/G81XOSUTLxTZ3Eo+YRDQDnIBN2pQk2O7FM1e\r
+       r6Rbg80r5iOgF4cqnnivVOnYVYUaFBQVsByfczePi17UNjcS8MM4Toxmauu0tQX7eqpf\r
+       tiq3LDzJRU60NGqNsQiEi3pu2kaQoj1k0Sp5EaQ+mzbltPQvqpbbW13HnEFStIKczB8u\r
+       Da966slyrM5ey9jApIrSsDD8DyaoFpnZ1fxX7j71+/Ze1k+MyRlPeeHD9AJBngqiDyNK\r
+       x8Ig==\r
+X-Gm-Message-State:\r
+ ALoCoQkMBe7m86ljBFITE4rnR2dK5EyObnF8mLXCZaiZjcsuDxmuX7Y4bKRCgaB55JUprKHTHNbj\r
+X-Received: by 10.14.220.130 with SMTP id o2mr15815574eep.42.1395767275613;\r
+       Tue, 25 Mar 2014 10:07:55 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
+       [88.195.111.91])\r
+       by mx.google.com with ESMTPSA id x3sm41360271eep.17.2014.03.25.10.07.54\r
+       for <multiple recipients>\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Tue, 25 Mar 2014 10:07:54 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/2] cli: abstract database dumping from the dump command\r
+Date: Tue, 25 Mar 2014 19:07:49 +0200\r
+Message-Id:\r
+ <8fce111b04768fda5594a0c1883d780183d19cda.1395767165.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.9.0\r
+In-Reply-To: <cover.1395767165.git.jani@nikula.org>\r
+References: <cover.1395767165.git.jani@nikula.org>\r
+In-Reply-To: <cover.1395767165.git.jani@nikula.org>\r
+References: <cover.1395767165.git.jani@nikula.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: Tue, 25 Mar 2014 17:08:02 -0000\r
+\r
+No functional changes, except for slight improvement in error\r
+handling.\r
+---\r
+ notmuch-dump.c | 107 ++++++++++++++++++++++++++++++++-------------------------\r
+ 1 file changed, 61 insertions(+), 46 deletions(-)\r
+\r
+diff --git a/notmuch-dump.c b/notmuch-dump.c\r
+index 158142f55b49..179e2e97bf61 100644\r
+--- a/notmuch-dump.c\r
++++ b/notmuch-dump.c\r
+@@ -22,56 +22,17 @@\r
+ #include "dump-restore-private.h"\r
+ #include "string-util.h"\r
\r
+-int\r
+-notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
++static int\r
++database_dump_file (notmuch_database_t *notmuch, FILE *output,\r
++                  const char *query_str, int output_format)\r
+ {\r
+-    notmuch_database_t *notmuch;\r
+     notmuch_query_t *query;\r
+-    FILE *output = stdout;\r
+     notmuch_messages_t *messages;\r
+     notmuch_message_t *message;\r
+     notmuch_tags_t *tags;\r
+-    const char *query_str = "";\r
+-\r
+-    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+-                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
+-      return EXIT_FAILURE;\r
+-\r
+-    char *output_file_name = NULL;\r
+-    int opt_index;\r
+-\r
+-    int output_format = DUMP_FORMAT_BATCH_TAG;\r
\r
+-    notmuch_opt_desc_t options[] = {\r
+-      { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',\r
+-        (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
+-                                { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
+-                                { 0, 0 } } },\r
+-      { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
+-      { 0, 0, 0, 0, 0 }\r
+-    };\r
+-\r
+-    opt_index = parse_arguments (argc, argv, options, 1);\r
+-    if (opt_index < 0)\r
+-      return EXIT_FAILURE;\r
+-\r
+-    if (output_file_name) {\r
+-      output = fopen (output_file_name, "w");\r
+-      if (output == NULL) {\r
+-          fprintf (stderr, "Error opening %s for writing: %s\n",\r
+-                   output_file_name, strerror (errno));\r
+-          return EXIT_FAILURE;\r
+-      }\r
+-    }\r
+-\r
+-\r
+-    if (opt_index < argc) {\r
+-      query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
+-      if (query_str == NULL) {\r
+-          fprintf (stderr, "Out of memory.\n");\r
+-          return EXIT_FAILURE;\r
+-      }\r
+-    }\r
++    if (! query_str)\r
++      query_str = "";\r
\r
+     query = notmuch_query_create (notmuch, query_str);\r
+     if (query == NULL) {\r
+@@ -149,11 +110,65 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
+       notmuch_message_destroy (message);\r
+     }\r
\r
++    notmuch_query_destroy (query);\r
++\r
++    return EXIT_SUCCESS;\r
++}\r
++\r
++int\r
++notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
++{\r
++    notmuch_database_t *notmuch;\r
++    FILE *output = stdout;\r
++    const char *query_str = NULL;\r
++    int ret;\r
++\r
++    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
++                             NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
++      return EXIT_FAILURE;\r
++\r
++    char *output_file_name = NULL;\r
++    int opt_index;\r
++\r
++    int output_format = DUMP_FORMAT_BATCH_TAG;\r
++\r
++    notmuch_opt_desc_t options[] = {\r
++      { NOTMUCH_OPT_KEYWORD, &output_format, "format", 'f',\r
++        (notmuch_keyword_t []){ { "sup", DUMP_FORMAT_SUP },\r
++                                { "batch-tag", DUMP_FORMAT_BATCH_TAG },\r
++                                { 0, 0 } } },\r
++      { NOTMUCH_OPT_STRING, &output_file_name, "output", 'o', 0  },\r
++      { 0, 0, 0, 0, 0 }\r
++    };\r
++\r
++    opt_index = parse_arguments (argc, argv, options, 1);\r
++    if (opt_index < 0)\r
++      return EXIT_FAILURE;\r
++\r
++    if (output_file_name) {\r
++      output = fopen (output_file_name, "w");\r
++      if (output == NULL) {\r
++          fprintf (stderr, "Error opening %s for writing: %s\n",\r
++                   output_file_name, strerror (errno));\r
++          return EXIT_FAILURE;\r
++      }\r
++    }\r
++\r
++\r
++    if (opt_index < argc) {\r
++      query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);\r
++      if (query_str == NULL) {\r
++          fprintf (stderr, "Out of memory.\n");\r
++          return EXIT_FAILURE;\r
++      }\r
++    }\r
++\r
++    ret = database_dump_file (notmuch, output, query_str, output_format);\r
++\r
+     if (output != stdout)\r
+       fclose (output);\r
\r
+-    notmuch_query_destroy (query);\r
+     notmuch_database_destroy (notmuch);\r
\r
+-    return EXIT_SUCCESS;\r
++    return ret;\r
+ }\r
+-- \r
+1.9.0\r
+\r