Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 8D931431FDA for ; Sat, 19 Jan 2013 16:50:52 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sQzUuZa0UrhL for ; Sat, 19 Jan 2013 16:50:51 -0800 (PST) Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com [209.85.220.47]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 43BB7431FDB for ; Sat, 19 Jan 2013 16:50:44 -0800 (PST) Received: by mail-pa0-f47.google.com with SMTP id fa10so2756590pad.34 for ; Sat, 19 Jan 2013 16:50:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=jtWJ+EJNDECM7VA36hsi/3Mr+iVFnCwBd3E1VSnxHhA=; b=kMRaa2oBjBmGlmk4eqObsvNxoh3ChaEBWov//BzhF+ofnb2h6f1RjFZhg0Zo5U7cZr 4p+aMt/ihnHlvj80JZ5V2BL4lJaOtjKcHchHD1Q3cRY+Z/5Zx2U8UIXIVhpa9p1FChhV uapjbLtbqF97RCkIfRZqK6QgEMnmM8MbWwZlGgU7rnxb28KFJqQG7COAIjayD43k28lV rZkcqigMlqlbxCh4bmsoUGdN19IEUq7jMRxxyTxBsH1rmlL8v/ttMHaABOfwlE9p1N2i r6rYi24ghJnjWNooJglpWbO6KzDIZ5kYO6YpbxJDRUw5YGYN2t0epKqItX556bUXL+0g 9I1Q== X-Received: by 10.66.73.105 with SMTP id k9mr36418382pav.37.1358643043230; Sat, 19 Jan 2013 16:50:43 -0800 (PST) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id vo6sm5808798pbc.8.2013.01.19.16.50.40 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 19 Jan 2013 16:50:42 -0800 (PST) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH v3 02/20] insert: open Maildir tmp file Date: Sun, 20 Jan 2013 11:49:46 +1100 Message-Id: <1358643004-14522-3-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com> References: <1358643004-14522-1-git-send-email-novalazy@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Jan 2013 00:50:53 -0000 Open a unique file in the Maildir tmp directory. The new message is not yet copied into the file. --- notmuch-insert.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index c1d359c..7d100ae 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -20,12 +20,107 @@ #include "notmuch-client.h" +#include +#include +#include + +/* Like gethostname but guarantees that a null-terminated hostname is + * returned, even if it has to make one up. + * Returns true unless hostname contains a slash. */ +static notmuch_bool_t +safe_gethostname (char *hostname, size_t len) +{ + if (gethostname (hostname, len) == -1) { + strncpy (hostname, "unknown", len); + } + hostname[len - 1] = '\0'; + + return (strchr (hostname, '/') == NULL); +} + +/* Open a unique file in the Maildir 'tmp' directory. + * Returns the file descriptor on success, or -1 on failure. + * On success, file paths for the message in the 'tmp' and 'new' + * directories are returned via tmppath and newpath. */ +static int +maildir_open_tmp_file (void *ctx, const char *dir, + char **tmppath, char **newpath) +{ + pid_t pid; + char hostname[256]; + struct timeval tv; + char *filename; + int fd = -1; + + /* We follow the Dovecot file name generation algorithm. */ + pid = getpid (); + if (! safe_gethostname (hostname, sizeof (hostname))) { + fprintf (stderr, "Error: invalid host name.\n"); + return -1; + } + do { + gettimeofday (&tv, NULL); + filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s", + tv.tv_sec, tv.tv_usec, pid, hostname); + if (! filename) { + fprintf (stderr, "Out of memory\n"); + return -1; + } + + *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename); + if (! *tmppath) { + fprintf (stderr, "Out of memory\n"); + return -1; + } + + fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600); + } while (fd == -1 && errno == EEXIST); + + if (fd == -1) { + fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno)); + return -1; + } + + *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename); + if (! *newpath) { + fprintf (stderr, "Out of memory\n"); + close (fd); + unlink (*tmppath); + return -1; + } + + talloc_free (filename); + + return fd; +} + +static notmuch_bool_t +insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, + const char *dir) +{ + char *tmppath; + char *newpath; + int fdout; + + fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath); + if (fdout < 0) { + return FALSE; + } + + /* For now we just delete the tmp file immediately. */ + close (fdout); + unlink (tmppath); + return FALSE; +} + int notmuch_insert_command (void *ctx, int argc, char *argv[]) { notmuch_config_t *config; notmuch_database_t *notmuch; const char *db_path; + char *maildir; + notmuch_bool_t ret; config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) @@ -33,11 +128,19 @@ notmuch_insert_command (void *ctx, int argc, char *argv[]) db_path = notmuch_config_get_database_path (config); + maildir = talloc_asprintf (ctx, "%s", db_path); + if (! maildir) { + fprintf (stderr, "Out of memory\n"); + return 1; + } + if (notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) return 1; + ret = insert_message (ctx, notmuch, STDIN_FILENO, maildir); + notmuch_database_destroy (notmuch); - return 1; + return (ret) ? 0 : 1; } -- 1.7.12.1