Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 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)) { |
Johannes Sixt | b2f5e26 | 2007-08-17 18:40:36 +0200 | [diff] [blame] | 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) |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 43 | exit(0); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 44 | die_errno("write failure on '%s'", desc); |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 48 | void fsync_or_die(int fd, const char *msg) |
| 49 | { |
| 50 | if (fsync(fd) < 0) { |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 51 | die_errno("fsync error on '%s'", msg); |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 55 | void write_or_die(int fd, const void *buf, size_t count) |
| 56 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 57 | if (write_in_full(fd, buf, count) < 0) { |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 58 | if (errno == EPIPE) |
| 59 | exit(0); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 60 | die_errno("write error"); |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 61 | } |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg) |
| 65 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 66 | if (write_in_full(fd, buf, count) < 0) { |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 67 | if (errno == EPIPE) |
| 68 | exit(0); |
| 69 | fprintf(stderr, "%s: write error (%s)\n", |
| 70 | msg, strerror(errno)); |
| 71 | return 0; |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return 1; |
| 75 | } |
Andy Whitcroft | 825cee7 | 2007-01-02 14:12:09 +0000 | [diff] [blame] | 76 | |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 77 | int write_or_whine(int fd, const void *buf, size_t count, const char *msg) |
Andy Whitcroft | 825cee7 | 2007-01-02 14:12:09 +0000 | [diff] [blame] | 78 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 79 | if (write_in_full(fd, buf, count) < 0) { |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 80 | fprintf(stderr, "%s: write error (%s)\n", |
| 81 | msg, strerror(errno)); |
| 82 | return 0; |
Andy Whitcroft | 825cee7 | 2007-01-02 14:12:09 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return 1; |
| 86 | } |