Fix signedness on return value from xread()
[git/git.git] / copy.c
CommitLineData
f3123c4a
JH
1#include "cache.h"
2
3int copy_fd(int ifd, int ofd)
4{
5 while (1) {
6 int len;
7 char buffer[8192];
8 char *buf = buffer;
1c15afb9 9 len = xread(ifd, buffer, sizeof(buffer));
f3123c4a
JH
10 if (!len)
11 break;
12 if (len < 0) {
e6c64fc1 13 int read_error;
e6c64fc1
JH
14 read_error = errno;
15 close(ifd);
f3123c4a 16 return error("copy-fd: read returned %s",
e6c64fc1 17 strerror(read_error));
f3123c4a 18 }
1c15afb9
JH
19 while (len) {
20 int written = xwrite(ofd, buf, len);
f3123c4a
JH
21 if (written > 0) {
22 buf += written;
23 len -= written;
f3123c4a 24 }
08337a97
SR
25 else if (!written) {
26 close(ifd);
f3123c4a 27 return error("copy-fd: write returned 0");
08337a97
SR
28 } else {
29 close(ifd);
1c15afb9
JH
30 return error("copy-fd: write returned %s",
31 strerror(errno));
08337a97 32 }
f3123c4a
JH
33 }
34 }
35 close(ifd);
36 return 0;
37}
38