Create read_or_die utility routine.
[git/fastimport.git] / write_or_die.c
blob8cf6486025a0fffba889618ce07a0d4eadd139df
1 #include "cache.h"
3 void read_or_die(int fd, void *buf, size_t count)
5 char *p = buf;
6 ssize_t loaded;
8 while (count > 0) {
9 loaded = xread(fd, p, count);
10 if (loaded == 0)
11 die("unexpected end of file");
12 else if (loaded < 0)
13 die("read error (%s)", strerror(errno));
14 count -= loaded;
15 p += loaded;
19 void write_or_die(int fd, const void *buf, size_t count)
21 const char *p = buf;
22 ssize_t written;
24 while (count > 0) {
25 written = xwrite(fd, p, count);
26 if (written == 0)
27 die("disk full?");
28 else if (written < 0) {
29 if (errno == EPIPE)
30 exit(0);
31 die("write error (%s)", strerror(errno));
33 count -= written;
34 p += written;
38 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
40 const char *p = buf;
41 ssize_t written;
43 while (count > 0) {
44 written = xwrite(fd, p, count);
45 if (written == 0) {
46 fprintf(stderr, "%s: disk full?\n", msg);
47 return 0;
49 else if (written < 0) {
50 if (errno == EPIPE)
51 exit(0);
52 fprintf(stderr, "%s: write error (%s)\n",
53 msg, strerror(errno));
54 return 0;
56 count -= written;
57 p += written;
60 return 1;