rebase: do not get confused in fast-forward situation.
[git] / copy.c
1 #include "cache.h"
2
3 int copy_fd(int ifd, int ofd)
4 {
5         while (1) {
6                 int len;
7                 char buffer[8192];
8                 char *buf = buffer;
9                 len = read(ifd, buffer, sizeof(buffer));
10                 if (!len)
11                         break;
12                 if (len < 0) {
13                         int read_error;
14                         if (errno == EAGAIN)
15                                 continue;
16                         read_error = errno;
17                         close(ifd);
18                         return error("copy-fd: read returned %s",
19                                      strerror(read_error));
20                 }
21                 while (1) {
22                         int written = write(ofd, buf, len);
23                         if (written > 0) {
24                                 buf += written;
25                                 len -= written;
26                                 if (!len)
27                                         break;
28                         }
29                         if (!written)
30                                 return error("copy-fd: write returned 0");
31                         if (errno == EAGAIN || errno == EINTR)
32                                 continue;
33                         return error("copy-fd: write returned %s",
34                                      strerror(errno));
35                 }
36         }
37         close(ifd);
38         return 0;
39 }
40