3 #include "run-command.h"
6 * Some cases use stdio, but want to flush after the write
7 * to get error handling (and to get better interactive
8 * behaviour - not buffering excessively).
10 * Of course, if the flush happened within the write itself,
11 * we've already lost the error code, and cannot report it any
12 * more. So we just ignore that case instead (and hope we get
13 * the right error code on the flush).
15 * If the file handle is stdout, and stdout is a file, then skip the
16 * flush entirely since it's not needed.
18 void maybe_flush_or_die(FILE *f
, const char *desc
)
20 static int skip_stdout_flush
= -1;
25 if (skip_stdout_flush
< 0) {
26 /* NEEDSWORK: make this a normal Boolean */
27 cp
= getenv("GIT_FLUSH");
29 skip_stdout_flush
= (atoi(cp
) == 0);
30 else if ((fstat(fileno(stdout
), &st
) == 0) &&
32 skip_stdout_flush
= 1;
34 skip_stdout_flush
= 0;
36 if (skip_stdout_flush
&& !ferror(f
))
41 die_errno("write failure on '%s'", desc
);
45 void fprintf_or_die(FILE *f
, const char *fmt
, ...)
51 ret
= vfprintf(f
, fmt
, ap
);
56 die_errno("write error");
60 static int maybe_fsync(int fd
)
63 use_fsync
= git_env_bool("GIT_TEST_FSYNC", 1);
67 if (fsync_method
== FSYNC_METHOD_WRITEOUT_ONLY
&&
68 git_fsync(fd
, FSYNC_WRITEOUT_ONLY
) >= 0)
71 return git_fsync(fd
, FSYNC_HARDWARE_FLUSH
);
74 void fsync_or_die(int fd
, const char *msg
)
76 if (maybe_fsync(fd
) < 0)
77 die_errno("fsync error on '%s'", msg
);
80 int fsync_component(enum fsync_component component
, int fd
)
82 if (fsync_components
& component
)
83 return maybe_fsync(fd
);
87 void fsync_component_or_die(enum fsync_component component
, int fd
, const char *msg
)
89 if (fsync_components
& component
)
90 fsync_or_die(fd
, msg
);
93 void write_or_die(int fd
, const void *buf
, size_t count
)
95 if (write_in_full(fd
, buf
, count
) < 0) {
97 die_errno("write error");
101 void fwrite_or_die(FILE *f
, const void *buf
, size_t count
)
103 if (fwrite(buf
, 1, count
, f
) != count
)
104 die_errno("fwrite error");
107 void fflush_or_die(FILE *f
)
110 die_errno("fflush error");