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 8D931431FDA
\r
6 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:50:52 -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 sQzUuZa0UrhL for <notmuch@notmuchmail.org>;
\r
17 Sat, 19 Jan 2013 16:50:51 -0800 (PST)
\r
18 Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com
\r
19 [209.85.220.47]) (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 43BB7431FDB
\r
22 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:50:44 -0800 (PST)
\r
23 Received: by mail-pa0-f47.google.com with SMTP id fa10so2756590pad.34
\r
24 for <notmuch@notmuchmail.org>; Sat, 19 Jan 2013 16:50:43 -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=jtWJ+EJNDECM7VA36hsi/3Mr+iVFnCwBd3E1VSnxHhA=;
\r
28 b=kMRaa2oBjBmGlmk4eqObsvNxoh3ChaEBWov//BzhF+ofnb2h6f1RjFZhg0Zo5U7cZr
\r
29 4p+aMt/ihnHlvj80JZ5V2BL4lJaOtjKcHchHD1Q3cRY+Z/5Zx2U8UIXIVhpa9p1FChhV
\r
30 uapjbLtbqF97RCkIfRZqK6QgEMnmM8MbWwZlGgU7rnxb28KFJqQG7COAIjayD43k28lV
\r
31 rZkcqigMlqlbxCh4bmsoUGdN19IEUq7jMRxxyTxBsH1rmlL8v/ttMHaABOfwlE9p1N2i
\r
32 r6rYi24ghJnjWNooJglpWbO6KzDIZ5kYO6YpbxJDRUw5YGYN2t0epKqItX556bUXL+0g
\r
34 X-Received: by 10.66.73.105 with SMTP id k9mr36418382pav.37.1358643043230;
\r
35 Sat, 19 Jan 2013 16:50:43 -0800 (PST)
\r
36 Received: from localhost (215.42.233.220.static.exetel.com.au.
\r
38 by mx.google.com with ESMTPS id vo6sm5808798pbc.8.2013.01.19.16.50.40
\r
39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128);
\r
40 Sat, 19 Jan 2013 16:50:42 -0800 (PST)
\r
41 From: Peter Wang <novalazy@gmail.com>
\r
42 To: notmuch@notmuchmail.org
\r
43 Subject: [PATCH v3 02/20] insert: open Maildir tmp file
\r
44 Date: Sun, 20 Jan 2013 11:49:46 +1100
\r
45 Message-Id: <1358643004-14522-3-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:50:53 -0000
\r
63 Open a unique file in the Maildir tmp directory.
\r
64 The new message is not yet copied into the file.
\r
66 notmuch-insert.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
\r
67 1 file changed, 104 insertions(+), 1 deletion(-)
\r
69 diff --git a/notmuch-insert.c b/notmuch-insert.c
\r
70 index c1d359c..7d100ae 100644
\r
71 --- a/notmuch-insert.c
\r
72 +++ b/notmuch-insert.c
\r
73 @@ -20,12 +20,107 @@
\r
75 #include "notmuch-client.h"
\r
77 +#include <sys/types.h>
\r
78 +#include <sys/stat.h>
\r
81 +/* Like gethostname but guarantees that a null-terminated hostname is
\r
82 + * returned, even if it has to make one up.
\r
83 + * Returns true unless hostname contains a slash. */
\r
84 +static notmuch_bool_t
\r
85 +safe_gethostname (char *hostname, size_t len)
\r
87 + if (gethostname (hostname, len) == -1) {
\r
88 + strncpy (hostname, "unknown", len);
\r
90 + hostname[len - 1] = '\0';
\r
92 + return (strchr (hostname, '/') == NULL);
\r
95 +/* Open a unique file in the Maildir 'tmp' directory.
\r
96 + * Returns the file descriptor on success, or -1 on failure.
\r
97 + * On success, file paths for the message in the 'tmp' and 'new'
\r
98 + * directories are returned via tmppath and newpath. */
\r
100 +maildir_open_tmp_file (void *ctx, const char *dir,
\r
101 + char **tmppath, char **newpath)
\r
104 + char hostname[256];
\r
105 + struct timeval tv;
\r
109 + /* We follow the Dovecot file name generation algorithm. */
\r
111 + if (! safe_gethostname (hostname, sizeof (hostname))) {
\r
112 + fprintf (stderr, "Error: invalid host name.\n");
\r
116 + gettimeofday (&tv, NULL);
\r
117 + filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",
\r
118 + tv.tv_sec, tv.tv_usec, pid, hostname);
\r
119 + if (! filename) {
\r
120 + fprintf (stderr, "Out of memory\n");
\r
124 + *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);
\r
125 + if (! *tmppath) {
\r
126 + fprintf (stderr, "Out of memory\n");
\r
130 + fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
\r
131 + } while (fd == -1 && errno == EEXIST);
\r
134 + fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno));
\r
138 + *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename);
\r
139 + if (! *newpath) {
\r
140 + fprintf (stderr, "Out of memory\n");
\r
142 + unlink (*tmppath);
\r
146 + talloc_free (filename);
\r
151 +static notmuch_bool_t
\r
152 +insert_message (void *ctx, notmuch_database_t *notmuch, int fdin,
\r
159 + fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath);
\r
164 + /* For now we just delete the tmp file immediately. */
\r
166 + unlink (tmppath);
\r
171 notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
173 notmuch_config_t *config;
\r
174 notmuch_database_t *notmuch;
\r
175 const char *db_path;
\r
177 + notmuch_bool_t ret;
\r
179 config = notmuch_config_open (ctx, NULL, NULL);
\r
180 if (config == NULL)
\r
181 @@ -33,11 +128,19 @@ notmuch_insert_command (void *ctx, int argc, char *argv[])
\r
183 db_path = notmuch_config_get_database_path (config);
\r
185 + maildir = talloc_asprintf (ctx, "%s", db_path);
\r
187 + fprintf (stderr, "Out of memory\n");
\r
191 if (notmuch_database_open (notmuch_config_get_database_path (config),
\r
192 NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
\r
195 + ret = insert_message (ctx, notmuch, STDIN_FILENO, maildir);
\r
197 notmuch_database_destroy (notmuch);
\r
200 + return (ret) ? 0 : 1;
\r