entry.c: check if file exists after checkout

If we are checking out a file and somebody else racily deletes our file, then we would write garbage to the cache entry. Fix that by checking the result of the lstat() call on that file. Print an error to the user if the file does not exist. Reported-by: Jeff King <peff@peff.net> Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Oct 5, 2017 at 12:44 UTC 11179eb31178d3d162e1b0af50edbdc2a14da23b
1 file changed +3 -1
entry.c
+3 -1
@@ -341,7 +341,9 @@ finish:
341 if (state->refresh_cache) {
342 assert(state->istate);
343 if (!fstat_done)
344 - lstat(ce->name, &st);
344 + if (lstat(ce->name, &st) < 0)
345 + return error_errno("unable to stat just-written file %s",
346 + ce->name);
347 fill_stat_cache_info(ce, &st);
348 ce->ce_flags |= CE_UPDATE_IN_BASE;
349 state->istate->cache_changed |= CE_ENTRY_CHANGED;