unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [RFC PATCH] CLI/insert: convert copy_fd to use stdio
@ 2022-02-13 14:10 David Bremner
  0 siblings, 0 replies; only message in thread
From: David Bremner @ 2022-02-13 14:10 UTC (permalink / raw)
  To: notmuch

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-02-13 14:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-13 14:10 [RFC PATCH] CLI/insert: convert copy_fd to use stdio David Bremner

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).