builtin-prune: memory diet.
[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 }
46
47 int write_in_full(int fd, const void *buf, size_t count, const char *msg)
48 {
49         const char *p = buf;
50         ssize_t written;
51
52         while (count > 0) {
53                 written = xwrite(fd, p, count);
54                 if (written == 0) {
55                         fprintf(stderr, "%s: disk full?\n", msg);
56                         return 0;
57                 }
58                 else if (written < 0) {
59                         fprintf(stderr, "%s: write error (%s)\n",
60                                 msg, strerror(errno));
61                         return 0;
62                 }
63                 count -= written;
64                 p += written;
65         }
66
67         return 1;
68 }