Merge branch 'cr/tag'
[git/git.git] / write_or_die.c
CommitLineData
7230e6d0
RS
1#include "cache.h"
2
93d26e4c 3int read_in_full(int fd, void *buf, size_t count)
75025ccd
SP
4{
5 char *p = buf;
93d26e4c 6 ssize_t total = 0;
75025ccd
SP
7
8 while (count > 0) {
4494c656
LT
9 ssize_t loaded = xread(fd, p, count);
10 if (loaded <= 0)
11 return total ? total : loaded;
75025ccd
SP
12 count -= loaded;
13 p += loaded;
93d26e4c 14 total += loaded;
75025ccd 15 }
93d26e4c
AW
16
17 return total;
18}
19
93822c22 20int write_in_full(int fd, const void *buf, size_t count)
7230e6d0
RS
21{
22 const char *p = buf;
93822c22 23 ssize_t total = 0;
7230e6d0
RS
24
25 while (count > 0) {
d848804a 26 ssize_t written = xwrite(fd, p, count);
f6aa66cb
LT
27 if (written < 0)
28 return -1;
29 if (!written) {
30 errno = ENOSPC;
31 return -1;
7230e6d0
RS
32 }
33 count -= written;
34 p += written;
93822c22 35 total += written;
7230e6d0 36 }
93822c22
AW
37
38 return total;
7230e6d0 39}
7cf67205 40
93822c22 41void write_or_die(int fd, const void *buf, size_t count)
7cf67205 42{
d34cf19b 43 if (write_in_full(fd, buf, count) < 0) {
93822c22
AW
44 if (errno == EPIPE)
45 exit(0);
46 die("write error (%s)", strerror(errno));
e0814056 47 }
e0814056
AW
48}
49
50int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
51{
d34cf19b 52 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
53 if (errno == EPIPE)
54 exit(0);
55 fprintf(stderr, "%s: write error (%s)\n",
56 msg, strerror(errno));
57 return 0;
7cf67205
CC
58 }
59
60 return 1;
61}
825cee7b 62
e0814056 63int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
825cee7b 64{
d34cf19b 65 if (write_in_full(fd, buf, count) < 0) {
e0814056
AW
66 fprintf(stderr, "%s: write error (%s)\n",
67 msg, strerror(errno));
68 return 0;
825cee7b
AW
69 }
70
71 return 1;
72}