+/*
+ * Depending on `mmap_strategy`, either mmap or read the contents of
+ * the `packed-refs` file into the `packed_refs` instance. Return 1 if
+ * the file existed and was read, or 0 if the file was absent. Die on
+ * errors.
+ */
+static int load_contents(struct packed_ref_cache *packed_refs)
+{
+ int fd;
+ struct stat st;
+ size_t size;
+ ssize_t bytes_read;
+
+ fd = open(packed_refs->refs->path, O_RDONLY);
+ if (fd < 0) {
+ if (errno == ENOENT) {
+ /*
+ * This is OK; it just means that no
+ * "packed-refs" file has been written yet,
+ * which is equivalent to it being empty,
+ * which is its state when initialized with
+ * zeros.
+ */
+ return 0;
+ } else {
+ die_errno("couldn't read %s", packed_refs->refs->path);
+ }
+ }
+
+ stat_validity_update(&packed_refs->validity, fd);
+
+ if (fstat(fd, &st) < 0)
+ die_errno("couldn't stat %s", packed_refs->refs->path);
+ size = xsize_t(st.st_size);
+
+ switch (mmap_strategy) {
+ case MMAP_NONE:
+ case MMAP_TEMPORARY:
+ packed_refs->buf = xmalloc(size);
+ bytes_read = read_in_full(fd, packed_refs->buf, size);
+ if (bytes_read < 0 || bytes_read != size)
+ die_errno("couldn't read %s", packed_refs->refs->path);
+ packed_refs->eof = packed_refs->buf + size;
+ packed_refs->mmapped = 0;
+ break;
+ case MMAP_OK:
+ packed_refs->buf = xmmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+ packed_refs->eof = packed_refs->buf + size;
+ packed_refs->mmapped = 1;
+ break;
+ }
+ close(fd);
+
+ return 1;
+}
+