diff options
Diffstat (limited to 'write-or-die.c')
-rw-r--r-- | write-or-die.c | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/write-or-die.c b/write-or-die.c index 0b1ec8190b..c4fd91b5b4 100644 --- a/write-or-die.c +++ b/write-or-die.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "config.h" #include "run-command.h" /* @@ -55,12 +56,37 @@ void fprintf_or_die(FILE *f, const char *fmt, ...) } } +static int maybe_fsync(int fd) +{ + if (use_fsync < 0) + use_fsync = git_env_bool("GIT_TEST_FSYNC", 1); + if (!use_fsync) + return 0; + + if (fsync_method == FSYNC_METHOD_WRITEOUT_ONLY && + git_fsync(fd, FSYNC_WRITEOUT_ONLY) >= 0) + return 0; + + return git_fsync(fd, FSYNC_HARDWARE_FLUSH); +} + void fsync_or_die(int fd, const char *msg) { - while (fsync(fd) < 0) { - if (errno != EINTR) - die_errno("fsync error on '%s'", msg); - } + if (maybe_fsync(fd) < 0) + die_errno("fsync error on '%s'", msg); +} + +int fsync_component(enum fsync_component component, int fd) +{ + if (fsync_components & component) + return maybe_fsync(fd); + return 0; +} + +void fsync_component_or_die(enum fsync_component component, int fd, const char *msg) +{ + if (fsync_components & component) + fsync_or_die(fd, msg); } void write_or_die(int fd, const void *buf, size_t count) |