aboutsummaryrefslogtreecommitdiff
path: root/write_or_die.c
diff options
context:
space:
mode:
authorRene Scharfe <rene.scharfe@lsrfire.ath.cx>2006-08-21 20:43:43 +0200
committerJunio C Hamano <junkio@cox.net>2006-08-21 20:22:23 -0700
commit7230e6d042ae385377f09c4d226d9b1aa7a2c13b (patch)
tree15c64d30f517ba50cac77f81efddc71d22666023 /write_or_die.c
parent3f0073a2fabce18303aeef154dd6ec5aa8faa5e7 (diff)
downloadgit-7230e6d042ae385377f09c4d226d9b1aa7a2c13b.tar.gz
git-7230e6d042ae385377f09c4d226d9b1aa7a2c13b.tar.xz
Add write_or_die(), a helper function
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>
Diffstat (limited to 'write_or_die.c')
-rw-r--r--write_or_die.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/write_or_die.c b/write_or_die.c
new file mode 100644
index 000000000..ab4cb8a69
--- /dev/null
+++ b/write_or_die.c
@@ -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;
+ }
+}