From: Peter Wang Date: Sun, 25 Nov 2012 01:16:30 +0000 (+1100) Subject: [PATCH v2 04/20] insert: open Maildir tmp file X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=ea33f35cb4177268e67a3ee71724f7dd63a1a735;p=notmuch-archives.git [PATCH v2 04/20] insert: open Maildir tmp file --- diff --git a/6e/7fc0855a54c768f4912d492156c695cd4f4ca0 b/6e/7fc0855a54c768f4912d492156c695cd4f4ca0 new file mode 100644 index 000000000..56147f02f --- /dev/null +++ b/6e/7fc0855a54c768f4912d492156c695cd4f4ca0 @@ -0,0 +1,204 @@ +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 194F1431FAF + for ; Sat, 24 Nov 2012 17:17:28 -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 vHayD3OkBpr1 for ; + Sat, 24 Nov 2012 17:17:27 -0800 (PST) +Received: from mail-pb0-f53.google.com (mail-pb0-f53.google.com + [209.85.160.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 7D927431FAE + for ; Sat, 24 Nov 2012 17:17:27 -0800 (PST) +Received: by mail-pb0-f53.google.com with SMTP id jt11so7944513pbb.26 + for ; Sat, 24 Nov 2012 17:17:27 -0800 (PST) +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=44e0RtZhTqEJF1pdiLyS07Ki8mQ//QWBID0Upf/H53w=; + b=fnilvtOPjkpM/aEQWC+mhhjDg1YPEl/IMJQNQd875eg67XnSzH2wnn+2Lw4YuItMoK + PXcDy8BNJFpGEVJomV/VQd9e94xBi6EonsNLaJ3ytcrVy7VCcR275CE9UywGeKID6NPE + DBHr5zuBvUnjnPmECwcBPHWYRoLj4qoeLROX7IW4XrsbBTXOcFn8f0qksercVKuZtnXm + WBJIKptQ4qduioexPZJ7HmBwu8Yq1YdQVR8EDxDuGsqLJ82oMIKqLXIvcvX/vgnFjywx + aZGwk6mWUn2sXeVoQPkRa2TrRsxbOONTLrX+g53irhtxe2ZJpDlnwpvbie6jaa+nwixq + TfZA== +Received: by 10.66.89.9 with SMTP id bk9mr21843834pab.67.1353806247257; + Sat, 24 Nov 2012 17:17:27 -0800 (PST) +Received: from localhost (215.42.233.220.static.exetel.com.au. + [220.233.42.215]) + by mx.google.com with ESMTPS id gl9sm6222474pbc.51.2012.11.24.17.17.24 + (version=TLSv1/SSLv3 cipher=OTHER); + Sat, 24 Nov 2012 17:17:26 -0800 (PST) +From: Peter Wang +To: notmuch@notmuchmail.org +Subject: [PATCH v2 04/20] insert: open Maildir tmp file +Date: Sun, 25 Nov 2012 12:16:30 +1100 +Message-Id: <1353806206-29133-5-git-send-email-novalazy@gmail.com> +X-Mailer: git-send-email 1.7.12.1 +In-Reply-To: <1353806206-29133-1-git-send-email-novalazy@gmail.com> +References: <1353806206-29133-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, 25 Nov 2012 01:17:28 -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 21424cf..371fb47 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 into 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 +