Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 3 | static void check_pipe(int err) |
| 4 | { |
| 5 | if (err == EPIPE) { |
| 6 | signal(SIGPIPE, SIG_DFL); |
| 7 | raise(SIGPIPE); |
| 8 | /* Should never happen, but just in case... */ |
| 9 | exit(141); |
| 10 | } |
| 11 | } |
| 12 | |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 13 | /* |
| 14 | * Some cases use stdio, but want to flush after the write |
| 15 | * to get error handling (and to get better interactive |
| 16 | * behaviour - not buffering excessively). |
| 17 | * |
| 18 | * Of course, if the flush happened within the write itself, |
| 19 | * we've already lost the error code, and cannot report it any |
| 20 | * more. So we just ignore that case instead (and hope we get |
| 21 | * the right error code on the flush). |
| 22 | * |
| 23 | * If the file handle is stdout, and stdout is a file, then skip the |
| 24 | * flush entirely since it's not needed. |
| 25 | */ |
| 26 | void maybe_flush_or_die(FILE *f, const char *desc) |
| 27 | { |
| 28 | static int skip_stdout_flush = -1; |
| 29 | struct stat st; |
| 30 | char *cp; |
| 31 | |
| 32 | if (f == stdout) { |
| 33 | if (skip_stdout_flush < 0) { |
| 34 | cp = getenv("GIT_FLUSH"); |
| 35 | if (cp) |
| 36 | skip_stdout_flush = (atoi(cp) == 0); |
| 37 | else if ((fstat(fileno(stdout), &st) == 0) && |
| 38 | S_ISREG(st.st_mode)) |
| 39 | skip_stdout_flush = 1; |
| 40 | else |
| 41 | skip_stdout_flush = 0; |
| 42 | } |
| 43 | if (skip_stdout_flush && !ferror(f)) |
| 44 | return; |
| 45 | } |
| 46 | if (fflush(f)) { |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 47 | check_pipe(errno); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 48 | die_errno("write failure on '%s'", desc); |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 52 | void fsync_or_die(int fd, const char *msg) |
| 53 | { |
| 54 | if (fsync(fd) < 0) { |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 55 | die_errno("fsync error on '%s'", msg); |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 59 | void write_or_die(int fd, const void *buf, size_t count) |
| 60 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 61 | if (write_in_full(fd, buf, count) < 0) { |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 62 | check_pipe(errno); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 63 | die_errno("write error"); |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 64 | } |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg) |
| 68 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 69 | if (write_in_full(fd, buf, count) < 0) { |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 70 | check_pipe(errno); |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 71 | fprintf(stderr, "%s: write error (%s)\n", |
| 72 | msg, strerror(errno)); |
| 73 | return 0; |
Christian Couder | 7cf6720 | 2006-08-31 08:42:11 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return 1; |
| 77 | } |
Andy Whitcroft | 825cee7 | 2007-01-02 14:12:09 +0000 | [diff] [blame] | 78 | |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 79 | 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] | 80 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 81 | if (write_in_full(fd, buf, count) < 0) { |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 82 | fprintf(stderr, "%s: write error (%s)\n", |
| 83 | msg, strerror(errno)); |
| 84 | return 0; |
Andy Whitcroft | 825cee7 | 2007-01-02 14:12:09 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return 1; |
| 88 | } |