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

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

Reply via email to