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 DAF12431FC2 for ; Sat, 20 Jul 2013 17:08:03 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, 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 nwV+ObB15cWw for ; Sat, 20 Jul 2013 17:07:56 -0700 (PDT) Received: from mail-pb0-f51.google.com (mail-pb0-f51.google.com [209.85.160.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 27C48431FB6 for ; Sat, 20 Jul 2013 17:07:56 -0700 (PDT) Received: by mail-pb0-f51.google.com with SMTP id um15so5763157pbc.10 for ; Sat, 20 Jul 2013 17:07:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=xy4St4yA/Cfj6QR8U0e1DLl54f8kW6J3/5v0YQNjyT4=; b=KJ1WZ5UwwlQatlIrSjIuwlLBkFLanp5S6VWJBSnKidaZgNEXVsxD4LR+O3399QzCgw qUInfNNc4ZO2bQxdjH0PP0H2ridGypYkoR7U/rt84BAW+ZB0UT5bFtRQeckjJB7eRyri 91mh4VZZ02XWhXeJesPDQqhFmgRufrcfh1cdEPkNBtwOrdkr5rex3bOX43CSgoC8kR04 kFxL28gcN9Z13dRBFaF7Ztvqeen7uII1pB9FRkHz5NV1pl7p9AYw/YFGpE9FwA8FtmyQ NVxak5CnI6Mzm3zx1/wBqPt7vwPe7fHmh0MW18kRo8f6xzzTJHizXLNOA9hUdQfFOSNL itPA== X-Received: by 10.66.240.2 with SMTP id vw2mr25577980pac.137.1374365274354; Sat, 20 Jul 2013 17:07:54 -0700 (PDT) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPSA id ht5sm27318443pbb.29.2013.07.20.17.07.50 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 20 Jul 2013 17:07:53 -0700 (PDT) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH 1/3] cli: add insert --must-index option Date: Sun, 21 Jul 2013 10:07:32 +1000 Message-Id: <1374365254-13227-1-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 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, 21 Jul 2013 00:08:04 -0000 This option causes notmuch insert to fail as a whole if the message failed to be added to the notmuch database. The new message file will be deleted from disk, and a distinct status code (2) returned. --- notmuch-insert.c | 76 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 2207b1e..505b647 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -28,6 +28,10 @@ #include #include +#define INSERT_EXIT_SUCCESS 0 +#define INSERT_EXIT_FAIL_WRITE 1 +#define INSERT_EXIT_FAIL_INDEX 2 + static volatile sig_atomic_t interrupted; static void @@ -293,12 +297,13 @@ copy_stdin (int fdin, int fdout) /* Add the specified message file to the notmuch database, applying tags. * The file is renamed to encode notmuch tags as maildir flags. */ -static void +static notmuch_status_t add_file_to_database (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops) { notmuch_message_t *message; notmuch_status_t status; + notmuch_status_t sync; status = notmuch_database_add_message (notmuch, path, &message); switch (status) { @@ -318,47 +323,52 @@ add_file_to_database (notmuch_database_t *notmuch, const char *path, case NOTMUCH_STATUS_LAST_STATUS: fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n", path, notmuch_status_to_string (status)); - return; + return status; } if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { /* Don't change tags of an existing message. */ - status = notmuch_message_tags_to_maildir_flags (message); - if (status != NOTMUCH_STATUS_SUCCESS) + sync = notmuch_message_tags_to_maildir_flags (message); + if (sync != NOTMUCH_STATUS_SUCCESS) fprintf (stderr, "Error: failed to sync tags to maildir flags\n"); } else { tag_op_list_apply (message, tag_ops, TAG_FLAG_MAILDIR_SYNC); } notmuch_message_destroy (message); + + return status; } -static notmuch_bool_t +static int insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, - const char *dir, tag_op_list_t *tag_ops) + const char *dir, tag_op_list_t *tag_ops, + notmuch_bool_t must_index) { char *tmppath; char *newpath; char *newdir; int fdout; - char *cleanup_path; + notmuch_status_t status; fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir); if (fdout < 0) - return FALSE; + return INSERT_EXIT_FAIL_WRITE; - cleanup_path = tmppath; - - if (! copy_stdin (fdin, fdout)) - goto FAIL; + if (! copy_stdin (fdin, fdout)) { + close (fdout); + unlink (tmppath); + return INSERT_EXIT_FAIL_WRITE; + } if (fsync (fdout) != 0) { fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno)); - goto FAIL; + close (fdout); + unlink (tmppath); + return INSERT_EXIT_FAIL_WRITE; } close (fdout); - fdout = -1; /* Atomically move the new message file from the Maildir 'tmp' directory * to the 'new' directory. We follow the Dovecot recommendation to @@ -367,25 +377,28 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, */ if (rename (tmppath, newpath) != 0) { fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno)); - goto FAIL; + unlink (tmppath); + return INSERT_EXIT_FAIL_WRITE; } - cleanup_path = newpath; - - if (! sync_dir (newdir)) - goto FAIL; + if (! sync_dir (newdir)) { + unlink (newpath); + return INSERT_EXIT_FAIL_WRITE; + } - /* 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); + status = add_file_to_database (notmuch, newpath, tag_ops); - return TRUE; + /* If must_index is TRUE, then indexing must succeed. Otherwise, we + * consider the delivery completed as long as the message is on disk. */ + if (must_index && + status != NOTMUCH_STATUS_SUCCESS && + status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) + { + unlink (newpath); + return INSERT_EXIT_FAIL_INDEX; + } - FAIL: - if (fdout >= 0) - close (fdout); - unlink (cleanup_path); - return FALSE; + return INSERT_EXIT_SUCCESS; } int @@ -400,6 +413,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 must_index = FALSE; const char *maildir; int opt_index; unsigned int i; @@ -408,6 +422,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, &must_index, "must-index", 0, 0 }, { NOTMUCH_OPT_END, 0, 0, 0, 0 } }; @@ -471,9 +486,10 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) return 1; - ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops); + ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops, + must_index); notmuch_database_destroy (notmuch); - return (ret) ? 0 : 1; + return ret; } -- 1.7.12.1