--- /dev/null
+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 643C9431FDB\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:43:21 -0700 (PDT)\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 IWvkh1C+KgNc for <notmuch@notmuchmail.org>;\r
+ Tue, 2 Apr 2013 15:43:17 -0700 (PDT)\r
+Received: from mail-da0-f53.google.com (mail-da0-f53.google.com\r
+ [209.85.210.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 9F697431FBD\r
+ for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:43:08 -0700 (PDT)\r
+Received: by mail-da0-f53.google.com with SMTP id n34so379344dal.26\r
+ for <notmuch@notmuchmail.org>; Tue, 02 Apr 2013 15:43:07 -0700 (PDT)\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=az4pLuANKfNkjKQTXi6FMnSfcEs4B0zDJjbLCrVTd4Y=;\r
+ b=wMzeKtJh3oxPyxli4TXdsKxljbNfTBjLGSEOsdt5Np1G7EMRSCjouwQ7b8+Ezde8UV\r
+ QFzIeWehwlkWy7czuDxkV3Ye9hTRauZdsQMrJzk2Dez20nJZ1vNLVy6+wP8IcP9bPUwb\r
+ 6x1fw1aiGpivkQsBv6ogMBJ/yf13vr7KfPgeel1lSz+Rb700WKn+gaEoIWTtLLlaLHjX\r
+ 0nZSOxSm9y9vCJO/9QT6V3VyZa7Jy3unsKaLwAYb1v5VM7j+RMqHJa8rZUVLcV9nKl9T\r
+ 9KRYpCMQ5tXEARqdFdY9jXQjJIf/9uE4Aegrd//1SDRl2/NjvAsRuLuI7w2No15HsJ0O\r
+ RaTw==\r
+X-Received: by 10.68.87.98 with SMTP id w2mr26748872pbz.190.1364942587840;\r
+ Tue, 02 Apr 2013 15:43:07 -0700 (PDT)\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 rt13sm4015068pac.14.2013.04.02.15.43.04\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Tue, 02 Apr 2013 15:43:07 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v5 10/12] insert: add --create-folder option\r
+Date: Wed, 3 Apr 2013 09:41:55 +1100\r
+Message-Id: <1364942517-6982-11-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1364942517-6982-1-git-send-email-novalazy@gmail.com>\r
+References: <1364942517-6982-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: Tue, 02 Apr 2013 22:43:21 -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 778ac04..8ae5dc9 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -100,6 +100,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
+@@ -305,6 +398,7 @@ notmuch_insert_command (notmuch_config_t *config, 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
+@@ -312,6 +406,7 @@ notmuch_insert_command (notmuch_config_t *config, 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
+@@ -357,6 +452,11 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ fprintf (stderr, "Out of memory\n");\r
+ return 1;\r
+ }\r
++ if (create_folder && ! maildir_create_folder (config, 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