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 CCB9A431FDC
\r
6 for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:15 -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 LB-TgXl-yvFk for <notmuch@notmuchmail.org>;
\r
17 Thu, 24 Jan 2013 04:10:14 -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 E6DE2431FDE
\r
22 for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:04 -0800 (PST)
\r
23 Received: by mail-pa0-f53.google.com with SMTP id hz1so5455330pad.12
\r
24 for <notmuch@notmuchmail.org>; Thu, 24 Jan 2013 04:10:04 -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=3TltoTMAK8jiGFLLoSUX2+bLtr+s39AWl6Kni3z1HHs=;
\r
28 b=UDkbICpdhIS3RclkY2e37OH+jI05OTsaOrTpHfIv7Lz/FWpRS1LagCxnTkVVSxBExX
\r
29 18CAs1w1K/CMBA8nSo2sMdDz1bRTBtCiyjJU+bM11Yo9OWzKtCgRyyTVVunDoMImf0mV
\r
30 +Orvspd2DpI06go2VLU7CkzF2st616oaQj7EuJOvTtdlZ92610Laarfqc1htpgzxlg12
\r
31 BhxRzCHzvOhBc3q5WZWg6vDy4z/sMnXdJLoHZoXDeq7ddFSOS1EjT1rbHOYn7fF/TzGl
\r
32 RXkeWBh8+St6WiMnXZCopx7YaZXCvgIZVDGZVGc97TXQIsatKcXr4Xivc26WFNfSihXN
\r
34 X-Received: by 10.66.52.102 with SMTP id s6mr4194398pao.6.1359029404142;
\r
35 Thu, 24 Jan 2013 04:10:04 -0800 (PST)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id ov4sm14779538pbb.45.2013.01.24.04.10.01
\r
39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
40 Thu, 24 Jan 2013 04:10:03 -0800 (PST)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v4 10/12] insert: add --create-folder option
\r
44 Date: Thu, 24 Jan 2013 23:08:06 +1100
\r
45 Message-Id: <1359029288-12132-11-git-send-email-novalazy@gmail.com>
\r
46 X-Mailer: git-send-email 1.7.12.1
\r
47 In-Reply-To: <1359029288-12132-1-git-send-email-novalazy@gmail.com>
\r
48 References: <1359029288-12132-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: Thu, 24 Jan 2013 12:10:17 -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 bc848ff..69329ad 100644
\r
71 --- a/notmuch-insert.c
\r
72 +++ b/notmuch-insert.c
\r
73 @@ -102,6 +102,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 @@ -306,6 +399,7 @@ notmuch_insert_command (void *ctx, 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 @@ -313,6 +407,7 @@ notmuch_insert_command (void *ctx, 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 @@ -362,6 +457,11 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
190 fprintf (stderr, "Out of memory\n");
\r
193 + if (create_folder && ! maildir_create_folder (ctx, 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