unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [RFC PATCH] CLI/insert: convert copy_fd to use stdio
Date: Sun, 13 Feb 2022 10:10:45 -0400	[thread overview]
Message-ID: <20220213141045.2891934-1-david@tethera.net> (raw)

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

                 reply	other threads:[~2022-02-13 14:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220213141045.2891934-1-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).