[PATCH v2 13/14] lib: Return an error from operations that require an upgrade
authorAustin Clements <amdragon@MIT.EDU>
Tue, 29 Jul 2014 16:48:11 +0000 (12:48 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 18:03:57 +0000 (10:03 -0800)
9a/98eb37d24cbf3f59551f5bbb813960941a58f2 [new file with mode: 0644]

diff --git a/9a/98eb37d24cbf3f59551f5bbb813960941a58f2 b/9a/98eb37d24cbf3f59551f5bbb813960941a58f2
new file mode 100644 (file)
index 0000000..7a285da
--- /dev/null
@@ -0,0 +1,218 @@
+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 109F8429E27\r
+       for <notmuch@notmuchmail.org>; Tue, 29 Jul 2014 09:48:54 -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 6wi0IWg979Eh for <notmuch@notmuchmail.org>;\r
+       Tue, 29 Jul 2014 09:48:47 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-5.mit.edu (dmz-mailsec-scanner-5.mit.edu\r
+       [18.7.68.34])\r
+       by olra.theworths.org (Postfix) with ESMTP id 7872F431FCF\r
+       for <notmuch@notmuchmail.org>; Tue, 29 Jul 2014 09:48:22 -0700 (PDT)\r
+X-AuditID: 12074422-f79be6d000007518-ed-53d7d05395ff\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-5.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id C7.02.29976.350D7D35; Tue, 29 Jul 2014 12:48:19 -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 s6TGmI6r030917; \r
+       Tue, 29 Jul 2014 12:48:18 -0400\r
+Received: from drake.dyndns.org (31-33-71.wireless.csail.mit.edu\r
+       [128.31.33.71]) (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 s6TGmFRk030274\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Tue, 29 Jul 2014 12:48:17 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1XCAZT-0007HB-1I; Tue, 29 Jul 2014 12:48:15 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 13/14] lib: Return an error from operations that require an\r
+       upgrade\r
+Date: Tue, 29 Jul 2014 12:48:11 -0400\r
+Message-Id: <1406652492-27803-14-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 2.0.0\r
+In-Reply-To: <1406652492-27803-1-git-send-email-amdragon@mit.edu>\r
+References: <1406652492-27803-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixCmqrRt84XqwwZa96har5/JYXL85k9mB\r
+       yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyrh94zJzwQ3tiv6unawNjM0qXYycHBICJhL7\r
+       JtxkhrDFJC7cW8/WxcjFISQwm0liwop+NpCEkMBGRon2GZIQiWNMEr+PTmOGcOYySszd084I\r
+       UsUmoCGxbf9yMFtEQFpi593ZrCA2s4CjxOf9i8AmCQuESUy9/AmshkVAVeLE8XksIDYvUE3b\r
+       y2YWiDPkJBpufAKr5wSK37p3lwniCgeJQ7M2s0xg5F/AyLCKUTYlt0o3NzEzpzg1Wbc4OTEv\r
+       L7VI11QvN7NELzWldBMjKJDYXZR2MP48qHSIUYCDUYmHd8Pca8FCrIllxZW5hxglOZiURHn1\r
+       9l0PFuJLyk+pzEgszogvKs1JLT7EKMHBrCTCy3cWKMebklhZlVqUD5OS5mBREud9a20VLCSQ\r
+       nliSmp2aWpBaBJOV4eBQkuA1Og/UKFiUmp5akZaZU4KQZuLgBBnOAzS8GaSGt7ggMbc4Mx0i\r
+       f4pRUUqcN/scUEIAJJFRmgfXC4v0V4ziQK8I87aBtPMAkwRc9yugwUxAg1ldwAaXJCKkpBoY\r
+       S78sOBr4WFjlydsDCdOlj06cc33++l5v+4RF7HnTFDY+67xzMoZRaNLS1XcVnF8GWDKt/Vze\r
+       vHde/LW+B25FkmeskyqEF8y/33w7Z8klm0N2nQrb2lI4uN3eH9yY8W/ajeCpm2q219aUJX9S\r
+       tU66tWjmjP1ec0UUJ6tcsnefZHvZxmLuhovsl5VYijMSDbWYi4oTAT5BzPHPAgAA\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: Tue, 29 Jul 2014 16:48:54 -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 987b0a6..b323691 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -310,6 +310,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
+@@ -2217,6 +2219,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