[RFC PATCH] CLI/insert: convert copy_fd to use stdio

Subject: [RFC PATCH] CLI/insert: convert copy_fd to use stdio

Date: Sun, 13 Feb 2022 10:10:45 -0400

To: notmuch@notmuchmail.org

Cc:

From: David Bremner


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

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: