@matheusbsbs / matheus-bagliano / commits / 75025ccdb7
Fork 0

Create read_or_die utility routine.

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@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Dec 23, 2006 at 02:33 UTC 75025ccdb79b5d6b290c630f8777c9f7bb9d257c
2 files changed +17
cache.h
+1
@@ -428,6 +428,7 @@ extern char *git_commit_encoding;
428 extern char *git_log_output_encoding;
429
430 extern int copy_fd(int ifd, int ofd);
431 +extern void read_or_die(int fd, void *buf, size_t count);
432 extern void write_or_die(int fd, const void *buf, size_t count);
433 extern int write_or_whine(int fd, const void *buf, size_t count, const char *msg);
434
write_or_die.c
+16
@@ -1,5 +1,21 @@
1 #include "cache.h"
2
3 +void read_or_die(int fd, void *buf, size_t count)
4 +{
5 + char *p = buf;
6 + ssize_t loaded;
7 +
8 + while (count > 0) {
9 + loaded = xread(fd, p, count);
10 + if (loaded == 0)
11 + die("unexpected end of file");
12 + else if (loaded < 0)
13 + die("read error (%s)", strerror(errno));
14 + count -= loaded;
15 + p += loaded;
16 + }
17 +}
18 +
19 void write_or_die(int fd, const void *buf, size_t count)
20 {
21 const char *p = buf;