Merge branch 'cb/do-not-use-test-cmp-with-a'
[git/git.git] / write-or-die.c
CommitLineData
7230e6d0 1#include "cache.h"
9658846c 2#include "run-command.h"
7230e6d0 3
06f59e9f
TT
4/*
5 * Some cases use stdio, but want to flush after the write
6 * to get error handling (and to get better interactive
7 * behaviour - not buffering excessively).
8 *
9 * Of course, if the flush happened within the write itself,
10 * we've already lost the error code, and cannot report it any
11 * more. So we just ignore that case instead (and hope we get
12 * the right error code on the flush).
13 *
14 * If the file handle is stdout, and stdout is a file, then skip the
15 * flush entirely since it's not needed.
16 */
17void maybe_flush_or_die(FILE *f, const char *desc)
18{
19 static int skip_stdout_flush = -1;
20 struct stat st;
21 char *cp;
22
23 if (f == stdout) {
24 if (skip_stdout_flush < 0) {
25 cp = getenv("GIT_FLUSH");
26 if (cp)
27 skip_stdout_flush = (atoi(cp) == 0);
28 else if ((fstat(fileno(stdout), &st) == 0) &&
29 S_ISREG(st.st_mode))
30 skip_stdout_flush = 1;
31 else
32 skip_stdout_flush = 0;
33 }
34 if (skip_stdout_flush && !ferror(f))
35 return;
36 }
37 if (fflush(f)) {
756e676c 38 check_pipe(errno);
d824cbba 39 die_errno("write failure on '%s'", desc);
06f59e9f
TT
40 }
41}
42
9540ce50
JK
43void fprintf_or_die(FILE *f, const char *fmt, ...)
44{
45 va_list ap;
46 int ret;
47
48 va_start(ap, fmt);
49 ret = vfprintf(f, fmt, ap);
50 va_end(ap);
51
52 if (ret < 0) {
53 check_pipe(errno);
54 die_errno("write error");
55 }
56}
57
4c81b03e
LT
58void fsync_or_die(int fd, const char *msg)
59{
60 if (fsync(fd) < 0) {
d824cbba 61 die_errno("fsync error on '%s'", msg);
4c81b03e
LT
62 }
63}
64
93822c22 65void write_or_die(int fd, const void *buf, size_t count)
7cf67205 66{
d34cf19b 67 if (write_in_full(fd, buf, count) < 0) {
756e676c 68 check_pipe(errno);
d824cbba 69 die_errno("write error");
e0814056 70 }
e0814056 71}