upload-archive: use argv_array to store client arguments
[git/git.git] / write_or_die.c
CommitLineData
7230e6d0
RS
1#include "cache.h"
2
06f59e9f
TT
3/*
4 * Some cases use stdio, but want to flush after the write
5 * to get error handling (and to get better interactive
6 * behaviour - not buffering excessively).
7 *
8 * Of course, if the flush happened within the write itself,
9 * we've already lost the error code, and cannot report it any
10 * more. So we just ignore that case instead (and hope we get
11 * the right error code on the flush).
12 *
13 * If the file handle is stdout, and stdout is a file, then skip the
14 * flush entirely since it's not needed.
15 */
16void maybe_flush_or_die(FILE *f, const char *desc)
17{
18 static int skip_stdout_flush = -1;
19 struct stat st;
20 char *cp;
21
22 if (f == stdout) {
23 if (skip_stdout_flush < 0) {
24 cp = getenv("GIT_FLUSH");
25 if (cp)
26 skip_stdout_flush = (atoi(cp) == 0);
27 else if ((fstat(fileno(stdout), &st) == 0) &&
28 S_ISREG(st.st_mode))
29 skip_stdout_flush = 1;
30 else
31 skip_stdout_flush = 0;
32 }
33 if (skip_stdout_flush && !ferror(f))
34 return;
35 }
36 if (fflush(f)) {
84adb641 37 if (errno == EPIPE)
06f59e9f 38 exit(0);
d824cbba 39 die_errno("write failure on '%s'", desc);
06f59e9f
TT
40 }
41}
42
4c81b03e
LT
43void fsync_or_die(int fd, const char *msg)
44{
45 if (fsync(fd) < 0) {
d824cbba 46 die_errno("fsync error on '%s'", msg);
4c81b03e
LT
47 }
48}
49
93822c22 50void write_or_die(int fd, const void *buf, size_t count)
7cf67205 51{
d34cf19b 52 if (write_in_full(fd, buf, count) < 0) {
93822c22
AW
53 if (errno == EPIPE)
54 exit(0);
d824cbba 55 die_errno("write error");
e0814056 56 }
e0814056
AW
57}
58
59int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
60{
d34cf19b 61 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
62 if (errno == EPIPE)
63 exit(0);
64 fprintf(stderr, "%s: write error (%s)\n",
65 msg, strerror(errno));
66 return 0;
7cf67205
CC
67 }
68
69 return 1;
70}
825cee7b 71
e0814056 72int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
825cee7b 73{
d34cf19b 74 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
75 fprintf(stderr, "%s: write error (%s)\n",
76 msg, strerror(errno));
77 return 0;
825cee7b
AW
78 }
79
80 return 1;
81}