]>
Commit | Line | Data |
---|---|---|
7230e6d0 RS |
1 | #include "cache.h" |
2 | ||
756e676c JK |
3 | static void check_pipe(int err) |
4 | { | |
5 | if (err == EPIPE) { | |
6 | signal(SIGPIPE, SIG_DFL); | |
7 | raise(SIGPIPE); | |
8 | /* Should never happen, but just in case... */ | |
9 | exit(141); | |
10 | } | |
11 | } | |
12 | ||
06f59e9f TT |
13 | /* |
14 | * Some cases use stdio, but want to flush after the write | |
15 | * to get error handling (and to get better interactive | |
16 | * behaviour - not buffering excessively). | |
17 | * | |
18 | * Of course, if the flush happened within the write itself, | |
19 | * we've already lost the error code, and cannot report it any | |
20 | * more. So we just ignore that case instead (and hope we get | |
21 | * the right error code on the flush). | |
22 | * | |
23 | * If the file handle is stdout, and stdout is a file, then skip the | |
24 | * flush entirely since it's not needed. | |
25 | */ | |
26 | void maybe_flush_or_die(FILE *f, const char *desc) | |
27 | { | |
28 | static int skip_stdout_flush = -1; | |
29 | struct stat st; | |
30 | char *cp; | |
31 | ||
32 | if (f == stdout) { | |
33 | if (skip_stdout_flush < 0) { | |
34 | cp = getenv("GIT_FLUSH"); | |
35 | if (cp) | |
36 | skip_stdout_flush = (atoi(cp) == 0); | |
37 | else if ((fstat(fileno(stdout), &st) == 0) && | |
38 | S_ISREG(st.st_mode)) | |
39 | skip_stdout_flush = 1; | |
40 | else | |
41 | skip_stdout_flush = 0; | |
42 | } | |
43 | if (skip_stdout_flush && !ferror(f)) | |
44 | return; | |
45 | } | |
46 | if (fflush(f)) { | |
756e676c | 47 | check_pipe(errno); |
d824cbba | 48 | die_errno("write failure on '%s'", desc); |
06f59e9f TT |
49 | } |
50 | } | |
51 | ||
9540ce50 JK |
52 | void fprintf_or_die(FILE *f, const char *fmt, ...) |
53 | { | |
54 | va_list ap; | |
55 | int ret; | |
56 | ||
57 | va_start(ap, fmt); | |
58 | ret = vfprintf(f, fmt, ap); | |
59 | va_end(ap); | |
60 | ||
61 | if (ret < 0) { | |
62 | check_pipe(errno); | |
63 | die_errno("write error"); | |
64 | } | |
65 | } | |
66 | ||
4c81b03e LT |
67 | void fsync_or_die(int fd, const char *msg) |
68 | { | |
69 | if (fsync(fd) < 0) { | |
d824cbba | 70 | die_errno("fsync error on '%s'", msg); |
4c81b03e LT |
71 | } |
72 | } | |
73 | ||
93822c22 | 74 | void write_or_die(int fd, const void *buf, size_t count) |
7cf67205 | 75 | { |
d34cf19b | 76 | if (write_in_full(fd, buf, count) < 0) { |
756e676c | 77 | check_pipe(errno); |
d824cbba | 78 | die_errno("write error"); |
e0814056 | 79 | } |
e0814056 AW |
80 | } |
81 | ||
82 | int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg) | |
83 | { | |
d34cf19b | 84 | if (write_in_full(fd, buf, count) < 0) { |
756e676c | 85 | check_pipe(errno); |
e0814056 AW |
86 | fprintf(stderr, "%s: write error (%s)\n", |
87 | msg, strerror(errno)); | |
88 | return 0; | |
7cf67205 CC |
89 | } |
90 | ||
91 | return 1; | |
92 | } | |
825cee7b | 93 | |
e0814056 | 94 | int write_or_whine(int fd, const void *buf, size_t count, const char *msg) |
825cee7b | 95 | { |
d34cf19b | 96 | if (write_in_full(fd, buf, count) < 0) { |
e0814056 AW |
97 | fprintf(stderr, "%s: write error (%s)\n", |
98 | msg, strerror(errno)); | |
99 | return 0; | |
825cee7b AW |
100 | } |
101 | ||
102 | return 1; | |
103 | } |