Re: Synchronising mail and notmuch tags between machines
[notmuch-archives.git] / e4 / 567c28326981e9150d97a9a7224bd4db3233ef
1 Return-Path: <amdragon@mit.edu>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id B4619431FBD\r
6         for <notmuch@notmuchmail.org>; Mon, 25 Aug 2014 10:28:56 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 2rkgTrINkSl2 for <notmuch@notmuchmail.org>;\r
16         Mon, 25 Aug 2014 10:28:49 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu\r
18         [18.9.25.15])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 7901A431FC2\r
22         for <notmuch@notmuchmail.org>; Mon, 25 Aug 2014 10:28:21 -0700 (PDT)\r
23 X-AuditID: 1209190f-f79aa6d000005b45-b5-53fb7234b52e\r
24 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
25         (using TLS with cipher AES256-SHA (256/256 bits))\r
26         (Client did not present a certificate)\r
27         by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
28         id 16.D9.23365.4327BF35; Mon, 25 Aug 2014 13:28:20 -0400 (EDT)\r
29 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
30         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s7PHQEx3001075; \r
31         Mon, 25 Aug 2014 13:26:15 -0400\r
32 Received: from drake.dyndns.org (31-35-14.wireless.csail.mit.edu\r
33         [128.31.35.14]) (authenticated bits=0)\r
34         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
35         by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s7PHQBNZ029607\r
36         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
37         Mon, 25 Aug 2014 13:26:13 -0400\r
38 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
39         (envelope-from <amdragon@mit.edu>)\r
40         id 1XLy1z-0003jM-TA; Mon, 25 Aug 2014 13:26:11 -0400\r
41 From: Austin Clements <amdragon@mit.edu>\r
42 To: notmuch@notmuchmail.org\r
43 Subject: [PATCH v4 02/11] lib: Database version 3: Introduce fine-grained\r
44         "features"\r
45 Date: Mon, 25 Aug 2014 13:26:00 -0400\r
46 Message-Id: <1408987569-14146-3-git-send-email-amdragon@mit.edu>\r
47 X-Mailer: git-send-email 2.0.0\r
48 In-Reply-To: <1408987569-14146-1-git-send-email-amdragon@mit.edu>\r
49 References: <1408987569-14146-1-git-send-email-amdragon@mit.edu>\r
50 X-Brightmail-Tracker:\r
51  H4sIAAAAAAAAA+NgFjrBIsWRmVeSWpSXmKPExsUixG6nrmtS9DvY4NY5fYsbrd2MFk3TnS2u\r
52         35zJ7MDscev+a3aPZ6tuMXtsOfSeOYA5issmJTUnsyy1SN8ugSuj6chutoLH3YwVC/9dZW5g\r
53         7MjtYuTkkBAwkWjq/skMYYtJXLi3nq2LkYtDSGA2k8Tk4zdZIZyNjBLLNtxkgXCOMUnMevGU\r
54         HcKZyyixvHcRI0g/m4CGxO9bi5lAbBEBaYmdd2ezgtjMAl4Syy70g9nCAiESt9pmsIHYLAKq\r
55         EnvuXgPr5RVwkLj5o5kF4g45iYYbn8BqOAUcJaYvnA02Uwio5vvl88wTGPkXMDKsYpRNya3S\r
56         zU3MzClOTdYtTk7My0st0jXRy80s0UtNKd3ECAowTkn+HYzfDiodYhTgYFTi4b0R/ztYiDWx\r
57         rLgy9xCjJAeTkijvtHygEF9SfkplRmJxRnxRaU5q8SFGCQ5mJRHeZpAcb0piZVVqUT5MSpqD\r
58         RUmc9621VbCQQHpiSWp2ampBahFMVoaDQ0mC934BUKNgUWp6akVaZk4JQpqJgxNkOA/QcP9C\r
59         kOHFBYm5xZnpEPlTjIpS4ryGIAkBkERGaR5cLywBvGIUB3pFmNcGpIoHmDzgul8BDWYCGmza\r
60         8xNkcEkiQkqqgbGqMGdlybb8BTcSb8xR/rtqn7ORgfDUKR3nNn9b83T71iOxs9h3/Ar99cl6\r
61         8q+FOtafXp95qphRa/0m0F+5n2GPCEPkv6j6nfOPl+6yvSXRFz89eWsY1+VfduJt/r8mikjN\r
62         tV7rvSPuk8qH5JUM3bt7pbbvUhOp+s8rUfypruVOS8/W9r4NLNuUWIozEg21mIuKEwGTgOK5\r
63         2wIAAA==\r
64 X-BeenThere: notmuch@notmuchmail.org\r
65 X-Mailman-Version: 2.1.13\r
66 Precedence: list\r
67 List-Id: "Use and development of the notmuch mail system."\r
68         <notmuch.notmuchmail.org>\r
69 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
71 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
72 List-Post: <mailto:notmuch@notmuchmail.org>\r
73 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
74 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
75         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
76 X-List-Received-Date: Mon, 25 Aug 2014 17:28:57 -0000\r
77 \r
78 Previously, our database schema was versioned by a single number.\r
79 Each database schema change had to occur "atomically" in Notmuch's\r
80 development history: before some commit, Notmuch used version N, after\r
81 that commit, it used version N+1.  Hence, each new schema version\r
82 could introduce only one change, the task of developing a schema\r
83 change fell on a single person, and it all had to happen and be\r
84 perfect in a single commit series.  This made introducing a new schema\r
85 version hard.  We've seen only two schema changes in the history of\r
86 Notmuch.\r
87 \r
88 This commit introduces database schema version 3; hopefully the last\r
89 schema version we'll need for a while.  With this version, we switch\r
90 from a single version number to "features": a set of named,\r
91 independent aspects of the database schema.\r
92 \r
93 Features should make backwards compatibility easier.  For many things,\r
94 it should be easy to support databases both with and without a\r
95 feature, which will allow us to make upgrades optional and will enable\r
96 "unstable" features that can be developed and tested over time.\r
97 \r
98 Features also make forwards compatibility easier.  The features\r
99 recorded in a database include "compatibility flags," which can\r
100 indicate to an older version of Notmuch when it must support a given\r
101 feature to open the database for read or for write.  This lets us\r
102 replace the old vague "I don't recognize this version, so something\r
103 might go wrong, but I promise to try my best" warnings upon opening a\r
104 database with an unknown version with precise errors.  If a database\r
105 is safe to open for read/write despite unknown features, an older\r
106 version will know that and issue no message at all.  If the database\r
107 is not safe to open for read/write because of unknown features, an\r
108 older version will know that, too, and can tell the user exactly which\r
109 required features it lacks support for.\r
110 ---\r
111  lib/database-private.h | 108 ++++++++++++++++++++++++++-\r
112  lib/database.cc        | 197 ++++++++++++++++++++++++++++++++++++++++---------\r
113  2 files changed, 271 insertions(+), 34 deletions(-)\r
114 \r
115 diff --git a/lib/database-private.h b/lib/database-private.h\r
116 index d3e65fd..ca0751c 100644\r
117 --- a/lib/database-private.h\r
118 +++ b/lib/database-private.h\r
119 @@ -36,16 +36,106 @@\r
120  \r
121  #pragma GCC visibility push(hidden)\r
122  \r
123 +/* Bit masks for _notmuch_database::features.  Features are named,\r
124 + * independent aspects of the database schema.\r
125 + *\r
126 + * A database stores the set of features that it "uses" (implicitly\r
127 + * before database version 3 and explicitly as of version 3).\r
128 + *\r
129 + * A given library version will "recognize" a particular set of\r
130 + * features; if a database uses a feature that the library does not\r
131 + * recognize, the library will refuse to open it.  It is assumed the\r
132 + * set of recognized features grows monotonically over time.  A\r
133 + * library version will "implement" some subset of the recognized\r
134 + * features: some operations may require that the database use (or not\r
135 + * use) some feature, while other operations may support both\r
136 + * databases that use and that don't use some feature.\r
137 + *\r
138 + * On disk, the database stores string names for these features (see\r
139 + * the feature_names array).  These enum bit values are never\r
140 + * persisted to disk and may change freely.\r
141 + */\r
142 +enum _notmuch_features {\r
143 +    /* If set, file names are stored in "file-direntry" terms.  If\r
144 +     * unset, file names are stored in document data.\r
145 +     *\r
146 +     * Introduced: version 1. */\r
147 +    NOTMUCH_FEATURE_FILE_TERMS = 1 << 0,\r
148 +\r
149 +    /* If set, directory timestamps are stored in documents with\r
150 +     * XDIRECTORY terms and relative paths.  If unset, directory\r
151 +     * timestamps are stored in documents with XTIMESTAMP terms and\r
152 +     * absolute paths.\r
153 +     *\r
154 +     * Introduced: version 1. */\r
155 +    NOTMUCH_FEATURE_DIRECTORY_DOCS = 1 << 1,\r
156 +\r
157 +    /* If set, the from, subject, and message-id headers are stored in\r
158 +     * message document values.  If unset, message documents *may*\r
159 +     * have these values, but if the value is empty, it must be\r
160 +     * retrieved from the message file.\r
161 +     *\r
162 +     * Introduced: optional in version 1, required as of version 3.\r
163 +     */\r
164 +    NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES = 1 << 2,\r
165 +\r
166 +    /* If set, folder terms are boolean and path terms exist.  If\r
167 +     * unset, folder terms are probabilistic and stemmed and path\r
168 +     * terms do not exist.\r
169 +     *\r
170 +     * Introduced: version 2. */\r
171 +    NOTMUCH_FEATURE_BOOL_FOLDER = 1 << 3,\r
172 +};\r
173 +\r
174 +/* In C++, a named enum is its own type, so define bitwise operators\r
175 + * on _notmuch_features. */\r
176 +inline _notmuch_features\r
177 +operator|(_notmuch_features a, _notmuch_features b)\r
178 +{\r
179 +    return static_cast<_notmuch_features>(\r
180 +       static_cast<unsigned>(a) | static_cast<unsigned>(b));\r
181 +}\r
182 +\r
183 +inline _notmuch_features\r
184 +operator&(_notmuch_features a, _notmuch_features b)\r
185 +{\r
186 +    return static_cast<_notmuch_features>(\r
187 +       static_cast<unsigned>(a) & static_cast<unsigned>(b));\r
188 +}\r
189 +\r
190 +inline _notmuch_features\r
191 +operator~(_notmuch_features a)\r
192 +{\r
193 +    return static_cast<_notmuch_features>(~static_cast<unsigned>(a));\r
194 +}\r
195 +\r
196 +inline _notmuch_features&\r
197 +operator|=(_notmuch_features &a, _notmuch_features b)\r
198 +{\r
199 +    a = a | b;\r
200 +    return a;\r
201 +}\r
202 +\r
203 +inline _notmuch_features&\r
204 +operator&=(_notmuch_features &a, _notmuch_features b)\r
205 +{\r
206 +    a = a & b;\r
207 +    return a;\r
208 +}\r
209 +\r
210  struct _notmuch_database {\r
211      notmuch_bool_t exception_reported;\r
212  \r
213      char *path;\r
214  \r
215 -    notmuch_bool_t needs_upgrade;\r
216      notmuch_database_mode_t mode;\r
217      int atomic_nesting;\r
218      Xapian::Database *xapian_db;\r
219  \r
220 +    /* Bit mask of features used by this database.  This is a\r
221 +     * bitwise-OR of NOTMUCH_FEATURE_* values (above). */\r
222 +    enum _notmuch_features features;\r
223 +\r
224      unsigned int last_doc_id;\r
225      uint64_t last_thread_id;\r
226  \r
227 @@ -55,6 +145,22 @@ struct _notmuch_database {\r
228      Xapian::ValueRangeProcessor *date_range_processor;\r
229  };\r
230  \r
231 +/* Prior to database version 3, features were implied by the database\r
232 + * version number, so hard-code them for earlier versions. */\r
233 +#define NOTMUCH_FEATURES_V0 ((enum _notmuch_features)0)\r
234 +#define NOTMUCH_FEATURES_V1 (NOTMUCH_FEATURES_V0 | NOTMUCH_FEATURE_FILE_TERMS | \\r
235 +                            NOTMUCH_FEATURE_DIRECTORY_DOCS)\r
236 +#define NOTMUCH_FEATURES_V2 (NOTMUCH_FEATURES_V1 | NOTMUCH_FEATURE_BOOL_FOLDER)\r
237 +\r
238 +/* Current database features.  If any of these are missing from a\r
239 + * database, request an upgrade.\r
240 + * NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES is not included because\r
241 + * upgrade doesn't currently introduce the feature (though brand new\r
242 + * databases will have it). */\r
243 +#define NOTMUCH_FEATURES_CURRENT \\r
244 +    (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_DIRECTORY_DOCS | \\r
245 +     NOTMUCH_FEATURE_BOOL_FOLDER)\r
246 +\r
247  /* Return the list of terms from the given iterator matching a prefix.\r
248   * The prefix will be stripped from the strings in the returned list.\r
249   * The list will be allocated using ctx as the talloc context.\r
250 diff --git a/lib/database.cc b/lib/database.cc\r
251 index 9c0952a..2b566f7 100644\r
252 --- a/lib/database.cc\r
253 +++ b/lib/database.cc\r
254 @@ -20,6 +20,7 @@\r
255  \r
256  #include "database-private.h"\r
257  #include "parse-time-vrp.h"\r
258 +#include "string-util.h"\r
259  \r
260  #include <iostream>\r
261  \r
262 @@ -42,7 +43,7 @@ typedef struct {\r
263      const char *prefix;\r
264  } prefix_t;\r
265  \r
266 -#define NOTMUCH_DATABASE_VERSION 2\r
267 +#define NOTMUCH_DATABASE_VERSION 3\r
268  \r
269  #define STRINGIFY(s) _SUB_STRINGIFY(s)\r
270  #define _SUB_STRINGIFY(s) #s\r
271 @@ -154,6 +155,17 @@ typedef struct {\r
272   *                     changes are made to the database (such as by\r
273   *                     indexing new fields).\r
274   *\r
275 + *     features        The set of features supported by this\r
276 + *                     database. This consists of a set of\r
277 + *                     '\n'-separated lines, where each is a feature\r
278 + *                     name, a '\t', and compatibility flags.  If the\r
279 + *                     compatibility flags contain 'w', then the\r
280 + *                     opener must support this feature to safely\r
281 + *                     write this database.  If the compatibility\r
282 + *                     flags contain 'r', then the opener must\r
283 + *                     support this feature to read this database.\r
284 + *                     Introduced in database version 3.\r
285 + *\r
286   *     last_thread_id  The last thread ID generated. This is stored\r
287   *                     as a 16-byte hexadecimal ASCII representation\r
288   *                     of a 64-bit unsigned integer. The first ID\r
289 @@ -254,6 +266,28 @@ _find_prefix (const char *name)\r
290      return "";\r
291  }\r
292  \r
293 +static const struct {\r
294 +    /* NOTMUCH_FEATURE_* value. */\r
295 +    _notmuch_features value;\r
296 +    /* Feature name as it appears in the database.  This name should\r
297 +     * be appropriate for displaying to the user if an older version\r
298 +     * of notmuch doesn't support this feature. */\r
299 +    const char *name;\r
300 +    /* Compatibility flags when this feature is declared. */\r
301 +    const char *flags;\r
302 +} feature_names[] = {\r
303 +    { NOTMUCH_FEATURE_FILE_TERMS,\r
304 +      "multiple paths per message", "rw" },\r
305 +    { NOTMUCH_FEATURE_DIRECTORY_DOCS,\r
306 +      "relative directory paths", "rw" },\r
307 +    /* Header values are not required for reading a database because a\r
308 +     * reader can just refer to the message file. */\r
309 +    { NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES,\r
310 +      "from/subject/message-ID in database", "w" },\r
311 +    { NOTMUCH_FEATURE_BOOL_FOLDER,\r
312 +      "exact folder:/path: search", "rw" },\r
313 +};\r
314 +\r
315  const char *\r
316  notmuch_status_to_string (notmuch_status_t status)\r
317  {\r
318 @@ -591,6 +625,11 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
319                                     &notmuch);\r
320      if (status)\r
321         goto DONE;\r
322 +\r
323 +    /* Upgrade doesn't add this feature to existing databases, but new\r
324 +     * databases have it. */\r
325 +    notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;\r
326 +\r
327      status = notmuch_database_upgrade (notmuch, NULL, NULL);\r
328      if (status) {\r
329         notmuch_database_close(notmuch);\r
330 @@ -619,6 +658,83 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)\r
331      return NOTMUCH_STATUS_SUCCESS;\r
332  }\r
333  \r
334 +/* Parse a database features string from the given database version.\r
335 + * Returns the feature bit set.\r
336 + *\r
337 + * For version < 3, this ignores the features string and returns a\r
338 + * hard-coded set of features.\r
339 + *\r
340 + * If there are unrecognized features that are required to open the\r
341 + * database in mode (which should be 'r' or 'w'), return a\r
342 + * comma-separated list of unrecognized but required features in\r
343 + * *incompat_out suitable for presenting to the user.  *incompat_out\r
344 + * will be allocated from ctx.\r
345 + */\r
346 +static _notmuch_features\r
347 +_parse_features (const void *ctx, const char *features, unsigned int version,\r
348 +                char mode, char **incompat_out)\r
349 +{\r
350 +    _notmuch_features res = static_cast<_notmuch_features>(0);\r
351 +    unsigned int namelen, i;\r
352 +    size_t llen = 0;\r
353 +    const char *flags;\r
354 +\r
355 +    /* Prior to database version 3, features were implied by the\r
356 +     * version number. */\r
357 +    if (version == 0)\r
358 +       return NOTMUCH_FEATURES_V0;\r
359 +    else if (version == 1)\r
360 +       return NOTMUCH_FEATURES_V1;\r
361 +    else if (version == 2)\r
362 +       return NOTMUCH_FEATURES_V2;\r
363 +\r
364 +    /* Parse the features string */\r
365 +    while ((features = strtok_len_c (features + llen, "\n", &llen)) != NULL) {\r
366 +       flags = strchr (features, '\t');\r
367 +       if (! flags || flags > features + llen)\r
368 +           continue;\r
369 +       namelen = flags - features;\r
370 +\r
371 +       for (i = 0; i < ARRAY_SIZE (feature_names); ++i) {\r
372 +           if (strlen (feature_names[i].name) == namelen &&\r
373 +               strncmp (feature_names[i].name, features, namelen) == 0) {\r
374 +               res |= feature_names[i].value;\r
375 +               break;\r
376 +           }\r
377 +       }\r
378 +\r
379 +       if (i == ARRAY_SIZE (feature_names) && incompat_out) {\r
380 +           /* Unrecognized feature */\r
381 +           const char *have = strchr (flags, mode);\r
382 +           if (have && have < features + llen) {\r
383 +               /* This feature is required to access this database in\r
384 +                * 'mode', but we don't understand it. */\r
385 +               if (! *incompat_out)\r
386 +                   *incompat_out = talloc_strdup (ctx, "");\r
387 +               *incompat_out = talloc_asprintf_append_buffer (\r
388 +                   *incompat_out, "%s%.*s", **incompat_out ? ", " : "",\r
389 +                   namelen, features);\r
390 +           }\r
391 +       }\r
392 +    }\r
393 +\r
394 +    return res;\r
395 +}\r
396 +\r
397 +static char *\r
398 +_print_features (const void *ctx, unsigned int features)\r
399 +{\r
400 +    unsigned int i;\r
401 +    char *res = talloc_strdup (ctx, "");\r
402 +\r
403 +    for (i = 0; i < ARRAY_SIZE (feature_names); ++i)\r
404 +       if (features & feature_names[i].value)\r
405 +           res = talloc_asprintf_append_buffer (\r
406 +               res, "%s\t%s\n", feature_names[i].name, feature_names[i].flags);\r
407 +\r
408 +    return res;\r
409 +}\r
410 +\r
411  notmuch_status_t\r
412  notmuch_database_open (const char *path,\r
413                        notmuch_database_mode_t mode,\r
414 @@ -627,7 +743,7 @@ notmuch_database_open (const char *path,\r
415      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
416      void *local = talloc_new (NULL);\r
417      notmuch_database_t *notmuch = NULL;\r
418 -    char *notmuch_path, *xapian_path;\r
419 +    char *notmuch_path, *xapian_path, *incompat_features;\r
420      struct stat st;\r
421      int err;\r
422      unsigned int i, version;\r
423 @@ -677,7 +793,6 @@ notmuch_database_open (const char *path,\r
424      if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
425         notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
426  \r
427 -    notmuch->needs_upgrade = FALSE;\r
428      notmuch->mode = mode;\r
429      notmuch->atomic_nesting = 0;\r
430      try {\r
431 @@ -686,37 +801,44 @@ notmuch_database_open (const char *path,\r
432         if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {\r
433             notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,\r
434                                                                Xapian::DB_CREATE_OR_OPEN);\r
435 -           version = notmuch_database_get_version (notmuch);\r
436 -\r
437 -           if (version > NOTMUCH_DATABASE_VERSION) {\r
438 -               fprintf (stderr,\r
439 -                        "Error: Notmuch database at %s\n"\r
440 -                        "       has a newer database format version (%u) than supported by this\n"\r
441 -                        "       version of notmuch (%u). Refusing to open this database in\n"\r
442 -                        "       read-write mode.\n",\r
443 -                        notmuch_path, version, NOTMUCH_DATABASE_VERSION);\r
444 -               notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
445 -               notmuch_database_destroy (notmuch);\r
446 -               notmuch = NULL;\r
447 -               status = NOTMUCH_STATUS_FILE_ERROR;\r
448 -               goto DONE;\r
449 -           }\r
450 -\r
451 -           if (version < NOTMUCH_DATABASE_VERSION)\r
452 -               notmuch->needs_upgrade = TRUE;\r
453         } else {\r
454             notmuch->xapian_db = new Xapian::Database (xapian_path);\r
455 -           version = notmuch_database_get_version (notmuch);\r
456 -           if (version > NOTMUCH_DATABASE_VERSION)\r
457 -           {\r
458 -               fprintf (stderr,\r
459 -                        "Warning: Notmuch database at %s\n"\r
460 -                        "         has a newer database format version (%u) than supported by this\n"\r
461 -                        "         version of notmuch (%u). Some operations may behave incorrectly,\n"\r
462 -                        "         (but the database will not be harmed since it is being opened\n"\r
463 -                        "         in read-only mode).\n",\r
464 -                        notmuch_path, version, NOTMUCH_DATABASE_VERSION);\r
465 -           }\r
466 +       }\r
467 +\r
468 +       /* Check version.  As of database version 3, we represent\r
469 +        * changes in terms of features, so assume a version bump\r
470 +        * means a dramatically incompatible change. */\r
471 +       version = notmuch_database_get_version (notmuch);\r
472 +       if (version > NOTMUCH_DATABASE_VERSION) {\r
473 +           fprintf (stderr,\r
474 +                    "Error: Notmuch database at %s\n"\r
475 +                    "       has a newer database format version (%u) than supported by this\n"\r
476 +                    "       version of notmuch (%u).\n",\r
477 +                    notmuch_path, version, NOTMUCH_DATABASE_VERSION);\r
478 +           notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
479 +           notmuch_database_destroy (notmuch);\r
480 +           notmuch = NULL;\r
481 +           status = NOTMUCH_STATUS_FILE_ERROR;\r
482 +           goto DONE;\r
483 +       }\r
484 +\r
485 +       /* Check features. */\r
486 +       incompat_features = NULL;\r
487 +       notmuch->features = _parse_features (\r
488 +           local, notmuch->xapian_db->get_metadata ("features").c_str (),\r
489 +           version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',\r
490 +           &incompat_features);\r
491 +       if (incompat_features) {\r
492 +           fprintf (stderr,\r
493 +                    "Error: Notmuch database at %s\n"\r
494 +                    "       requires features (%s)\n"\r
495 +                    "       not supported by this version of notmuch.\n",\r
496 +                    notmuch_path, incompat_features);\r
497 +           notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;\r
498 +           notmuch_database_destroy (notmuch);\r
499 +           notmuch = NULL;\r
500 +           status = NOTMUCH_STATUS_FILE_ERROR;\r
501 +           goto DONE;\r
502         }\r
503  \r
504         notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();\r
505 @@ -1048,7 +1170,9 @@ notmuch_database_get_version (notmuch_database_t *notmuch)\r
506  notmuch_bool_t\r
507  notmuch_database_needs_upgrade (notmuch_database_t *notmuch)\r
508  {\r
509 -    return notmuch->needs_upgrade;\r
510 +    return notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE &&\r
511 +       ((NOTMUCH_FEATURES_CURRENT & ~notmuch->features) ||\r
512 +        (notmuch_database_get_version (notmuch) < NOTMUCH_DATABASE_VERSION));\r
513  }\r
514  \r
515  static volatile sig_atomic_t do_progress_notify = 0;\r
516 @@ -1077,6 +1201,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
517                                                    double progress),\r
518                           void *closure)\r
519  {\r
520 +    void *local = talloc_new (NULL);\r
521      Xapian::WritableDatabase *db;\r
522      struct sigaction action;\r
523      struct itimerval timerval;\r
524 @@ -1114,6 +1239,10 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
525         timer_is_active = TRUE;\r
526      }\r
527  \r
528 +    /* Set the target features so we write out changes in the desired\r
529 +     * format. */\r
530 +    notmuch->features |= NOTMUCH_FEATURES_CURRENT;\r
531 +\r
532      /* Before version 1, each message document had its filename in the\r
533       * data field. Copy that into the new format by calling\r
534       * notmuch_message_add_filename.\r
535 @@ -1226,6 +1355,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
536         notmuch_query_destroy (query);\r
537      }\r
538  \r
539 +    db->set_metadata ("features", _print_features (local, notmuch->features));\r
540      db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));\r
541      db->flush ();\r
542  \r
543 @@ -1302,6 +1432,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
544         sigaction (SIGALRM, &action, NULL);\r
545      }\r
546  \r
547 +    talloc_free (local);\r
548      return NOTMUCH_STATUS_SUCCESS;\r
549  }\r
550  \r
551 -- \r
552 2.0.0\r
553 \r