[PATCH 5/17] Create read_or_die utility routine.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Like write_or_die read_or_die reads the entire length requested
or it kills the current process with a die call.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 cache.h        |    1 +
 write_or_die.c |   19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index 936555c..77f2338 100644
--- a/cache.h
+++ b/cache.h
@@ -428,6 +428,7 @@ extern char git_default_name[MAX_GITNAME];
 extern char git_commit_encoding[MAX_ENCODING_LENGTH];
 
 extern int copy_fd(int ifd, int ofd);
+extern void read_or_die(int fd, void *buf, size_t count);
 extern void write_or_die(int fd, const void *buf, size_t count);
 extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
 
diff --git a/write_or_die.c b/write_or_die.c
index bfe4eeb..a56d992 100644
--- a/write_or_die.c
+++ b/write_or_die.c
@@ -1,5 +1,24 @@
 #include "cache.h"
 
+void read_or_die(int fd, void *buf, size_t count)
+{
+	char *p = buf;
+	ssize_t loaded;
+
+	while (count > 0) {
+		loaded = xread(fd, p, count);
+		if (loaded == 0)
+			die("unexpected end of file");
+		else if (loaded < 0) {
+			if (errno == EPIPE)
+				exit(0);
+			die("read error (%s)", strerror(errno));
+		}
+		count -= loaded;
+		p += loaded;
+	}
+}
+
 void write_or_die(int fd, const void *buf, size_t count)
 {
 	const char *p = buf;
-- 
1.4.4.3.g87d8

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]