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 99376431FDF for ; Mon, 22 Sep 2014 02:55:31 -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 gjpDth77LbDT for ; Mon, 22 Sep 2014 02:55:26 -0700 (PDT) Received: from mail-we0-f176.google.com (mail-we0-f176.google.com [74.125.82.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 29FE8429E26 for ; Mon, 22 Sep 2014 02:55:01 -0700 (PDT) Received: by mail-we0-f176.google.com with SMTP id w61so1742241wes.35 for ; Mon, 22 Sep 2014 02:55: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:in-reply-to:references; bh=acttXzxAi/1AvUHUOanoOBLDkWKGmWhG+eJ4FyJWBLA=; b=VTYDxlShsDt13c98hrs2KepWQ53iakb8ZM1UFrIp98j5TsjxfPXmaJYxRwj3IXxyHZ AfyOW6+xWBDOSGaF2WUcHltI6rILfYp7w+AB6UjOTnVHEL+uCOZZVZ0ByXsyRc7+KLJn fuVC+0ps7ODO/w+1aT/BMjbhSJTrw5I+v2IuU9fE4WI3EmeB83XbiYG8Ngii7o60rYWW JuXkx6KbvvC1ct9W//5OKwoylCzTACo0qm8RCKcIosUzmuOisX+6WqVrWtZahCwpbqOu OmODV75eEwRNjaI7INSxjoJyImaMDkfTKNBKJ0ChFUnWK0IXHVuscGmESSNoD5CSibzR sGXA== X-Gm-Message-State: ALoCoQkV5RumLQhCMr3ssUqgLypC5S24+XAclXSaUdm7nTJkeqzkBcBww0E4U/9XTU8jrRsD4GAo X-Received: by 10.194.157.135 with SMTP id wm7mr1485658wjb.117.1411379699962; Mon, 22 Sep 2014 02:54:59 -0700 (PDT) Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3]) by mx.google.com with ESMTPSA id ek8sm9670807wib.23.2014.09.22.02.54.59 for (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Mon, 22 Sep 2014 02:54:59 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH 09/11] cli/insert: add fail path to add_file_to_database Date: Mon, 22 Sep 2014 11:55:00 +0200 Message-Id: <5089ce31afa0ba764520270af1e680a4ab50b4e6.1411379395.git.jani@nikula.org> X-Mailer: git-send-email 1.7.2.5 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: Mon, 22 Sep 2014 09:55:32 -0000 Handle failures gracefully in add_file_to_database, renamed simply add_file while at it. Add keep option to not remove the message from database if tagging or tag syncing to maildir flags fails. Expand the function documentation to cover the changes. --- notmuch-insert.c | 89 ++++++++++++++++++++++++++++++++--------------------- 1 files changed, 54 insertions(+), 35 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 5ef6e66..80f52d4 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -364,50 +364,70 @@ FAIL: return NULL; } -/* Add the specified message file to the notmuch database, applying tags. - * The file is renamed to encode notmuch tags as maildir flags. */ -static void -add_file_to_database (notmuch_database_t *notmuch, const char *path, - tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags) +/* + * Add the specified message file to the notmuch database, applying + * tags in tag_ops. If synchronize_flags is TRUE, the tags are + * synchronized to maildir flags (which may result in message file + * rename). + * + * Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep + * is TRUE, errors in tag changes and flag syncing are ignored and + * success status is returned; otherwise such errors cause the message + * to be removed from the database. Failure to add the message to the + * database results in error status regardless of keep. + */ +static notmuch_status_t +add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops, + notmuch_bool_t synchronize_flags, notmuch_bool_t keep) { notmuch_message_t *message; notmuch_status_t status; status = notmuch_database_add_message (notmuch, path, &message); - switch (status) { - case NOTMUCH_STATUS_SUCCESS: - case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: - break; - default: - case NOTMUCH_STATUS_FILE_NOT_EMAIL: - case NOTMUCH_STATUS_READ_ONLY_DATABASE: - case NOTMUCH_STATUS_XAPIAN_EXCEPTION: - case NOTMUCH_STATUS_OUT_OF_MEMORY: - case NOTMUCH_STATUS_FILE_ERROR: - case NOTMUCH_STATUS_NULL_POINTER: - case NOTMUCH_STATUS_TAG_TOO_LONG: - case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: - case NOTMUCH_STATUS_UNBALANCED_ATOMIC: - case NOTMUCH_STATUS_LAST_STATUS: - fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n", - path, notmuch_status_to_string (status)); - return; - } - - if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { - /* Don't change tags of an existing message. */ - if (synchronize_flags) { - status = notmuch_message_tags_to_maildir_flags (message); - if (status != NOTMUCH_STATUS_SUCCESS) - fprintf (stderr, "Error: failed to sync tags to maildir flags\n"); + if (status == NOTMUCH_STATUS_SUCCESS) { + status = tag_op_list_apply (message, tag_ops, 0); + if (status) { + fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n", + keep ? "Warning" : "Error", + path, notmuch_status_to_string (status)); + goto DONE; } + } else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { + status = NOTMUCH_STATUS_SUCCESS; + } else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) { + fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path); + return status; } else { - tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0; + fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n", + path, notmuch_status_to_string (status)); + return status; + } - tag_op_list_apply (message, tag_ops, flags); + if (synchronize_flags) { + status = notmuch_message_tags_to_maildir_flags (message); + if (status != NOTMUCH_STATUS_SUCCESS) + fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n", + keep ? "Warning" : "Error", + path, notmuch_status_to_string (status)); + + /* + * Note: Unfortunately a failed maildir flag sync might + * already have renamed the file, in which case the cleanup + * path will fail. + */ } +DONE: notmuch_message_destroy (message); + + if (status) { + if (keep) + status = NOTMUCH_STATUS_SUCCESS; + else + notmuch_database_remove_message (notmuch, path); + } + + return status; } int @@ -508,8 +528,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) /* 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_to_database (notmuch, newpath, tag_ops, - synchronize_flags); + add_file (notmuch, newpath, tag_ops, synchronize_flags, TRUE); notmuch_database_destroy (notmuch); return EXIT_SUCCESS; -- 1.7.2.5