From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id OIFvJ4cRCWKdlAAAgWs5BA (envelope-from ) for ; Sun, 13 Feb 2022 15:11:19 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id mPbTJIcRCWIqHgEA9RJhRA (envelope-from ) for ; Sun, 13 Feb 2022 15:11:19 +0100 Received: from mail.notmuchmail.org (yantan.tethera.net [135.181.149.255]) (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 56AC2113F7 for ; Sun, 13 Feb 2022 15:11:19 +0100 (CET) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 48AE05F6BC; Sun, 13 Feb 2022 14:11:17 +0000 (UTC) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id E4AC25F401 for ; Sun, 13 Feb 2022 14:11:14 +0000 (UTC) Received: by fethera.tethera.net (Postfix, from userid 1001) id DE9505FBD2; Sun, 13 Feb 2022 09:11:13 -0500 (EST) Received: (nullmailer pid 2898628 invoked by uid 1000); Sun, 13 Feb 2022 14:10:51 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [RFC PATCH] CLI/insert: convert copy_fd to use stdio Date: Sun, 13 Feb 2022 10:10:45 -0400 Message-Id: <20220213141045.2891934-1-david@tethera.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Message-ID-Hash: WQPUZEOQU3L2LDJQDZ44CQTE34MSGTUZ X-Message-ID-Hash: WQPUZEOQU3L2LDJQDZ44CQTE34MSGTUZ 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=1644761479; 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:list-id:list-help: list-owner:list-unsubscribe:list-subscribe:list-post; bh=7AV4PffZWnhPlIXf3BAYiWHDT4c3QosjWhXFj3GHShI=; b=jMwAWpxIW9IHtTLmzbwAJdDZbp9g5LuUpKPBrIa4fM0mEs3Va/mReJl6ygutq8I7gWUUbv asev/PFMMqcLyt8TPU1HYR2B7uECxFLW/GbgbgCNYP3JDh+bMNWrwQqH351P2u3WuRR+eO /4t3evxLQqLq/jBMi95oxqO7e1WR8z8/GVsAcEXmfO9FMpgpG6AI/2lil2+vNHqLg1KUxN QLapRr0YiJgp5JBifwcj23zexjNjsEmVrfnU8FCpziKVRhlMu+IRGVAou5R0VwjtL85mgP 8YlyTVGQ6zv4z7XSKbZ0ujt6rfZjAzbw1mFbr/YqbW4KIosZ48CgKDq9PgEivQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1644761479; a=rsa-sha256; cv=none; b=q7Cjo6v3ppoqgA3YzfvwzqhIauQYtBCwXjGfv1l8tFaJZGpf56oKzUO7iU9ItPVdMQReZ9 Ph40Hw48W5LeNGOrEh5PcmM0V0wl365pIB6jgOwlVbu8YCH9JIT8GCmCQAtqsaobX+mWhN r9LfG6p/7YiD0YftJ250sTQygfAYJENe8i2s9lQZqX+cWfpD89cPV4O2mAeu4iMF0irT0n +QnHaXfENAzE3guixOaGY61ypkBD1mWUbSsylkKxf/t1AHuEZWiYZQqLbFImJaKW8cPU2Z HHPX73PDvdCHXnVIMxiZy6Oh10wibz9OZOr0E83JwoaT3T6cnd2UcVEwJ7jJFg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 135.181.149.255 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.85 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 135.181.149.255 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 56AC2113F7 X-Spam-Score: -1.85 X-Migadu-Scanner: scn0.migadu.com X-TUID: tGqJshEIVhr9 Rather than doing our own buffer management, use stdio, and line based I/O. This will simplify doing some simple header filtering. --- As hinted in id:87pmnqx3mk.fsf@tethera.net , this looks like a dead end to me at the moment, but perhaps of interest to someone looking at a larger rewrite of notmuch-insert. notmuch-insert.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 214d4d03..4ba6f993 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -249,38 +249,33 @@ static bool copy_fd (int fdout, int fdin) { bool empty = true; + FILE *fin = fdopen (fdin, "r"); + FILE *fout = fdopen (fdout, "w"); + char *line = NULL; + size_t line_size; + ssize_t line_len; - while (! interrupted) { - ssize_t remain; - char buf[4096]; - char *p; + if (! fin || ! fout) + return false; - remain = read (fdin, buf, sizeof (buf)); - if (remain == 0) - break; - if (remain < 0) { + while (! interrupted && + ( (line_len = getline (&line, &line_size, fin)) >= 0)) { + empty = false; + if (fputs (line, fout) < 0) { if (errno == EINTR) continue; - fprintf (stderr, "Error: reading from standard input: %s\n", + fprintf (stderr, "Error: writing to temporary file: %s", 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 (line_len < 0 && (errno == EINVAL || errno == ENOMEM)) { + fprintf (stderr, "Error: reading from standard input: %s\n", + strerror (errno)); + return false; + } + + fflush(fout); return (! interrupted && ! empty); } -- 2.34.1