[RFC PATCH 11/14] notmuch-new: pull out useful bits of add_files_recursive
authorEthan Glasser-Camp <ethan.glasser.camp@gmail.com>
Mon, 25 Jun 2012 20:51:54 +0000 (16:51 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:49 +0000 (09:47 -0800)
a6/7cd38bbc89187bf74aecf31f51a4699f815b38 [new file with mode: 0644]

diff --git a/a6/7cd38bbc89187bf74aecf31f51a4699f815b38 b/a6/7cd38bbc89187bf74aecf31f51a4699f815b38
new file mode 100644 (file)
index 0000000..e375018
--- /dev/null
@@ -0,0 +1,315 @@
+Return-Path: <ethan.glasser.camp@gmail.com>\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 1C210431FB6\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:08 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.061\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.061 tagged_above=-999 required=5\r
+       tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+       FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.246,\r
+       RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_SORBS_WEB=0.614] 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 1mh-z2GsTlVk for <notmuch@notmuchmail.org>;\r
+       Mon, 25 Jun 2012 13:54:07 -0700 (PDT)\r
+Received: from mail-we0-f181.google.com (mail-we0-f181.google.com\r
+       [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 09637431FAF\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:06 -0700 (PDT)\r
+Received: by werj55 with SMTP id j55so3649559wer.26\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:54:05 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+       h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+       bh=nrU9nVmzJXJsXKqS8m11Z17v5gUa/3MWhEq27JiKUS0=;\r
+       b=06M9rpHV51pq0hSDUnstkj43T96lpndJYsr7E4fkp9ggg47Cp1277Rs7O7Zu4gO0DC\r
+       L6D7d1TdJztUdDfVcBItE86vII7fymd9GecWXTJ3JRBu87qq5ACbwnXBbGdrYkAA2FwX\r
+       ef33s8OZ4w3CvknGX55pWvnQsgeewwGZzyr3ONT5sdV+qg69wgxEi575UFTOl8Y+1h9M\r
+       WhfK3Z/jNHZx1dl4KWBRY8nPLKJr9ndIwvY8+EjOVUtI3HFK/DKPlqnylUrYPksaCU6c\r
+       p3isfgN7zSTp9hCeQ90zLZRjfFN7TCWYmLuiOwZNMljTiWH0R7uPFaq1qVDiAR2s3/VW\r
+       Ga0A==\r
+Received: by 10.180.24.39 with SMTP id r7mr27675594wif.9.1340657645740;\r
+       Mon, 25 Jun 2012 13:54:05 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+       by mx.google.com with ESMTPS id t8sm34175wiy.3.2012.06.25.13.54.00\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Mon, 25 Jun 2012 13:54:04 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 11/14] notmuch-new: pull out useful bits of\r
+       add_files_recursive\r
+Date: Mon, 25 Jun 2012 16:51:54 -0400\r
+Message-Id: <1340657517-6539-7-git-send-email-ethan@betacantrips.com>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To: <1340657517-6539-1-git-send-email-ethan@betacantrips.com>\r
+References: <1340657517-6539-1-git-send-email-ethan@betacantrips.com>\r
+X-Mailman-Approved-At: Tue, 26 Jun 2012 03:51:54 -0700\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: Mon, 25 Jun 2012 20:54:08 -0000\r
+\r
+This is part of notmuch-new refactor phase 1: make add_files stuff\r
+safe for other backends. add_files_recursive is essentially a\r
+maildir-crawling function that periodically adds files to the database\r
+or adds filenames to remove_files or remove_directory lists. I don't\r
+see an easy way to adapt add_files_recursive for other backends who\r
+might not have concepts of directories with other directories inside\r
+of them, so instead just provide an add_files method for each backend.\r
+\r
+This patch pulls some bits out of add_files_recursive which will be\r
+useful for other backends: two reporting functions\r
+_report_before_adding_file and _report_added_file, as well as\r
+_add_message, which actually does the message adding.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ notmuch-new.c |  192 +++++++++++++++++++++++++++++++++++----------------------\r
+ 1 file changed, 119 insertions(+), 73 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 57b27bf..1bf4e25 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -249,6 +249,122 @@ add_files_uri (unused(notmuch_database_t *notmuch),\r
+     return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
++/* Progress-reporting function.\r
++ *\r
++ * Can be used by any mailstore-crawling function that wants to alert\r
++ * users what message it's about to add. Subsequent errors will be due\r
++ * to this message ;)\r
++ */\r
++static void\r
++_report_before_adding_file (add_files_state_t *state, const char *filename)\r
++{\r
++    state->processed_files++;\r
++\r
++    if (state->verbose) {\r
++      if (state->output_is_a_tty)\r
++          printf("\r\033[K");\r
++\r
++      printf ("%i/%i: %s",\r
++              state->processed_files,\r
++              state->total_files,\r
++              filename);\r
++\r
++      putchar((state->output_is_a_tty) ? '\r' : '\n');\r
++      fflush (stdout);\r
++    }\r
++}\r
++\r
++/* Progress-reporting function.\r
++ *\r
++ * Call this to respond to the signal handler for SIGALRM.\r
++ */\r
++static void\r
++_report_added_file (add_files_state_t *state)\r
++{\r
++    if (do_print_progress) {\r
++      do_print_progress = 0;\r
++      generic_print_progress ("Processed", "files", state->tv_start,\r
++                              state->processed_files, state->total_files);\r
++    }\r
++}\r
++\r
++\r
++/* Atomically handles adding a message to the database.\r
++ *\r
++ * Should be used by any mailstore-crawling function that finds a new\r
++ * message to add.\r
++ */\r
++static notmuch_status_t\r
++_add_message (add_files_state_t *state, notmuch_database_t *notmuch,\r
++            const char *filename)\r
++{\r
++    notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
++    notmuch_message_t *message;\r
++    const char **tag;\r
++\r
++    status = notmuch_database_begin_atomic (notmuch);\r
++    if (status) {\r
++      ret = status;\r
++      goto DONE;\r
++    }\r
++\r
++    status = notmuch_database_add_message (notmuch, filename, &message);\r
++\r
++    switch (status) {\r
++    /* success */\r
++    case NOTMUCH_STATUS_SUCCESS:\r
++      state->added_messages++;\r
++      notmuch_message_freeze (message);\r
++      for (tag=state->new_tags; *tag != NULL; tag++)\r
++          notmuch_message_add_tag (message, *tag);\r
++      if (state->synchronize_flags == TRUE)\r
++          notmuch_message_maildir_flags_to_tags (message);\r
++      notmuch_message_thaw (message);\r
++      break;\r
++    /* Non-fatal issues (go on to next file) */\r
++    case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
++      if (state->synchronize_flags == TRUE)\r
++          notmuch_message_maildir_flags_to_tags (message);\r
++      break;\r
++    case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
++      fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
++               filename);\r
++      break;\r
++    /* Fatal issues. Don't process anymore. */\r
++    case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
++    case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
++    case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
++      fprintf (stderr, "Error: %s. Halting processing.\n",\r
++               notmuch_status_to_string (status));\r
++      ret = status;\r
++      goto DONE;\r
++    default:\r
++    case NOTMUCH_STATUS_FILE_ERROR:\r
++    case NOTMUCH_STATUS_NULL_POINTER:\r
++    case NOTMUCH_STATUS_TAG_TOO_LONG:\r
++    case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
++    case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
++    case NOTMUCH_STATUS_LAST_STATUS:\r
++      INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);\r
++      ret = status;\r
++      goto DONE;\r
++    }\r
++\r
++    status = notmuch_database_end_atomic (notmuch);\r
++    if (status) {\r
++      ret = status;\r
++      goto DONE;\r
++    }\r
++\r
++  DONE:\r
++    if (message) {\r
++      notmuch_message_destroy (message);\r
++      message = NULL;\r
++    }\r
++\r
++    return ret;\r
++}\r
++\r
+ /* Examine 'path' recursively as follows:\r
+  *\r
+  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)\r
+@@ -300,7 +416,6 @@ add_files (notmuch_database_t *notmuch,\r
+     char *next = NULL, *path_uri = NULL;\r
+     time_t fs_mtime, db_mtime;\r
+     notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
+-    notmuch_message_t *message = NULL;\r
+     struct dirent **fs_entries = NULL;\r
+     int i, num_fs_entries = 0, entry_type;\r
+     notmuch_directory_t *directory;\r
+@@ -309,7 +424,6 @@ add_files (notmuch_database_t *notmuch,\r
+     time_t stat_time;\r
+     struct stat st;\r
+     notmuch_bool_t is_maildir;\r
+-    const char **tag;\r
+ \r
+     if (stat (path, &st)) {\r
+       fprintf (stderr, "Error reading directory %s: %s\n",\r
+@@ -488,83 +602,15 @@ add_files (notmuch_database_t *notmuch,\r
+        * in the database, so add it. */\r
+       next = talloc_asprintf (notmuch, "%s/%s", path_uri, entry->d_name);\r
+ \r
+-      state->processed_files++;\r
+-\r
+-      if (state->verbose) {\r
+-          if (state->output_is_a_tty)\r
+-              printf("\r\033[K");\r
+-\r
+-          printf ("%i/%i: %s",\r
+-                  state->processed_files,\r
+-                  state->total_files,\r
+-                  next);\r
+-\r
+-          putchar((state->output_is_a_tty) ? '\r' : '\n');\r
+-          fflush (stdout);\r
+-      }\r
++      _report_before_adding_file (state, next);\r
+ \r
+-      status = notmuch_database_begin_atomic (notmuch);\r
++      status = _add_message (state, notmuch, next);\r
+       if (status) {\r
+           ret = status;\r
+           goto DONE;\r
+       }\r
+ \r
+-      status = notmuch_database_add_message (notmuch, next, &message);\r
+-      switch (status) {\r
+-      /* success */\r
+-      case NOTMUCH_STATUS_SUCCESS:\r
+-          state->added_messages++;\r
+-          notmuch_message_freeze (message);\r
+-          for (tag=state->new_tags; *tag != NULL; tag++)\r
+-              notmuch_message_add_tag (message, *tag);\r
+-          if (state->synchronize_flags == TRUE)\r
+-              notmuch_message_maildir_flags_to_tags (message);\r
+-          notmuch_message_thaw (message);\r
+-          break;\r
+-      /* Non-fatal issues (go on to next file) */\r
+-      case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:\r
+-          if (state->synchronize_flags == TRUE)\r
+-              notmuch_message_maildir_flags_to_tags (message);\r
+-          break;\r
+-      case NOTMUCH_STATUS_FILE_NOT_EMAIL:\r
+-          fprintf (stderr, "Note: Ignoring non-mail file: %s\n",\r
+-                   next);\r
+-          break;\r
+-      /* Fatal issues. Don't process anymore. */\r
+-      case NOTMUCH_STATUS_READ_ONLY_DATABASE:\r
+-      case NOTMUCH_STATUS_XAPIAN_EXCEPTION:\r
+-      case NOTMUCH_STATUS_OUT_OF_MEMORY:\r
+-          fprintf (stderr, "Error: %s. Halting processing.\n",\r
+-                   notmuch_status_to_string (status));\r
+-          ret = status;\r
+-          goto DONE;\r
+-      default:\r
+-      case NOTMUCH_STATUS_FILE_ERROR:\r
+-      case NOTMUCH_STATUS_NULL_POINTER:\r
+-      case NOTMUCH_STATUS_TAG_TOO_LONG:\r
+-      case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:\r
+-      case NOTMUCH_STATUS_UNBALANCED_ATOMIC:\r
+-      case NOTMUCH_STATUS_LAST_STATUS:\r
+-          INTERNAL_ERROR ("add_message returned unexpected value: %d",  status);\r
+-          goto DONE;\r
+-      }\r
+-\r
+-      status = notmuch_database_end_atomic (notmuch);\r
+-      if (status) {\r
+-          ret = status;\r
+-          goto DONE;\r
+-      }\r
+-\r
+-      if (message) {\r
+-          notmuch_message_destroy (message);\r
+-          message = NULL;\r
+-      }\r
+-\r
+-      if (do_print_progress) {\r
+-          do_print_progress = 0;\r
+-          generic_print_progress ("Processed", "files", state->tv_start,\r
+-                                  state->processed_files, state->total_files);\r
+-      }\r
++      _report_added_file (state);\r
+ \r
+       talloc_free (next);\r
+       next = NULL;\r
+-- \r
+1.7.9.5\r
+\r