From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 439CD139085 for ; Thu, 29 Dec 2016 02:25:56 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6BFCBE0BF9; Thu, 29 Dec 2016 02:25:55 +0000 (UTC) Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 49A25E0BF9 for ; Thu, 29 Dec 2016 02:25:55 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id D0C20341647 for ; Thu, 29 Dec 2016 02:25:53 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id BBEC524EE for ; Thu, 29 Dec 2016 02:25:51 +0000 (UTC) From: "Mike Frysinger" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Mike Frysinger" Message-ID: <1482963265.2863e563f7b37d9e9cbb56551c8a4c033f802750.vapier@gentoo> Subject: [gentoo-commits] proj/portage-utils:master commit in: libq/ X-VCS-Repository: proj/portage-utils X-VCS-Files: libq/copy_file.c X-VCS-Directories: libq/ X-VCS-Committer: vapier X-VCS-Committer-Name: Mike Frysinger X-VCS-Revision: 2863e563f7b37d9e9cbb56551c8a4c033f802750 X-VCS-Branch: master Date: Thu, 29 Dec 2016 02:25:51 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 4db3f8f0-7d4c-4a9f-aad1-554c975cfb51 X-Archives-Hash: b51821e8b22f2ea31f11a5062d9ccdd1 commit: 2863e563f7b37d9e9cbb56551c8a4c033f802750 Author: Mike Frysinger gentoo org> AuthorDate: Wed Dec 28 22:14:25 2016 +0000 Commit: Mike Frysinger gentoo org> CommitDate: Wed Dec 28 22:14:25 2016 +0000 URL: https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=2863e563 copy_file: rewrite to use fds This avoids FILE* leakage and produces a little bit smaller code. libq/copy_file.c | 63 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/libq/copy_file.c b/libq/copy_file.c index ee1338d..4230b73 100644 --- a/libq/copy_file.c +++ b/libq/copy_file.c @@ -1,31 +1,50 @@ -static int copy_file_fd(int fd_src, int fd_dst) +static ssize_t safe_read(int fd, void *buf, size_t len) { - FILE *fp_src, *fp_dst; - size_t rcnt, wcnt; - char buf[BUFSIZE]; + ssize_t ret; - /* dont fclose() as that implicitly close()'s */ + while (1) { + ret = read(fd, buf, len); + if (ret >= 0) + break; + else if (errno != EINTR) + break; + } - fp_src = fdopen(fd_src, "r"); - if (!fp_src) - return -1; + return ret; +} - fp_dst = fdopen(fd_dst, "w"); - if (!fp_dst) - return -1; +static ssize_t safe_write(int fd, const void *buf, size_t len) +{ + ssize_t ret; - while (1) { - rcnt = fread(buf, sizeof(buf[0]), sizeof(buf), fp_src); - if (!rcnt) { - fflush(fp_dst); - return feof(fp_src) ? 0 : -1; + while (len) { + ret = write(fd, buf, len); + if (ret < 0) { + if (errno == EINTR) + continue; + return -1; } + buf += ret; + len -= ret; + } - wcnt = fwrite(buf, sizeof(buf[0]), rcnt, fp_dst); - if (wcnt != rcnt) { - if (ferror(fp_dst)) - return -1; - fseek(fp_src, wcnt - rcnt, SEEK_CUR); - } + return ret; +} + +static int copy_file_fd(int fd_src, int fd_dst) +{ + ssize_t rcnt, wcnt; + char buf[64 * 1024]; + + while (1) { + rcnt = safe_read(fd_src, buf, sizeof(buf)); + if (rcnt < 0) + return -1; + else if (rcnt == 0) + return 0; + + wcnt = safe_write(fd_dst, buf, rcnt); + if (wcnt == -1) + return -1; } }