@@ -2963,7 +2963,7 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2963
2963
2964
2964
if (err ) {
2965
2965
free (ieot );
2966
- return err ;
2966
+ goto cleanup ;
2967
2967
}
2968
2968
2969
2969
offset = hashfile_total (f );
@@ -2992,8 +2992,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
2992
2992
hashwrite (f , sb .buf , sb .len );
2993
2993
strbuf_release (& sb );
2994
2994
free (ieot );
2995
- if (err )
2996
- return -1 ;
2995
+ /*
2996
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
2997
+ * and this part may want to be simplified.
2998
+ */
2999
+ if (err ) {
3000
+ err = -1 ;
3001
+ goto cleanup ;
3002
+ }
2997
3003
}
2998
3004
2999
3005
if (write_extensions & WRITE_SPLIT_INDEX_EXTENSION &&
@@ -3008,8 +3014,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3008
3014
sb .len ) < 0 ;
3009
3015
hashwrite (f , sb .buf , sb .len );
3010
3016
strbuf_release (& sb );
3011
- if (err )
3012
- return -1 ;
3017
+ /*
3018
+ * NEEDSWORK: write_link_extension() never returns a failure,
3019
+ * and this part may want to be simplified.
3020
+ */
3021
+ if (err ) {
3022
+ err = -1 ;
3023
+ goto cleanup ;
3024
+ }
3013
3025
}
3014
3026
if (write_extensions & WRITE_CACHE_TREE_EXTENSION &&
3015
3027
!drop_cache_tree && istate -> cache_tree ) {
@@ -3019,8 +3031,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3019
3031
err = write_index_ext_header (f , eoie_c , CACHE_EXT_TREE , sb .len ) < 0 ;
3020
3032
hashwrite (f , sb .buf , sb .len );
3021
3033
strbuf_release (& sb );
3022
- if (err )
3023
- return -1 ;
3034
+ /*
3035
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3036
+ * and this part may want to be simplified.
3037
+ */
3038
+ if (err ) {
3039
+ err = -1 ;
3040
+ goto cleanup ;
3041
+ }
3024
3042
}
3025
3043
if (write_extensions & WRITE_RESOLVE_UNDO_EXTENSION &&
3026
3044
istate -> resolve_undo ) {
@@ -3031,8 +3049,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3031
3049
sb .len ) < 0 ;
3032
3050
hashwrite (f , sb .buf , sb .len );
3033
3051
strbuf_release (& sb );
3034
- if (err )
3035
- return -1 ;
3052
+ /*
3053
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3054
+ * and this part may want to be simplified.
3055
+ */
3056
+ if (err ) {
3057
+ err = -1 ;
3058
+ goto cleanup ;
3059
+ }
3036
3060
}
3037
3061
if (write_extensions & WRITE_UNTRACKED_CACHE_EXTENSION &&
3038
3062
istate -> untracked ) {
@@ -3043,8 +3067,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3043
3067
sb .len ) < 0 ;
3044
3068
hashwrite (f , sb .buf , sb .len );
3045
3069
strbuf_release (& sb );
3046
- if (err )
3047
- return -1 ;
3070
+ /*
3071
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3072
+ * and this part may want to be simplified.
3073
+ */
3074
+ if (err ) {
3075
+ err = -1 ;
3076
+ goto cleanup ;
3077
+ }
3048
3078
}
3049
3079
if (write_extensions & WRITE_FSMONITOR_EXTENSION &&
3050
3080
istate -> fsmonitor_last_update ) {
@@ -3054,12 +3084,25 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3054
3084
err = write_index_ext_header (f , eoie_c , CACHE_EXT_FSMONITOR , sb .len ) < 0 ;
3055
3085
hashwrite (f , sb .buf , sb .len );
3056
3086
strbuf_release (& sb );
3057
- if (err )
3058
- return -1 ;
3087
+ /*
3088
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3089
+ * and this part may want to be simplified.
3090
+ */
3091
+ if (err ) {
3092
+ err = -1 ;
3093
+ goto cleanup ;
3094
+ }
3059
3095
}
3060
3096
if (istate -> sparse_index ) {
3061
- if (write_index_ext_header (f , eoie_c , CACHE_EXT_SPARSE_DIRECTORIES , 0 ) < 0 )
3062
- return -1 ;
3097
+ err = write_index_ext_header (f , eoie_c , CACHE_EXT_SPARSE_DIRECTORIES , 0 );
3098
+ /*
3099
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3100
+ * and this part may want to be simplified.
3101
+ */
3102
+ if (err ) {
3103
+ err = -1 ;
3104
+ goto cleanup ;
3105
+ }
3063
3106
}
3064
3107
3065
3108
/*
@@ -3075,8 +3118,14 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3075
3118
err = write_index_ext_header (f , NULL , CACHE_EXT_ENDOFINDEXENTRIES , sb .len ) < 0 ;
3076
3119
hashwrite (f , sb .buf , sb .len );
3077
3120
strbuf_release (& sb );
3078
- if (err )
3079
- return -1 ;
3121
+ /*
3122
+ * NEEDSWORK: write_index_ext_header() never returns a failure,
3123
+ * and this part may want to be simplified.
3124
+ */
3125
+ if (err ) {
3126
+ err = -1 ;
3127
+ goto cleanup ;
3128
+ }
3080
3129
}
3081
3130
3082
3131
csum_fsync_flag = 0 ;
@@ -3085,13 +3134,16 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3085
3134
3086
3135
finalize_hashfile (f , istate -> oid .hash , FSYNC_COMPONENT_INDEX ,
3087
3136
CSUM_HASH_IN_STREAM | csum_fsync_flag );
3137
+ f = NULL ;
3088
3138
3089
3139
if (close_tempfile_gently (tempfile )) {
3090
- error (_ ("could not close '%s'" ), get_tempfile_path (tempfile ));
3091
- return -1 ;
3140
+ err = error (_ ("could not close '%s'" ), get_tempfile_path (tempfile ));
3141
+ goto cleanup ;
3142
+ }
3143
+ if (stat (get_tempfile_path (tempfile ), & st )) {
3144
+ err = error_errno (_ ("could not stat '%s'" ), get_tempfile_path (tempfile ));
3145
+ goto cleanup ;
3092
3146
}
3093
- if (stat (get_tempfile_path (tempfile ), & st ))
3094
- return -1 ;
3095
3147
istate -> timestamp .sec = (unsigned int )st .st_mtime ;
3096
3148
istate -> timestamp .nsec = ST_MTIME_NSEC (st );
3097
3149
trace_performance_since (start , "write index, changed mask = %x" , istate -> cache_changed );
@@ -3106,6 +3158,11 @@ static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
3106
3158
istate -> cache_nr );
3107
3159
3108
3160
return 0 ;
3161
+
3162
+ cleanup :
3163
+ if (f )
3164
+ discard_hashfile (f );
3165
+ return err ;
3109
3166
}
3110
3167
3111
3168
void set_alternate_index_output (const char * name )
0 commit comments