From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 13AAB431FCF for ; Wed, 25 Jul 2012 06:43:30 -0700 (PDT) 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 8ZgnZp9Nhj+8 for ; Wed, 25 Jul 2012 06:43:28 -0700 (PDT) Received: from mail-gg0-f181.google.com (mail-gg0-f181.google.com [209.85.161.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 7D8B2431FC7 for ; Wed, 25 Jul 2012 06:43:26 -0700 (PDT) Received: by mail-gg0-f181.google.com with SMTP id v5so882427ggn.26 for ; Wed, 25 Jul 2012 06:43:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=HVSIbOtlhADhGBcZWSsAP2oPF/SCdkQEIwlYnzC5hKY=; b=vWRdJmOgy+uTazJ9x0s2DmAQhQJUQVUpO6imeCrMg9NoqX/9Ql304RLYGi6SmOI9ka tElyXTM9gV2sXc7/sovqFJrU7BEQ70P5sDhCxWHvBekOs1bwIMkAcXYcoOxUuY11M5Pv YUlaLn5PsIe83i8ZXf4vBiVNVqNnDK1JbgiUAFr7nfvGrNV8SSRneAETIFzACKw6pH3E v7G/1gbmJZSIVz7hTOz1D+Gz1Bgm5pGWKH0/Xsh17SbRYAlD2OFX6w99BIhO/6eIlSEn UVS7aahngtp1I9HQKesf++5joefSIWyCt67pD+ZwcCFZD/TjS1grOUWgE/glIAIYtWdR Iwag== Received: by 10.66.73.202 with SMTP id n10mr12776437pav.80.1343223805948; Wed, 25 Jul 2012 06:43:25 -0700 (PDT) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id sk5sm14419691pbc.7.2012.07.25.06.43.23 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 25 Jul 2012 06:43:25 -0700 (PDT) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH 03/18] insert: open Maildir tmp file Date: Wed, 25 Jul 2012 23:42:32 +1000 Message-Id: <1343223767-9812-3-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1343223767-9812-1-git-send-email-novalazy@gmail.com> References: <1343223767-9812-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: Wed, 25 Jul 2012 13:43:30 -0000 Open a unique file in the Maildir tmp directory. The new message is not yet copied into the file. --- notmuch-insert.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 79 insertions(+), 1 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 21424cf..f01a6f2 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -20,12 +20,86 @@ #include "notmuch-client.h" +#include +#include +#include + +static notmuch_bool_t +safe_gethostname (char *hostname, size_t hostname_size) +{ + if (gethostname (hostname, hostname_size) == -1) { + strncpy (hostname, "unknown", hostname_size); + } + hostname[hostname_size - 1] = '\0'; + + return (strchr (hostname, '/') == NULL); +} + +static int +maildir_open_tmp (void *ctx, const char *dir, char **tmppath, char **newpath) +{ + pid_t pid; + char hostname[256]; + struct timeval tv; + char *filename; + int fd = -1; + + /* This is the file name format used by Dovecot. */ + pid = getpid (); + if (! safe_gethostname (hostname, sizeof (hostname))) { + fprintf (stderr, "Error: invalid host name.\n"); + return -1; + } + gettimeofday (&tv, NULL); + filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s", + tv.tv_sec, tv.tv_usec, pid, hostname); + + *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename); + + do { + fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0666); + } while (fd == -1 && errno == EEXIST); + + if (fd != -1) { + *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename); + } + else { + fprintf (stderr, "Error: opening %s: %s\n", + *tmppath, strerror (errno)); + talloc_free (*tmppath); + } + + 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 (ctx, dir, &tmppath, &newpath); + if (fdout < 0) { + return FALSE; + } + + 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 +107,15 @@ 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 (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.4.4