diff options
author | Steffen Prohaska <prohaska@zib.de> | 2014-08-26 17:23:24 +0200 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-08-28 10:25:14 -0700 |
commit | b29763aa9bcbb99a59aec3820e30ff1864cfa765 (patch) | |
tree | d3dcda097520b70a990e48e7ea52b9bbb9ad206d /copy.c | |
parent | 02710228dd79d9f9c6fa180233491639b603c06d (diff) | |
download | git-b29763aa9bcbb99a59aec3820e30ff1864cfa765.tar.gz |
copy_fd(): do not close the input file descriptor
The caller, not this function, opened the file descriptor; it is
selfish for the callee to close it when it is done reading from it.
The caller may want an option to rewind and re-read the contents
after it returns.
Simplify the loop to copy the input in full to the output; its
body essentially is what a call to write_in_full() helper does.
Signed-off-by: Steffen Prohaska <prohaska@zib.de>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'copy.c')
-rw-r--r-- | copy.c | 26 |
1 files changed, 5 insertions, 21 deletions
@@ -4,34 +4,17 @@ int copy_fd(int ifd, int ofd) { while (1) { char buffer[8192]; - char *buf = buffer; ssize_t len = xread(ifd, buffer, sizeof(buffer)); if (!len) break; if (len < 0) { - int read_error = errno; - close(ifd); return error("copy-fd: read returned %s", - strerror(read_error)); - } - while (len) { - int written = xwrite(ofd, buf, len); - if (written > 0) { - buf += written; - len -= written; - } - else if (!written) { - close(ifd); - return error("copy-fd: write returned 0"); - } else { - int write_error = errno; - close(ifd); - return error("copy-fd: write returned %s", - strerror(write_error)); - } + strerror(errno)); } + if (write_in_full(ofd, buffer, len) < 0) + return error("copy-fd: write returned %s", + strerror(errno)); } - close(ifd); return 0; } @@ -60,6 +43,7 @@ int copy_file(const char *dst, const char *src, int mode) return fdo; } status = copy_fd(fdi, fdo); + close(fdi); if (close(fdo) != 0) return error("%s: close error: %s", dst, strerror(errno)); |