--- /dev/null
+Return-Path: <jani@nikula.org>\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 CD2EA431FC0\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:11 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[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 Xs7z0sPjbAyE for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 02:55:07 -0700 (PDT)\r
+Received: from mail-wi0-f170.google.com (mail-wi0-f170.google.com\r
+ [209.85.212.170]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id C50C0431FDE\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:56 -0700 (PDT)\r
+Received: by mail-wi0-f170.google.com with SMTP id fb4so2486823wid.3\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:55 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=+eJmjEEFeki5WtosF6F2YBxONWtxesmKdqnnEpOzjBk=;\r
+ b=V7RyB2D9NOHfx7axwrDe1aqhWmgpfBAbEhV8kJBNWu3NbUHnMpgO/owi1o5Rh1dRKR\r
+ p2o5F41oFfucEM665JBfKmw/61YOU1QGvXOXxUnHEJ01myngkxvwbPJ3SAnkWLXmG3Ad\r
+ nx8tnqMh6U6UYCaU1NtaxsiFwfunPI0mR9U5RrOcTl+UZLWb1e3uGEuGP4PrAnxJG9UU\r
+ GdtCZaF7MCftKJ/ziLR+sr5Z3jE8ddhZO6LTv6/lrLDApCFSlpHOdAxXckROyqx1zQGz\r
+ WA+NrDR6vCbIOt+SFQgGqKXzZp7abV4HLDmnjCAlrHvJt/obxKbwORmPjsewvS9TTOWk\r
+ Q+AQ==\r
+X-Gm-Message-State:\r
+ ALoCoQlzw4D32NBOxTVZciiM1YX08srcr/bLmrCMk09EjNkT0Nukcy8L8zWiuh841xBB2kBFDwa7\r
+X-Received: by 10.180.75.143 with SMTP id c15mr14512427wiw.31.1411379695711;\r
+ Mon, 22 Sep 2014 02:54:55 -0700 (PDT)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+ by mx.google.com with ESMTPSA id\r
+ ky3sm11695685wjb.39.2014.09.22.02.54.55 for <multiple recipients>\r
+ (version=TLSv1.1 cipher=RC4-SHA bits=128/128);\r
+ Mon, 22 Sep 2014 02:54:55 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 06/11] cli/insert: use a single recursive mkdir function\r
+Date: Mon, 22 Sep 2014 11:54:57 +0200\r
+Message-Id:\r
+ <3059bb1ef1dede24e28d9827082ca36c8e6c5996.1411379395.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.2.5\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.git.jani@nikula.org>\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.git.jani@nikula.org>\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, 22 Sep 2014 09:55:12 -0000\r
+\r
+Combine make_directory() and make_directory_and_parents() into a\r
+single recursive mkdir_recursive() function. Clarify the code and\r
+improve error handling. Improve error messages. Switch to using the\r
+new function in maildir_create_folder(). Constify talloc context.\r
+---\r
+ notmuch-insert.c | 131 +++++++++++++++++++++++-------------------------------\r
+ 1 files changed, 55 insertions(+), 76 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 7375c54..cdeeb41 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -104,96 +104,78 @@ is_valid_folder_name (const char *folder)\r
+ }\r
+ }\r
+ \r
+-/* Make the given directory, succeeding if it already exists. */\r
++/*\r
++ * Make the given directory and its parents as necessary, using the\r
++ * given mode. Return TRUE on success, FALSE otherwise. Partial\r
++ * results are not cleaned up on errors.\r
++ */\r
+ static notmuch_bool_t\r
+-make_directory (char *path, int mode)\r
++mkdir_recursive (const void *ctx, const char *path, int mode)\r
+ {\r
+- notmuch_bool_t ret;\r
+- char *slash;\r
++ struct stat st;\r
++ int r;\r
++ char *parent = NULL, *slash;\r
+ \r
+- if (mkdir (path, mode) != 0)\r
+- return (errno == EEXIST);\r
++ /* First check the common case: directory already exists. */\r
++ r = stat (path, &st);\r
++ if (r == 0) {\r
++ if (! S_ISDIR (st.st_mode)) {\r
++ fprintf (stderr, "Error: '%s' is not a directory: %s\n",\r
++ path, strerror (EEXIST));\r
++ return FALSE;\r
++ }\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
++ return TRUE;\r
++ } else if (errno != ENOENT) {\r
++ fprintf (stderr, "Error: stat '%s': %s\n", path, strerror (errno));\r
++ return FALSE;\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
++ /* mkdir parents, if any */\r
++ slash = strrchr (path, '/');\r
++ if (slash && slash != path) {\r
++ parent = talloc_strndup (ctx, path, slash - path);\r
++ if (! parent) {\r
++ fprintf (stderr, "Error: %s\n", strerror (ENOMEM));\r
++ return FALSE;\r
+ }\r
++\r
++ if (! mkdir_recursive (ctx, parent, mode))\r
++ return FALSE;\r
+ }\r
+ \r
+- return TRUE;\r
++ if (mkdir (path, mode)) {\r
++ fprintf (stderr, "Error: mkdir '%s': %s\n", path, strerror (errno));\r
++ return FALSE;\r
++ }\r
++\r
++ return parent ? sync_dir (parent) : TRUE;\r
+ }\r
+ \r
+-/* Create the given maildir folder, i.e. dir and its subdirectories\r
+- * 'cur', 'new', 'tmp'. */\r
++/*\r
++ * Create the given maildir folder, i.e. maildir and its\r
++ * subdirectories cur/new/tmp. Return TRUE on success, FALSE\r
++ * otherwise. Partial results are not cleaned up on errors.\r
++ */\r
+ static notmuch_bool_t\r
+-maildir_create_folder (void *ctx, const char *dir)\r
++maildir_create_folder (const void *ctx, const char *maildir)\r
+ {\r
++ const char *subdirs[] = { "cur", "new", "tmp" };\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
++ unsigned int i;\r
+ \r
+- /* Create 'new' directory. */\r
+- strcpy (tail, "new");\r
+- if (! make_directory (subdir, mode))\r
+- return FALSE;\r
++ for (i = 0; i < ARRAY_SIZE (subdirs); i++) {\r
++ subdir = talloc_asprintf (ctx, "%s/%s", maildir, subdirs[i]);\r
++ if (! subdir) {\r
++ fprintf (stderr, "Error: %s\n", strerror (ENOMEM));\r
++ return FALSE;\r
++ }\r
+ \r
+- /* Create 'tmp' directory. */\r
+- strcpy (tail, "tmp");\r
+- if (! make_directory (subdir, mode))\r
+- return FALSE;\r
++ if (! mkdir_recursive (ctx, subdir, mode))\r
++ return FALSE;\r
++ }\r
+ \r
+- talloc_free (subdir);\r
+ return TRUE;\r
+ }\r
+ \r
+@@ -463,11 +445,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])\r
+ fprintf (stderr, "Out of memory\n");\r
+ return EXIT_FAILURE;\r
+ }\r
+- if (create_folder && ! maildir_create_folder (config, maildir)) {\r
+- fprintf (stderr, "Error: creating maildir %s: %s\n",\r
+- maildir, strerror (errno));\r
++ if (create_folder && ! maildir_create_folder (config, maildir))\r
+ return EXIT_FAILURE;\r
+- }\r
+ }\r
+ \r
+ /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying\r
+-- \r
+1.7.2.5\r
+\r