]> git.ipfire.org Git - thirdparty/git.git/blame - write_or_die.c
Merge branch 'rs/use-modern-git-merge-syntax' of git-gui into rs/git-gui-use-modern...
[thirdparty/git.git] / write_or_die.c
CommitLineData
7230e6d0 1#include "cache.h"
9658846c 2#include "run-command.h"
7230e6d0 3
756e676c
JK
4static void check_pipe(int err)
5{
6 if (err == EPIPE) {
9658846c
JK
7 if (in_async())
8 async_exit(141);
9
756e676c
JK
10 signal(SIGPIPE, SIG_DFL);
11 raise(SIGPIPE);
12 /* Should never happen, but just in case... */
13 exit(141);
14 }
15}
16
06f59e9f
TT
17/*
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).
21 *
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).
26 *
27 * If the file handle is stdout, and stdout is a file, then skip the
28 * flush entirely since it's not needed.
29 */
30void maybe_flush_or_die(FILE *f, const char *desc)
31{
32 static int skip_stdout_flush = -1;
33 struct stat st;
34 char *cp;
35
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;
46 }
47 if (skip_stdout_flush && !ferror(f))
48 return;
49 }
50 if (fflush(f)) {
756e676c 51 check_pipe(errno);
d824cbba 52 die_errno("write failure on '%s'", desc);
06f59e9f
TT
53 }
54}
55
9540ce50
JK
56void fprintf_or_die(FILE *f, const char *fmt, ...)
57{
58 va_list ap;
59 int ret;
60
61 va_start(ap, fmt);
62 ret = vfprintf(f, fmt, ap);
63 va_end(ap);
64
65 if (ret < 0) {
66 check_pipe(errno);
67 die_errno("write error");
68 }
69}
70
4c81b03e
LT
71void fsync_or_die(int fd, const char *msg)
72{
73 if (fsync(fd) < 0) {
d824cbba 74 die_errno("fsync error on '%s'", msg);
4c81b03e
LT
75 }
76}
77
93822c22 78void write_or_die(int fd, const void *buf, size_t count)
7cf67205 79{
d34cf19b 80 if (write_in_full(fd, buf, count) < 0) {
756e676c 81 check_pipe(errno);
d824cbba 82 die_errno("write error");
e0814056 83 }
e0814056
AW
84}
85
86int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
87{
d34cf19b 88 if (write_in_full(fd, buf, count) < 0) {
756e676c 89 check_pipe(errno);
e0814056
AW
90 fprintf(stderr, "%s: write error (%s)\n",
91 msg, strerror(errno));
92 return 0;
7cf67205
CC
93 }
94
95 return 1;
96}
825cee7b 97
e0814056 98int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
825cee7b 99{
d34cf19b 100 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
101 fprintf(stderr, "%s: write error (%s)\n",
102 msg, strerror(errno));
103 return 0;
825cee7b
AW
104 }
105
106 return 1;
107}