"git checkout -- paths..." should error out when paths cannot be written
[git/git.git] / write_or_die.c
CommitLineData
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 */
16void 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)) {
37 if (errno == EPIPE)
38 exit(0);
39 die("write failure on %s: %s", desc, strerror(errno));
40 }
41}
42
0104ca09 43ssize_t read_in_full(int fd, void *buf, size_t count)
75025ccd
SP
44{
45 char *p = buf;
93d26e4c 46 ssize_t total = 0;
75025ccd
SP
47
48 while (count > 0) {
4494c656
LT
49 ssize_t loaded = xread(fd, p, count);
50 if (loaded <= 0)
51 return total ? total : loaded;
75025ccd
SP
52 count -= loaded;
53 p += loaded;
93d26e4c 54 total += loaded;
75025ccd 55 }
93d26e4c
AW
56
57 return total;
58}
59
0104ca09 60ssize_t write_in_full(int fd, const void *buf, size_t count)
7230e6d0
RS
61{
62 const char *p = buf;
93822c22 63 ssize_t total = 0;
7230e6d0
RS
64
65 while (count > 0) {
d848804a 66 ssize_t written = xwrite(fd, p, count);
f6aa66cb
LT
67 if (written < 0)
68 return -1;
69 if (!written) {
70 errno = ENOSPC;
71 return -1;
7230e6d0
RS
72 }
73 count -= written;
74 p += written;
93822c22 75 total += written;
7230e6d0 76 }
93822c22
AW
77
78 return total;
7230e6d0 79}
7cf67205 80
93822c22 81void write_or_die(int fd, const void *buf, size_t count)
7cf67205 82{
d34cf19b 83 if (write_in_full(fd, buf, count) < 0) {
93822c22
AW
84 if (errno == EPIPE)
85 exit(0);
86 die("write error (%s)", strerror(errno));
e0814056 87 }
e0814056
AW
88}
89
90int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
91{
d34cf19b 92 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
93 if (errno == EPIPE)
94 exit(0);
95 fprintf(stderr, "%s: write error (%s)\n",
96 msg, strerror(errno));
97 return 0;
7cf67205
CC
98 }
99
100 return 1;
101}
825cee7b 102
e0814056 103int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
825cee7b 104{
d34cf19b 105 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
106 fprintf(stderr, "%s: write error (%s)\n",
107 msg, strerror(errno));
108 return 0;
825cee7b
AW
109 }
110
111 return 1;
112}