From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id iE+uC7YjCWKL8AAAgWs5BA (envelope-from ) for ; Sun, 13 Feb 2022 16:28:54 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id oI8OCLYjCWKsLwEAauVa8A (envelope-from ) for ; Sun, 13 Feb 2022 16:28:54 +0100 Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id D3DD7EBF4 for ; Sun, 13 Feb 2022 16:28:53 +0100 (CET) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 7662D5F70A; Sun, 13 Feb 2022 15:28:52 +0000 (UTC) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 9C0EE5F6BC for ; Sun, 13 Feb 2022 15:28:50 +0000 (UTC) Received: by fethera.tethera.net (Postfix, from userid 1001) id 11C1A5FBD2; Sun, 13 Feb 2022 10:28:50 -0500 (EST) Received: (nullmailer pid 3015408 invoked by uid 1000); Sun, 13 Feb 2022 15:27:56 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH 3/4] CLI/insert: split copy_fd Date: Sun, 13 Feb 2022 11:27:50 -0400 Message-Id: <20220213152751.3015318-4-david@tethera.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220213152751.3015318-1-david@tethera.net> References: <20220213152751.3015318-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: UDJWL2AUS4CFVXKAWQAXV4XZKLP7YOFK X-Message-ID-Hash: UDJWL2AUS4CFVXKAWQAXV4XZKLP7YOFK X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Country: DE ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1644766133; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-owner:list-unsubscribe:list-subscribe:list-post; bh=1buboffblmgYQy8xGZ7SjLYO0UcKQMkfmXoKofaFTEY=; b=tAJedQn+KpIQnHihYC6SeKeo6x4pIgPd3eXuLfKWU0O4yhyMPdi54HMQ/kDaepZzSyqiwI KO0NcQKPM+ALMpq1iKBqaLpgWfu4BOFvCB25UYu+PXm6zl+MlaKqel6Egar6WmR6HX8uIF hinRt9vhQvZXiwcWA8ApLSyL4rYwEMKCoi86wYCgg4HSahC+jmm4vkvrB2/FlFfKJqEL+y Iis96jnsJj7+ak/sIyEc0z/C6zHvgSqTTcTatVstfe8qZ8ImnKCqHWcz78hQ0XjV74AVVC uw8H2d08ZnmImZtfdwtN8bCBFRqHQQityKua79hesP+cNzfJUnTsdKnTLAFtfg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1644766133; a=rsa-sha256; cv=none; b=PL73bXiiuUe87dLheZuj2kHt4csXvvnO2q2tgys5/imFlhUL3yQCT+zvvgZ+ZOdVWUZJ9l AIkBza6L4l1lEymQCyyAT3/FruIWAu5Vw8OSK+ziqmhnum2F99sclouHQbaii+x7xf22mn jGRVu99hNoWIWwJ+bvpq1XBmZ0YQvpfbIfIptaqtZHJORxaKJXP15aTaNSIjnKgwFgiq6T N7nvuXwzts1PohOO2xDJyHTu5ngolEQFOsoS0eWPQeUYB9nz5tLmpQOzNisEHFacMCL7ag tD+KcWOsIkisI+EY2oMEp5bSH0gMXNcVebA5Hv6vA6JLTlwVOQv21y3MGw2NgA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.34 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: D3DD7EBF4 X-Spam-Score: -1.34 X-Migadu-Scanner: scn1.migadu.com X-TUID: IPD6FTN5eRir This helps maintainability and enables code-reuse of our home-brewed buffered-write code. This commit is mostly code movement. --- notmuch-insert.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 214d4d03..bec25a2a 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -241,6 +241,24 @@ maildir_mktemp (const void *ctx, const char *maildir, bool world_readable, char return fd; } +static bool +write_buf (const char *buf, int fdout, ssize_t remain) { + const char *p = buf; + do { + ssize_t written = write (fdout, p, remain); + if (written < 0 && errno == EINTR) + continue; + if (written <= 0) { + fprintf (stderr, "Error: writing to temporary file: %s", + strerror (errno)); + return false; + } + p += written; + remain -= written; + } while (remain > 0); + return true; +} + /* * Copy fdin to fdout, return true on success, and false on errors and * empty input. @@ -253,7 +271,6 @@ copy_fd (int fdout, int fdin) while (! interrupted) { ssize_t remain; char buf[4096]; - char *p; remain = read (fdin, buf, sizeof (buf)); if (remain == 0) @@ -265,21 +282,9 @@ copy_fd (int fdout, int fdin) strerror (errno)); return false; } - - p = buf; - do { - ssize_t written = write (fdout, p, remain); - if (written < 0 && errno == EINTR) - continue; - if (written <= 0) { - fprintf (stderr, "Error: writing to temporary file: %s", - strerror (errno)); - return false; - } - p += written; - remain -= written; - empty = false; - } while (remain > 0); + if (! write_buf (buf, fdout, remain)) + return false; + empty = false; } return (! interrupted && ! empty); -- 2.34.1