[PATCH v2 13/20] insert: add --create-folder option
authorPeter Wang <novalazy@gmail.com>
Sun, 25 Nov 2012 01:16:39 +0000 (12:16 +1100)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:55 +0000 (09:50 -0800)
ad/bc465cf984d93d63ccc46a13b4c03b1b1688a8 [new file with mode: 0644]

diff --git a/ad/bc465cf984d93d63ccc46a13b4c03b1b1688a8 b/ad/bc465cf984d93d63ccc46a13b4c03b1b1688a8
new file mode 100644 (file)
index 0000000..f8e8ffe
--- /dev/null
@@ -0,0 +1,194 @@
+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 CD93F431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:19 -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 55G8P7IW1bWR for <notmuch@notmuchmail.org>;\r
+       Sat, 24 Nov 2012 17:18:18 -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 A82FD431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)\r
+Received: by mail-pa0-f53.google.com with SMTP id hz1so3911431pad.26\r
+       for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)\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=xn5U2n/VUXiTHcThk3h/4CVQiEsyI/PGZWaFXJa0Eaw=;\r
+       b=KVRQpQkWj0ShcjtmeWQPz1KQK+vyf4exBK1+AJurUmaK1GBal6PoiCZV1F3z71B+Sq\r
+       clOWmOB1XmdN5YerYnY90PK23aREg3jWXVGgQO+Mvt+QkLwrATqBPFW5hPT7TzgxzXM6\r
+       M1Z1MkuoD1DZILTNZ8UmPsE3QkH4j37ACB8ZChX8n7z5QyZE4osCEfmBHF5ofeGZNyYz\r
+       PmMedYVnHhDw4Eos80p57XMriyGhfBtcxkoZb9fns2+ygz/JDjTTJGV9VIyN4ygLs3Hb\r
+       8Frfgw6rsSYRjV0FLHY6KxqYIeNsp/1jaxZPFhsT/CY/J2g+JQZWkF8/SXXpMhnLfuSR\r
+       lvFw==\r
+Received: by 10.68.218.97 with SMTP id pf1mr26282231pbc.96.1353806298424;\r
+       Sat, 24 Nov 2012 17:18:18 -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 gu5sm6232468pbc.10.2012.11.24.17.18.15\r
+       (version=TLSv1/SSLv3 cipher=OTHER);\r
+       Sat, 24 Nov 2012 17:18:17 -0800 (PST)\r
+From: Peter Wang <novalazy@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v2 13/20] insert: add --create-folder option\r
+Date: Sun, 25 Nov 2012 12:16:39 +1100\r
+Message-Id: <1353806206-29133-14-git-send-email-novalazy@gmail.com>\r
+X-Mailer: git-send-email 1.7.12.1\r
+In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com>\r
+References: <1353806206-29133-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, 25 Nov 2012 01:18:19 -0000\r
+\r
+Support an option to create a new folder in the maildir.\r
+---\r
+ notmuch-insert.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ 1 file changed, 92 insertions(+)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index 81a528c..b7aef95 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -55,6 +55,91 @@ check_folder_name (const char *folder)\r
+     }\r
+ }\r
\r
++/* Make the given directory but succeed if it already exists. */\r
++static int\r
++mkdir_exists_ok (const char *path, int mode)\r
++{\r
++    int ret;\r
++\r
++    ret = mkdir (path, mode);\r
++    if (ret == 0 || errno == EEXIST)\r
++      return 0;\r
++    else\r
++      return ret;\r
++}\r
++\r
++/* Make the given directory including its parent directory as necessary.\r
++ * Return 0 on success, -1 on error. */\r
++static int\r
++mkdir_parents (char *path, int mode)\r
++{\r
++    struct stat st;\r
++    char *start;\r
++    char *end;\r
++    int ret;\r
++\r
++    /* First check the common case: directory already exists. */\r
++    if (stat (path, &st) == 0)\r
++      return (S_ISDIR (st.st_mode)) ? 0 : -1;\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 mkdir_exists_ok (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 = mkdir_exists_ok (path, mode);\r
++          *end = '/';\r
++          if (ret != 0)\r
++              return ret;\r
++      }\r
++    }\r
++\r
++    return 0;\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 (mkdir_parents (subdir, mode) != 0)\r
++      return FALSE;\r
++\r
++    tail = subdir + strlen (subdir) - 3;\r
++\r
++    /* Create 'new' directory. */\r
++    strcpy (tail, "new");\r
++    if (mkdir_exists_ok (subdir, mode) != 0)\r
++      return FALSE;\r
++\r
++    /* Create 'tmp' directory. */\r
++    strcpy (tail, "tmp");\r
++    if (mkdir_exists_ok (subdir, mode) != 0)\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 into the 'tmp' and 'new' directories are returned\r
+@@ -272,6 +357,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])\r
+     const char **new_tags;\r
+     size_t new_tags_length;\r
+     const char *folder = NULL;\r
++    notmuch_bool_t create_folder = FALSE;\r
+     notmuch_tag_operation_t *tag_ops;\r
+     int tag_ops_count;\r
+     char *maildir;\r
+@@ -280,6 +366,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
+@@ -332,6 +419,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
+     if (notmuch_database_open (notmuch_config_get_database_path (config),\r
+                              NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
+-- \r
+1.7.12.1\r
+\r