--- /dev/null
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id DB3D2429E36\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:55:14 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id uEJWtHJi3Udy for <notmuch@notmuchmail.org>;\r
+ Mon, 22 Sep 2014 02:55:10 -0700 (PDT)\r
+Received: from mail-wi0-f173.google.com (mail-wi0-f173.google.com\r
+ [209.85.212.173]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 518F4431FBC\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:58 -0700 (PDT)\r
+Received: by mail-wi0-f173.google.com with SMTP id r20so2554607wiv.12\r
+ for <notmuch@notmuchmail.org>; Mon, 22 Sep 2014 02:54:57 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=PP97ENfqgEyJSXqIRz7IAcgX8LNKWfS+l2CzJgGJNQI=;\r
+ b=D8h6soj/XK8J1rvHgAT7p22/CPm4Zwr5rU0BLB6Z2MW8CkQolYN7rKiyUtX291JNo1\r
+ dvFl4sXbBLZtzXlKqdSbUXreDf+ysf6H7JAmcsLMgKYCTNngURSfNMvr/yJAc8Gir7/E\r
+ tLHBXxeuaWgfDeRxkmV7R/CP1GtSVDRNYK4j2uPmWvtW2amt22j6QOIv9NwbPCLqz6lI\r
+ tYW9fk/JXaXGQl/e7XN0k7qjU7GmizqORajAGCglmhL4AFEFftSJTQ8Q4bPt5b6XWz2K\r
+ /4BiRJ49TBEtrmtplOP2KF+GEI/eOWscxk8wRZZeXL3KC1l0ikNWzdBs6OoV8ouwK+ya\r
+ rGfA==\r
+X-Gm-Message-State:\r
+ ALoCoQn+pKp9IezDt6vr2QrdOqdE2jTfvvpa4ryJDfQyGdi1gS2izpbKa3kgecFMFbS0VgO738uj\r
+X-Received: by 10.194.24.101 with SMTP id t5mr19092602wjf.76.1411379697162;\r
+ Mon, 22 Sep 2014 02:54:57 -0700 (PDT)\r
+Received: from localhost ([2001:4b98:dc0:43:216:3eff:fe1b:25f3])\r
+ by mx.google.com with ESMTPSA id s7sm11686495wjo.48.2014.09.22.02.54.56\r
+ for <multiple recipients>\r
+ (version=TLSv1.1 cipher=RC4-SHA bits=128/128);\r
+ Mon, 22 Sep 2014 02:54:56 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 07/11] cli/insert: abstract temporary filename generation\r
+Date: Mon, 22 Sep 2014 11:54:58 +0200\r
+Message-Id:\r
+ <550178f85df340b38eb82094a5f823c9673d83dc.1411379395.git.jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.2.5\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.git.jani@nikula.org>\r
+In-Reply-To: <cover.1411379395.git.jani@nikula.org>\r
+References: <cover.1411379395.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Mon, 22 Sep 2014 09:55:15 -0000\r
+\r
+This will clean up the usage. There's the slight functional change of\r
+potentially ending up doing extra gethostname and getpid calls, but\r
+this is neglible.\r
+---\r
+ notmuch-insert.c | 39 +++++++++++++++++++++++++++------------\r
+ 1 files changed, 27 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/notmuch-insert.c b/notmuch-insert.c\r
+index cdeeb41..a1d564c 100644\r
+--- a/notmuch-insert.c\r
++++ b/notmuch-insert.c\r
+@@ -179,6 +179,31 @@ maildir_create_folder (const void *ctx, const char *maildir)\r
+ return TRUE;\r
+ }\r
+ \r
++/*\r
++ * Generate a temporary file basename, no path, do not create an\r
++ * actual file. Return the basename, or NULL on errors.\r
++ */\r
++static char *\r
++tempfilename (const void *ctx)\r
++{\r
++ char *filename;\r
++ char hostname[256];\r
++ struct timeval tv;\r
++ pid_t pid;\r
++\r
++ /* We follow the Dovecot file name generation algorithm. */\r
++ pid = getpid ();\r
++ safe_gethostname (hostname, sizeof (hostname));\r
++ gettimeofday (&tv, NULL);\r
++\r
++ filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",\r
++ tv.tv_sec, tv.tv_usec, pid, hostname);\r
++ if (! filename)\r
++ fprintf (stderr, "Error: %s\n", strerror (ENOMEM));\r
++\r
++ return filename;\r
++}\r
++\r
+ /* Open a unique file in the 'tmp' sub-directory of dir.\r
+ * Returns the file descriptor on success, or -1 on failure.\r
+ * On success, file paths for the message in the 'tmp' and 'new'\r
+@@ -188,23 +213,13 @@ static int\r
+ maildir_open_tmp_file (void *ctx, const char *dir,\r
+ char **tmppath, char **newpath, char **newdir)\r
+ {\r
+- pid_t pid;\r
+- char hostname[256];\r
+- struct timeval tv;\r
+ char *filename;\r
+ int fd = -1;\r
+ \r
+- /* We follow the Dovecot file name generation algorithm. */\r
+- pid = getpid ();\r
+- safe_gethostname (hostname, sizeof (hostname));\r
+ do {\r
+- gettimeofday (&tv, NULL);\r
+- filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s",\r
+- tv.tv_sec, tv.tv_usec, pid, hostname);\r
+- if (! filename) {\r
+- fprintf (stderr, "Out of memory\n");\r
++ filename = tempfilename (ctx);\r
++ if (! filename)\r
+ return -1;\r
+- }\r
+ \r
+ *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename);\r
+ if (! *tmppath) {\r
+-- \r
+1.7.2.5\r
+\r