Documentation/technical: signed commit format
[git.git] / write_or_die.c
blob49e80aa222132c3c151b79bd37749d92a12f745a
1 #include "cache.h"
2 #include "run-command.h"
4 static void check_pipe(int err)
6 if (err == EPIPE) {
7 if (in_async())
8 async_exit(141);
10 signal(SIGPIPE, SIG_DFL);
11 raise(SIGPIPE);
12 /* Should never happen, but just in case... */
13 exit(141);
18 * Some cases use stdio, but want to flush after the write
19 * to get error handling (and to get better interactive
20 * behaviour - not buffering excessively).
22 * Of course, if the flush happened within the write itself,
23 * we've already lost the error code, and cannot report it any
24 * more. So we just ignore that case instead (and hope we get
25 * the right error code on the flush).
27 * If the file handle is stdout, and stdout is a file, then skip the
28 * flush entirely since it's not needed.
30 void maybe_flush_or_die(FILE *f, const char *desc)
32 static int skip_stdout_flush = -1;
33 struct stat st;
34 char *cp;
36 if (f == stdout) {
37 if (skip_stdout_flush < 0) {
38 cp = getenv("GIT_FLUSH");
39 if (cp)
40 skip_stdout_flush = (atoi(cp) == 0);
41 else if ((fstat(fileno(stdout), &st) == 0) &&
42 S_ISREG(st.st_mode))
43 skip_stdout_flush = 1;
44 else
45 skip_stdout_flush = 0;
47 if (skip_stdout_flush && !ferror(f))
48 return;
50 if (fflush(f)) {
51 check_pipe(errno);
52 die_errno("write failure on '%s'", desc);
56 void fprintf_or_die(FILE *f, const char *fmt, ...)
58 va_list ap;
59 int ret;
61 va_start(ap, fmt);
62 ret = vfprintf(f, fmt, ap);
63 va_end(ap);
65 if (ret < 0) {
66 check_pipe(errno);
67 die_errno("write error");
71 void fsync_or_die(int fd, const char *msg)
73 if (fsync(fd) < 0) {
74 die_errno("fsync error on '%s'", msg);
78 void write_or_die(int fd, const void *buf, size_t count)
80 if (write_in_full(fd, buf, count) < 0) {
81 check_pipe(errno);
82 die_errno("write error");
86 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
88 if (write_in_full(fd, buf, count) < 0) {
89 check_pipe(errno);
90 fprintf(stderr, "%s: write error (%s)\n",
91 msg, strerror(errno));
92 return 0;
95 return 1;
98 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
100 if (write_in_full(fd, buf, count) < 0) {
101 fprintf(stderr, "%s: write error (%s)\n",
102 msg, strerror(errno));
103 return 0;
106 return 1;