[PATCH v2 5/8] notmuch-new: pull out useful bits of add_files_recursive
authorEthan Glasser-Camp <ethan.glasser.camp@gmail.com>
Sun, 1 Jul 2012 16:39:47 +0000 (12:39 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:55 +0000 (09:47 -0800)
ba/1ba25a640ae9d4756f796b4fb88cb20ae26408 [new file with mode: 0644]

diff --git a/ba/1ba25a640ae9d4756f796b4fb88cb20ae26408 b/ba/1ba25a640ae9d4756f796b4fb88cb20ae26408
new file mode 100644 (file)
index 0000000..d89757c
--- /dev/null
@@ -0,0 +1,311 @@
+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 8E8FF431FBC\r
+       for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 09:46:26 -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 I+E0g9gaJq2d for <notmuch@notmuchmail.org>;\r
+       Sun,  1 Jul 2012 09:46:25 -0700 (PDT)\r
+Received: from mail-wg0-f41.google.com (mail-wg0-f41.google.com\r
+ [74.125.82.41])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 16B1F431FAF   for <notmuch@notmuchmail.org>; Sun,  1 Jul 2012 09:46:24 -0700\r
+ (PDT)\r
+Received: by wgbds1 with SMTP id ds1so2232210wgb.2\r
+       for <notmuch@notmuchmail.org>; Sun, 01 Jul 2012 09:46:23 -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=kVMM807Ap9n6I8OuylbnndHJh8sh447voBoKA/GofhM=;\r
+       b=rhHYaX62pp+1q6eXlC0g4xQysh8skL23Aua3WJF9iPGOWTSu9MSLoz2QYELtvLCOUk\r
+       wM3BtIWacfEfx2tqCLhiGO2EamNIYd23GKTxSX3khuc/B1NBbd6B/fMyxiBOHzVO5x0a\r
+       Y3GqDh+nF9wx+xvAglJwEZmtpmNJL6kOMUXq5X2UJD+v3xffmThb2Q8n+YjMgU5AhMpT\r
+       3666EAInl3UzFL45vWT7nuIH/+d/1PXzohBlI1OaHzHbN0r3RVFEn77E5YwAiQCZ78GP\r
+       e9lzV1G2dYg43YLbA8KhjyrLDTR9q3baP5ee2bUnX4kKmljD8k4EdFItgwzn1Fija4Ba\r
+       fE/w==\r
+Received: by 10.180.109.129 with SMTP id hs1mr11249730wib.0.1341161183713;\r
+       Sun, 01 Jul 2012 09:46:23 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+       by mx.google.com with ESMTPS id ch9sm35160214wib.8.2012.07.01.09.46.16\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Sun, 01 Jul 2012 09:46:22 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 5/8] notmuch-new: pull out useful bits of\r
+       add_files_recursive\r
+Date: Sun,  1 Jul 2012 12:39:47 -0400\r
+Message-Id: <1341160790-14525-6-git-send-email-ethan@betacantrips.com>\r
+X-Mailer: git-send-email 1.7.9.5\r
+In-Reply-To: <1341160790-14525-1-git-send-email-ethan@betacantrips.com>\r
+References:\r
+ <CAOJ+Ob0MSOez2MvD2fCgF7t32kFPk4g2+xCud88QmBLt_b5pOA@mail.gmail.com>\r
+       <1341160790-14525-1-git-send-email-ethan@betacantrips.com>\r
+Cc: Ethan Glasser-Camp <ethan@betacantrips.com>\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: Sun, 01 Jul 2012 16:46:26 -0000\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
+No functional changes.\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 8377750..5250562 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -239,6 +239,122 @@ _entry_in_ignore_list (const char *entry, add_files_state_t *state)\r
+     return FALSE;\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
+@@ -290,7 +406,6 @@ add_files (notmuch_database_t *notmuch,\r
+     char *next = 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
+@@ -299,7 +414,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
+@@ -469,83 +583,15 @@ add_files (notmuch_database_t *notmuch,\r
+        * in the database, so add it. */\r
+       next = talloc_asprintf (notmuch, "%s/%s", path, 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