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