[RFC PATCH 14/14] new: Add scan support for mbox:// URIs
authorEthan Glasser-Camp <ethan.glasser.camp@gmail.com>
Mon, 25 Jun 2012 20:51:57 +0000 (16:51 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:47:50 +0000 (09:47 -0800)
f6/ab2e565614d5ea55a69f0001e7bb487b137f34 [new file with mode: 0644]

diff --git a/f6/ab2e565614d5ea55a69f0001e7bb487b137f34 b/f6/ab2e565614d5ea55a69f0001e7bb487b137f34
new file mode 100644 (file)
index 0000000..cda76d3
--- /dev/null
@@ -0,0 +1,398 @@
+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 9DD71431FB6\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:55:14 -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 NgwX7iUdAUdb for <notmuch@notmuchmail.org>;\r
+       Mon, 25 Jun 2012 13:55:13 -0700 (PDT)\r
+Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com\r
+ [74.125.82.45])       (using TLSv1 with cipher RC4-SHA (128/128 bits))        (No client\r
+ certificate requested)        by olra.theworths.org (Postfix) with ESMTPS id\r
+ 4949F431FAF   for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:55:13 -0700\r
+ (PDT)\r
+Received: by wgbdt14 with SMTP id dt14so3711317wgb.2\r
+       for <notmuch@notmuchmail.org>; Mon, 25 Jun 2012 13:55:12 -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=5yIyyO80WMJ6eRq9x6aRcGJCgDQstm6+2ZZ67HRJ6p4=;\r
+       b=otFB4H3/+huSv6oaF1MpuP584b3syoWjBAc1d25osDKatgVn1Ln/UIMYN5KQdh11JI\r
+       mzJW0moD61QMEvdeRkWjtwdXrVZ4//HEiMncVAnhZIqo0Ip7WHLwf4AOe+U1JOlywq53\r
+       6/DNQhsxFDKGbWRAu+3GWGYPQKH01iwVxw2w5CGxIxSIB0exWWqDNp7MUXW3XpU8Iqby\r
+       CmzyeW+uXtUmKqtBfBg546AHmpubeKzpuFmTbwIQ0iZSk/FRJFJclxoSiYsKbkt1MARi\r
+       huH8XKsSfWqIww4EQOb7DwhdtpCokBPaQU278e8xZpSGJzHPzUh4Q+Q+IAUx8YYdMmNl\r
+       Hzog==\r
+Received: by 10.180.78.197 with SMTP id d5mr27170402wix.7.1340657712026;\r
+       Mon, 25 Jun 2012 13:55:12 -0700 (PDT)\r
+Received: from localhost ([195.24.209.21])\r
+       by mx.google.com with ESMTPS id fm1sm22630wib.10.2012.06.25.13.54.57\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Mon, 25 Jun 2012 13:55:11 -0700 (PDT)\r
+From: Ethan Glasser-Camp <ethan.glasser.camp@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 14/14] new: Add scan support for mbox:// URIs\r
+Date: Mon, 25 Jun 2012 16:51:57 -0400\r
+Message-Id: <1340657517-6539-10-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:55:14 -0000\r
+\r
+A lot of code is duplicated from maildir, I don't think I handled all\r
+those errors correctly, and I didn't report any progress.\r
+\r
+Signed-off-by: Ethan Glasser-Camp <ethan@betacantrips.com>\r
+---\r
+ notmuch-new.c |  299 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ 1 file changed, 289 insertions(+), 10 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 1bf4e25..36fee34 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -19,6 +19,7 @@\r
+  */\r
+ \r
+ #include "notmuch-client.h"\r
++#include <uriparser/Uri.h>\r
+ \r
+ #include <unistd.h>\r
+ \r
+@@ -239,16 +240,6 @@ _entry_in_ignore_list (const char *entry, add_files_state_t *state)\r
+     return FALSE;\r
+ }\r
+ \r
+-/* Call out to the appropriate add_files function, based on the URI. */\r
+-static notmuch_status_t\r
+-add_files_uri (unused(notmuch_database_t *notmuch),\r
+-             unused(const char *uri),\r
+-             unused(add_files_state_t *state))\r
+-{\r
+-    /* Stub for now */\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
+@@ -674,6 +665,294 @@ add_files (notmuch_database_t *notmuch,\r
+     return ret;\r
+ }\r
+ \r
++/* Scan an mbox file for messages.\r
++ *\r
++ * We assume that mboxes grow monotonically only.\r
++ *\r
++ * The mtime of the mbox file is stored in a "directory" document in\r
++ * Xapian.\r
++ */\r
++static notmuch_status_t\r
++add_messages_mbox_file (notmuch_database_t *notmuch,\r
++                      const char *path,\r
++                      add_files_state_t *state)\r
++{\r
++    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, status;\r
++    struct stat st;\r
++    time_t fs_mtime, db_mtime, stat_time;\r
++    FILE *mbox;\r
++    char *line, *path_uri = NULL, *message_uri = NULL;\r
++    int line_len;\r
++    size_t offset, end_offset, line_size = 0;\r
++    notmuch_directory_t *directory;\r
++    int content_length = -1, is_headers;\r
++\r
++    if (stat (path, &st)) {\r
++      fprintf (stderr, "Error reading mbox file %s: %s\n",\r
++               path, strerror (errno));\r
++      return NOTMUCH_STATUS_FILE_ERROR;\r
++    }\r
++\r
++    stat_time = time (NULL);\r
++    if (! S_ISREG (st.st_mode)) {\r
++      fprintf (stderr, "Error: %s is not a file.\n", path);\r
++      return NOTMUCH_STATUS_FILE_ERROR;\r
++    }\r
++\r
++    fs_mtime = st.st_mtime;\r
++\r
++    path_uri = talloc_asprintf (notmuch, "mbox://%s", path);\r
++    status = notmuch_database_get_directory (notmuch, path_uri, &directory);\r
++    if (status) {\r
++      ret = status;\r
++      goto DONE;\r
++    }\r
++    db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0;\r
++\r
++    if (directory && db_mtime == fs_mtime) {\r
++      goto DONE;\r
++    }\r
++\r
++    mbox = fopen (path, "r");\r
++    if (mbox == NULL) {\r
++      fprintf (stderr, "Error: couldn't open %s for reading.\n",\r
++               path);\r
++      ret = NOTMUCH_STATUS_FILE_ERROR;\r
++      goto DONE;\r
++    }\r
++\r
++    line_len = getline (&line, &line_size, mbox);\r
++\r
++    if (line_len == -1) {\r
++      fprintf (stderr, "Error: reading from %s failed: %s\n",\r
++               path, strerror (errno));\r
++      ret = NOTMUCH_STATUS_FILE_ERROR;\r
++      goto DONE;\r
++    }\r
++\r
++    if (strncmp (line, "From ", 5) != 0) {\r
++      fprintf (stderr, "Note: Ignoring non-mbox file: %s\n",\r
++               path);\r
++      ret = NOTMUCH_STATUS_FILE_ERROR;\r
++      goto DONE;\r
++    }\r
++    free(line);\r
++    line = NULL;\r
++\r
++    /* Loop invariant: At the beginning of the loop, we have just read\r
++     * a From_ line, but haven't yet read any of the headers.\r
++     */\r
++    while (! feof (mbox)) {\r
++      is_headers = 1;\r
++      offset = ftell (mbox);\r
++      content_length = -1;\r
++\r
++      /* Read lines until we either get to the next From_ header, or\r
++       * we find a Content-Length header (mboxcl) and we run out of headers.\r
++       */\r
++      do {\r
++          /* Get the offset before we read, in case we got another From_ header. */\r
++          end_offset = ftell (mbox);\r
++\r
++          line_len = getline (&line, &line_size, mbox);\r
++\r
++          /* Check to see if this line is a content-length header,\r
++           * or the end of the headers. */\r
++          if (is_headers && strncasecmp (line, "Content-Length: ",\r
++                                         strlen ("Content-Length: ")) == 0)\r
++              content_length = strtol (line + strlen ("Content-Length: "),\r
++                                       NULL, 10);\r
++\r
++          if (is_headers && strlen (line) == 1 && *line == '\n') {\r
++              is_headers = 0;\r
++              /* If we got a content_length, skip the message body. */\r
++              if (content_length != -1) {\r
++                  fseek (mbox, content_length, SEEK_CUR);\r
++                  line_len = getline (&line, &line_size, mbox);\r
++\r
++                  /* We should be at the end of the message.  Sanity\r
++                   * check: there should be a blank line, and then\r
++                   * another From_ header. */\r
++                  if (strlen (line) != 1 || *line != '\n') {\r
++                      fprintf (stderr, "Warning: message with Content-Length not "\r
++                               "immediately followed by blank line (%d)\n", offset);\r
++                  }\r
++\r
++                  end_offset = ftell (mbox);\r
++                  line_len = getline (&line, &line_size, mbox);\r
++\r
++                  if (line_len != -1 && strncmp (line, "From ", 5) != 0) {\r
++                      fprintf (stderr, "Warning: message with Content-Length not "\r
++                               "immediately followed by another message (%d)\n", offset);\r
++                      fprintf (stderr, "Line was: %s", line);\r
++                  }\r
++              }\r
++          }\r
++\r
++      } while (! feof (mbox) && strncmp (line, "From ", 5) != 0);\r
++\r
++      /* end_offset is now after the \n but before the From_. */\r
++      message_uri = talloc_asprintf (notmuch, "mbox://%s#%d+%d",\r
++                                     path, offset, (end_offset - 1) - offset);\r
++      status = _add_message (state, notmuch, message_uri);\r
++      talloc_free (message_uri);\r
++      message_uri = NULL;\r
++    }\r
++\r
++    if (fs_mtime != stat_time)\r
++      _filename_list_add (state->directory_mtimes, path_uri)->mtime = fs_mtime;\r
++\r
++DONE:\r
++    if (line)\r
++      free (line);\r
++    if (path_uri)\r
++      talloc_free (path_uri);\r
++    if (message_uri)\r
++      talloc_free (message_uri);\r
++    if (directory)\r
++      notmuch_directory_destroy (directory);\r
++\r
++    return ret;\r
++}\r
++\r
++/*\r
++ * Examine path recursively as follows:\r
++ *\r
++ * - Recurse on each subdirectory, as in add_files.\r
++ *\r
++ * - Call add_messages_mbox_file on every non-directory.\r
++ */\r
++static notmuch_status_t\r
++add_files_mbox (notmuch_database_t *notmuch,\r
++              const char *path,\r
++              add_files_state_t *state)\r
++{\r
++    struct dirent **fs_entries = NULL;\r
++    struct dirent *entry = NULL;\r
++    char *next = NULL;\r
++    int num_fs_entries = 0, i, entry_type;\r
++    struct stat st;\r
++    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, status;\r
++\r
++    if (stat (path, &st)) {\r
++      fprintf (stderr, "Error reading directory %s: %s\n",\r
++               path, strerror (errno));\r
++      return NOTMUCH_STATUS_FILE_ERROR;\r
++    }\r
++\r
++    num_fs_entries = scandir (path, &fs_entries, 0,\r
++                            dirent_sort_inode);\r
++\r
++    if (num_fs_entries == -1) {\r
++      fprintf (stderr, "Error opening directory %s: %s\n",\r
++               path, strerror (errno));\r
++      ret = NOTMUCH_STATUS_FILE_ERROR;\r
++      goto DONE;\r
++    }\r
++\r
++    for (i = 0; i < num_fs_entries; i++) {\r
++      if (interrupted)\r
++          break;\r
++\r
++      entry = fs_entries[i];\r
++\r
++      entry_type = dirent_type (path, entry);\r
++      if (entry_type == -1) {\r
++          /* Be pessimistic, e.g. so we don't lose lots of mail just\r
++           * because a user broke a symlink. */\r
++          fprintf (stderr, "Error reading file %s/%s: %s\n",\r
++                   path, entry->d_name, strerror (errno));\r
++          return NOTMUCH_STATUS_FILE_ERROR;\r
++      } else if (entry_type != S_IFDIR) {\r
++          continue;\r
++      }\r
++\r
++      /* Ignore special directories to avoid infinite recursion.\r
++       * Also ignore the .notmuch directory, any "tmp" directory\r
++       * that appears within a maildir and files/directories\r
++       * the user has configured to be ignored.\r
++       */\r
++      if (strcmp (entry->d_name, ".") == 0 ||\r
++          strcmp (entry->d_name, "..") == 0 ||\r
++          strcmp (entry->d_name, ".notmuch") == 0 ||\r
++          _entry_in_ignore_list (entry->d_name, state))\r
++      {\r
++          continue;\r
++      }\r
++\r
++      next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
++      status = add_files_mbox (notmuch, next, state);\r
++      if (status) {\r
++          ret = status;\r
++          goto DONE;\r
++      }\r
++      talloc_free (next);\r
++      next = NULL;\r
++    }\r
++\r
++    /* Pass 2: Scan for new files, removed files, and removed directories. */\r
++    for (i = 0; i < num_fs_entries; i++)\r
++    {\r
++      if (interrupted)\r
++          break;\r
++\r
++        entry = fs_entries[i];\r
++\r
++      /* Ignore files & directories user has configured to be ignored */\r
++      if (_entry_in_ignore_list (entry->d_name, state))\r
++          continue;\r
++\r
++      /* Only add regular files (and symlinks to regular files). */\r
++      entry_type = dirent_type (path, entry);\r
++      if (entry_type == -1) {\r
++          fprintf (stderr, "Error reading file %s/%s: %s\n",\r
++                   path, entry->d_name, strerror (errno));\r
++          return NOTMUCH_STATUS_FILE_ERROR;\r
++      } else if (entry_type != S_IFREG) {\r
++          continue;\r
++      }\r
++\r
++      next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);\r
++      status = add_messages_mbox_file (notmuch, next, state);\r
++      talloc_free (next);\r
++      next = NULL;\r
++\r
++      if (status) {\r
++          ret = status;\r
++          goto DONE;\r
++      }\r
++    }\r
++\r
++DONE:\r
++    if (next)\r
++      talloc_free (next);\r
++    return ret;\r
++}\r
++\r
++/* Call out to the appropriate add_files function, based on the URI. */\r
++static notmuch_status_t\r
++add_files_uri (notmuch_database_t *notmuch,\r
++             const char *uri,\r
++             add_files_state_t *state)\r
++{\r
++    UriUriA parsed;\r
++    UriParserStateA parser;\r
++    notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
++    parser.uri = &parsed;\r
++    if (uriParseUriA (&parser, uri) != URI_SUCCESS)\r
++      goto DONE;\r
++\r
++    if (strncmp (parsed.scheme.first, "mbox",\r
++               parsed.scheme.afterLast - parsed.scheme.first) == 0) {\r
++      ret = add_files_mbox (notmuch, parsed.pathHead->text.first - 1, state);\r
++      goto DONE;\r
++    }\r
++\r
++DONE:\r
++    uriFreeUriMembersA (&parsed);\r
++    return ret;\r
++}\r
++\r
+ static void\r
+ setup_progress_printing_timer (void)\r
+ {\r
+-- \r
+1.7.9.5\r
+\r