--- /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 17F00431FBD\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:54:43 -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 SzGkrtxcB5HN for <notmuch@notmuchmail.org>;\r
+ Fri, 21 Jun 2013 22:54:35 -0700 (PDT)\r
+Received: from mail-pa0-f48.google.com (mail-pa0-f48.google.com\r
+ [209.85.220.48]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 7F1D9431FAE\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:54:35 -0700 (PDT)\r
+Received: by mail-pa0-f48.google.com with SMTP id kp12so8748553pab.35\r
+ for <notmuch@notmuchmail.org>; Fri, 21 Jun 2013 22:54:34 -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=g2H80/PuFFSB1PFtZQL3zjGorrJhMiGUV1WiUmbhnBE=;\r
+ b=Pt7XxLjbKIjXy6gcoRIb53n/P/z5h8CbAVYe3u3lQSX8K4TExLrxGsW+mt75tApPMy\r
+ GPEZSzIJiLwGVRPFpu8cAL8ImOfc2D+D9m/WvGC+xfRw8kd3AzWMCtZMaBz7gQb03y0D\r
+ 5fm2qlNI6ROIed6nmfDmzpcPY/xEX33mcJltYDS+NAPlhXuDIbvmqclsCeSSsKEaIS/2\r
+ BLTWxpSTnsTt8NGXUtBEiZSLudVjJj7zAS0ptFHRG+5poCAf//b7NMlztDv7RcG6kO8P\r
+ KGhyryzxtVhV4NwquZ/X0v6oglYPvpSy6DVq1+M8Uo/ZfKan/S2NQSQloCsm+aHD4WM4\r
+ QkZw==\r
+X-Received: by 10.66.161.41 with SMTP id xp9mr19351165pab.166.1371880066032;\r
+ Fri, 21 Jun 2013 22:47:46 -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 ESMTPSA id xz1sm8947990pab.5.2013.06.21.22.47.43\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Fri, 21 Jun 2013 22:47:45 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v6 10/12] insert: add --create-folder option\r
+Date: Sat, 22 Jun 2013 15:46:46 +1000\r
+Message-Id: <1371880008-18312-11-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1371880008-18312-1-git-send-email-novalazy@gmail.com>\r
+References: <1371880008-18312-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: Sat, 22 Jun 2013 05:54:43 -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 a6134a8..25df3be 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
+@@ -304,6 +397,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
+@@ -311,6 +405,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
+@@ -355,6 +450,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