[RFC PATCH 10/13] Introduce concept of mailstore "constructor"

Ethan Glasser-Camp glasse at cs.rpi.edu
Wed Feb 15 14:02:03 PST 2012


From: Ethan Glasser-Camp <ethan at betacantrips.com>

Right now this is a fancy no-op because maildir doesn't need any
special data, but getting the API right is good. A constructor can
fail, so return a notmuch_status_t.

Signed-off-by: Ethan Glasser-Camp <ethan at betacantrips.com>
---
 lib/mailstore.c  |   36 +++++++++++++++++++++++++++++++++---
 lib/notmuch.h    |    7 +++++++
 notmuch-config.c |    8 +++++++-
 3 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/lib/mailstore.c b/lib/mailstore.c
index 2c6beab..b4d512d 100644
--- a/lib/mailstore.c
+++ b/lib/mailstore.c
@@ -17,15 +17,25 @@
  */
 
 #include <stdio.h>
+#include <stdarg.h>
 
 #include "notmuch-private.h"
 
 typedef struct _notmuch_mailstore {
+    notmuch_status_t (*constructor) (void **data, va_list args);
     FILE *(*open) (struct _notmuch_mailstore *mailstore, const char *filename);
     int (*rename) (struct _notmuch_mailstore *mailstore, const char *old_filename,
 		   const char *new_filename);
+    void *data;
 } _notmuch_mailstore;
 
+static notmuch_status_t
+_maildir_constructor (void **data, unused (va_list ap))
+{
+    (*data) = NULL;
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
 static FILE *
 _maildir_open_function (unused (notmuch_mailstore_t *mailstore),
 			const char *filename)
@@ -48,12 +58,18 @@ _maildir_rename_function (unused (notmuch_mailstore_t *mailstore),
  * - A function to "rename" a mail message, which is currently only
  *   used in tags_to_maildir_flags.
  *
- * - TODO: A way to scan for new messages?
+ * - A "constructor" that creates a mailstore object of the requisite
+ *   type. Arguments are passed via va_args.
  *
- * - TODO: A "constructor"?
+ * A mailstore also has a "data" field that can be used to store
+ * instance-specific information about this mailstore -- for example,
+ * a CouchDB URL or a path. FIXME: mailstores are all statically
+ * allocated, so maybe we shouldn't do this.
  */
 _notmuch_mailstore
-notmuch_mailstore_maildir = { _maildir_open_function, _maildir_rename_function };
+notmuch_mailstore_maildir = { _maildir_constructor,
+			      _maildir_open_function, _maildir_rename_function,
+			      NULL };
 
 _notmuch_mailstore *
 notmuch_mailstore_get_by_name (const char *name)
@@ -76,3 +92,17 @@ notmuch_mailstore_rename (notmuch_mailstore_t *mailstore, const char *old_filena
 {
     return mailstore->rename (mailstore, old_filename, new_filename);
 }
+
+notmuch_status_t
+notmuch_mailstore_construct (notmuch_mailstore_t *mailstore, ...)
+{
+    va_list va_args;
+    notmuch_status_t status;
+
+    va_start (va_args, mailstore);
+
+    status = mailstore->constructor (&mailstore->data, va_args);
+
+    va_end (va_args);
+    return status;
+}
diff --git a/lib/notmuch.h b/lib/notmuch.h
index b6e66a9..7f48507 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -430,6 +430,13 @@ int
 notmuch_mailstore_rename (notmuch_mailstore_t *mailstore, const char *old_filename,
 			  const char *new_filename);
 
+/* Initialize the mailstore.
+ *
+ * Arguments are dependent on the mailstore.
+ */
+notmuch_status_t
+notmuch_mailstore_construct (notmuch_mailstore_t *mailstore, ...);
+
 /* Create a new query for 'database'.
  *
  * Here, 'database' should be an open database, (see
diff --git a/notmuch-config.c b/notmuch-config.c
index f611b26..99f872d 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -592,8 +592,14 @@ notmuch_config_get_mailstore (notmuch_config_t *config)
      * When there are multiple mailstore types and "constructors" for
      * them, this may have to be much more complicated.
      */
+    notmuch_status_t status;
     const char *type = notmuch_config_get_database_type (config);
-    return notmuch_mailstore_get_by_name (type);
+    notmuch_mailstore_t *mailstore = notmuch_mailstore_get_by_name (type);
+    status = notmuch_mailstore_construct (mailstore);
+    if (status != NOTMUCH_STATUS_SUCCESS) {
+	/* abort messily? */
+    }
+    return mailstore;
 }
 
 const char *
-- 
1.7.5.4



More information about the notmuch mailing list