]>
Commit | Line | Data |
---|---|---|
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 | */ | |
19 | void maybe_flush_or_die(FILE *f, const char *desc) | |
20 | { | |
21 | static int skip_stdout_flush = -1; | |
22 | struct stat st; | |
23 | char *cp; | |
24 | ||
25 | if (f == stdout) { | |
26 | if (skip_stdout_flush < 0) { | |
fd01795b | 27 | /* NEEDSWORK: make this a normal Boolean */ |
06f59e9f TT |
28 | cp = getenv("GIT_FLUSH"); |
29 | if (cp) | |
30 | skip_stdout_flush = (atoi(cp) == 0); | |
31 | else if ((fstat(fileno(stdout), &st) == 0) && | |
32 | S_ISREG(st.st_mode)) | |
33 | skip_stdout_flush = 1; | |
34 | else | |
35 | skip_stdout_flush = 0; | |
36 | } | |
37 | if (skip_stdout_flush && !ferror(f)) | |
38 | return; | |
39 | } | |
40 | if (fflush(f)) { | |
756e676c | 41 | check_pipe(errno); |
d824cbba | 42 | die_errno("write failure on '%s'", desc); |
06f59e9f TT |
43 | } |
44 | } | |
45 | ||
9540ce50 JK |
46 | void fprintf_or_die(FILE *f, const char *fmt, ...) |
47 | { | |
48 | va_list ap; | |
49 | int ret; | |
50 | ||
51 | va_start(ap, fmt); | |
52 | ret = vfprintf(f, fmt, ap); | |
53 | va_end(ap); | |
54 | ||
55 | if (ret < 0) { | |
56 | check_pipe(errno); | |
57 | die_errno("write error"); | |
58 | } | |
59 | } | |
60 | ||
020406ea | 61 | static int maybe_fsync(int fd) |
4c81b03e | 62 | { |
412e4cae EW |
63 | if (use_fsync < 0) |
64 | use_fsync = git_env_bool("GIT_TEST_FSYNC", 1); | |
65 | if (!use_fsync) | |
020406ea | 66 | return 0; |
abf38abe NS |
67 | |
68 | if (fsync_method == FSYNC_METHOD_WRITEOUT_ONLY && | |
69 | git_fsync(fd, FSYNC_WRITEOUT_ONLY) >= 0) | |
020406ea NS |
70 | return 0; |
71 | ||
72 | return git_fsync(fd, FSYNC_HARDWARE_FLUSH); | |
73 | } | |
abf38abe | 74 | |
020406ea NS |
75 | void fsync_or_die(int fd, const char *msg) |
76 | { | |
77 | if (maybe_fsync(fd) < 0) | |
abf38abe | 78 | die_errno("fsync error on '%s'", msg); |
4c81b03e LT |
79 | } |
80 | ||
020406ea NS |
81 | int fsync_component(enum fsync_component component, int fd) |
82 | { | |
83 | if (fsync_components & component) | |
84 | return maybe_fsync(fd); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | void fsync_component_or_die(enum fsync_component component, int fd, const char *msg) | |
89 | { | |
90 | if (fsync_components & component) | |
91 | fsync_or_die(fd, msg); | |
92 | } | |
93 | ||
93822c22 | 94 | void write_or_die(int fd, const void *buf, size_t count) |
7cf67205 | 95 | { |
d34cf19b | 96 | if (write_in_full(fd, buf, count) < 0) { |
756e676c | 97 | check_pipe(errno); |
d824cbba | 98 | die_errno("write error"); |
e0814056 | 99 | } |
e0814056 | 100 | } |
96328398 JV |
101 | |
102 | void fwrite_or_die(FILE *f, const void *buf, size_t count) | |
103 | { | |
104 | if (fwrite(buf, 1, count, f) != count) | |
105 | die_errno("fwrite error"); | |
106 | } | |
107 | ||
108 | void fflush_or_die(FILE *f) | |
109 | { | |
110 | if (fflush(f)) | |
111 | die_errno("fflush error"); | |
112 | } |