From c4fd35411fe274a6ca7b0b30b03f313512eadb84 Mon Sep 17 00:00:00 2001 From: Maarten Aertsen Date: Sun, 7 Feb 2016 22:13:40 +0100 Subject: [PATCH] [PATCH] cli: avoid non-zero exits in notmuch insert --keep --- fb/cbe03fe02181fb6df65d8a46d7b90b475fdbc7 | 197 ++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 fb/cbe03fe02181fb6df65d8a46d7b90b475fdbc7 diff --git a/fb/cbe03fe02181fb6df65d8a46d7b90b475fdbc7 b/fb/cbe03fe02181fb6df65d8a46d7b90b475fdbc7 new file mode 100644 index 000000000..4988c3362 --- /dev/null +++ b/fb/cbe03fe02181fb6df65d8a46d7b90b475fdbc7 @@ -0,0 +1,197 @@ +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 E36236DE0C4A + for ; Sun, 7 Feb 2016 13:14:02 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at cworth.org +X-Spam-Flag: NO +X-Spam-Score: -0.303 +X-Spam-Level: +X-Spam-Status: No, score=-0.303 tagged_above=-999 required=5 tests=[AWL=0.249, + RP_MATCHES_RCVD=-0.55, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001] + 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 cO64LdXF3QyV for ; + Sun, 7 Feb 2016 13:14:00 -0800 (PST) +Received: from kardo2.rtsn.nl (rtsn.nl [37.97.129.244]) + by arlo.cworth.org (Postfix) with ESMTPS id 76F016DE0B27 + for ; Sun, 7 Feb 2016 13:14:00 -0800 (PST) +Received: by kardo2.rtsn.nl (Postfix, from userid 1000) + id 02EBA1FF571; Sun, 7 Feb 2016 22:13:40 +0100 (CET) +Content-Type: text/plain; charset="utf-8" +MIME-Version: 1.0 +Content-Transfer-Encoding: quoted-printable +From: Maarten Aertsen +User-Agent: alot/0.3.6 +To: "Notmuch Mail" +References: <20160124154800.19103.24730@kardo2.rtsn.nl> + <87a8nsz8bd.fsf@zancas.localnet> +In-Reply-To: <87a8nsz8bd.fsf@zancas.localnet> +Message-ID: <20160207211340.15609.92485@kardo2.rtsn.nl> +Subject: [PATCH] cli: avoid non-zero exits in notmuch insert --keep +Date: Sun, 07 Feb 2016 22:13:40 +0100 +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: Sun, 07 Feb 2016 21:14:03 -0000 + +Re-ordered code that touches the database to try and deliver e-mail to +at least try to deliver to the Maildir (which, with --keep should return +success). + +In the case of any failure, we now return EX_TEMPFAIL (a sendmail +convention, defined in sysexits.h) to signal to the LDA that it should +retry later. This prevents a direct reject or bounce of e-mail. +--- + notmuch-client.h | 1 + + notmuch-insert.c | 42 +++++++++++++++++++++++------------------- + notmuch.c | 17 +++++++++++++---- + 3 files changed, 37 insertions(+), 23 deletions(-) + +diff --git a/notmuch-client.h b/notmuch-client.h +index 18e6c60..e3d6a46 100644 +--- a/notmuch-client.h ++++ b/notmuch-client.h +@@ -466,6 +466,7 @@ print_status_query (const char *loc, + = + + extern char *notmuch_requested_db_uuid; + extern const notmuch_opt_desc_t notmuch_shared_options []; ++notmuch_bool_t notmuch_has_unmatched_db_uuid (notmuch_database_t *notmuch); + void notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch); + = + + void notmuch_process_shared_options (const char* subcommand_name); +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 5205c17..35b6779 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -28,6 +28,8 @@ + #include + #include + = + ++#include ++ + static volatile sig_atomic_t interrupted; + = + + static void +@@ -532,31 +534,33 @@ notmuch_insert_command (notmuch_config_t *config, int= + argc, char *argv[]) + action.sa_flags =3D 0; + sigaction (SIGINT, &action, NULL); + = + +- if (notmuch_database_open (notmuch_config_get_database_path (config), +- NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) +- return EXIT_FAILURE; +- +- notmuch_exit_if_unmatched_db_uuid (notmuch); +- + /* Write the message to the Maildir new directory. */ + newpath =3D maildir_write_new (config, STDIN_FILENO, maildir); + if (! newpath) { +- notmuch_database_destroy (notmuch); + return EXIT_FAILURE; + } + = + +- /* Index the message. */ +- status =3D add_file (notmuch, newpath, tag_ops, synchronize_flags, kee= +p); +- +- /* Commit changes. */ +- close_status =3D notmuch_database_destroy (notmuch); +- if (close_status) { +- /* Hold on to the first error, if any. */ +- if (! status) +- status =3D close_status; +- fprintf (stderr, "%s: failed to commit database changes: %s\n", +- keep ? "Warning" : "Error", +- notmuch_status_to_string (close_status)); ++ status =3D notmuch_database_open (notmuch_config_get_database_path (co= +nfig), ++ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much); ++ if (! status) { ++ /* with keep, send EX_TEMPFAIL per sysexits.h to invite the caller to ++ * retry at some later point and avoid permanent failure */ ++ if (notmuch_has_unmatched_db_uuid(notmuch)) ++ exit (keep ? EX_TEMPFAIL : EXIT_FAILURE); ++ ++ /* Index the message. */ ++ status =3D add_file (notmuch, newpath, tag_ops, synchronize_flags,= + keep); ++ ++ /* Commit changes. */ ++ close_status =3D notmuch_database_destroy (notmuch); ++ if (close_status) { ++ /* Hold on to the first error, if any. */ ++ if (! status) ++ status =3D close_status; ++ fprintf (stderr, "%s: failed to commit database changes: %s\n", ++ keep ? "Warning" : "Error", ++ notmuch_status_to_string (close_status)); ++ } + } + = + + if (status) { +diff --git a/notmuch.c b/notmuch.c +index ce6c575..783bb2a 100644 +--- a/notmuch.c ++++ b/notmuch.c +@@ -220,20 +220,29 @@ be supported in the future.\n", notmuch_format_versio= +n); + } + } + = + +-void +-notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch) ++notmuch_bool_t ++notmuch_has_unmatched_db_uuid (notmuch_database_t *notmuch) + { + const char *uuid =3D NULL; + = + + if (!notmuch_requested_db_uuid) +- return; ++ return FALSE; + IGNORE_RESULT (notmuch_database_get_revision (notmuch, &uuid)); + = + + if (strcmp (notmuch_requested_db_uuid, uuid) !=3D 0){ + fprintf (stderr, "Error: requested database revision %s does not match %s= +\n", + notmuch_requested_db_uuid, uuid); +- exit (1); ++ return TRUE; + } ++ ++ return FALSE; ++} ++ ++void ++notmuch_exit_if_unmatched_db_uuid (notmuch_database_t *notmuch) ++{ ++ if (notmuch_has_unmatched_db_uuid(notmuch)) ++ exit (1); + } + = + + static void +-- = + +2.7.0 -- 2.26.2