git-svn: add --prefix= option to multi-init
[git.git] / write_or_die.c
blob6db1d3123d1761dee4850e0a56a9b86b4bf1722a
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;
63 int write_in_full(int fd, const void *buf, size_t count, const char *msg)
65 const char *p = buf;
66 ssize_t written;
68 while (count > 0) {
69 written = xwrite(fd, p, count);
70 if (written == 0) {
71 fprintf(stderr, "%s: disk full?\n", msg);
72 return 0;
74 else if (written < 0) {
75 fprintf(stderr, "%s: write error (%s)\n",
76 msg, strerror(errno));
77 return 0;
79 count -= written;
80 p += written;
83 return 1;