short i/o: fix calls to read to use xread or read_in_full
[git] / write_or_die.c
1 #include "cache.h"
2
3 int read_in_full(int fd, void *buf, size_t count)
4 {
5         char *p = buf;
6         ssize_t total = 0;
7         ssize_t loaded = 0;
8
9         while (count > 0) {
10                 loaded = xread(fd, p, count);
11                 if (loaded <= 0) {
12                         if (total)
13                                 return total;
14                         else
15                                 return loaded;
16                 }
17                 count -= loaded;
18                 p += loaded;
19                 total += loaded;
20         }
21
22         return total;
23 }
24
25 void read_or_die(int fd, void *buf, size_t count)
26 {
27         ssize_t loaded;
28
29         loaded = read_in_full(fd, buf, count);
30         if (loaded == 0)
31                 die("unexpected end of file");
32         else if (loaded < 0)
33                 die("read error (%s)", strerror(errno));
34 }
35
36 void write_or_die(int fd, const void *buf, size_t count)
37 {
38         const char *p = buf;
39         ssize_t written;
40
41         while (count > 0) {
42                 written = xwrite(fd, p, count);
43                 if (written == 0)
44                         die("disk full?");
45                 else if (written < 0) {
46                         if (errno == EPIPE)
47                                 exit(0);
48                         die("write error (%s)", strerror(errno));
49                 }
50                 count -= written;
51                 p += written;
52         }
53 }
54
55 int write_in_full(int fd, const void *buf, size_t count)
56 {
57         const char *p = buf;
58         ssize_t total = 0;
59         ssize_t wcount = 0;
60
61         while (count > 0) {
62                 wcount = xwrite(fd, p, count);
63                 if (wcount <= 0) {
64                         if (total)
65                                 return total;
66                         else
67                                 return wcount;
68                 }
69                 count -= wcount;
70                 p += wcount;
71                 total += wcount;
72         }
73
74         return wcount;
75 }
76
77 int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg)
78 {
79         ssize_t written;
80
81         written = write_in_full(fd, buf, count);
82         if (written == 0) {
83                 fprintf(stderr, "%s: disk full?\n", msg);
84                 return 0;
85         }
86         else if (written < 0) {
87                 if (errno == EPIPE)
88                         exit(0);
89                 fprintf(stderr, "%s: write error (%s)\n",
90                         msg, strerror(errno));
91                 return 0;
92         }
93
94         return 1;
95 }
96
97 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
98 {
99         ssize_t written;
100
101         written = write_in_full(fd, buf, count);
102         if (written == 0) {
103                 fprintf(stderr, "%s: disk full?\n", msg);
104                 return 0;
105         }
106         else if (written < 0) {
107                 fprintf(stderr, "%s: write error (%s)\n",
108                         msg, strerror(errno));
109                 return 0;
110         }
111
112         return 1;
113 }