1 #include "git-compat-util.h"
3 #include "run-command.h"
5 #include "write-or-die.h"
8 * Some cases use stdio, but want to flush after the write
9 * to get error handling (and to get better interactive
10 * behaviour - not buffering excessively).
12 * Of course, if the flush happened within the write itself,
13 * we've already lost the error code, and cannot report it any
14 * more. So we just ignore that case instead (and hope we get
15 * the right error code on the flush).
17 * If the file handle is stdout, and stdout is a file, then skip the
18 * flush entirely since it's not needed.
20 void maybe_flush_or_die(FILE *f
, const char *desc
)
22 static int skip_stdout_flush
= -1;
27 if (skip_stdout_flush
< 0) {
28 /* NEEDSWORK: make this a normal Boolean */
29 cp
= getenv("GIT_FLUSH");
31 skip_stdout_flush
= (atoi(cp
) == 0);
32 else if ((fstat(fileno(stdout
), &st
) == 0) &&
34 skip_stdout_flush
= 1;
36 skip_stdout_flush
= 0;
38 if (skip_stdout_flush
&& !ferror(f
))
43 die_errno("write failure on '%s'", desc
);
47 void fprintf_or_die(FILE *f
, const char *fmt
, ...)
53 ret
= vfprintf(f
, fmt
, ap
);
58 die_errno("write error");
62 static int maybe_fsync(int fd
)
65 use_fsync
= git_env_bool("GIT_TEST_FSYNC", 1);
69 if (fsync_method
== FSYNC_METHOD_WRITEOUT_ONLY
&&
70 git_fsync(fd
, FSYNC_WRITEOUT_ONLY
) >= 0)
73 return git_fsync(fd
, FSYNC_HARDWARE_FLUSH
);
76 void fsync_or_die(int fd
, const char *msg
)
78 if (maybe_fsync(fd
) < 0)
79 die_errno("fsync error on '%s'", msg
);
82 int fsync_component(enum fsync_component component
, int fd
)
84 if (fsync_components
& component
)
85 return maybe_fsync(fd
);
89 void fsync_component_or_die(enum fsync_component component
, int fd
, const char *msg
)
91 if (fsync_components
& component
)
92 fsync_or_die(fd
, msg
);
95 void write_or_die(int fd
, const void *buf
, size_t count
)
97 if (write_in_full(fd
, buf
, count
) < 0) {
99 die_errno("write error");
103 void fwrite_or_die(FILE *f
, const void *buf
, size_t count
)
105 if (fwrite(buf
, 1, count
, f
) != count
)
106 die_errno("fwrite error");
109 void fflush_or_die(FILE *f
)
112 die_errno("fflush error");