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