9
0
Fork 0

copy_file: handle write return value correctly

write() does not necessarily consume all input, handle this
case correctly.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Sascha Hauer 2011-04-10 19:00:37 +02:00
parent d354cee3d0
commit 583fe4fc0e
1 changed files with 11 additions and 4 deletions

View File

@ -17,6 +17,7 @@ int copy_file(const char *src, const char *dst)
int srcfd = 0, dstfd = 0;
int r, w;
int ret = 1;
void *buf;
rw_buf = xmalloc(RW_BUF_SIZE);
@ -40,10 +41,16 @@ int copy_file(const char *src, const char *dst)
}
if (!r)
break;
w = write(dstfd, rw_buf, r);
if (w < 0) {
perror("write");
goto out;
buf = rw_buf;
while (r) {
w = write(dstfd, buf, r);
if (w < 0) {
perror("write");
goto out;
}
buf += w;
r -= w;
}
}