1 #include "git-compat-util.h"
3 #include "run-command.h"
4 #include "write-or-die.h"
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).
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).
16 * If the file handle is stdout, and stdout is a file, then skip the
17 * flush entirely since it's not needed.
19 void maybe_flush_or_die(FILE *f
, const char *desc
)
21 static int skip_stdout_flush
= -1;
26 if (skip_stdout_flush
< 0) {
27 /* NEEDSWORK: make this a normal Boolean */
28 cp
= getenv("GIT_FLUSH");
30 skip_stdout_flush
= (atoi(cp
) == 0);
31 else if ((fstat(fileno(stdout
), &st
) == 0) &&
33 skip_stdout_flush
= 1;
35 skip_stdout_flush
= 0;
37 if (skip_stdout_flush
&& !ferror(f
))
42 die_errno("write failure on '%s'", desc
);
46 void fprintf_or_die(FILE *f
, const char *fmt
, ...)
52 ret
= vfprintf(f
, fmt
, ap
);
57 die_errno("write error");
61 static int maybe_fsync(int fd
)
64 use_fsync
= git_env_bool("GIT_TEST_FSYNC", 1);
68 if (fsync_method
== FSYNC_METHOD_WRITEOUT_ONLY
&&
69 git_fsync(fd
, FSYNC_WRITEOUT_ONLY
) >= 0)
72 return git_fsync(fd
, FSYNC_HARDWARE_FLUSH
);
75 void fsync_or_die(int fd
, const char *msg
)
77 if (maybe_fsync(fd
) < 0)
78 die_errno("fsync error on '%s'", msg
);
81 int fsync_component(enum fsync_component component
, int fd
)
83 if (fsync_components
& component
)
84 return maybe_fsync(fd
);
88 void fsync_component_or_die(enum fsync_component component
, int fd
, const char *msg
)
90 if (fsync_components
& component
)
91 fsync_or_die(fd
, msg
);
94 void write_or_die(int fd
, const void *buf
, size_t count
)
96 if (write_in_full(fd
, buf
, count
) < 0) {
98 die_errno("write error");
102 void fwrite_or_die(FILE *f
, const void *buf
, size_t count
)
104 if (fwrite(buf
, 1, count
, f
) != count
)
105 die_errno("fwrite error");
108 void fflush_or_die(FILE *f
)
111 die_errno("fflush error");