Clean up write_in_full() users
[git/mingw/4msysgit/wingit-dll.git] / write_or_die.c
blob1224cac5da7e8bf51266eadf24e9e58e6e5a37cb
1 #include "cache.h"
3 int read_in_full(int fd, void *buf, size_t count)
5 char *p = buf;
6 ssize_t total = 0;
7 ssize_t loaded = 0;
9 while (count > 0) {
10 loaded = xread(fd, p, count);
11 if (loaded <= 0) {
12 if (total)
13 return total;
14 else
15 return loaded;
17 count -= loaded;
18 p += loaded;
19 total += loaded;
22 return total;
25 void read_or_die(int fd, void *buf, size_t count)
27 ssize_t loaded;
29 if (!count)
30 return;
31 loaded = read_in_full(fd, buf, count);
32 if (loaded == 0)
33 die("unexpected end of file");
34 else if (loaded < 0)
35 die("read error (%s)", strerror(errno));
38 int write_in_full(int fd, const void *buf, size_t count)
40 const char *p = buf;
41 ssize_t total = 0;
43 while (count > 0) {
44 size_t written = xwrite(fd, p, count);
45 if (written < 0)
46 return -1;
47 if (!written) {
48 errno = ENOSPC;
49 return -1;
51 count -= written;
52 p += written;
53 total += written;
56 return total;
59 void write_or_die(int fd, const void *buf, size_t count)
61 if (write_in_full(fd, buf, count) < 0) {
62 if (errno == EPIPE)
63 exit(0);
64 die("write error (%s)", strerror(errno));
68 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
70 if (write_in_full(fd, buf, count) < 0) {
71 if (errno == EPIPE)
72 exit(0);
73 fprintf(stderr, "%s: write error (%s)\n",
74 msg, strerror(errno));
75 return 0;
78 return 1;
81 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
83 if (write_in_full(fd, buf, count) < 0) {
84 fprintf(stderr, "%s: write error (%s)\n",
85 msg, strerror(errno));
86 return 0;
89 return 1;