Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Eric Wong | 412e4ca | 2021-10-29 00:15:52 +0000 | [diff] [blame] | 2 | #include "config.h" |
Jeff King | 9658846 | 2016-02-24 02:40:16 -0500 | [diff] [blame] | 3 | #include "run-command.h" |
Elijah Newren | d5ebb50 | 2023-03-21 06:26:01 +0000 | [diff] [blame] | 4 | #include "wrapper.h" |
Elijah Newren | d48be35 | 2023-03-21 06:26:07 +0000 | [diff] [blame] | 5 | #include "write-or-die.h" |
Rene Scharfe | 7230e6d | 2006-08-21 20:43:43 +0200 | [diff] [blame] | 6 | |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 7 | /* |
| 8 | * Some cases use stdio, but want to flush after the write |
| 9 | * to get error handling (and to get better interactive |
| 10 | * behaviour - not buffering excessively). |
| 11 | * |
| 12 | * Of course, if the flush happened within the write itself, |
| 13 | * we've already lost the error code, and cannot report it any |
| 14 | * more. So we just ignore that case instead (and hope we get |
| 15 | * the right error code on the flush). |
| 16 | * |
| 17 | * If the file handle is stdout, and stdout is a file, then skip the |
| 18 | * flush entirely since it's not needed. |
| 19 | */ |
| 20 | void maybe_flush_or_die(FILE *f, const char *desc) |
| 21 | { |
| 22 | static int skip_stdout_flush = -1; |
| 23 | struct stat st; |
| 24 | char *cp; |
| 25 | |
| 26 | if (f == stdout) { |
| 27 | if (skip_stdout_flush < 0) { |
Junio C Hamano | fd01795 | 2022-09-15 09:06:57 -0700 | [diff] [blame] | 28 | /* NEEDSWORK: make this a normal Boolean */ |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 29 | cp = getenv("GIT_FLUSH"); |
| 30 | if (cp) |
| 31 | skip_stdout_flush = (atoi(cp) == 0); |
| 32 | else if ((fstat(fileno(stdout), &st) == 0) && |
| 33 | S_ISREG(st.st_mode)) |
| 34 | skip_stdout_flush = 1; |
| 35 | else |
| 36 | skip_stdout_flush = 0; |
| 37 | } |
| 38 | if (skip_stdout_flush && !ferror(f)) |
| 39 | return; |
| 40 | } |
| 41 | if (fflush(f)) { |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 42 | check_pipe(errno); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 43 | die_errno("write failure on '%s'", desc); |
Theodore Ts'o | 06f59e9 | 2007-06-29 13:40:46 -0400 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Jeff King | 9540ce5 | 2014-09-10 06:03:52 -0400 | [diff] [blame] | 47 | void fprintf_or_die(FILE *f, const char *fmt, ...) |
| 48 | { |
| 49 | va_list ap; |
| 50 | int ret; |
| 51 | |
| 52 | va_start(ap, fmt); |
| 53 | ret = vfprintf(f, fmt, ap); |
| 54 | va_end(ap); |
| 55 | |
| 56 | if (ret < 0) { |
| 57 | check_pipe(errno); |
| 58 | die_errno("write error"); |
| 59 | } |
| 60 | } |
| 61 | |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 62 | static int maybe_fsync(int fd) |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 63 | { |
Eric Wong | 412e4ca | 2021-10-29 00:15:52 +0000 | [diff] [blame] | 64 | if (use_fsync < 0) |
| 65 | use_fsync = git_env_bool("GIT_TEST_FSYNC", 1); |
| 66 | if (!use_fsync) |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 67 | return 0; |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 68 | |
| 69 | if (fsync_method == FSYNC_METHOD_WRITEOUT_ONLY && |
| 70 | git_fsync(fd, FSYNC_WRITEOUT_ONLY) >= 0) |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 71 | return 0; |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 72 | |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 73 | return git_fsync(fd, FSYNC_HARDWARE_FLUSH); |
| 74 | } |
| 75 | |
| 76 | void fsync_or_die(int fd, const char *msg) |
| 77 | { |
| 78 | if (maybe_fsync(fd) < 0) |
Neeraj Singh | abf38ab | 2022-03-10 22:43:20 +0000 | [diff] [blame] | 79 | die_errno("fsync error on '%s'", msg); |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Neeraj Singh | 020406e | 2022-03-10 22:43:21 +0000 | [diff] [blame] | 82 | int fsync_component(enum fsync_component component, int fd) |
| 83 | { |
| 84 | if (fsync_components & component) |
| 85 | return maybe_fsync(fd); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | void fsync_component_or_die(enum fsync_component component, int fd, const char *msg) |
| 90 | { |
| 91 | if (fsync_components & component) |
| 92 | fsync_or_die(fd, msg); |
| 93 | } |
| 94 | |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 95 | void write_or_die(int fd, const void *buf, size_t count) |
| 96 | { |
Linus Torvalds | d34cf19 | 2007-01-11 20:23:00 -0800 | [diff] [blame] | 97 | if (write_in_full(fd, buf, count) < 0) { |
Jeff King | 756e676 | 2013-02-20 15:01:36 -0500 | [diff] [blame] | 98 | check_pipe(errno); |
Thomas Rast | d824cbb | 2009-06-27 17:58:46 +0200 | [diff] [blame] | 99 | die_errno("write error"); |
Andy Whitcroft | 93822c2 | 2007-01-08 15:58:23 +0000 | [diff] [blame] | 100 | } |
Andy Whitcroft | e081405 | 2007-01-08 15:57:52 +0000 | [diff] [blame] | 101 | } |
Jacob Vosmaer | 9632839 | 2021-09-01 14:54:41 +0200 | [diff] [blame] | 102 | |
| 103 | void fwrite_or_die(FILE *f, const void *buf, size_t count) |
| 104 | { |
| 105 | if (fwrite(buf, 1, count, f) != count) |
| 106 | die_errno("fwrite error"); |
| 107 | } |
| 108 | |
| 109 | void fflush_or_die(FILE *f) |
| 110 | { |
| 111 | if (fflush(f)) |
| 112 | die_errno("fflush error"); |
| 113 | } |