* On success, `tempfile` is closed. If it is the temporary file * of a `struct lock_file`, we will therefore effectively perform * a 'close_lock_file_gently()`. Since that is an implementation * detail of lockfiles, callers of `do_write_index()` should not * rely on it. */
| 2804 | * rely on it. |
| 2805 | */ |
| 2806 | static int do_write_index(struct index_state *istate, struct tempfile *tempfile, |
| 2807 | enum write_extensions write_extensions, unsigned flags) |
| 2808 | { |
| 2809 | uint64_t start = getnanotime(); |
| 2810 | struct hashfile *f; |
| 2811 | struct git_hash_ctx *eoie_c = NULL; |
| 2812 | struct cache_header hdr; |
| 2813 | int i, err = 0, removed, extended, hdr_version; |
| 2814 | struct cache_entry **cache = istate->cache; |
| 2815 | int entries = istate->cache_nr; |
| 2816 | struct stat st; |
| 2817 | struct ondisk_cache_entry ondisk; |
| 2818 | struct strbuf previous_name_buf = STRBUF_INIT, *previous_name; |
| 2819 | int drop_cache_tree = istate->drop_cache_tree; |
| 2820 | off_t offset; |
| 2821 | int csum_fsync_flag; |
| 2822 | int ieot_entries = 1; |
| 2823 | struct index_entry_offset_table *ieot = NULL; |
| 2824 | struct repository *r = istate->repo; |
| 2825 | struct strbuf sb = STRBUF_INIT; |
| 2826 | int nr, nr_threads, ret; |
| 2827 | |
| 2828 | f = hashfd(the_repository->hash_algo, tempfile->fd, tempfile->filename.buf); |
| 2829 | |
| 2830 | prepare_repo_settings(r); |
| 2831 | f->skip_hash = r->settings.index_skip_hash; |
| 2832 | |
| 2833 | for (i = removed = extended = 0; i < entries; i++) { |
| 2834 | if (cache[i]->ce_flags & CE_REMOVE) |
| 2835 | removed++; |
| 2836 | |
| 2837 | /* reduce extended entries if possible */ |
| 2838 | cache[i]->ce_flags &= ~CE_EXTENDED; |
| 2839 | if (cache[i]->ce_flags & CE_EXTENDED_FLAGS) { |
| 2840 | extended++; |
| 2841 | cache[i]->ce_flags |= CE_EXTENDED; |
| 2842 | } |
| 2843 | } |
| 2844 | |
| 2845 | if (!istate->version) |
| 2846 | istate->version = get_index_format_default(r); |
| 2847 | |
| 2848 | /* demote version 3 to version 2 when the latter suffices */ |
| 2849 | if (istate->version == 3 || istate->version == 2) |
| 2850 | istate->version = extended ? 3 : 2; |
| 2851 | |
| 2852 | hdr_version = istate->version; |
| 2853 | |
| 2854 | hdr.hdr_signature = htonl(CACHE_SIGNATURE); |
| 2855 | hdr.hdr_version = htonl(hdr_version); |
| 2856 | hdr.hdr_entries = htonl(entries - removed); |
| 2857 | |
| 2858 | hashwrite(f, &hdr, sizeof(hdr)); |
| 2859 | |
| 2860 | if (!HAVE_THREADS || repo_config_get_index_threads(the_repository, &nr_threads)) |
| 2861 | nr_threads = 1; |
| 2862 | |
| 2863 | if (nr_threads != 1 && record_ieot()) { |
no test coverage detected