Merge branch 'jc/merge' into next
[git/dscho.git] / copy.c
blob08a3d388a4c312fca18558b2b8e5dcf027c7afaf
1 #include "cache.h"
3 int copy_fd(int ifd, int ofd)
5 while (1) {
6 int len;
7 char buffer[8192];
8 char *buf = buffer;
9 len = xread(ifd, buffer, sizeof(buffer));
10 if (!len)
11 break;
12 if (len < 0) {
13 int read_error;
14 read_error = errno;
15 close(ifd);
16 return error("copy-fd: read returned %s",
17 strerror(read_error));
19 while (len) {
20 int written = xwrite(ofd, buf, len);
21 if (written > 0) {
22 buf += written;
23 len -= written;
25 else if (!written) {
26 close(ifd);
27 return error("copy-fd: write returned 0");
28 } else {
29 close(ifd);
30 return error("copy-fd: write returned %s",
31 strerror(errno));
35 close(ifd);
36 return 0;