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 88CE1429E39
\r
6 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:48 -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 yhyNqErdoMvE for <notmuch@notmuchmail.org>;
\r
17 Sat, 19 Jan 2013 16:51:47 -0800 (PST)
\r
18 Received: from mail-pb0-f50.google.com (mail-pb0-f50.google.com
\r
19 [209.85.160.50]) (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 A7F59431FDE
\r
22 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:32 -0800 (PST)
\r
23 Received: by mail-pb0-f50.google.com with SMTP id wz7so2678857pbc.37
\r
24 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:51:31 -0800 (PST)
\r
25 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
\r
26 h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to
\r
27 :references; bh=XZmlr2r+CyR8wzPpF1KLP4/EmSvx0VmdJJkR8eI9f6Q=;
\r
28 b=uiif3DoQYxo4aqDFRvCsPKnwoSb8z4Av7v9jLIt6Kj8wkyKqvGdbu9AyI/5RXKejPL
\r
29 ziGFrBknJc/mnrG2I9s2VeLsVU5muP5rMWNCr21xTfUdqxzu4zX/S1aBiVwHcs8zko92
\r
30 L7Re0Oev7wKpTZhjSCeWj/1T9f6aszUg2JqpcbBX0Jhjaq0iFfn4muaKsWdiwgSGKEUV
\r
31 fkGYE8ipK+MZeqOo87o7GkCQWTjbEc/B8o8scUh7c7HMWqBl4IXAvk15IcrW5S9On7Lb
\r
32 DlAZtFPIrQXuwLfBaehR2cn+ZPTWhPr0sNMIGbuhRLuMR9lSabRlfSu0V2j2fcY7T9Uh
\r
34 X-Received: by 10.68.237.135 with SMTP id vc7mr18417215pbc.2.1358643091805;
\r
35 Sat, 19 Jan 2013 16:51:31 -0800 (PST)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id q4sm6268532paz.20.2013.01.19.16.51.29
\r
39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
40 Sat, 19 Jan 2013 16:51:31 -0800 (PST)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v3 12/20] insert: add --create-folder option
\r
44 Date: Sun, 20 Jan 2013 11:49:56 +1100
\r
45 Message-Id: <1358643004-14522-13-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com>
\r
48 References: <1358643004-14522-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, 20 Jan 2013 00:51:49 -0000
\r
63 Support an option to create a new folder in the maildir.
\r
65 notmuch-insert.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
66 1 file changed, 88 insertions(+)
\r
68 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
69 index 67ef94a..85acaea 100644
\r
70 --- a/notmuch-insert.c
\r
71 +++ b/notmuch-insert.c
\r
72 @@ -56,6 +56,87 @@ check_folder_name (const char *folder)
\r
76 +/* Make the given directory, succeeding if it already exists. */
\r
77 +static notmuch_bool_t
\r
78 +make_directory (char *path, int mode)
\r
80 + if (mkdir (path, mode) != 0)
\r
81 + return (errno == EEXIST);
\r
85 +/* Make the given directory including its parent directories as necessary.
\r
86 + * Return TRUE on success, FALSE on error. */
\r
87 +static notmuch_bool_t
\r
88 +make_directory_and_parents (char *path, int mode)
\r
93 + notmuch_bool_t ret;
\r
95 + /* First check the common case: directory already exists. */
\r
96 + if (stat (path, &st) == 0)
\r
97 + return S_ISDIR (st.st_mode) ? TRUE : FALSE;
\r
99 + for (start = path; *start != '\0'; start = end + 1) {
\r
100 + /* start points to the first unprocessed character.
\r
101 + * Find the next slash from start onwards. */
\r
102 + end = strchr (start, '/');
\r
104 + /* If there are no more slashes then all the parent directories
\r
105 + * have been made. Now attempt to make the whole path. */
\r
107 + return make_directory (path, mode);
\r
109 + /* Make the path up to the next slash, unless the current
\r
110 + * directory component is actually empty. */
\r
111 + if (end > start) {
\r
113 + ret = make_directory (path, mode);
\r
123 +/* Create the given maildir folder, i.e. dir and its subdirectories
\r
124 + * 'cur', 'new', 'tmp'. */
\r
125 +static notmuch_bool_t
\r
126 +maildir_create_folder (void *ctx, const char *dir)
\r
128 + const int mode = 0700;
\r
132 + /* Create 'cur' directory, including parent directories. */
\r
133 + subdir = talloc_asprintf (ctx, "%s/cur", dir);
\r
135 + fprintf (stderr, "Out of memory.\n");
\r
138 + if (! make_directory_and_parents (subdir, mode))
\r
141 + tail = subdir + strlen (subdir) - 3;
\r
143 + /* Create 'new' directory. */
\r
144 + strcpy (tail, "new");
\r
145 + if (! make_directory (subdir, mode))
\r
148 + /* Create 'tmp' directory. */
\r
149 + strcpy (tail, "tmp");
\r
150 + if (! make_directory (subdir, mode))
\r
153 + talloc_free (subdir);
\r
157 /* Open a unique file in the Maildir 'tmp' directory.
\r
158 * Returns the file descriptor on success, or -1 on failure.
\r
159 * On success, file paths for the message in the 'tmp' and 'new'
\r
160 @@ -254,6 +335,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
161 tag_op_list_t *tag_ops;
\r
162 char *query_string = NULL;
\r
163 const char *folder = NULL;
\r
164 + notmuch_bool_t create_folder = FALSE;
\r
168 @@ -261,6 +343,7 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
170 notmuch_opt_desc_t options[] = {
\r
171 { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
\r
172 + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
\r
173 { NOTMUCH_OPT_END, 0, 0, 0, 0 }
\r
176 @@ -311,6 +394,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
177 fprintf (stderr, "Out of memory\n");
\r
180 + if (create_folder && ! maildir_create_folder (ctx, maildir)) {
\r
181 + fprintf (stderr, "Error: creating maildir %s: %s\n",
\r
182 + maildir, strerror (errno));
\r
186 if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
187 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r