#include "cache.h"
+#include "run-command.h"
+
+static void check_pipe(int err)
+{
+ if (err == EPIPE) {
+ if (in_async())
+ async_exit(141);
+
+ signal(SIGPIPE, SIG_DFL);
+ raise(SIGPIPE);
+ /* Should never happen, but just in case... */
+ exit(141);
+ }
+}
/*
* Some cases use stdio, but want to flush after the write
return;
}
if (fflush(f)) {
- /*
- * On Windows, EPIPE is returned only by the first write()
- * after the reading end has closed its handle; subsequent
- * write()s return EINVAL.
- */
- if (errno == EPIPE || errno == EINVAL)
- exit(0);
- die("write failure on %s: %s", desc, strerror(errno));
+ check_pipe(errno);
+ die_errno("write failure on '%s'", desc);
}
}
-ssize_t read_in_full(int fd, void *buf, size_t count)
+void fprintf_or_die(FILE *f, const char *fmt, ...)
{
- char *p = buf;
- ssize_t total = 0;
-
- while (count > 0) {
- ssize_t loaded = xread(fd, p, count);
- if (loaded <= 0)
- return total ? total : loaded;
- count -= loaded;
- p += loaded;
- total += loaded;
- }
-
- return total;
-}
+ va_list ap;
+ int ret;
-ssize_t write_in_full(int fd, const void *buf, size_t count)
-{
- const char *p = buf;
- ssize_t total = 0;
+ va_start(ap, fmt);
+ ret = vfprintf(f, fmt, ap);
+ va_end(ap);
- while (count > 0) {
- ssize_t written = xwrite(fd, p, count);
- if (written < 0)
- return -1;
- if (!written) {
- errno = ENOSPC;
- return -1;
- }
- count -= written;
- p += written;
- total += written;
+ if (ret < 0) {
+ check_pipe(errno);
+ die_errno("write error");
}
-
- return total;
}
void fsync_or_die(int fd, const char *msg)
{
if (fsync(fd) < 0) {
- die("%s: fsync error (%s)", msg, strerror(errno));
+ die_errno("fsync error on '%s'", msg);
}
}
void write_or_die(int fd, const void *buf, size_t count)
{
if (write_in_full(fd, buf, count) < 0) {
- if (errno == EPIPE)
- exit(0);
- die("write error (%s)", strerror(errno));
+ check_pipe(errno);
+ die_errno("write error");
}
}
int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
{
if (write_in_full(fd, buf, count) < 0) {
- if (errno == EPIPE)
- exit(0);
+ check_pipe(errno);
fprintf(stderr, "%s: write error (%s)\n",
msg, strerror(errno));
return 0;