Skip to content

Commit 9c77381

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b4fb09e commit 9c77381

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

lockfile.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,10 @@ char *get_locked_file_path(struct lock_file *lk)
389389
{
390390
if (!lk->active)
391391
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))
393394
die("BUG: get_locked_file_path() called for malformed lock object");
395+
/* remove ".lock": */
394396
return xmemdupz(lk->filename.buf, lk->filename.len - LOCK_SUFFIX_LEN);
395397
}
396398

@@ -458,22 +460,16 @@ int commit_lock_file_to(struct lock_file *lk, const char *path)
458460

459461
int commit_lock_file(struct lock_file *lk)
460462
{
461-
static struct strbuf result_file = STRBUF_INIT;
462-
int err;
463+
char *result_path = get_locked_file_path(lk);
463464

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;
477473
}
478474

479475
void rollback_lock_file(struct lock_file *lk)

0 commit comments

Comments
 (0)