55 lines
1.2 KiB
Diff
55 lines
1.2 KiB
Diff
From d56778515e88e8fd5821b49850006962fe81b18d Mon Sep 17 00:00:00 2001
|
|
From: Baruch Even <baruch@ev-en.org>
|
|
Date: Wed, 25 Nov 2015 08:58:10 +0200
|
|
Subject: [PATCH 4/5] Speed up by reading larger chunks
|
|
|
|
Based on work initiated by mwilck
|
|
---
|
|
src/gpart.c | 27 ++++++++++++++++-----------
|
|
1 file changed, 16 insertions(+), 11 deletions(-)
|
|
|
|
diff --git src/gpart.c src/gpart.c
|
|
index 63b36a5..d530547 100644
|
|
--- src/gpart.c
|
|
+++ src/gpart.c
|
|
@@ -163,20 +163,25 @@ byte_t *alloc(ssize_t s)
|
|
|
|
ssize_t bread(int fd,byte_t *buf,size_t ssize,size_t nsecs)
|
|
{
|
|
- ssize_t cs = 0, nr = 0;
|
|
-
|
|
- for ( ; nsecs > 0; nsecs--)
|
|
- {
|
|
- if ((nr = read(fd,buf,ssize)) == -1)
|
|
- {
|
|
+ const size_t total = ssize * nsecs;
|
|
+ size_t read_bytes = 0;
|
|
+
|
|
+ while (read_bytes < total) {
|
|
+ ssize_t ret = read(fd, buf + read_bytes, total - read_bytes);
|
|
+ if (ret > 0)
|
|
+ read_bytes += ret;
|
|
+ else if (ret == 0) {
|
|
+ return read_bytes;
|
|
+ } else {
|
|
+ // ret < 0, an error case
|
|
+ if (errno == EINTR)
|
|
+ continue; // Rogue signal interruption, retry
|
|
berrno = errno;
|
|
- return ((cs == 0) ? -1 : cs);
|
|
- }
|
|
- cs += nr; buf += nr;
|
|
- if (nr < ssize)
|
|
break;
|
|
+ }
|
|
}
|
|
- return (cs);
|
|
+
|
|
+ return read_bytes ? read_bytes : -1;
|
|
}
|
|
|
|
|
|
--
|
|
2.24.0
|
|
|