Merge branch 'ew/rerere'
[git] / write_or_die.c
1 #include "cache.h"
2
3 void write_or_die(int fd, const void *buf, size_t count)
4 {
5         const char *p = buf;
6         ssize_t written;
7
8         while (count > 0) {
9                 written = xwrite(fd, p, count);
10                 if (written == 0)
11                         die("disk full?");
12                 else if (written < 0) {
13                         if (errno == EPIPE)
14                                 exit(0);
15                         die("write error (%s)", strerror(errno));
16                 }
17                 count -= written;
18                 p += written;
19         }
20 }
21
22 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
23 {
24         const char *p = buf;
25         ssize_t written;
26
27         while (count > 0) {
28                 written = xwrite(fd, p, count);
29                 if (written == 0) {
30                         fprintf(stderr, "%s: disk full?\n", msg);
31                         return 0;
32                 }
33                 else if (written < 0) {
34                         if (errno == EPIPE)
35                                 exit(0);
36                         fprintf(stderr, "%s: write error (%s)\n",
37                                 msg, strerror(errno));
38                         return 0;
39                 }
40                 count -= written;
41                 p += written;
42         }
43
44         return 1;
45 }