git
/
write-or-die.c
109 строк · 2.3 Кб
1#include "git-compat-util.h"
2#include "parse.h"
3#include "run-command.h"
4#include "write-or-die.h"
5
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{
21if (f == stdout) {
22static int force_flush_stdout = -1;
23
24if (force_flush_stdout < 0) {
25force_flush_stdout = git_env_bool("GIT_FLUSH", -1);
26if (force_flush_stdout < 0) {
27struct stat st;
28if (fstat(fileno(stdout), &st))
29force_flush_stdout = 1;
30else
31force_flush_stdout = !S_ISREG(st.st_mode);
32}
33}
34if (!force_flush_stdout && !ferror(f))
35return;
36}
37if (fflush(f)) {
38check_pipe(errno);
39die_errno("write failure on '%s'", desc);
40}
41}
42
43void fprintf_or_die(FILE *f, const char *fmt, ...)
44{
45va_list ap;
46int ret;
47
48va_start(ap, fmt);
49ret = vfprintf(f, fmt, ap);
50va_end(ap);
51
52if (ret < 0) {
53check_pipe(errno);
54die_errno("write error");
55}
56}
57
58static int maybe_fsync(int fd)
59{
60if (use_fsync < 0)
61use_fsync = git_env_bool("GIT_TEST_FSYNC", 1);
62if (!use_fsync)
63return 0;
64
65if (fsync_method == FSYNC_METHOD_WRITEOUT_ONLY &&
66git_fsync(fd, FSYNC_WRITEOUT_ONLY) >= 0)
67return 0;
68
69return git_fsync(fd, FSYNC_HARDWARE_FLUSH);
70}
71
72void fsync_or_die(int fd, const char *msg)
73{
74if (maybe_fsync(fd) < 0)
75die_errno("fsync error on '%s'", msg);
76}
77
78int fsync_component(enum fsync_component component, int fd)
79{
80if (fsync_components & component)
81return maybe_fsync(fd);
82return 0;
83}
84
85void fsync_component_or_die(enum fsync_component component, int fd, const char *msg)
86{
87if (fsync_components & component)
88fsync_or_die(fd, msg);
89}
90
91void write_or_die(int fd, const void *buf, size_t count)
92{
93if (write_in_full(fd, buf, count) < 0) {
94check_pipe(errno);
95die_errno("write error");
96}
97}
98
99void fwrite_or_die(FILE *f, const void *buf, size_t count)
100{
101if (fwrite(buf, 1, count, f) != count)
102die_errno("fwrite error");
103}
104
105void fflush_or_die(FILE *f)
106{
107if (fflush(f))
108die_errno("fflush error");
109}
110