Skip to content

Commit

Permalink
Create read_or_die utility routine.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Shawn O. Pearce authored and Junio C Hamano committed Dec 29, 2006
1 parent 2dc3a23 commit 75025cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ extern char *git_commit_encoding;
extern char *git_log_output_encoding;

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);

Expand Down
16 changes: 16 additions & 0 deletions write_or_die.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
#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)
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;
Expand Down

0 comments on commit 75025cc

Please sign in to comment.