unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [PATCH 3/4] CLI/insert: split copy_fd
Date: Sun, 13 Feb 2022 11:27:50 -0400	[thread overview]
Message-ID: <20220213152751.3015318-4-david@tethera.net> (raw)
In-Reply-To: <20220213152751.3015318-1-david@tethera.net>

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

  parent reply	other threads:[~2022-02-13 15:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-13 15:27 Escape mbox "From " header in notmuch-insert David Bremner
2022-02-13 15:27 ` [PATCH 1/4] test: start new corpus of test messages for indexing code David Bremner
2022-02-13 15:27 ` [PATCH 2/4] test: add known broken test for insert with mbox as input David Bremner
2022-02-13 15:27 ` David Bremner [this message]
2022-02-13 15:27 ` [PATCH 4/4] CLI/insert: escape envelope from David Bremner
2022-02-19 21:47   ` Tomi Ollila
2022-02-20  2:25     ` David Bremner

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=20220213152751.3015318-4-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).