remember to discard_cache() before reading a different cache! */
| 2196 | |
| 2197 | /* remember to discard_cache() before reading a different cache! */ |
| 2198 | int do_read_index(struct index_state *istate, const char *path, int must_exist) |
| 2199 | { |
| 2200 | int fd; |
| 2201 | struct stat st; |
| 2202 | unsigned long src_offset; |
| 2203 | const struct cache_header *hdr; |
| 2204 | const char *mmap; |
| 2205 | size_t mmap_size; |
| 2206 | struct load_index_extensions p; |
| 2207 | size_t extension_offset = 0; |
| 2208 | int nr_threads, cpus; |
| 2209 | struct index_entry_offset_table *ieot = NULL; |
| 2210 | |
| 2211 | if (istate->initialized) |
| 2212 | return istate->cache_nr; |
| 2213 | |
| 2214 | istate->timestamp.sec = 0; |
| 2215 | istate->timestamp.nsec = 0; |
| 2216 | fd = open(path, O_RDONLY); |
| 2217 | if (fd < 0) { |
| 2218 | if (!must_exist && errno == ENOENT) { |
| 2219 | set_new_index_sparsity(istate); |
| 2220 | istate->initialized = 1; |
| 2221 | return 0; |
| 2222 | } |
| 2223 | die_errno(_("%s: index file open failed"), path); |
| 2224 | } |
| 2225 | |
| 2226 | if (fstat(fd, &st)) |
| 2227 | die_errno(_("%s: cannot stat the open index"), path); |
| 2228 | |
| 2229 | mmap_size = xsize_t(st.st_size); |
| 2230 | if (mmap_size < sizeof(struct cache_header) + the_hash_algo->rawsz) |
| 2231 | die(_("%s: index file smaller than expected"), path); |
| 2232 | |
| 2233 | mmap = xmmap_gently(NULL, mmap_size, PROT_READ, MAP_PRIVATE, fd, 0); |
| 2234 | if (mmap == MAP_FAILED) |
| 2235 | die_errno(_("%s: unable to map index file%s"), path, |
| 2236 | mmap_os_err()); |
| 2237 | close(fd); |
| 2238 | |
| 2239 | hdr = (const struct cache_header *)mmap; |
| 2240 | if (verify_hdr(hdr, mmap_size) < 0) |
| 2241 | goto unmap; |
| 2242 | |
| 2243 | oidread(&istate->oid, (const unsigned char *)hdr + mmap_size - the_hash_algo->rawsz, |
| 2244 | the_repository->hash_algo); |
| 2245 | istate->version = ntohl(hdr->hdr_version); |
| 2246 | istate->cache_nr = ntohl(hdr->hdr_entries); |
| 2247 | istate->cache_alloc = alloc_nr(istate->cache_nr); |
| 2248 | CALLOC_ARRAY(istate->cache, istate->cache_alloc); |
| 2249 | istate->initialized = 1; |
| 2250 | |
| 2251 | p.istate = istate; |
| 2252 | p.mmap = mmap; |
| 2253 | p.mmap_size = mmap_size; |
| 2254 | |
| 2255 | src_offset = sizeof(*hdr); |