From 802aaf99dbf2110c802a0d446c36dc6198564c86 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Sun, 28 Sep 2014 17:40:58 +0300 Subject: [PATCH] [PATCH v2 2/3] cli/insert: require succesful message indexing for success status --- be/1376a8854f6883386036b7866c6aefc324d072 | 195 ++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 be/1376a8854f6883386036b7866c6aefc324d072 diff --git a/be/1376a8854f6883386036b7866c6aefc324d072 b/be/1376a8854f6883386036b7866c6aefc324d072 new file mode 100644 index 000000000..56b658f59 --- /dev/null +++ b/be/1376a8854f6883386036b7866c6aefc324d072 @@ -0,0 +1,195 @@ +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 362BD431FC2 + for ; Sun, 28 Sep 2014 07:41:26 -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 5DJXtGY08lpG for ; + Sun, 28 Sep 2014 07:41:22 -0700 (PDT) +Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com + [74.125.82.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client + certificate requested) by olra.theworths.org (Postfix) with ESMTPS id + 09285431FC9 for ; Sun, 28 Sep 2014 07:41:14 -0700 + (PDT) +Received: by mail-wg0-f51.google.com with SMTP id b13so3106608wgh.34 + for ; Sun, 28 Sep 2014 07:41:13 -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=3EV3jE6RmW2Q945sUGQHm0+z6FhInuvENA4SdquFU0c=; + b=k3cGsDY9Fkv2AFOo1WIBqJ8TnApsBOwzZwSQm8D1IFn4DVbwrGm5imSatENJSLvkPG + CuHoJp9Xwa5ohDGxBs8UgR4m0WBdaawJVeZfhcaLW3qREx5LJIXp+yK9pvft0MGkFsq5 + MB3fOfJ8Ag6iqYSw0Kd0rIkS+efac6hS3NFjActwIhG/vtc8/Ass5tJfOrwyXNEk4hie + LhLNTB7+uB3UdHlUmu6TqviFfJcvqQUqJfXffrr8dCE9/vkh0vfb4NbXfOschqsfpjJG + YrvCupNZCL8s5p4SDajewL048mSzRfQw3GNRFsfvYmTJhXGG1PuP4QOZe3PO9QY1rA/Q + mvhg== +X-Gm-Message-State: + ALoCoQlS2mAazokYLwcHIEGqBY4aNcHnTav0Erfr9wPzNqhd1p9idzQaHle2DKhieY3bYBg0mLka +X-Received: by 10.180.21.199 with SMTP id x7mr23392746wie.73.1411915273736; + Sun, 28 Sep 2014 07:41:13 -0700 (PDT) +Received: from localhost (dsl-hkibrasgw2-58c36c-245.dhcp.inet.fi. + [88.195.108.245]) by mx.google.com with ESMTPSA id + lg6sm12673477wjb.40.2014.09.28.07.41.12 for + (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); + Sun, 28 Sep 2014 07:41:13 -0700 (PDT) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH v2 2/3] cli/insert: require succesful message indexing for + success status +Date: Sun, 28 Sep 2014 17:40:58 +0300 +Message-Id: + <627e789a0574d93b66b3e935f36328661df4e99d.1411914914.git.jani@nikula.org> +X-Mailer: git-send-email 2.1.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: Sun, 28 Sep 2014 14:41:26 -0000 + +Add --keep option to keep any remaining stuff in index or file. We +could distinguish between failures to index and failures to apply tags +or maildir sync, but for simplicity just have one. +--- + doc/man1/notmuch-insert.rst | 19 ++++++++++++------- + notmuch-insert.c | 36 ++++++++++++++++++++++++++++++------ + test/T070-insert.sh | 2 +- + 3 files changed, 43 insertions(+), 14 deletions(-) + +diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst +index 2be1a7b8841c..e396f6cf2279 100644 +--- a/doc/man1/notmuch-insert.rst ++++ b/doc/man1/notmuch-insert.rst +@@ -38,16 +38,21 @@ Supported options for **insert** include + does not exist. Otherwise the folder must already exist for mail + delivery to succeed. + ++ ``--keep`` ++ Keep the message file if indexing fails, and keep the message ++ indexed if applying tags or maildir flag synchronization ++ fails. Ignore these errors and return exit status 0 to ++ indicate succesful mail delivery. ++ + EXIT STATUS + =========== + +-This command returns exit status 0 if the message was successfully added +-to the mail directory, even if the message could not be indexed and +-added to the notmuch database. In the latter case, a warning will be +-printed to standard error but the message file will be left on disk. +- +-If the message could not be written to disk then a non-zero exit status +-is returned. ++This command returns exit status 0 on succesful mail delivery, ++non-zero otherwise. The default is to indicate failed mail delivery on ++any errors, including message file delivery to the filesystem, message ++indexing to Notmuch database, changing tags, and synchronizing tags to ++maildir flags. The ``--keep`` option may be used to settle for ++successful message file delivery. + + SEE ALSO + ======== +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 0ea438015dbe..7074077ca699 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -443,6 +443,7 @@ add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops, + int + notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + { ++ notmuch_status_t status, close_status; + notmuch_database_t *notmuch; + struct sigaction action; + const char *db_path; +@@ -452,6 +453,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + char *query_string = NULL; + const char *folder = NULL; + notmuch_bool_t create_folder = FALSE; ++ notmuch_bool_t keep = FALSE; + notmuch_bool_t synchronize_flags; + const char *maildir; + char *newpath; +@@ -461,6 +463,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 }, ++ { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 }, + { NOTMUCH_OPT_END, 0, 0, 0, 0 } + }; + +@@ -535,11 +538,32 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + return EXIT_FAILURE; + } + +- /* Add the message to the index. +- * Even if adding the message to the notmuch database fails, +- * the message is on disk and we consider the delivery completed. */ +- add_file (notmuch, newpath, tag_ops, synchronize_flags, TRUE); ++ /* Index the message. */ ++ status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep); ++ ++ /* Commit changes. */ ++ close_status = notmuch_database_destroy (notmuch); ++ if (close_status) { ++ /* Hold on to the first error, if any. */ ++ if (! status) ++ status = close_status; ++ fprintf (stderr, "%s: failed to commit database changes: %s\n", ++ keep ? "Warning" : "Error", ++ notmuch_status_to_string (close_status)); ++ } ++ ++ if (status) { ++ if (keep) { ++ status = NOTMUCH_STATUS_SUCCESS; ++ } else { ++ /* If maildir flag sync failed, this might fail. */ ++ if (unlink (newpath)) { ++ fprintf (stderr, "Warning: failed to remove '%s' from maildir " ++ "after errors: %s. Please run 'notmuch new' to fix.\n", ++ newpath, strerror (errno)); ++ } ++ } ++ } + +- notmuch_database_destroy (notmuch); +- return EXIT_SUCCESS; ++ return status ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/test/T070-insert.sh b/test/T070-insert.sh +index ea9db07e2fa2..aacc643b7288 100755 +--- a/test/T070-insert.sh ++++ b/test/T070-insert.sh +@@ -23,7 +23,7 @@ test_expect_code 1 "Insert zero-length file" \ + + # This test is a proxy for other errors that may occur while trying to + # add a message to the notmuch database, e.g. database locked. +-test_expect_code 0 "Insert non-message" \ ++test_expect_code 1 "Insert non-message" \ + "echo bad_message | notmuch insert" + + test_begin_subtest "Database empty so far" +-- +2.1.0 + -- 2.26.2