[PATCH v4 10/12] insert: add --create-folder option
authorPeter Wang <novalazy@gmail.com>
Thu, 24 Jan 2013 12:08:06 +0000 (23:08 +1100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:53:22 +0000 (09:53 -0800)
5f/b8654ee7ffe938da7d1ea37730af15a7b5b8c5 [new file with mode: 0644]

diff --git a/5f/b8654ee7ffe938da7d1ea37730af15a7b5b8c5 b/5f/b8654ee7ffe938da7d1ea37730af15a7b5b8c5
new file mode 100644 (file)
index 0000000..8c63921
--- /dev/null
@@ -0,0 +1,203 @@
+Return-Path: <novalazy@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 CCB9A431FDC\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:15 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.799\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.799 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_DNSWL_LOW=-0.7] 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 LB-TgXl-yvFk for <notmuch@notmuchmail.org>;\r
+       Thu, 24 Jan 2013 04:10:14 -0800 (PST)\r
+Received: from mail-pa0-f53.google.com (mail-pa0-f53.google.com\r
+       [209.85.220.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id E6DE2431FDE\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:04 -0800 (PST)\r
+Received: by mail-pa0-f53.google.com with SMTP id hz1so5455330pad.12\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:04 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+       h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to\r
+       :references; bh=3TltoTMAK8jiGFLLoSUX2+bLtr+s39AWl6Kni3z1HHs=;\r
+       b=UDkbICpdhIS3RclkY2e37OH+jI05OTsaOrTpHfIv7Lz/FWpRS1LagCxnTkVVSxBExX\r
+       18CAs1w1K/CMBA8nSo2sMdDz1bRTBtCiyjJU+bM11Yo9OWzKtCgRyyTVVunDoMImf0mV\r
+       +Orvspd2DpI06go2VLU7CkzF2st616oaQj7EuJOvTtdlZ92610Laarfqc1htpgzxlg12\r
+       BhxRzCHzvOhBc3q5WZWg6vDy4z/sMnXdJLoHZoXDeq7ddFSOS1EjT1rbHOYn7fF/TzGl\r
+       RXkeWBh8+St6WiMnXZCopx7YaZXCvgIZVDGZVGc97TXQIsatKcXr4Xivc26WFNfSihXN\r
+       A3hQ==\r
+X-Received: by 10.66.52.102 with SMTP id s6mr4194398pao.6.1359029404142;\r
+       Thu, 24 Jan 2013 04:10:04 -0800 (PST)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+       [220.233.42.215])\r
+       by mx.google.com with ESMTPS id ov4sm14779538pbb.45.2013.01.24.04.10.01\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Thu, 24 Jan 2013 04:10:03 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 10/12] insert: add --create-folder option\r
+Date: Thu, 24 Jan 2013 23:08:06 +1100\r
+Message-Id: <1359029288-12132-11-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1359029288-12132-1-git-send-email-novalazy@gmail.com>\r
+References: <1359029288-12132-1-git-send-email-novalazy@gmail.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: Thu, 24 Jan 2013 12:10:17 -0000\r
+\r
+Allow the insert command to create the maildir folder\r
+into which the new message should be delivered.\r
+---\r
+ notmuch-insert.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 100 insertions(+)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index bc848ff..69329ad 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -102,6 +102,99 @@ check_folder_name (const char *folder)\r
+     }\r
+ }\r
\r
++/* Make the given directory, succeeding if it already exists. */\r
++static notmuch_bool_t\r
++make_directory (char *path, int mode)\r
++{\r
++    notmuch_bool_t ret;\r
++    char *slash;\r
++\r
++    if (mkdir (path, mode) != 0)\r
++      return (errno == EEXIST);\r
++\r
++    /* Sync the parent directory for durability. */\r
++    ret = TRUE;\r
++    slash = strrchr (path, '/');\r
++    if (slash) {\r
++      *slash = '\0';\r
++      ret = sync_dir (path);\r
++      *slash = '/';\r
++    }\r
++    return ret;\r
++}\r
++\r
++/* Make the given directory including its parent directories as necessary.\r
++ * Return TRUE on success, FALSE on error. */\r
++static notmuch_bool_t\r
++make_directory_and_parents (char *path, int mode)\r
++{\r
++    struct stat st;\r
++    char *start;\r
++    char *end;\r
++    notmuch_bool_t ret;\r
++\r
++    /* First check the common case: directory already exists. */\r
++    if (stat (path, &st) == 0)\r
++      return S_ISDIR (st.st_mode) ? TRUE : FALSE;\r
++\r
++    for (start = path; *start != '\0'; start = end + 1) {\r
++      /* start points to the first unprocessed character.\r
++       * Find the next slash from start onwards. */\r
++      end = strchr (start, '/');\r
++\r
++      /* If there are no more slashes then all the parent directories\r
++       * have been made.  Now attempt to make the whole path. */\r
++      if (end == NULL)\r
++          return make_directory (path, mode);\r
++\r
++      /* Make the path up to the next slash, unless the current\r
++       * directory component is actually empty. */\r
++      if (end > start) {\r
++          *end = '\0';\r
++          ret = make_directory (path, mode);\r
++          *end = '/';\r
++          if (! ret)\r
++              return FALSE;\r
++      }\r
++    }\r
++\r
++    return TRUE;\r
++}\r
++\r
++/* Create the given maildir folder, i.e. dir and its subdirectories\r
++ * 'cur', 'new', 'tmp'. */\r
++static notmuch_bool_t\r
++maildir_create_folder (void *ctx, const char *dir)\r
++{\r
++    const int mode = 0700;\r
++    char *subdir;\r
++    char *tail;\r
++\r
++    /* Create 'cur' directory, including parent directories. */\r
++    subdir = talloc_asprintf (ctx, "%s/cur", dir);\r
++    if (! subdir) {\r
++      fprintf (stderr, "Out of memory.\n");\r
++      return FALSE;\r
++    }\r
++    if (! make_directory_and_parents (subdir, mode))\r
++      return FALSE;\r
++\r
++    tail = subdir + strlen (subdir) - 3;\r
++\r
++    /* Create 'new' directory. */\r
++    strcpy (tail, "new");\r
++    if (! make_directory (subdir, mode))\r
++      return FALSE;\r
++\r
++    /* Create 'tmp' directory. */\r
++    strcpy (tail, "tmp");\r
++    if (! make_directory (subdir, mode))\r
++      return FALSE;\r
++\r
++    talloc_free (subdir);\r
++    return TRUE;\r
++}\r
++\r
+ /* Open a unique file in the Maildir 'tmp' directory.\r
+  * Returns the file descriptor on success, or -1 on failure.\r
+  * On success, file paths for the message in the 'tmp' and 'new'\r
+@@ -306,6 +399,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
+     tag_op_list_t *tag_ops;\r
+     char *query_string = NULL;\r
+     const char *folder = NULL;\r
++    notmuch_bool_t create_folder = FALSE;\r
+     const char *maildir;\r
+     int opt_index;\r
+     unsigned int i;\r
+@@ -313,6 +407,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
\r
+     notmuch_opt_desc_t options[] = {\r
+       { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },\r
++      { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },\r
+       { NOTMUCH_OPT_END, 0, 0, 0, 0 }\r
+     };\r
\r
+@@ -362,6 +457,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
+           fprintf (stderr, "Out of memory\n");\r
+           return 1;\r
+       }\r
++      if (create_folder && ! maildir_create_folder (ctx, maildir)) {\r
++          fprintf (stderr, "Error: creating maildir %s: %s\n",\r
++                   maildir, strerror (errno));\r
++          return 1;\r
++      }\r
+     }\r
\r
+     /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
+-- \r
+1.7.12.1\r
+\r