close_lock_file(): exit (successfully) if file is already closed

Suggested-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC 419f0c0f681b76d720699977abe03d29e22db554
2 files changed +6 -2
lockfile.c
+5 -1
@@ -233,6 +233,10 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
233 int close_lock_file(struct lock_file *lk)
234 {
235 int fd = lk->fd;
236 +
237 + if (fd < 0)
238 + return 0;
239 +
240 lk->fd = -1;
241 return close(fd);
242 }
@@ -251,7 +255,7 @@ int commit_lock_file(struct lock_file *lk)
255 {
256 char result_file[PATH_MAX];
257 size_t i;
254 - if (lk->fd >= 0 && close_lock_file(lk))
258 + if (close_lock_file(lk))
259 return -1;
260 strcpy(result_file, lk->filename);
261 i = strlen(result_file) - 5; /* .lock */
read-cache.c
+1 -1
@@ -2042,7 +2042,7 @@ void set_alternate_index_output(const char *name)
2042 static int commit_locked_index(struct lock_file *lk)
2043 {
2044 if (alternate_index_output) {
2045 - if (lk->fd >= 0 && close_lock_file(lk))
2045 + if (close_lock_file(lk))
2046 return -1;
2047 if (rename(lk->filename, alternate_index_output))
2048 return -1;