[RFC PATCH 08/13] count_files and add_files shall be generic

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


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

Rename current count_files and add_files to maildir_count_files and
maildir_add_files. This allows the possibility, at least, of having
other backends.

Signed-off-by: Ethan Glasser-Camp <ethan at betacantrips.com>
---
 notmuch-new.c |   62 +++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 50 insertions(+), 12 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index dbdfbb6..d30fba1 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -340,9 +340,9 @@ _add_message (add_files_state_t *state, notmuch_database_t *notmuch,
  *     if fs_mtime isn't the current wall-clock time.
  */
 static notmuch_status_t
-add_files_recursive (notmuch_database_t *notmuch,
-		     const char *path,
-		     add_files_state_t *state)
+maildir_add_files_recursive (notmuch_database_t *notmuch,
+			     const char *path,
+			     add_files_state_t *state)
 {
     DIR *dir = NULL;
     struct dirent *entry = NULL;
@@ -449,7 +449,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 	}
 
 	next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
-	status = add_files_recursive (notmuch, next, state);
+	status = maildir_add_files_recursive (notmuch, next, state);
 	if (status && ret == NOTMUCH_STATUS_SUCCESS)
 	    ret = status;
 	talloc_free (next);
@@ -663,13 +663,34 @@ stop_progress_printing_timer (void)
     sigaction (SIGALRM, &action, NULL);
 }
 
+static notmuch_status_t
+maildir_add_files (notmuch_database_t *notmuch,
+		   const char *path,
+		   add_files_state_t *state);
+
+/* Dispatch function to call the correct mailstore_add_files
+ * function. */
+static notmuch_status_t
+add_files (notmuch_database_t *notmuch, notmuch_config_t *config,
+	   add_files_state_t *state)
+{
+    const char *path = notmuch_config_get_database_path (config);
+    if (strcmp (notmuch_config_get_database_type (config), "maildir") == 0)
+	return maildir_add_files (notmuch, path, state);
+
+    /* Default case */
+    fprintf (stderr, "Could not add files for mailstore %s: unknown mailstore\n",
+	     notmuch_config_get_database_type (config));
+    /* FIXME: "invalid argument" error code would be nice */
+    return NOTMUCH_STATUS_FILE_ERROR;
+}
 
 /* This is the top-level entry point for add_files. It does a couple
  * of error checks and then calls into the recursive function. */
 static notmuch_status_t
-add_files (notmuch_database_t *notmuch,
-	   const char *path,
-	   add_files_state_t *state)
+maildir_add_files (notmuch_database_t *notmuch,
+		   const char *path,
+		   add_files_state_t *state)
 {
     notmuch_status_t status;
     struct stat st;
@@ -685,11 +706,28 @@ add_files (notmuch_database_t *notmuch,
 	return NOTMUCH_STATUS_FILE_ERROR;
     }
 
-    status = add_files_recursive (notmuch, path, state);
+    status = maildir_add_files_recursive (notmuch, path, state);
 
     return status;
 }
 
+static void
+maildir_count_files (const char *path, int *count);
+
+/* Dispatch function to call the correct mailstore_count_files
+ * function. N.B. This function may get refactored into add_files! */
+static void
+count_files (unused (notmuch_database_t *notmuch), notmuch_config_t *config, int *count)
+{
+    if (strcmp (notmuch_config_get_database_type (config), "maildir") == 0) {
+	maildir_count_files (notmuch_config_get_database_path (config), count);
+	return;
+    }
+
+    /* Eh, screw it, this function shouldn't even exist */
+    *count = 0;
+}
+
 /* XXX: This should be merged with the add_files function since it
  * shares a lot of logic with it. */
 /* Recursively count all regular files in path and all sub-directories
@@ -697,7 +735,7 @@ add_files (notmuch_database_t *notmuch,
  * initialized to zero by the top-level caller before calling
  * count_files). */
 static void
-count_files (const char *path, int *count)
+maildir_count_files (const char *path, int *count)
 {
     struct dirent *entry = NULL;
     char *next;
@@ -746,7 +784,7 @@ count_files (const char *path, int *count)
 		fflush (stdout);
 	    }
 	} else if (S_ISDIR (st.st_mode)) {
-	    count_files (next, count);
+	    maildir_count_files (next, count);
 	}
 
 	free (next);
@@ -905,7 +943,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	int count;
 
 	count = 0;
-	count_files (db_path, &count);
+	count_files (notmuch, config, &count);
 	if (interrupted)
 	    return 1;
 
@@ -962,7 +1000,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
 	timer_is_active = TRUE;
     }
 
-    ret = add_files (notmuch, db_path, &add_files_state);
+    ret = add_files (notmuch, config, &add_files_state);
 
     gettimeofday (&tv_start, NULL);
     for (f = add_files_state.removed_files->head; f && !interrupted; f = f->next) {
-- 
1.7.5.4



More information about the notmuch mailing list