[PATCH 13/14] lib: Return an error from operations that require an upgrade
authorAustin Clements <amdragon@MIT.EDU>
Sun, 27 Jul 2014 03:52:52 +0000 (23:52 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:03:53 +0000 (10:03 -0800)
f2/bd42da5550cc8bd5f59c0236310477b867393f [new file with mode: 0644]

diff --git a/f2/bd42da5550cc8bd5f59c0236310477b867393f b/f2/bd42da5550cc8bd5f59c0236310477b867393f
new file mode 100644 (file)
index 0000000..cbdf21d
--- /dev/null
@@ -0,0 +1,221 @@
+Return-Path: <amdragon@mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 10C0C431FD5\r
+       for <notmuch@notmuchmail.org>; Sat, 26 Jul 2014 20:53:13 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id CEJ4n+PJjDqO for <notmuch@notmuchmail.org>;\r
+       Sat, 26 Jul 2014 20:53:05 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu\r
+       [18.9.25.15])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 7454C431FB6\r
+       for <notmuch@notmuchmail.org>; Sat, 26 Jul 2014 20:53:05 -0700 (PDT)\r
+X-AuditID: 1209190f-f79f86d0000061c8-dd-53d4779fc9a2\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+       (using TLS with cipher AES256-SHA (256/256 bits))\r
+       (Client did not present a certificate)\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 6E.91.25032.F9774D35; Sat, 26 Jul 2014 23:53:03 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+       by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id s6R3r2BV004148; \r
+       Sat, 26 Jul 2014 23:53:02 -0400\r
+Received: from drake.dyndns.org\r
+       (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+       [216.15.114.40]) (authenticated bits=0)\r
+       (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+       by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s6R3r012016412\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Sat, 26 Jul 2014 23:53:01 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1XBFW8-00051h-HJ; Sat, 26 Jul 2014 23:53:00 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 13/14] lib: Return an error from operations that require an\r
+       upgrade\r
+Date: Sat, 26 Jul 2014 23:52:52 -0400\r
+Message-Id: <1406433173-19169-14-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 2.0.0\r
+In-Reply-To: <1406433173-19169-1-git-send-email-amdragon@mit.edu>\r
+References: <1406433173-19169-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsUixCmqrTu//EqwwfMFKhbXb85kdmD0eLbq\r
+       FnMAYxSXTUpqTmZZapG+XQJXxp7uXvaCG9oVZxpnszUwNqt0MXJwSAiYSCz4b9PFyAlkiklc\r
+       uLeerYuRi0NIYDaTxJ9rHxghnI2MEq8+rWWFcO4wSTz48o4ZwpnLKPH0wzk2kH42AQ2JbfuX\r
+       M4LYIgLSEjvvzmYFWcEsoCbxp0sFJCwsECwx++IfVhCbRUBV4sjbY0wgNq+Ao8TnmQdZIc6Q\r
+       k2i48QlsJCdQ/PizE2wgY4QEHCRuXUqcwMi/gJFhFaNsSm6Vbm5iZk5xarJucXJiXl5qka6J\r
+       Xm5miV5qSukmRnDISPLvYPx2UOkQowAHoxIPbwbblWAh1sSy4srcQ4ySHExKorz7zYBCfEn5\r
+       KZUZicUZ8UWlOanFhxglOJiVRHiFioFyvCmJlVWpRfkwKWkOFiVx3rfWVsFCAumJJanZqakF\r
+       qUUwWRkODiUJ3htlQI2CRanpqRVpmTklCGkmDk6Q4TxAwzeUggwvLkjMLc5Mh8ifYlSUEuf9\r
+       BZIQAElklObB9cJi+hWjONArwrysICt4gOkArvsV0GAmoMEs/pdBBpckIqSkGhhZDl+ddKBZ\r
+       /vqhNdOzlmRKcyz3ux6wOT+wrNLo9orlbzmvcf2ubnyQ90tgZunKKO/KXepKL+b6HzVd88qS\r
+       bUHCgqgV/gmVgUellF0cJyk1CyhvzWCPsFDIOBoQ1aw96Q9PUptIO+9HBRetXdeCfTi3fzFS\r
+       ztlZo1StbXyF54PLjmrbHe8nX1NiKc5INNRiLipOBADn3u6ZxAIAAA==\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 27 Jul 2014 03:53:13 -0000\r
+\r
+Previously, there was no protection against a caller invoking an\r
+operation on an old database version that would effectively corrupt\r
+the database by treating it like a newer version.\r
+\r
+According to notmuch.h, any caller that opens the database in\r
+read/write mode is supposed to check if the database needs upgrading\r
+and perform an upgrade if it does.  This would protect against this,\r
+but nobody (even the CLI) actually does this.\r
+\r
+However, with features, it's easy to protect against incompatible\r
+operations on a fine-grained basis.  This lightweight change allows\r
+callers to safely operate on old database versions, while preventing\r
+specific operations that would corrupt the database with an\r
+informative error message.\r
+---\r
+ lib/database.cc  |  5 +++++\r
+ lib/directory.cc |  5 +++++\r
+ lib/message.cc   |  8 ++++++++\r
+ lib/notmuch.h    | 16 ++++++++++++++++\r
+ 4 files changed, 34 insertions(+)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index a93281c..f105e27 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -311,6 +311,8 @@ notmuch_status_to_string (notmuch_status_t status)\r
+       return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic";\r
+     case NOTMUCH_STATUS_UNSUPPORTED_OPERATION:\r
+       return "Unsupported operation";\r
++    case NOTMUCH_STATUS_UPGRADE_REQUIRED:\r
++      return "Operation requires a database upgrade";\r
+     default:\r
+     case NOTMUCH_STATUS_LAST_STATUS:\r
+       return "Unknown error status value";\r
+@@ -2223,6 +2225,9 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
+     if (message_ret == NULL)\r
+       return NOTMUCH_STATUS_NULL_POINTER;\r
\r
++    if (! (notmuch->features & NOTMUCH_FEATURE_FILE_TERMS))\r
++      return NOTMUCH_STATUS_UPGRADE_REQUIRED;\r
++\r
+     /* return NULL on any failure */\r
+     *message_ret = NULL;\r
\r
+diff --git a/lib/directory.cc b/lib/directory.cc\r
+index 6a3ffed..8daaec8 100644\r
+--- a/lib/directory.cc\r
++++ b/lib/directory.cc\r
+@@ -105,6 +105,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch,\r
+     const char *db_path;\r
+     notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE);\r
\r
++    if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) {\r
++      *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED;\r
++      return NULL;\r
++    }\r
++\r
+     *status_ret = NOTMUCH_STATUS_SUCCESS;\r
\r
+     path = _notmuch_database_relative_path (notmuch, path);\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 4fc427f..1618e81 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -653,6 +653,10 @@ _notmuch_message_add_filename (notmuch_message_t *message,\r
+     if (filename == NULL)\r
+       INTERNAL_ERROR ("Message filename cannot be NULL.");\r
\r
++    if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||\r
++      ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))\r
++      return NOTMUCH_STATUS_UPGRADE_REQUIRED;\r
++\r
+     relative = _notmuch_database_relative_path (message->notmuch, filename);\r
\r
+     status = _notmuch_database_split_path (local, relative, &directory, NULL);\r
+@@ -697,6 +701,10 @@ _notmuch_message_remove_filename (notmuch_message_t *message,\r
+     notmuch_private_status_t private_status;\r
+     notmuch_status_t status;\r
\r
++    if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) ||\r
++      ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER))\r
++      return NOTMUCH_STATUS_UPGRADE_REQUIRED;\r
++\r
+     status = _notmuch_database_filename_to_direntry (\r
+       local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);\r
+     if (status || !direntry)\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 3c5ec98..cbf2ba5 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -160,6 +160,10 @@ typedef enum _notmuch_status {\r
+      */\r
+     NOTMUCH_STATUS_UNSUPPORTED_OPERATION,\r
+     /**\r
++     * The operation requires a database upgrade.\r
++     */\r
++    NOTMUCH_STATUS_UPGRADE_REQUIRED,\r
++    /**\r
+      * Not an actual status value. Just a way to find out how many\r
+      * valid status values there are.\r
+      */\r
+@@ -438,6 +442,9 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch);\r
+  *\r
+  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;\r
+  *    directory not retrieved.\r
++ *\r
++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the\r
++ *    database to use this function.\r
+  */\r
+ notmuch_status_t\r
+ notmuch_database_get_directory (notmuch_database_t *database,\r
+@@ -490,6 +497,9 @@ notmuch_database_get_directory (notmuch_database_t *database,\r
+  *\r
+  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only\r
+  *    mode so no message can be added.\r
++ *\r
++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the\r
++ *    database to use this function.\r
+  */\r
+ notmuch_status_t\r
+ notmuch_database_add_message (notmuch_database_t *database,\r
+@@ -520,6 +530,9 @@ notmuch_database_add_message (notmuch_database_t *database,\r
+  *\r
+  * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only\r
+  *    mode so no message can be removed.\r
++ *\r
++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the\r
++ *    database to use this function.\r
+  */\r
+ notmuch_status_t\r
+ notmuch_database_remove_message (notmuch_database_t *database,\r
+@@ -575,6 +588,9 @@ notmuch_database_find_message (notmuch_database_t *database,\r
+  * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object\r
+  *\r
+  * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred\r
++ *\r
++ * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the\r
++ *    database to use this function.\r
+  */\r
+ notmuch_status_t\r
+ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
+-- \r
+2.0.0\r
+\r