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 643C9431FDB
\r
6 for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:43:21 -0700 (PDT)
\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 IWvkh1C+KgNc for <notmuch@notmuchmail.org>;
\r
17 Tue, 2 Apr 2013 15:43:17 -0700 (PDT)
\r
18 Received: from mail-da0-f53.google.com (mail-da0-f53.google.com
\r
19 [209.85.210.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 9F697431FBD
\r
22 for <notmuch@notmuchmail.org>; Tue, 2 Apr 2013 15:43:08 -0700 (PDT)
\r
23 Received: by mail-da0-f53.google.com with SMTP id n34so379344dal.26
\r
24 for <notmuch@notmuchmail.org>; Tue, 02 Apr 2013 15:43:07 -0700 (PDT)
\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=az4pLuANKfNkjKQTXi6FMnSfcEs4B0zDJjbLCrVTd4Y=;
\r
28 b=wMzeKtJh3oxPyxli4TXdsKxljbNfTBjLGSEOsdt5Np1G7EMRSCjouwQ7b8+Ezde8UV
\r
29 QFzIeWehwlkWy7czuDxkV3Ye9hTRauZdsQMrJzk2Dez20nJZ1vNLVy6+wP8IcP9bPUwb
\r
30 6x1fw1aiGpivkQsBv6ogMBJ/yf13vr7KfPgeel1lSz+Rb700WKn+gaEoIWTtLLlaLHjX
\r
31 0nZSOxSm9y9vCJO/9QT6V3VyZa7Jy3unsKaLwAYb1v5VM7j+RMqHJa8rZUVLcV9nKl9T
\r
32 9KRYpCMQ5tXEARqdFdY9jXQjJIf/9uE4Aegrd//1SDRl2/NjvAsRuLuI7w2No15HsJ0O
\r
34 X-Received: by 10.68.87.98 with SMTP id w2mr26748872pbz.190.1364942587840;
\r
35 Tue, 02 Apr 2013 15:43:07 -0700 (PDT)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id rt13sm4015068pac.14.2013.04.02.15.43.04
\r
39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
40 Tue, 02 Apr 2013 15:43:07 -0700 (PDT)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v5 10/12] insert: add --create-folder option
\r
44 Date: Wed, 3 Apr 2013 09:41:55 +1100
\r
45 Message-Id: <1364942517-6982-11-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 In-Reply-To: <1364942517-6982-1-git-send-email-novalazy@gmail.com>
\r
48 References: <1364942517-6982-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: Tue, 02 Apr 2013 22:43:21 -0000
\r
63 Allow the insert command to create the maildir folder
\r
64 into which the new message should be delivered.
\r
66 notmuch-insert.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
\r
67 1 file changed, 100 insertions(+)
\r
69 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
70 index 778ac04..8ae5dc9 100644
\r
71 --- a/notmuch-insert.c
\r
72 +++ b/notmuch-insert.c
\r
73 @@ -100,6 +100,99 @@ check_folder_name (const char *folder)
\r
77 +/* Make the given directory, succeeding if it already exists. */
\r
78 +static notmuch_bool_t
\r
79 +make_directory (char *path, int mode)
\r
81 + notmuch_bool_t ret;
\r
84 + if (mkdir (path, mode) != 0)
\r
85 + return (errno == EEXIST);
\r
87 + /* Sync the parent directory for durability. */
\r
89 + slash = strrchr (path, '/');
\r
92 + ret = sync_dir (path);
\r
98 +/* Make the given directory including its parent directories as necessary.
\r
99 + * Return TRUE on success, FALSE on error. */
\r
100 +static notmuch_bool_t
\r
101 +make_directory_and_parents (char *path, int mode)
\r
106 + notmuch_bool_t ret;
\r
108 + /* First check the common case: directory already exists. */
\r
109 + if (stat (path, &st) == 0)
\r
110 + return S_ISDIR (st.st_mode) ? TRUE : FALSE;
\r
112 + for (start = path; *start != '\0'; start = end + 1) {
\r
113 + /* start points to the first unprocessed character.
\r
114 + * Find the next slash from start onwards. */
\r
115 + end = strchr (start, '/');
\r
117 + /* If there are no more slashes then all the parent directories
\r
118 + * have been made. Now attempt to make the whole path. */
\r
120 + return make_directory (path, mode);
\r
122 + /* Make the path up to the next slash, unless the current
\r
123 + * directory component is actually empty. */
\r
124 + if (end > start) {
\r
126 + ret = make_directory (path, mode);
\r
136 +/* Create the given maildir folder, i.e. dir and its subdirectories
\r
137 + * 'cur', 'new', 'tmp'. */
\r
138 +static notmuch_bool_t
\r
139 +maildir_create_folder (void *ctx, const char *dir)
\r
141 + const int mode = 0700;
\r
145 + /* Create 'cur' directory, including parent directories. */
\r
146 + subdir = talloc_asprintf (ctx, "%s/cur", dir);
\r
148 + fprintf (stderr, "Out of memory.\n");
\r
151 + if (! make_directory_and_parents (subdir, mode))
\r
154 + tail = subdir + strlen (subdir) - 3;
\r
156 + /* Create 'new' directory. */
\r
157 + strcpy (tail, "new");
\r
158 + if (! make_directory (subdir, mode))
\r
161 + /* Create 'tmp' directory. */
\r
162 + strcpy (tail, "tmp");
\r
163 + if (! make_directory (subdir, mode))
\r
166 + talloc_free (subdir);
\r
170 /* Open a unique file in the Maildir 'tmp' directory.
\r
171 * Returns the file descriptor on success, or -1 on failure.
\r
172 * On success, file paths for the message in the 'tmp' and 'new'
\r
173 @@ -305,6 +398,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
174 tag_op_list_t *tag_ops;
\r
175 char *query_string = NULL;
\r
176 const char *folder = NULL;
\r
177 + notmuch_bool_t create_folder = FALSE;
\r
178 const char *maildir;
\r
181 @@ -312,6 +406,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
183 notmuch_opt_desc_t options[] = {
\r
184 { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 },
\r
185 + { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 },
\r
186 { NOTMUCH_OPT_END, 0, 0, 0, 0 }
\r
189 @@ -357,6 +452,11 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
\r
190 fprintf (stderr, "Out of memory\n");
\r
193 + if (create_folder && ! maildir_create_folder (config, maildir)) {
\r
194 + fprintf (stderr, "Error: creating maildir %s: %s\n",
\r
195 + maildir, strerror (errno));
\r
200 /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying
\r