From: Jani Nikula Date: Tue, 25 Mar 2014 17:48:57 +0000 (+0200) Subject: [PATCH v2] cli: abstract dump file open from the dump command X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=8578ceb045e279c0c08b5c012ffc7e8a646e0312;p=notmuch-archives.git [PATCH v2] cli: abstract dump file open from the dump command --- diff --git a/f0/e438629f483dc7662f7143d51c4407eb3c0a9a b/f0/e438629f483dc7662f7143d51c4407eb3c0a9a new file mode 100644 index 000000000..b37c88089 --- /dev/null +++ b/f0/e438629f483dc7662f7143d51c4407eb3c0a9a @@ -0,0 +1,217 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 3873F431FBF + for ; Tue, 25 Mar 2014 10:49:08 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +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 4XnOq63NSIqT for ; + Tue, 25 Mar 2014 10:49:02 -0700 (PDT) +Received: from mail-ee0-f42.google.com (mail-ee0-f42.google.com + [74.125.83.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client + certificate requested) by olra.theworths.org (Postfix) with ESMTPS id + 7DD73431FBD for ; Tue, 25 Mar 2014 10:49:02 -0700 + (PDT) +Received: by mail-ee0-f42.google.com with SMTP id d17so743323eek.29 + for ; Tue, 25 Mar 2014 10:49:00 -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; + bh=Xgc2UWmTFhQZVkKmF56y2nz94HBdnmVQXwlfnWA7/Ik=; + b=CsKdcx1WL1EXL2izviNw42Zxiyk7FUYU4k6VHsVtGKhuDd4aCEaOHAgUb6gN8VVFsh + tgt3W/1Nkf0XQQWi1H/ir9trinXIQam9Me+vs/q9ghqKYelvP+iBBiyoGQVkYiW4wazg + YSxlbQJaXsxeKiAIVXVaD9+Sd0RnQla8JwN8XsmwgWMnxMyWFHs75XE8MuNMoLbAwkzW + MZdqMQrWgJZI4eLoQ0P7y6Tlep06lxfhWIqllwb/huO02/0bI/cFNK+mn6grGgTiJzXG + klb0xOR06iGeT79WUDU+sIzFm1njTqMXc+gBLqtM0SwuGghI7JzNFxya8mvw+3/TBtcP + HgRA== +X-Gm-Message-State: + ALoCoQkzk9e+GuLEqym9xaIMH6Oz1a0uqTZ9NZ2f6R/Z6MDxYE2oWsPVjNv81z85VG8+XYfBVbk0 +X-Received: by 10.14.198.197 with SMTP id v45mr71281009een.9.1395769739874; + Tue, 25 Mar 2014 10:48:59 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi. + [88.195.111.91]) + by mx.google.com with ESMTPSA id m1sm41573676een.7.2014.03.25.10.48.58 + for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Tue, 25 Mar 2014 10:48:58 -0700 (PDT) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH v2] cli: abstract dump file open from the dump command +Date: Tue, 25 Mar 2014 19:48:57 +0200 +Message-Id: <1395769737-14598-1-git-send-email-jani@nikula.org> +X-Mailer: git-send-email 1.9.0 +In-Reply-To: + <92929cc512b9e9cd5ccb7d4e537671ce84de514f.1395767165.git.jani@nikula.org> +References: + <92929cc512b9e9cd5ccb7d4e537671ce84de514f.1395767165.git.jani@nikula.org> +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:49:08 -0000 + +Also expose the dump function to the rest of notmuch. No functional +changes, except for slight improvement in error handling. +--- + dump-restore-private.h | 13 ------------- + notmuch-client.h | 11 +++++++++++ + notmuch-dump.c | 47 +++++++++++++++++++++++++++++++---------------- + notmuch-restore.c | 2 +- + 4 files changed, 43 insertions(+), 30 deletions(-) + delete mode 100644 dump-restore-private.h + +diff --git a/dump-restore-private.h b/dump-restore-private.h +deleted file mode 100644 +index 896a00430986..000000000000 +--- a/dump-restore-private.h ++++ /dev/null +@@ -1,13 +0,0 @@ +-#ifndef DUMP_RESTORE_PRIVATE_H +-#define DUMP_RESTORE_PRIVATE_H +- +-#include "hex-escape.h" +-#include "command-line-arguments.h" +- +-typedef enum dump_formats { +- DUMP_FORMAT_AUTO, +- DUMP_FORMAT_BATCH_TAG, +- DUMP_FORMAT_SUP +-} dump_format_t; +- +-#endif +diff --git a/notmuch-client.h b/notmuch-client.h +index 278b498a246a..d11064824ddf 100644 +--- a/notmuch-client.h ++++ b/notmuch-client.h +@@ -441,5 +441,16 @@ mime_node_child (mime_node_t *parent, int child); + mime_node_t * + mime_node_seek_dfs (mime_node_t *node, int n); + ++typedef enum dump_formats { ++ DUMP_FORMAT_AUTO, ++ DUMP_FORMAT_BATCH_TAG, ++ DUMP_FORMAT_SUP ++} dump_format_t; ++ ++int ++notmuch_database_dump (notmuch_database_t *notmuch, ++ const char *output_file_name, ++ const char *query_str, dump_format_t output_format); ++ + #include "command-line-arguments.h" + #endif +diff --git a/notmuch-dump.c b/notmuch-dump.c +index 179e2e97bf61..21702d793bcf 100644 +--- a/notmuch-dump.c ++++ b/notmuch-dump.c +@@ -19,7 +19,7 @@ + */ + + #include "notmuch-client.h" +-#include "dump-restore-private.h" ++#include "hex-escape.h" + #include "string-util.h" + + static int +@@ -115,11 +115,38 @@ database_dump_file (notmuch_database_t *notmuch, FILE *output, + return EXIT_SUCCESS; + } + ++/* Dump database into output_file_name if it's non-NULL, stdout ++ * otherwise. ++ */ ++int ++notmuch_database_dump (notmuch_database_t *notmuch, ++ const char *output_file_name, ++ const char *query_str, dump_format_t output_format) ++{ ++ FILE *output = stdout; ++ int ret; ++ ++ 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; ++ } ++ } ++ ++ ret = database_dump_file (notmuch, output, query_str, output_format); ++ ++ if (output != stdout) ++ fclose (output); ++ ++ return ret; ++} ++ + 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; + +@@ -145,16 +172,6 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + 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) { +@@ -163,10 +180,8 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + } + } + +- ret = database_dump_file (notmuch, output, query_str, output_format); +- +- if (output != stdout) +- fclose (output); ++ ret = notmuch_database_dump (notmuch, output_file_name, query_str, ++ output_format); + + notmuch_database_destroy (notmuch); + +diff --git a/notmuch-restore.c b/notmuch-restore.c +index f23ab983a93d..c54d51328b51 100644 +--- a/notmuch-restore.c ++++ b/notmuch-restore.c +@@ -19,7 +19,7 @@ + */ + + #include "notmuch-client.h" +-#include "dump-restore-private.h" ++#include "hex-escape.h" + #include "tag-util.h" + #include "string-util.h" + +-- +1.9.0 +