blob: d45b536021e15c5a674f7969be39f238194bef99 [file] [log] [blame]
Rene Scharfe7230e6d2006-08-21 20:43:43 +02001#include "cache.h"
2
Theodore Ts'o06f59e92007-06-29 13:40:46 -04003/*
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)) {
Johannes Sixtb2f5e262007-08-17 18:40:36 +020037 /*
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)
Theodore Ts'o06f59e92007-06-29 13:40:46 -040043 exit(0);
Thomas Rastd824cbb2009-06-27 17:58:46 +020044 die_errno("write failure on '%s'", desc);
Theodore Ts'o06f59e92007-06-29 13:40:46 -040045 }
46}
47
Linus Torvalds4c81b032008-05-30 08:42:16 -070048void fsync_or_die(int fd, const char *msg)
49{
50 if (fsync(fd) < 0) {
Thomas Rastd824cbb2009-06-27 17:58:46 +020051 die_errno("fsync error on '%s'", msg);
Linus Torvalds4c81b032008-05-30 08:42:16 -070052 }
53}
54
Andy Whitcroft93822c22007-01-08 15:58:23 +000055void write_or_die(int fd, const void *buf, size_t count)
56{
Linus Torvaldsd34cf192007-01-11 20:23:00 -080057 if (write_in_full(fd, buf, count) < 0) {
Andy Whitcroft93822c22007-01-08 15:58:23 +000058 if (errno == EPIPE)
59 exit(0);
Thomas Rastd824cbb2009-06-27 17:58:46 +020060 die_errno("write error");
Andy Whitcroft93822c22007-01-08 15:58:23 +000061 }
Andy Whitcrofte0814052007-01-08 15:57:52 +000062}
63
64int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
65{
Linus Torvaldsd34cf192007-01-11 20:23:00 -080066 if (write_in_full(fd, buf, count) < 0) {
Andy Whitcrofte0814052007-01-08 15:57:52 +000067 if (errno == EPIPE)
68 exit(0);
69 fprintf(stderr, "%s: write error (%s)\n",
70 msg, strerror(errno));
71 return 0;
Christian Couder7cf67202006-08-31 08:42:11 +020072 }
73
74 return 1;
75}
Andy Whitcroft825cee72007-01-02 14:12:09 +000076
Andy Whitcrofte0814052007-01-08 15:57:52 +000077int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
Andy Whitcroft825cee72007-01-02 14:12:09 +000078{
Linus Torvaldsd34cf192007-01-11 20:23:00 -080079 if (write_in_full(fd, buf, count) < 0) {
Andy Whitcrofte0814052007-01-08 15:57:52 +000080 fprintf(stderr, "%s: write error (%s)\n",
81 msg, strerror(errno));
82 return 0;
Andy Whitcroft825cee72007-01-02 14:12:09 +000083 }
84
85 return 1;
86}