From: David Bremner Date: Mon, 13 Jun 2016 01:05:53 +0000 (+2100) Subject: [PATCH 6/8] CLI: refactor dumping of tags. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=84239188dff124ecc091c71fdbf0b37c16b4fa0c;p=notmuch-archives.git [PATCH 6/8] CLI: refactor dumping of tags. --- diff --git a/2a/d3b7bd588938e8ae84572909c54ad55ee0bdc8 b/2a/d3b7bd588938e8ae84572909c54ad55ee0bdc8 new file mode 100644 index 000000000..1bc316090 --- /dev/null +++ b/2a/d3b7bd588938e8ae84572909c54ad55ee0bdc8 @@ -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 arlo.cworth.org (Postfix) with ESMTP id A29F06DE02DD + for ; Sun, 12 Jun 2016 18:06:16 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.011 +X-Spam-Level: +X-Spam-Status: No, score=-0.011 tagged_above=-999 required=5 + tests=[AWL=-0.000, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01] + autolearn=disabled +Received: from arlo.cworth.org ([127.0.0.1]) + by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id K2hK4z9PD-YG for ; + Sun, 12 Jun 2016 18:06:08 -0700 (PDT) +Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) + by arlo.cworth.org (Postfix) with ESMTPS id C6D326DE0173 + for ; Sun, 12 Jun 2016 18:06:08 -0700 (PDT) +Received: from remotemail by fethera.tethera.net with local (Exim 4.84) + (envelope-from ) + id 1bCGKA-0003wm-9T; Sun, 12 Jun 2016 21:05:54 -0400 +Received: (nullmailer pid 5678 invoked by uid 1000); + Mon, 13 Jun 2016 01:06:04 -0000 +From: David Bremner +To: notmuch@notmuchmail.org +Subject: [PATCH 6/8] CLI: refactor dumping of tags. +Date: Sun, 12 Jun 2016 22:05:53 -0300 +Message-Id: <1465779955-5539-7-git-send-email-david@tethera.net> +X-Mailer: git-send-email 2.8.1 +In-Reply-To: <1465779955-5539-1-git-send-email-david@tethera.net> +References: <1465779955-5539-1-git-send-email-david@tethera.net> +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.20 +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: Mon, 13 Jun 2016 01:06:16 -0000 + +This is mainly code movement, to make room in the loop over messages for +dumping properties. +--- + notmuch-dump.c | 127 +++++++++++++++++++++++++++++++-------------------------- + 1 file changed, 69 insertions(+), 58 deletions(-) + +diff --git a/notmuch-dump.c b/notmuch-dump.c +index cae1db8..d80ed8b8 100644 +--- a/notmuch-dump.c ++++ b/notmuch-dump.c +@@ -78,13 +78,78 @@ print_dump_header (gzFile output, int output_format, int include) + } + + static int ++dump_tags_message (void *ctx, ++ notmuch_message_t *message, int output_format, ++ gzFile output, ++ char **buffer_p, size_t *size_p) ++{ ++ int first = 1; ++ const char *message_id; ++ ++ message_id = notmuch_message_get_message_id (message); ++ ++ if (output_format == DUMP_FORMAT_BATCH_TAG && ++ strchr (message_id, '\n')) { ++ /* This will produce a line break in the output, which ++ * would be difficult to handle in tools. However, it's ++ * also impossible to produce an email containing a line ++ * break in a message ID because of unfolding, so we can ++ * safely disallow it. */ ++ fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id); ++ return EXIT_SUCCESS; ++ } ++ ++ if (output_format == DUMP_FORMAT_SUP) { ++ gzprintf (output, "%s (", message_id); ++ } ++ ++ for (notmuch_tags_t *tags = notmuch_message_get_tags (message); ++ notmuch_tags_valid (tags); ++ notmuch_tags_move_to_next (tags)) { ++ const char *tag_str = notmuch_tags_get (tags); ++ ++ if (! first) ++ gzputs (output, " "); ++ ++ first = 0; ++ ++ if (output_format == DUMP_FORMAT_SUP) { ++ gzputs (output, tag_str); ++ } else { ++ if (hex_encode (ctx, tag_str, ++ buffer_p, size_p) != HEX_SUCCESS) { ++ fprintf (stderr, "Error: failed to hex-encode tag %s\n", ++ tag_str); ++ return EXIT_FAILURE; ++ } ++ gzprintf (output, "+%s", *buffer_p); ++ } ++ } ++ ++ if (output_format == DUMP_FORMAT_SUP) { ++ gzputs (output, ")\n"); ++ } else { ++ if (make_boolean_term (ctx, "id", message_id, ++ buffer_p, size_p)) { ++ fprintf (stderr, "Error quoting message id %s: %s\n", ++ message_id, strerror (errno)); ++ return EXIT_FAILURE; ++ } ++ gzprintf (output, " -- %s\n", *buffer_p); ++ } ++ return EXIT_SUCCESS; ++} ++ ++static int + database_dump_file (notmuch_database_t *notmuch, gzFile output, + const char *query_str, int output_format, int include) + { + notmuch_query_t *query; + notmuch_messages_t *messages; + notmuch_message_t *message; +- notmuch_tags_t *tags; ++ notmuch_status_t status; ++ char *buffer = NULL; ++ size_t buffer_size = 0; + + print_dump_header (output, output_format, include); + +@@ -110,10 +175,6 @@ database_dump_file (notmuch_database_t *notmuch, gzFile output, + */ + notmuch_query_set_sort (query, NOTMUCH_SORT_UNSORTED); + +- char *buffer = NULL; +- size_t buffer_size = 0; +- notmuch_status_t status; +- + status = notmuch_query_search_messages_st (query, &messages); + if (print_status_query ("notmuch dump", query, status)) + return EXIT_FAILURE; +@@ -121,62 +182,12 @@ database_dump_file (notmuch_database_t *notmuch, gzFile output, + for (; + notmuch_messages_valid (messages); + notmuch_messages_move_to_next (messages)) { +- int first = 1; +- const char *message_id; + + message = notmuch_messages_get (messages); +- message_id = notmuch_message_get_message_id (message); +- +- if (output_format == DUMP_FORMAT_BATCH_TAG && +- strchr (message_id, '\n')) { +- /* This will produce a line break in the output, which +- * would be difficult to handle in tools. However, it's +- * also impossible to produce an email containing a line +- * break in a message ID because of unfolding, so we can +- * safely disallow it. */ +- fprintf (stderr, "Warning: skipping message id containing line break: \"%s\"\n", message_id); +- notmuch_message_destroy (message); +- continue; +- } + +- if (output_format == DUMP_FORMAT_SUP) { +- gzprintf (output, "%s (", message_id); +- } +- +- for (tags = notmuch_message_get_tags (message); +- notmuch_tags_valid (tags); +- notmuch_tags_move_to_next (tags)) { +- const char *tag_str = notmuch_tags_get (tags); +- +- if (! first) +- gzputs (output, " "); +- +- first = 0; +- +- if (output_format == DUMP_FORMAT_SUP) { +- gzputs (output, tag_str); +- } else { +- if (hex_encode (notmuch, tag_str, +- &buffer, &buffer_size) != HEX_SUCCESS) { +- fprintf (stderr, "Error: failed to hex-encode tag %s\n", +- tag_str); +- return EXIT_FAILURE; +- } +- gzprintf (output, "+%s", buffer); +- } +- } +- +- if (output_format == DUMP_FORMAT_SUP) { +- gzputs (output, ")\n"); +- } else { +- if (make_boolean_term (notmuch, "id", message_id, +- &buffer, &buffer_size)) { +- fprintf (stderr, "Error quoting message id %s: %s\n", +- message_id, strerror (errno)); +- return EXIT_FAILURE; +- } +- gzprintf (output, " -- %s\n", buffer); +- } ++ if (dump_tags_message (notmuch, message, output_format, output, ++ &buffer, &buffer_size)) ++ return EXIT_FAILURE; + + notmuch_message_destroy (message); + } +-- +2.8.1 +