@matheusbsbs / matheus-bagliano / commits / 559e840b74
Fork 0

Move read_in_full() and write_in_full() to wrapper.c

A few compat/* layer functions call these functions, but we would really want to keep them thin, without depending too much on the libgit proper. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 20, 2008 at 16:13 UTC 559e840b74dbbf39633aa68627937a5f7a6d4145
2 files changed +38 -38
wrapper.c
+38
@@ -133,6 +133,44 @@ ssize_t xwrite(int fd, const void *buf, size_t len)
133 }
134 }
135
136 +ssize_t read_in_full(int fd, void *buf, size_t count)
137 +{
138 + char *p = buf;
139 + ssize_t total = 0;
140 +
141 + while (count > 0) {
142 + ssize_t loaded = xread(fd, p, count);
143 + if (loaded <= 0)
144 + return total ? total : loaded;
145 + count -= loaded;
146 + p += loaded;
147 + total += loaded;
148 + }
149 +
150 + return total;
151 +}
152 +
153 +ssize_t write_in_full(int fd, const void *buf, size_t count)
154 +{
155 + const char *p = buf;
156 + ssize_t total = 0;
157 +
158 + while (count > 0) {
159 + ssize_t written = xwrite(fd, p, count);
160 + if (written < 0)
161 + return -1;
162 + if (!written) {
163 + errno = ENOSPC;
164 + return -1;
165 + }
166 + count -= written;
167 + p += written;
168 + total += written;
169 + }
170 +
171 + return total;
172 +}
173 +
174 int xdup(int fd)
175 {
176 int ret = dup(fd);
write_or_die.c
-38
@@ -45,44 +45,6 @@ void maybe_flush_or_die(FILE *f, const char *desc)
45 }
46 }
47
48 -ssize_t read_in_full(int fd, void *buf, size_t count)
49 -{
50 - char *p = buf;
51 - ssize_t total = 0;
52 -
53 - while (count > 0) {
54 - ssize_t loaded = xread(fd, p, count);
55 - if (loaded <= 0)
56 - return total ? total : loaded;
57 - count -= loaded;
58 - p += loaded;
59 - total += loaded;
60 - }
61 -
62 - return total;
63 -}
64 -
65 -ssize_t write_in_full(int fd, const void *buf, size_t count)
66 -{
67 - const char *p = buf;
68 - ssize_t total = 0;
69 -
70 - while (count > 0) {
71 - ssize_t written = xwrite(fd, p, count);
72 - if (written < 0)
73 - return -1;
74 - if (!written) {
75 - errno = ENOSPC;
76 - return -1;
77 - }
78 - count -= written;
79 - p += written;
80 - total += written;
81 - }
82 -
83 - return total;
84 -}
85 -
48 void fsync_or_die(int fd, const char *msg)
49 {
50 if (fsync(fd) < 0) {