2 #include "run-command.h"
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).
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).
14 * If the file handle is stdout, and stdout is a file, then skip the
15 * flush entirely since it's not needed.
17 void maybe_flush_or_die(FILE *f
, const char *desc
)
19 static int skip_stdout_flush
= -1;
24 if (skip_stdout_flush
< 0) {
25 cp
= getenv("GIT_FLUSH");
27 skip_stdout_flush
= (atoi(cp
) == 0);
28 else if ((fstat(fileno(stdout
), &st
) == 0) &&
30 skip_stdout_flush
= 1;
32 skip_stdout_flush
= 0;
34 if (skip_stdout_flush
&& !ferror(f
))
39 die_errno("write failure on '%s'", desc
);
43 void fprintf_or_die(FILE *f
, const char *fmt
, ...)
49 ret
= vfprintf(f
, fmt
, ap
);
54 die_errno("write error");
58 void fsync_or_die(int fd
, const char *msg
)
60 while (fsync(fd
) < 0) {
62 die_errno("fsync error on '%s'", msg
);
66 void write_or_die(int fd
, const void *buf
, size_t count
)
68 if (write_in_full(fd
, buf
, count
) < 0) {
70 die_errno("write error");
74 void fwrite_or_die(FILE *f
, const void *buf
, size_t count
)
76 if (fwrite(buf
, 1, count
, f
) != count
)
77 die_errno("fwrite error");
80 void fflush_or_die(FILE *f
)
83 die_errno("fflush error");