mirror of
https://git.notmuchmail.org/git/notmuch
synced 2024-11-28 21:54:10 +01:00
CLI/insert: split copy_fd
This helps maintainability and enables code-reuse of our home-brewed buffered-write code. This commit is mostly code movement.
This commit is contained in:
parent
4a380371e7
commit
329bb4ddf4
1 changed files with 23 additions and 16 deletions
|
@ -241,6 +241,26 @@ maildir_mktemp (const void *ctx, const char *maildir, bool world_readable, char
|
||||||
return fd;
|
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
|
* Copy fdin to fdout, return true on success, and false on errors and
|
||||||
* empty input.
|
* empty input.
|
||||||
|
@ -253,7 +273,6 @@ copy_fd (int fdout, int fdin)
|
||||||
while (! interrupted) {
|
while (! interrupted) {
|
||||||
ssize_t remain;
|
ssize_t remain;
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
char *p;
|
|
||||||
|
|
||||||
remain = read (fdin, buf, sizeof (buf));
|
remain = read (fdin, buf, sizeof (buf));
|
||||||
if (remain == 0)
|
if (remain == 0)
|
||||||
|
@ -265,21 +284,9 @@ copy_fd (int fdout, int fdin)
|
||||||
strerror (errno));
|
strerror (errno));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (! write_buf (buf, fdout, remain))
|
||||||
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;
|
return false;
|
||||||
}
|
|
||||||
p += written;
|
|
||||||
remain -= written;
|
|
||||||
empty = false;
|
empty = false;
|
||||||
} while (remain > 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (! interrupted && ! empty);
|
return (! interrupted && ! empty);
|
||||||
|
|
Loading…
Reference in a new issue