Document why header parsing won't exceed a window.
[git] / write_or_die.c
1 #include "cache.h"
2
3 void read_or_die(int fd, void *buf, size_t count)
4 {
5         char *p = buf;
6         ssize_t loaded;
7
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;
16         }
17 }
18
19 void write_or_die(int fd, const void *buf, size_t count)
20 {
21         const char *p = buf;
22         ssize_t written;
23
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));
32                 }
33                 count -= written;
34                 p += written;
35         }
36 }
37
38 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
39 {
40         const char *p = buf;
41         ssize_t written;
42
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;
48                 }
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;
55                 }
56                 count -= written;
57                 p += written;
58         }
59
60         return 1;
61 }