]> git.notmuchmail.org Git - notmuch/commitdiff
lib: Rework interface for maildir_flags synchronization
authorCarl Worth <cworth@cworth.org>
Thu, 11 Nov 2010 01:36:09 +0000 (17:36 -0800)
committerCarl Worth <cworth@cworth.org>
Thu, 11 Nov 2010 11:40:19 +0000 (03:40 -0800)
Instead of having an API for setting a library-wide flag for
synchronization (notmuch_database_set_maildir_sync) we instead
implement maildir synchronization with two new library functions:

notmuch_message_maildir_flags_to_tags
  and   notmuch_message_tags_to_maildir_flags

These functions are nicely documented here, (though the implementation
does not quite match the documentation yet---as plainly evidenced by
the current results of the test suite).

lib/database-private.h
lib/database.cc
lib/message.cc
lib/notmuch-private.h
lib/notmuch.h
notmuch-new.c
notmuch-restore.c
notmuch-tag.c

index 81097509244ae76aa04db74e795f122e49dc28fc..140b54edf499dea41f00c38a8bb2f00440b16648 100644 (file)
@@ -51,7 +51,6 @@ struct _notmuch_database {
     Xapian::QueryParser *query_parser;
     Xapian::TermGenerator *term_gen;
     Xapian::ValueRangeProcessor *value_range_processor;
     Xapian::QueryParser *query_parser;
     Xapian::TermGenerator *term_gen;
     Xapian::ValueRangeProcessor *value_range_processor;
-    notmuch_bool_t maildir_sync;
 };
 
 /* Convert tags from Xapian internal format to notmuch format.
 };
 
 /* Convert tags from Xapian internal format to notmuch format.
index 293d21aa01ff70c03ecdcbe3221c92dca21247f5..380bbe3daa66e9a878c084b6f1e7fc2b1ace1470 100644 (file)
@@ -688,8 +688,6 @@ notmuch_database_open (const char *path,
        notmuch = NULL;
     }
 
        notmuch = NULL;
     }
 
-    notmuch_database_set_maildir_sync (notmuch, FALSE);
-
   DONE:
     if (notmuch_path)
        free (notmuch_path);
   DONE:
     if (notmuch_path)
        free (notmuch_path);
@@ -719,13 +717,6 @@ notmuch_database_close (notmuch_database_t *notmuch)
     talloc_free (notmuch);
 }
 
     talloc_free (notmuch);
 }
 
-void
-notmuch_database_set_maildir_sync (notmuch_database_t *database,
-                                  notmuch_bool_t maildir_sync)
-{
-    database->maildir_sync = maildir_sync;
-}
-
 const char *
 notmuch_database_get_path (notmuch_database_t *notmuch)
 {
 const char *
 notmuch_database_get_path (notmuch_database_t *notmuch)
 {
index 80ff4ca9f77d646bc87af04c43980d47df079d8d..88f7c362dc6d2173d3b4b1d3709ed9e9d85e0e48 100644 (file)
@@ -610,29 +610,15 @@ _notmuch_message_set_date (notmuch_message_t *message,
                            Xapian::sortable_serialise (time_value));
 }
 
                            Xapian::sortable_serialise (time_value));
 }
 
-static notmuch_private_status_t
-_notmuch_message_tags_to_maildir (notmuch_message_t *message);
-
 /* Synchronize changes made to message->doc out into the database. */
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
     Xapian::WritableDatabase *db;
 /* Synchronize changes made to message->doc out into the database. */
 void
 _notmuch_message_sync (notmuch_message_t *message)
 {
     Xapian::WritableDatabase *db;
-    notmuch_private_status_t status;
 
     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
 
     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
        return;
 
-    if (message->notmuch->maildir_sync &&
-       !notmuch_message_get_flag(message, NOTMUCH_MESSAGE_FLAG_TAGS_INVALID)) {
-       status = _notmuch_message_tags_to_maildir (message);
-       if (status != NOTMUCH_PRIVATE_STATUS_SUCCESS) {
-           fprintf (stderr, "Error: Cannot sync tags to maildir (%s)\n",
-                    notmuch_status_to_string ((notmuch_status_t)status));
-           /* Exit to avoid unsynchronized mailstore. */
-           exit(1);
-       }
-    }
     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
     db->replace_document (message->doc_id, message->doc);
 }
     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
     db->replace_document (message->doc_id, message->doc);
 }
@@ -749,7 +735,7 @@ _notmuch_message_remove_term (notmuch_message_t *message,
  * This change will not be reflected in the database until the next
  * call to _notmuch_message_sync.
  */
  * This change will not be reflected in the database until the next
  * call to _notmuch_message_sync.
  */
-notmuch_private_status_t
+notmuch_status_t
 _notmuch_message_rename (notmuch_message_t *message,
                         const char *new_filename)
 {
 _notmuch_message_rename (notmuch_message_t *message,
                         const char *new_filename)
 {
@@ -757,29 +743,31 @@ _notmuch_message_rename (notmuch_message_t *message,
     char *direntry;
     Xapian::PostingIterator i, end;
     Xapian::Document document;
     char *direntry;
     Xapian::PostingIterator i, end;
     Xapian::Document document;
-    notmuch_private_status_t pstatus;
+    notmuch_private_status_t private_status;
     notmuch_status_t status;
     const char *old_filename;
 
     old_filename = notmuch_message_get_filename(message);
     old_filename = talloc_reference(local, old_filename);
     notmuch_status_t status;
     const char *old_filename;
 
     old_filename = notmuch_message_get_filename(message);
     old_filename = talloc_reference(local, old_filename);
-    if (unlikely(!old_filename))
-       return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
+    if (unlikely (! old_filename))
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
 
     status = _notmuch_message_add_filename (message, new_filename);
     if (status)
 
     status = _notmuch_message_add_filename (message, new_filename);
     if (status)
-       return (notmuch_private_status_t)status;
+       return status;
 
     status = _notmuch_database_filename_to_direntry (local, message->notmuch,
                                                     old_filename, &direntry);
     if (status)
 
     status = _notmuch_database_filename_to_direntry (local, message->notmuch,
                                                     old_filename, &direntry);
     if (status)
-       return (notmuch_private_status_t)status;
+       return status;
 
 
-    pstatus = _notmuch_message_remove_term (message, "file-direntry", direntry);
+    private_status = _notmuch_message_remove_term (message, "file-direntry", direntry);
+    status = COERCE_STATUS (private_status,
+                           "Unexpected error from _notmuch_message_remove_term");
 
     talloc_free (local);
 
 
     talloc_free (local);
 
-    return pstatus;
+    return status;
 }
 
 notmuch_status_t
 }
 
 notmuch_status_t
@@ -838,14 +826,18 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
     return NOTMUCH_STATUS_SUCCESS;
 }
 
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+/* XXX: Needs to iterate over all message filenames. */
 notmuch_status_t
 notmuch_status_t
-notmuch_message_maildir_to_tags (notmuch_message_t *message, const char *filename)
+notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
 {
     const char *flags, *p;
     char f;
     bool valid, unread;
     unsigned i;
     notmuch_status_t status;
 {
     const char *flags, *p;
     char f;
     bool valid, unread;
     unsigned i;
     notmuch_status_t status;
+    const char *filename;
+
+    filename = notmuch_message_get_filename (message);
 
     flags = strstr (filename, ":2,");
     if (!flags)
 
     flags = strstr (filename, ":2,");
     if (!flags)
@@ -942,10 +934,14 @@ maildir_get_subdir (char *filename)
     return subdir;
 }
 
     return subdir;
 }
 
-/* Rename the message file so that maildir flags corresponds to the
- * tags and, if aplicable, move the message from new/ to cur/. */
-static notmuch_private_status_t
-_notmuch_message_tags_to_maildir (notmuch_message_t *message)
+/* XXX: Needs to iterate over all filenames in the message
+ *
+ * XXX: Needs to ensure that existing, unsupported flags in the
+ *      filename are left unchanged (which also needs a test in the
+ *      test suite).
+ */
+notmuch_status_t
+notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
 {
     char flags[ARRAY_SIZE(flag2tag)+1];
     const char *filename, *p;
 {
     char flags[ARRAY_SIZE(flag2tag)+1];
     const char *filename, *p;
@@ -962,14 +958,15 @@ _notmuch_message_tags_to_maildir (notmuch_message_t *message)
        // Return if flags are not to be changed - this suppresses
        // moving the message from new/ to cur/ during initial
        // tagging.
        // Return if flags are not to be changed - this suppresses
        // moving the message from new/ to cur/ during initial
        // tagging.
-       return NOTMUCH_PRIVATE_STATUS_SUCCESS;
+       return NOTMUCH_STATUS_SUCCESS;
     }
     if (!p)
        p = filename + strlen(filename);
 
     filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));
     if (unlikely (filename_new == NULL))
     }
     if (!p)
        p = filename + strlen(filename);
 
     filename_new = (char*)talloc_size(message, (p-filename) + 3 + sizeof(flags));
     if (unlikely (filename_new == NULL))
-       return NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
     memcpy(filename_new, filename, p-filename);
     filename_new[p-filename] = '\0';
 
     memcpy(filename_new, filename, p-filename);
     filename_new[p-filename] = '\0';
 
@@ -991,7 +988,7 @@ _notmuch_message_tags_to_maildir (notmuch_message_t *message)
        return _notmuch_message_rename (message, filename_new);
        /* _notmuch_message_sync is our caller. Do not call it here. */
     }
        return _notmuch_message_rename (message, filename_new);
        /* _notmuch_message_sync is our caller. Do not call it here. */
     }
-    return NOTMUCH_PRIVATE_STATUS_SUCCESS;
+    return NOTMUCH_STATUS_SUCCESS;
 }
 
 notmuch_status_t
 }
 
 notmuch_status_t
index d602e157c80e8184bd5d23ece56dc6a4afb8343d..4c3d3eb81e630cefdbd8ec3f4b64206598b99a78 100644 (file)
@@ -261,7 +261,7 @@ notmuch_status_t
 _notmuch_message_add_filename (notmuch_message_t *message,
                               const char *filename);
 
 _notmuch_message_add_filename (notmuch_message_t *message,
                               const char *filename);
 
-notmuch_private_status_t
+notmuch_status_t
 _notmuch_message_rename (notmuch_message_t *message,
                         const char *new_filename);
 
 _notmuch_message_rename (notmuch_message_t *message,
                         const char *new_filename);
 
index 41820b596c19c1a73d341dd45c6d067199d799f9..ca8707e80e8d59229e9316ff17c8849b9ac898db 100644 (file)
@@ -176,12 +176,6 @@ notmuch_database_open (const char *path,
 void
 notmuch_database_close (notmuch_database_t *database);
 
 void
 notmuch_database_close (notmuch_database_t *database);
 
-/* Sets whether maildir flags should be synchronized with notmuch
- * tags. */
-void
-notmuch_database_set_maildir_sync (notmuch_database_t *database,
-                                  notmuch_bool_t maildir_sync);
-
 /* Return the database path of the given database.
  *
  * The return value is a string owned by notmuch so should not be
 /* Return the database path of the given database.
  *
  * The return value is a string owned by notmuch so should not be
@@ -903,11 +897,74 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag);
 notmuch_status_t
 notmuch_message_remove_all_tags (notmuch_message_t *message);
 
 notmuch_status_t
 notmuch_message_remove_all_tags (notmuch_message_t *message);
 
-/* Add or remove tags based on the maildir flags in the file name.
+/* Add/remove tags according to maildir flags in the message filename(s)
+ *
+ * This function examines the filenames of 'message' for maildir
+ * flags, and adds or removes tags on 'message' as follows when these
+ * flags are present:
+ *
+ *     Flag    Action
+ *     ----    ------
+ *     'D'     Adds the "draft" tag to the message
+ *     'F'     Adds the "flagged" tag to the message
+ *     'P'     Adds the "passed" tag to the message
+ *     'R'     Adds the "replied" tag to the message
+ *     'S'     Removes the "unread" tag from the message
+ *
+ * The only filenames examined for flags are filenames which appear to
+ * be within a maildir directory, (the file must be in a directory
+ * named "new" or "cur" and there must be a neighboring directory
+ * named respectively "cur" or "new"). The flags are identified as
+ * trailing components of the filename after a sequence of ":2,".
+ *
+ * If there are multiple filenames associated with this message, the
+ * flag is considered present if it appears in one or more
+ * filenames. (That is, the flags from the multiple filenames are
+ * combined with the logical OR operator.)
+ *
+ * A client can ensure that notmuch database tags remain synchronized
+ * with maildir flags by calling this function after each call to
+ * notmuch_database_add_message. See also
+ * notmuch_message_tags_to_maildir_flags for synchronizing tag changes
+ * back to maildir flags.
  */
 notmuch_status_t
  */
 notmuch_status_t
-notmuch_message_maildir_to_tags (notmuch_message_t *message,
-                                const char *filename);
+notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
+
+/* Rename message filename(s) to encode tags as maildir flags
+ *
+ * Specifically, for each filename corresponding to this message:
+ *
+ * If the filename is not in a maildir directory, do nothing.
+ * (A maildir directory is determined as a directory named "new" or
+ * "cur" with a neighboring directory named respectively "cur" or
+ * "new".)
+ *
+ * If the filename is in a maildir directory, rename the file so that
+ * its filename ends with the sequence ":2," followed by zero or more
+ * of the following single-character flags (in ASCII order):
+ *
+ *   'D' if the message has the "draft" tag
+ *   'F' if the message has the "flagged" tag
+ *   'P' if the message has the "passed" tag
+ *   'R' if the message has the "replied" tag
+ *   'S' if the message does not have the "unread" tag
+ *
+ * Any existing flags unmentioned in the list above are left
+ * unaffected by the rename.
+ *
+ * Also, if this filename is in a directory named "new", rename it to
+ * be within the neighboring directory named "cur".
+ *
+ * A client can ensure that maildir filename flags remain synchronized
+ * with notmuch database tags by calling this function after changing
+ * tags, (after calls to notmuch_message_add_tag,
+ * notmuch_message_remove_tag, or notmuch_message_freeze/
+ * notmuch_message_thaw). See also notmuch_message_maildir_flags_to_tags
+ * for synchronizing maildir flag changes back to tags.
+ */
+notmuch_status_t
+notmuch_message_tags_to_maildir_flags (notmuch_message_t *message);
 
 /* Freeze the current state of 'message' within the database.
  *
 
 /* Freeze the current state of 'message' within the database.
  *
index 23e7afc587b9c79e7dabe28b280955f5890930ef..3d628c23f2c8bb20ba6aa3cc49b91407a117afe6 100644 (file)
@@ -412,12 +412,12 @@ add_files_recursive (notmuch_database_t *notmuch,
            for (tag=state->new_tags; *tag != NULL; tag++)
                notmuch_message_add_tag (message, *tag);
            if (state->synchronize_flags == TRUE)
            for (tag=state->new_tags; *tag != NULL; tag++)
                notmuch_message_add_tag (message, *tag);
            if (state->synchronize_flags == TRUE)
-               notmuch_message_maildir_to_tags (message, next);
+               notmuch_message_maildir_flags_to_tags (message);
            break;
        /* Non-fatal issues (go on to next file) */
        case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
            if (state->synchronize_flags == TRUE)
            break;
        /* Non-fatal issues (go on to next file) */
        case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
            if (state->synchronize_flags == TRUE)
-               notmuch_message_maildir_to_tags (message, next);
+               notmuch_message_maildir_flags_to_tags (message);
            break;
        case NOTMUCH_STATUS_FILE_NOT_EMAIL:
            fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
            break;
        case NOTMUCH_STATUS_FILE_NOT_EMAIL:
            fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
index 98660b32cf0257b95da65450e37e4c080ca49b5e..f095f64a76b7054dbda62b1fd8f9f4efa975117b 100644 (file)
@@ -25,6 +25,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 {
     notmuch_config_t *config;
     notmuch_database_t *notmuch;
 {
     notmuch_config_t *config;
     notmuch_database_t *notmuch;
+    notmuch_bool_t synchronize_flags;
     FILE *input;
     char *line = NULL;
     size_t line_size;
     FILE *input;
     char *line = NULL;
     size_t line_size;
@@ -41,8 +42,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
     if (notmuch == NULL)
        return 1;
 
     if (notmuch == NULL)
        return 1;
 
-    notmuch_database_set_maildir_sync (notmuch,
-                                      notmuch_config_get_maildir_synchronize_flags (config));
+    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
+
     if (argc) {
        input = fopen (argv[0], "r");
        if (input == NULL) {
     if (argc) {
        input = fopen (argv[0], "r");
        if (input == NULL) {
@@ -133,6 +134,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 
        notmuch_message_thaw (message);
 
 
        notmuch_message_thaw (message);
 
+       if (synchronize_flags)
+           notmuch_message_tags_to_maildir_flags (message);
+
       NEXT_LINE:
        if (message)
            notmuch_message_destroy (message);
       NEXT_LINE:
        if (message)
            notmuch_message_destroy (message);
index 53f08f43dc1019a9f8ada6e905dc1d36f5c43d5c..60e21e0deb8c89e02e29d3845233fa7687701668 100644 (file)
@@ -45,6 +45,7 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     struct sigaction action;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     struct sigaction action;
+    notmuch_bool_t synchronize_flags;
     int i;
 
     /* Setup our handler for SIGINT */
     int i;
 
     /* Setup our handler for SIGINT */
@@ -100,8 +101,8 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
                                     NOTMUCH_DATABASE_MODE_READ_WRITE);
     if (notmuch == NULL)
        return 1;
                                     NOTMUCH_DATABASE_MODE_READ_WRITE);
     if (notmuch == NULL)
        return 1;
-    notmuch_database_set_maildir_sync (notmuch,
-                                      notmuch_config_get_maildir_synchronize_flags (config));
+
+    synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
 
     query = notmuch_query_create (notmuch, query_string);
     if (query == NULL) {
@@ -129,6 +130,9 @@ notmuch_tag_command (void *ctx, unused (int argc), unused (char *argv[]))
 
        notmuch_message_thaw (message);
 
 
        notmuch_message_thaw (message);
 
+       if (synchronize_flags)
+           notmuch_message_tags_to_maildir_flags (message);
+
        notmuch_message_destroy (message);
     }
 
        notmuch_message_destroy (message);
     }