--- /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 A9BC1431FC0\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:25:38 -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 er+1xHe7a6Dm for <notmuch@notmuchmail.org>;\r
+ Sat, 22 Jun 2013 21:25:33 -0700 (PDT)\r
+Received: from mail-pb0-f44.google.com (mail-pb0-f44.google.com\r
+ [209.85.160.44]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id BC9EA429E27\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:48 -0700 (PDT)\r
+Received: by mail-pb0-f44.google.com with SMTP id uo1so9534267pbc.31\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Jun 2013 21:24:48 -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=qS2cXxe91dFezu3yAB8P4nKJrfVOwE9mUljEiB+ZXqU=;\r
+ b=EGOId9U/S3cyUq0F4HonoS6voioG47JOxShSrxRhfyYOH7ZsGBuZ/5Gce70weiCh44\r
+ tD3PLLHugUg4WmVhE3nXqE3rqi+Ovd5dujuhIzTSuXGYtIf7E8k8qXOsg+izj1FusI1q\r
+ Pc8CgbrjRHekRWUQK8ZKGXKM+Xy4+qFJOD82ShXMVsP5Mvmr2l0tzZlEy+kXpp9bX1KT\r
+ kE8nJuZLzt0FqVuRxsvCqywLhyHLNnl8BW0sA7QZjySuddJU0ZuUKNjRQ+MjTbxMVARa\r
+ n51s8PaHWB6LUW3j6IA2WTV6RvlBsqHe1mpTpj1ZhiEtghoCwUb0sTePm4Es9I+LraM/\r
+ 0GoQ==\r
+X-Received: by 10.66.102.70 with SMTP id fm6mr22202082pab.78.1371961488073;\r
+ Sat, 22 Jun 2013 21:24:48 -0700 (PDT)\r
+Received: from localhost (215.42.233.220.static.exetel.com.au.\r
+ [220.233.42.215]) by mx.google.com with ESMTPSA id\r
+ dj5sm11850307pbc.25.2013.06.22.21.24.45 for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 22 Jun 2013 21:24:47 -0700 (PDT)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v7 10/12] insert: add --create-folder option\r
+Date: Sun, 23 Jun 2013 14:24:03 +1000\r
+Message-Id: <1371961445-15182-11-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1371961445-15182-1-git-send-email-novalazy@gmail.com>\r
+References: <1371961445-15182-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: Sun, 23 Jun 2013 04:25:38 -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 5a4b3ea..8d0235d 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 'tmp' sub-directory of dir.\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