]> git.ipfire.org Git - thirdparty/git.git/blame - copy.c
copy.c::copy_fd() - do not leak file descriptor on error return.
[thirdparty/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;
9 len = read(ifd, buffer, sizeof(buffer));
10 if (!len)
11 break;
12 if (len < 0) {
e6c64fc1 13 int read_error;
f3123c4a
JH
14 if (errno == EAGAIN)
15 continue;
e6c64fc1
JH
16 read_error = errno;
17 close(ifd);
f3123c4a 18 return error("copy-fd: read returned %s",
e6c64fc1 19 strerror(read_error));
f3123c4a
JH
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