+/* Read data from a file descriptor and return the amount of data read. A
+ * short read (less than the requested size) will only occur if end-of-file is
+ * hit. */
+size_t file_read(int fd, char *buf, size_t maxlen)
+{
+ size_t bytes_read = 0;
+
+ while (true) {
+ ssize_t res = read(fd, buf, maxlen);
+ if (res < 0) {
+ if (errno == EINTR)
+ continue;
+ throw IOException("file_read: error reading");
+ } else if (res == 0) {
+ break;
+ } else {
+ bytes_read += res;
+ buf += res;
+ maxlen -= res;
+ }
+ }
+
+ return bytes_read;
+}
+
+/* Read the contents of a file (specified by an open file descriptor) and copy
+ * the data to the store. */