From: Austin Clements Date: Mon, 25 Aug 2014 17:26:08 +0000 (+2000) Subject: [PATCH v4 10/11] lib: Return an error from operations that require an upgrade X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=fe11b0f913e2ab376dcc29288a21c9e2d5b902b1;p=notmuch-archives.git [PATCH v4 10/11] lib: Return an error from operations that require an upgrade --- diff --git a/7b/38101f55403e5f72f48e7eeb338561d5f04a17 b/7b/38101f55403e5f72f48e7eeb338561d5f04a17 new file mode 100644 index 000000000..2dfab3453 --- /dev/null +++ b/7b/38101f55403e5f72f48e7eeb338561d5f04a17 @@ -0,0 +1,220 @@ +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 C131F431FAF + for ; Mon, 25 Aug 2014 10:26:42 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -2.3 +X-Spam-Level: +X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_MED=-2.3] 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 FKz+dq5eoNK1 for ; + Mon, 25 Aug 2014 10:26:35 -0700 (PDT) +Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu + [18.9.25.12]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id AE854431FC0 + for ; Mon, 25 Aug 2014 10:26:22 -0700 (PDT) +X-AuditID: 1209190c-f795e6d000006c66-d5-53fb71be0f5e +Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) + (using TLS with cipher AES256-SHA (256/256 bits)) + (Client did not present a certificate) + by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP + id 51.07.27750.EB17BF35; Mon, 25 Aug 2014 13:26:22 -0400 (EDT) +Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) + by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s7PHQJ6D014430; + Mon, 25 Aug 2014 13:26:19 -0400 +Received: from drake.dyndns.org (31-35-14.wireless.csail.mit.edu + [128.31.35.14]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s7PHQC0J029623 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Mon, 25 Aug 2014 13:26:18 -0400 +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1XLy20-0003jk-28; Mon, 25 Aug 2014 13:26:12 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v4 10/11] lib: Return an error from operations that require an + upgrade +Date: Mon, 25 Aug 2014 13:26:08 -0400 +Message-Id: <1408987569-14146-11-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 2.0.0 +In-Reply-To: <1408987569-14146-1-git-send-email-amdragon@mit.edu> +References: <1408987569-14146-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixG6noruv8HewwfSL8hY3WrsZLZqmO1tc + vzmT2YHZ49b91+wez1bdYvbYcug9cwBzFJdNSmpOZllqkb5dAlfGi7+b2QtuaFe8+/uRtYGx + WaWLkZNDQsBEYmHrC3YIW0ziwr31bF2MXBxCArOZJF5M28YEkhAS2Mgo8aSJHyJxjEni+sbN + 7BDOXEaJvvtXwdrZBDQkft9aDNYhIiAtsfPubFYQm1nAS2LZhX4wW1ggTOLG9SVsIDaLgKrE + 9pOXmEFsXgFHiYNz10CdISfRcOMTWA0nUHz6wtlQVzhIfL98nnkCI/8CRoZVjLIpuVW6uYmZ + OcWpybrFyYl5ealFuoZ6uZkleqkppZsYQcHFKcmzg/HNQaVDjAIcjEo8vDfifwcLsSaWFVfm + HmKU5GBSEuWdlg8U4kvKT6nMSCzOiC8qzUktPsQowcGsJMLbDJLjTUmsrEotyodJSXOwKInz + vrW2ChYSSE8sSc1OTS1ILYLJynBwKEnw5hYANQoWpaanVqRl5pQgpJk4OEGG8wANjwep4S0u + SMwtzkyHyJ9iVJQS510LslUAJJFRmgfXC4v+V4ziQK8I8x4FaecBJg647ldAg5mABpv2/AQZ + XJKIkJJqYNwWIPrjrd5aLSv+5roKv0ZX5atpSiICYZN2yUQYrGSV3abxjnV5AQszW2b6grrT + abWbg1a1Xt0z9dcmxlAGgw/dcTUOqzedinJpd+6qiar2zmXyDwr+cdrvlW7UTe1CY7N/Haum + K55vzVtgPV1k/S4fxmMJJ9c8vDHjz9w/RvvOB585NuucvRJLcUaioRZzUXEiAIblWNnZAgAA +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, 25 Aug 2014 17:26:43 -0000 + +Previously, there was no protection against a caller invoking an +operation on an old database version that would effectively corrupt +the database by treating it like a newer version. + +According to notmuch.h, any caller that opens the database in +read/write mode is supposed to check if the database needs upgrading +and perform an upgrade if it does. This would protect against this, +but nobody (even the CLI) actually does this. + +However, with features, it's easy to protect against incompatible +operations on a fine-grained basis. This lightweight change allows +callers to safely operate on old database versions, while preventing +specific operations that would corrupt the database with an +informative error message. +--- + lib/database.cc | 5 +++++ + lib/directory.cc | 5 +++++ + lib/message.cc | 8 ++++++++ + lib/notmuch.h | 16 ++++++++++++++++ + 4 files changed, 34 insertions(+) + +diff --git a/lib/database.cc b/lib/database.cc +index 53397bb..5116188 100644 +--- a/lib/database.cc ++++ b/lib/database.cc +@@ -316,6 +316,8 @@ notmuch_status_to_string (notmuch_status_t status) + return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic"; + case NOTMUCH_STATUS_UNSUPPORTED_OPERATION: + return "Unsupported operation"; ++ case NOTMUCH_STATUS_UPGRADE_REQUIRED: ++ return "Operation requires a database upgrade"; + default: + case NOTMUCH_STATUS_LAST_STATUS: + return "Unknown error status value"; +@@ -2226,6 +2228,9 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, + if (message_ret == NULL) + return NOTMUCH_STATUS_NULL_POINTER; + ++ if (! (notmuch->features & NOTMUCH_FEATURE_FILE_TERMS)) ++ return NOTMUCH_STATUS_UPGRADE_REQUIRED; ++ + /* return NULL on any failure */ + *message_ret = NULL; + +diff --git a/lib/directory.cc b/lib/directory.cc +index 6a3ffed..8daaec8 100644 +--- a/lib/directory.cc ++++ b/lib/directory.cc +@@ -105,6 +105,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch, + const char *db_path; + notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE); + ++ if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) { ++ *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED; ++ return NULL; ++ } ++ + *status_ret = NOTMUCH_STATUS_SUCCESS; + + path = _notmuch_database_relative_path (notmuch, path); +diff --git a/lib/message.cc b/lib/message.cc +index ed8c59e..68f7e68 100644 +--- a/lib/message.cc ++++ b/lib/message.cc +@@ -655,6 +655,10 @@ _notmuch_message_add_filename (notmuch_message_t *message, + if (filename == NULL) + INTERNAL_ERROR ("Message filename cannot be NULL."); + ++ if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) || ++ ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER)) ++ return NOTMUCH_STATUS_UPGRADE_REQUIRED; ++ + relative = _notmuch_database_relative_path (message->notmuch, filename); + + status = _notmuch_database_split_path (local, relative, &directory, NULL); +@@ -699,6 +703,10 @@ _notmuch_message_remove_filename (notmuch_message_t *message, + notmuch_private_status_t private_status; + notmuch_status_t status; + ++ if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) || ++ ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER)) ++ return NOTMUCH_STATUS_UPGRADE_REQUIRED; ++ + status = _notmuch_database_filename_to_direntry ( + local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry); + if (status || !direntry) +diff --git a/lib/notmuch.h b/lib/notmuch.h +index 3c5ec98..cbf2ba5 100644 +--- a/lib/notmuch.h ++++ b/lib/notmuch.h +@@ -160,6 +160,10 @@ typedef enum _notmuch_status { + */ + NOTMUCH_STATUS_UNSUPPORTED_OPERATION, + /** ++ * The operation requires a database upgrade. ++ */ ++ NOTMUCH_STATUS_UPGRADE_REQUIRED, ++ /** + * Not an actual status value. Just a way to find out how many + * valid status values there are. + */ +@@ -438,6 +442,9 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch); + * + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; + * directory not retrieved. ++ * ++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the ++ * database to use this function. + */ + notmuch_status_t + notmuch_database_get_directory (notmuch_database_t *database, +@@ -490,6 +497,9 @@ notmuch_database_get_directory (notmuch_database_t *database, + * + * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only + * mode so no message can be added. ++ * ++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the ++ * database to use this function. + */ + notmuch_status_t + notmuch_database_add_message (notmuch_database_t *database, +@@ -520,6 +530,9 @@ notmuch_database_add_message (notmuch_database_t *database, + * + * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only + * mode so no message can be removed. ++ * ++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the ++ * database to use this function. + */ + notmuch_status_t + notmuch_database_remove_message (notmuch_database_t *database, +@@ -575,6 +588,9 @@ notmuch_database_find_message (notmuch_database_t *database, + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object + * + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred ++ * ++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the ++ * database to use this function. + */ + notmuch_status_t + notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, +-- +2.0.0 +