commit_lock_file(): use get_locked_file_path()

First beef up the sanity checking in get_locked_file_path() to match that in commit_lock_file(). Then rewrite commit_lock_file() to use get_locked_file_path() for its pathname computation. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 10, 2015 at 11:47 UTC 9c77381d6a495e102b811df954d0fa14e62250ab
1 file changed +12 -16
lockfile.c
+12 -16
@@ -389,8 +389,10 @@ char *get_locked_file_path(struct lock_file *lk)
389 {
390 if (!lk->active)
391 die("BUG: get_locked_file_path() called for unlocked object");
392 - if (lk->filename.len <= LOCK_SUFFIX_LEN)
392 + if (lk->filename.len <= LOCK_SUFFIX_LEN ||
393 + strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
394 die("BUG: get_locked_file_path() called for malformed lock object");
395 + /* remove ".lock": */
396 return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
397 }
398
@@ -458,22 +460,16 @@ int commit_lock_file_to(struct lock_file *lk, const char *path)
460
461 int commit_lock_file(struct lock_file *lk)
462 {
461 - static struct strbuf result_file = STRBUF_INIT;
462 - int err;
463 + char *result_path = get_locked_file_path(lk);
464
464 - if (!lk->active)
465 - die("BUG: attempt to commit unlocked object");
466 -
467 - if (lk->filename.len <= LOCK_SUFFIX_LEN ||
468 - strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
469 - die("BUG: lockfile filename corrupt");
470 -
471 - /* remove ".lock": */
472 - strbuf_add(&result_file, lk->filename.buf,
473 - lk->filename.len - LOCK_SUFFIX_LEN);
474 - err = commit_lock_file_to(lk, result_file.buf);
475 - strbuf_reset(&result_file);
476 - return err;
465 + if (commit_lock_file_to(lk, result_path)) {
466 + int save_errno = errno;
467 + free(result_path);
468 + errno = save_errno;
469 + return -1;
470 + }
471 + free(result_path);
472 + return 0;
473 }
474
475 void rollback_lock_file(struct lock_file *lk)