Browse Source
The little helper write_or_die() won't come back with bad news about full disks or broken pipes. It either succeeds or terminates the program, making additional error handling unnecessary. This patch adds the new function and uses it to replace two similar ones (the one in tar-tree originally has been copied from cat-file btw.). I chose to add the fd parameter which both lacked to make write_or_die() just as flexible as write() and thus suitable for lib-ification. There is a regression: error messages emitted by this function don't show the program name, while the replaced two functions did. That's acceptable, I think; a lot of other functions do the same. Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <junkio@cox.net>maint
Rene Scharfe
19 years ago
committed by
Junio C Hamano
5 changed files with 32 additions and 46 deletions
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
#include "cache.h" |
||||
|
||||
void write_or_die(int fd, const void *buf, size_t count) |
||||
{ |
||||
const char *p = buf; |
||||
ssize_t written; |
||||
|
||||
while (count > 0) { |
||||
written = xwrite(fd, p, count); |
||||
if (written == 0) |
||||
die("disk full?"); |
||||
else if (written < 0) { |
||||
if (errno == EPIPE) |
||||
exit(0); |
||||
die("write error (%s)", strerror(errno)); |
||||
} |
||||
count -= written; |
||||
p += written; |
||||
} |
||||
} |
Loading…
Reference in new issue