]>
Commit | Line | Data |
---|---|---|
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 | int write_in_full(int fd, const void *buf, size_t count) | |
37 | { | |
38 | const char *p = buf; | |
39 | ssize_t total = 0; | |
40 | ||
41 | while (count > 0) { | |
42 | size_t written = xwrite(fd, p, count); | |
43 | if (written < 0) | |
44 | return -1; | |
45 | if (!written) { | |
46 | errno = ENOSPC; | |
47 | return -1; | |
48 | } | |
49 | count -= written; | |
50 | p += written; | |
51 | total += written; | |
52 | } | |
53 | ||
54 | return total; | |
55 | } | |
56 | ||
57 | void write_or_die(int fd, const void *buf, size_t count) | |
58 | { | |
59 | ssize_t written; | |
60 | ||
61 | written = write_in_full(fd, buf, count); | |
62 | if (written == 0) | |
63 | die("disk full?"); | |
64 | else if (written < 0) { | |
65 | if (errno == EPIPE) | |
66 | exit(0); | |
67 | die("write error (%s)", strerror(errno)); | |
68 | } | |
69 | } | |
70 | ||
71 | int write_or_whine_pipe(int fd, const void *buf, size_t count, const char *msg) | |
72 | { | |
73 | ssize_t written; | |
74 | ||
75 | written = write_in_full(fd, buf, count); | |
76 | if (written == 0) { | |
77 | fprintf(stderr, "%s: disk full?\n", msg); | |
78 | return 0; | |
79 | } | |
80 | else if (written < 0) { | |
81 | if (errno == EPIPE) | |
82 | exit(0); | |
83 | fprintf(stderr, "%s: write error (%s)\n", | |
84 | msg, strerror(errno)); | |
85 | return 0; | |
86 | } | |
87 | ||
88 | return 1; | |
89 | } | |
90 | ||
91 | int write_or_whine(int fd, const void *buf, size_t count, const char *msg) | |
92 | { | |
93 | ssize_t written; | |
94 | ||
95 | written = write_in_full(fd, buf, count); | |
96 | if (written == 0) { | |
97 | fprintf(stderr, "%s: disk full?\n", msg); | |
98 | return 0; | |
99 | } | |
100 | else if (written < 0) { | |
101 | fprintf(stderr, "%s: write error (%s)\n", | |
102 | msg, strerror(errno)); | |
103 | return 0; | |
104 | } | |
105 | ||
106 | return 1; | |
107 | } |