From: Michael Haggerty Date: Tue, 01 Apr 2014 15:58:25 GMT Subject: [PATCH 17/22] lock_file(): exit early if lockfile cannot be opened Message-ID: <1396367910-7299-18-git-send-email-mhagger@alum.mit.edu> In-Reply-To: <1396367910-7299-1-git-send-email-mhagger@alum.mit.edu> 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 --- lockfile.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lockfile.c b/lockfile.c index c1ca5b1..87b40c4 100644 --- a/lockfile.c +++ b/lockfile.c @@ -182,15 +182,16 @@ static int lock_file(struct lock_file *lk, const char *path, int flags) strbuf_addstr(&lk->lock_filename, ".lock"); lk->fd = open(lk->lock_filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666); - if (0 <= lk->fd) { - if (adjust_shared_perm(lk->lock_filename.buf)) { - error("cannot fix permission bits on %s", lk->lock_filename.buf); - rollback_lock_file(lk); - return -1; - } - } - else + if (lk->fd < 0) { strbuf_setlen(&lk->lock_filename, 0); + return -1; + } + if (adjust_shared_perm(lk->lock_filename.buf)) { + error("cannot fix permission bits on %s", lk->lock_filename.buf); + rollback_lock_file(lk); + return -1; + } + return lk->fd; } -- 1.9.0