]> git.ipfire.org Git - thirdparty/git.git/blame - write-or-die.c
Documentation/RelNotes/2.45.0.txt: fix typo
[thirdparty/git.git] / write-or-die.c
CommitLineData
d48be35c 1#include "git-compat-util.h"
b1bda751 2#include "parse.h"
9658846c 3#include "run-command.h"
d48be35c 4#include "write-or-die.h"
7230e6d0 5
06f59e9f
TT
6/*
7 * Some cases use stdio, but want to flush after the write
8 * to get error handling (and to get better interactive
9 * behaviour - not buffering excessively).
10 *
11 * Of course, if the flush happened within the write itself,
12 * we've already lost the error code, and cannot report it any
13 * more. So we just ignore that case instead (and hope we get
14 * the right error code on the flush).
15 *
16 * If the file handle is stdout, and stdout is a file, then skip the
17 * flush entirely since it's not needed.
18 */
19void maybe_flush_or_die(FILE *f, const char *desc)
20{
06f59e9f 21 if (f == stdout) {
b40ba17e
JH
22 static int force_flush_stdout = -1;
23
24 if (force_flush_stdout < 0) {
25 force_flush_stdout = git_env_bool("GIT_FLUSH", -1);
26 if (force_flush_stdout < 0) {
556e6803
CP
27 struct stat st;
28 if (fstat(fileno(stdout), &st))
b40ba17e 29 force_flush_stdout = 1;
556e6803 30 else
b40ba17e 31 force_flush_stdout = !S_ISREG(st.st_mode);
556e6803 32 }
06f59e9f 33 }
b40ba17e 34 if (!force_flush_stdout && !ferror(f))
06f59e9f
TT
35 return;
36 }
37 if (fflush(f)) {
756e676c 38 check_pipe(errno);
d824cbba 39 die_errno("write failure on '%s'", desc);
06f59e9f
TT
40 }
41}
42
9540ce50
JK
43void fprintf_or_die(FILE *f, const char *fmt, ...)
44{
45 va_list ap;
46 int ret;
47
48 va_start(ap, fmt);
49 ret = vfprintf(f, fmt, ap);
50 va_end(ap);
51
52 if (ret < 0) {
53 check_pipe(errno);
54 die_errno("write error");
55 }
56}
57
020406ea 58static int maybe_fsync(int fd)
4c81b03e 59{
412e4cae
EW
60 if (use_fsync < 0)
61 use_fsync = git_env_bool("GIT_TEST_FSYNC", 1);
62 if (!use_fsync)
020406ea 63 return 0;
abf38abe
NS
64
65 if (fsync_method == FSYNC_METHOD_WRITEOUT_ONLY &&
66 git_fsync(fd, FSYNC_WRITEOUT_ONLY) >= 0)
020406ea
NS
67 return 0;
68
69 return git_fsync(fd, FSYNC_HARDWARE_FLUSH);
70}
abf38abe 71
020406ea
NS
72void fsync_or_die(int fd, const char *msg)
73{
74 if (maybe_fsync(fd) < 0)
abf38abe 75 die_errno("fsync error on '%s'", msg);
4c81b03e
LT
76}
77
020406ea
NS
78int fsync_component(enum fsync_component component, int fd)
79{
80 if (fsync_components & component)
81 return maybe_fsync(fd);
82 return 0;
83}
84
85void fsync_component_or_die(enum fsync_component component, int fd, const char *msg)
86{
87 if (fsync_components & component)
88 fsync_or_die(fd, msg);
89}
90
93822c22 91void write_or_die(int fd, const void *buf, size_t count)
7cf67205 92{
d34cf19b 93 if (write_in_full(fd, buf, count) < 0) {
756e676c 94 check_pipe(errno);
d824cbba 95 die_errno("write error");
e0814056 96 }
e0814056 97}
96328398
JV
98
99void fwrite_or_die(FILE *f, const void *buf, size_t count)
100{
101 if (fwrite(buf, 1, count, f) != count)
102 die_errno("fwrite error");
103}
104
105void fflush_or_die(FILE *f)
106{
107 if (fflush(f))
108 die_errno("fflush error");
109}