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>
This commit is contained in:
Michael Haggerty 2015-08-10 11:47:40 +02:00 committed by Junio C Hamano
parent b4fb09e4da
commit 9c77381d6a

View File

@ -389,8 +389,10 @@ char *get_locked_file_path(struct lock_file *lk)
{ {
if (!lk->active) if (!lk->active)
die("BUG: get_locked_file_path() called for unlocked object"); die("BUG: get_locked_file_path() called for unlocked object");
if (lk->filename.len <= LOCK_SUFFIX_LEN) if (lk->filename.len <= LOCK_SUFFIX_LEN ||
strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX))
die("BUG: get_locked_file_path() called for malformed lock object"); die("BUG: get_locked_file_path() called for malformed lock object");
/* remove ".lock": */
return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN); return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
} }
@ -458,22 +460,16 @@ int commit_lock_file_to(struct lock_file *lk, const char *path)
int commit_lock_file(struct lock_file *lk) int commit_lock_file(struct lock_file *lk)
{ {
static struct strbuf result_file = STRBUF_INIT; char *result_path = get_locked_file_path(lk);
int err;
if (!lk->active) if (commit_lock_file_to(lk, result_path)) {
die("BUG: attempt to commit unlocked object"); int save_errno = errno;
free(result_path);
if (lk->filename.len <= LOCK_SUFFIX_LEN || errno = save_errno;
strcmp(lk->filename.buf + lk->filename.len - LOCK_SUFFIX_LEN, LOCK_SUFFIX)) return -1;
die("BUG: lockfile filename corrupt"); }
free(result_path);
/* remove ".lock": */ return 0;
strbuf_add(&result_file, lk->filename.buf,
lk->filename.len - LOCK_SUFFIX_LEN);
err = commit_lock_file_to(lk, result_file.buf);
strbuf_reset(&result_file);
return err;
} }
void rollback_lock_file(struct lock_file *lk) void rollback_lock_file(struct lock_file *lk)