]>
Commit | Line | Data |
---|---|---|
7230e6d0 RS |
1 | #include "cache.h" |
2 | ||
06f59e9f TT |
3 | /* |
4 | * Some cases use stdio, but want to flush after the write | |
5 | * to get error handling (and to get better interactive | |
6 | * behaviour - not buffering excessively). | |
7 | * | |
8 | * Of course, if the flush happened within the write itself, | |
9 | * we've already lost the error code, and cannot report it any | |
10 | * more. So we just ignore that case instead (and hope we get | |
11 | * the right error code on the flush). | |
12 | * | |
13 | * If the file handle is stdout, and stdout is a file, then skip the | |
14 | * flush entirely since it's not needed. | |
15 | */ | |
16 | void maybe_flush_or_die(FILE *f, const char *desc) | |
17 | { | |
18 | static int skip_stdout_flush = -1; | |
19 | struct stat st; | |
20 | char *cp; | |
21 | ||
22 | if (f == stdout) { | |
23 | if (skip_stdout_flush < 0) { | |
24 | cp = getenv("GIT_FLUSH"); | |
25 | if (cp) | |
26 | skip_stdout_flush = (atoi(cp) == 0); | |
27 | else if ((fstat(fileno(stdout), &st) == 0) && | |
28 | S_ISREG(st.st_mode)) | |
29 | skip_stdout_flush = 1; | |
30 | else | |
31 | skip_stdout_flush = 0; | |
32 | } | |
33 | if (skip_stdout_flush && !ferror(f)) | |
34 | return; | |
35 | } | |
36 | if (fflush(f)) { | |
b2f5e268 JS |
37 | /* |
38 | * On Windows, EPIPE is returned only by the first write() | |
39 | * after the reading end has closed its handle; subsequent | |
40 | * write()s return EINVAL. | |
41 | */ | |
42 | if (errno == EPIPE || errno == EINVAL) | |
06f59e9f TT |
43 | exit(0); |
44 | die("write failure on %s: %s", desc, strerror(errno)); | |
45 | } | |
46 | } | |
47 | ||
0104ca09 | 48 | ssize_t read_in_full(int fd, void *buf, size_t count) |
75025ccd SP |
49 | { |
50 | char *p = buf; | |
93d26e4c | 51 | ssize_t total = 0; |
75025ccd SP |
52 | |
53 | while (count > 0) { | |
4494c656 LT |
54 | ssize_t loaded = xread(fd, p, count); |
55 | if (loaded <= 0) | |
56 | return total ? total : loaded; | |
75025ccd SP |
57 | count -= loaded; |
58 | p += loaded; | |
93d26e4c | 59 | total += loaded; |
75025ccd | 60 | } |
93d26e4c AW |
61 | |
62 | return total; | |
63 | } | |
64 | ||
0104ca09 | 65 | ssize_t write_in_full(int fd, const void *buf, size_t count) |
7230e6d0 RS |
66 | { |
67 | const char *p = buf; | |
93822c22 | 68 | ssize_t total = 0; |
7230e6d0 RS |
69 | |
70 | while (count > 0) { | |
d848804a | 71 | ssize_t written = xwrite(fd, p, count); |
f6aa66cb LT |
72 | if (written < 0) |
73 | return -1; | |
74 | if (!written) { | |
75 | errno = ENOSPC; | |
76 | return -1; | |
7230e6d0 RS |
77 | } |
78 | count -= written; | |
79 | p += written; | |
93822c22 | 80 | total += written; |
7230e6d0 | 81 | } |
93822c22 AW |
82 | |
83 | return total; | |
7230e6d0 | 84 | } |
7cf67205 | 85 | |
4c81b03e LT |
86 | void fsync_or_die(int fd, const char *msg) |
87 | { | |
88 | if (fsync(fd) < 0) { | |
89 | die("%s: fsync error (%s)", msg, strerror(errno)); | |
90 | } | |
91 | } | |
92 | ||
93822c22 | 93 | void write_or_die(int fd, const void *buf, size_t count) |
7cf67205 | 94 | { |
d34cf19b | 95 | if (write_in_full(fd, buf, count) < 0) { |
93822c22 AW |
96 | if (errno == EPIPE) |
97 | exit(0); | |
98 | die("write error (%s)", strerror(errno)); | |
e0814056 | 99 | } |
e0814056 AW |
100 | } |
101 | ||
102 | int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg) | |
103 | { | |
d34cf19b | 104 | if (write_in_full(fd, buf, count) < 0) { |
e0814056 AW |
105 | if (errno == EPIPE) |
106 | exit(0); | |
107 | fprintf(stderr, "%s: write error (%s)\n", | |
108 | msg, strerror(errno)); | |
109 | return 0; | |
7cf67205 CC |
110 | } |
111 | ||
112 | return 1; | |
113 | } | |
825cee7b | 114 | |
e0814056 | 115 | int write_or_whine(int fd, const void *buf, size_t count, const char *msg) |
825cee7b | 116 | { |
d34cf19b | 117 | if (write_in_full(fd, buf, count) < 0) { |
e0814056 AW |
118 | fprintf(stderr, "%s: write error (%s)\n", |
119 | msg, strerror(errno)); | |
120 | return 0; | |
825cee7b AW |
121 | } |
122 | ||
123 | return 1; | |
124 | } |