1 Return-Path: <novalazy@gmail.com>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id CD93F431FAF
\r
6 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:19 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5
\r
12 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,
\r
13 FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id 55G8P7IW1bWR for <notmuch@notmuchmail.org>;
\r
17 Sat, 24 Nov 2012 17:18:18 -0800 (PST)
\r
18 Received: from mail-pa0-f53.google.com (mail-pa0-f53.google.com
\r
19 [209.85.220.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id A82FD431FAE
\r
22 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)
\r
23 Received: by mail-pa0-f53.google.com with SMTP id hz1so3911431pad.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 24 Nov 2012 17:18:18 -0800 (PST)
\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
\r
26 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;
\r
27 bh=xn5U2n/VUXiTHcThk3h/4CVQiEsyI/PGZWaFXJa0Eaw=;
\r
28 b=KVRQpQkWj0ShcjtmeWQPz1KQK+vyf4exBK1+AJurUmaK1GBal6PoiCZV1F3z71B+Sq
\r
29 clOWmOB1XmdN5YerYnY90PK23aREg3jWXVGgQO+Mvt+QkLwrATqBPFW5hPT7TzgxzXM6
\r
30 M1Z1MkuoD1DZILTNZ8UmPsE3QkH4j37ACB8ZChX8n7z5QyZE4osCEfmBHF5ofeGZNyYz
\r
31 PmMedYVnHhDw4Eos80p57XMriyGhfBtcxkoZb9fns2+ygz/JDjTTJGV9VIyN4ygLs3Hb
\r
32 8Frfgw6rsSYRjV0FLHY6KxqYIeNsp/1jaxZPFhsT/CY/J2g+JQZWkF8/SXXpMhnLfuSR
\r
34 Received: by 10.68.218.97 with SMTP id pf1mr26282231pbc.96.1353806298424;
\r
35 Sat, 24 Nov 2012 17:18:18 -0800 (PST)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id gu5sm6232468pbc.10.2012.11.24.17.18.15
\r
39 (version=TLSv1/SSLv3 cipher=OTHER);
\r
40 Sat, 24 Nov 2012 17:18:17 -0800 (PST)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v2 13/20] insert: add --create-folder option
\r
44 Date: Sun, 25 Nov 2012 12:16:39 +1100
\r
45 Message-Id: <1353806206-29133-14-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com>
\r
48 References: <1353806206-29133-1-git-send-email-novalazy@gmail.com>
\r
49 X-BeenThere: notmuch@notmuchmail.org
\r
50 X-Mailman-Version: 2.1.13
\r
52 List-Id: "Use and development of the notmuch mail system."
\r
53 <notmuch.notmuchmail.org>
\r
54 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
55 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
56 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
57 List-Post: <mailto:notmuch@notmuchmail.org>
\r
58 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
59 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
60 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
61 X-List-Received-Date: Sun, 25 Nov 2012 01:18:19 -0000
\r
63 Support an option to create a new folder in the maildir.
\r
65 notmuch-insert.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
66 1 file changed, 92 insertions(+)
\r
68 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
69 index 81a528c..b7aef95 100644
\r
70 --- a/notmuch-insert.c
\r
71 +++ b/notmuch-insert.c
\r
72 @@ -55,6 +55,91 @@ check_folder_name (const char *folder)
\r
76 +/* Make the given directory but succeed if it already exists. */
\r
78 +mkdir_exists_ok (const char *path, int mode)
\r
82 + ret = mkdir (path, mode);
\r
83 + if (ret == 0 || errno == EEXIST)
\r
89 +/* Make the given directory including its parent directory as necessary.
\r
90 + * Return 0 on success, -1 on error. */
\r
92 +mkdir_parents (char *path, int mode)
\r
99 + /* First check the common case: directory already exists. */
\r
100 + if (stat (path, &st) == 0)
\r
101 + return (S_ISDIR (st.st_mode)) ? 0 : -1;
\r
103 + for (start = path; *start != '\0'; start = end + 1) {
\r
104 + /* start points to the first unprocessed character.
\r
105 + * Find the next slash from start onwards. */
\r
106 + end = strchr (start, '/');
\r
108 + /* If there are no more slashes then all the parent directories
\r
109 + * have been made. Now attempt to make the whole path. */
\r
111 + return mkdir_exists_ok (path, mode);
\r
113 + /* Make the path up to the next slash, unless the current
\r
114 + * directory component is actually empty. */
\r
115 + if (end > start) {
\r
117 + ret = mkdir_exists_ok (path, mode);
\r
127 +/* Create the given maildir folder, i.e. dir and its subdirectories
\r
128 + * 'cur', 'new', 'tmp'. */
\r
129 +static notmuch_bool_t
\r
130 +maildir_create_folder (void *ctx, const char *dir)
\r
132 + const int mode = 0700;
\r
136 + /* Create 'cur' directory, including parent directories. */
\r
137 + subdir = talloc_asprintf (ctx, "%s/cur", dir);
\r
139 + fprintf (stderr, "Out of memory.\n");
\r
142 + if (mkdir_parents (subdir, mode) != 0)
\r
145 + tail = subdir + strlen (subdir) - 3;
\r
147 + /* Create 'new' directory. */
\r
148 + strcpy (tail, "new");
\r
149 + if (mkdir_exists_ok (subdir, mode) != 0)
\r
152 + /* Create 'tmp' directory. */
\r
153 + strcpy (tail, "tmp");
\r
154 + if (mkdir_exists_ok (subdir, mode) != 0)
\r
157 + talloc_free (subdir);
\r
161 /* Open a unique file in the Maildir 'tmp' directory.
\r
162 * Returns the file descriptor on success, or -1 on failure.
\r
163 * On success, file paths into the 'tmp' and 'new' directories are returned
\r
164 @@ -272,6 +357,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
165 const char **new_tags;
\r
166 size_t new_tags_length;
\r
167 const char *folder = NULL;
\r
168 + notmuch_bool_t create_folder = FALSE;
\r
169 notmuch_tag_operation_t *tag_ops;
\r
172 @@ -280,6 +366,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
174 notmuch_opt_desc_t options[] = {
\r
175 { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
\r
176 + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
\r
177 { NOTMUCH_OPT_END, 0, 0, 0, 0 }
\r
180 @@ -332,6 +419,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
181 fprintf (stderr, "Out of memory\n");
\r
184 + if (create_folder && ! maildir_create_folder (ctx, maildir)) {
\r
185 + fprintf (stderr, "Error: creating maildir %s: %s\n",
\r
186 + maildir, strerror (errno));
\r
190 if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
191 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r