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