lock_file(): exit early if lockfile cannot be opened

This is a bit easier to read than the old version, which nested part of the non-error code in an "if" block. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Reviewed-by: Ronnie Sahlberg <sahlberg@google.com> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Oct 1, 2014 at 12:28 UTC e31e949b9f9b5e6dcff42e2242cf1a6fb3686b91
1 file changed +11 -12
lockfile.c
+11 -12
@@ -197,19 +197,18 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
197 resolve_symlink(lk->filename, max_path_len);
198 strcat(lk->filename, LOCK_SUFFIX);
199 lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
200 - if (0 <= lk->fd) {
201 - lk->owner = getpid();
202 - if (adjust_shared_perm(lk->filename)) {
203 - int save_errno = errno;
204 - error("cannot fix permission bits on %s",
205 - lk->filename);
206 - rollback_lock_file(lk);
207 - errno = save_errno;
208 - return -1;
209 - }
210 - }
211 - else
200 + if (lk->fd < 0) {
201 lk->filename[0] = 0;
202 + return -1;
203 + }
204 + lk->owner = getpid();
205 + if (adjust_shared_perm(lk->filename)) {
206 + int save_errno = errno;
207 + error("cannot fix permission bits on %s", lk->filename);
208 + rollback_lock_file(lk);
209 + errno = save_errno;
210 + return -1;
211 + }
212 return lk->fd;
213 }
214