Code

Merge branch 'sp/mmap'
[git.git] / write_or_die.c
index 650f13fc012b14a6aaa62534727d4ef9cdce58bd..6db1d3123d1761dee4850e0a56a9b86b4bf1722a 100644 (file)
@@ -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;