skip t5512 because remote does not yet work
[git/platforms/storm.git] / write_or_die.c
blob40b046acb048d4daa91ab7c11db46ebbd6781612
1 #include "cache.h"
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).
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).
13 * If the file handle is stdout, and stdout is a file, then skip the
14 * flush entirely since it's not needed.
16 void maybe_flush_or_die(FILE *f, const char *desc)
18 static int skip_stdout_flush = -1;
19 struct stat st;
20 char *cp;
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;
33 if (skip_stdout_flush && !ferror(f))
34 return;
36 if (fflush(f)) {
37 #ifndef __MINGW32__
38 if (errno == EPIPE)
39 #else
41 * On Windows, EPIPE is returned only by the first write()
42 * after the reading end has closed its handle; subsequent
43 * write()s return EINVAL.
45 if (errno == EPIPE || errno == EINVAL)
46 #endif
47 exit(0);
48 die("write failure on %s: %s", desc, strerror(errno));
52 int read_in_full(int fd, void *buf, size_t count)
54 char *p = buf;
55 ssize_t total = 0;
57 while (count > 0) {
58 ssize_t loaded = xread(fd, p, count);
59 if (loaded <= 0)
60 return total ? total : loaded;
61 count -= loaded;
62 p += loaded;
63 total += loaded;
66 return total;
69 int write_in_full(int fd, const void *buf, size_t count)
71 const char *p = buf;
72 ssize_t total = 0;
74 while (count > 0) {
75 ssize_t written = xwrite(fd, p, count);
76 if (written < 0)
77 return -1;
78 if (!written) {
79 errno = ENOSPC;
80 return -1;
82 count -= written;
83 p += written;
84 total += written;
87 return total;
90 void write_or_die(int fd, const void *buf, size_t count)
92 if (write_in_full(fd, buf, count) < 0) {
93 if (errno == EPIPE)
94 exit(0);
95 die("write error (%s)", strerror(errno));
99 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
101 if (write_in_full(fd, buf, count) < 0) {
102 if (errno == EPIPE)
103 exit(0);
104 fprintf(stderr, "%s: write error (%s)\n",
105 msg, strerror(errno));
106 return 0;
109 return 1;
112 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
114 if (write_in_full(fd, buf, count) < 0) {
115 fprintf(stderr, "%s: write error (%s)\n",
116 msg, strerror(errno));
117 return 0;
120 return 1;