lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}()

Add variants of hold_lock_file_for_update_timeout_mode() that handle
arbitrary repositories.

Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
René Scharfe 2026-07-14 19:59:54 +02:00 committed by Junio C Hamano
parent 8432a4dee1
commit d43f701d32
2 changed files with 53 additions and 8 deletions

View File

@ -2,11 +2,14 @@
* Copyright (c) 2005, Junio C Hamano * Copyright (c) 2005, Junio C Hamano
*/ */


#define USE_THE_REPOSITORY_VARIABLE

#include "git-compat-util.h" #include "git-compat-util.h"
#include "abspath.h" #include "abspath.h"
#include "gettext.h" #include "gettext.h"
#include "lockfile.h" #include "lockfile.h"
#include "parse.h" #include "parse.h"
#include "repository.h"
#include "strbuf.h" #include "strbuf.h"
#include "wrapper.h" #include "wrapper.h"


@ -162,8 +165,8 @@ out:
} }


/* Make sure errno contains a meaningful value on error */ /* Make sure errno contains a meaningful value on error */
static int lock_file(struct lock_file *lk, const char *path, int flags, static int lock_file(struct repository *r, struct lock_file *lk,
int mode) const char *path, int flags, int mode)
{ {
struct strbuf base_path = STRBUF_INIT; struct strbuf base_path = STRBUF_INIT;
struct strbuf lock_path = STRBUF_INIT; struct strbuf lock_path = STRBUF_INIT;
@ -176,7 +179,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
get_lock_path(&lock_path, base_path.buf); get_lock_path(&lock_path, base_path.buf);
get_pid_path(&pid_path, base_path.buf); get_pid_path(&pid_path, base_path.buf);


lk->tempfile = create_tempfile_mode(lock_path.buf, mode); lk->tempfile = repo_create_tempfile_mode(r, lock_path.buf, mode);
if (lk->tempfile) if (lk->tempfile)
lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode); lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode);


@ -200,8 +203,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
* timeout_ms milliseconds. If timeout_ms is 0, try locking the file * timeout_ms milliseconds. If timeout_ms is 0, try locking the file
* exactly once. If timeout_ms is -1, try indefinitely. * exactly once. If timeout_ms is -1, try indefinitely.
*/ */
static int lock_file_timeout(struct lock_file *lk, const char *path, static int lock_file_timeout(struct repository *r, struct lock_file *lk,
int flags, long timeout_ms, int mode) const char *path, int flags, long timeout_ms,
int mode)
{ {
int n = 1; int n = 1;
int multiplier = 1; int multiplier = 1;
@ -209,7 +213,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
static int random_initialized = 0; static int random_initialized = 0;


if (timeout_ms == 0) if (timeout_ms == 0)
return lock_file(lk, path, flags, mode); return lock_file(r, lk, path, flags, mode);


if (!random_initialized) { if (!random_initialized) {
srand((unsigned int)getpid()); srand((unsigned int)getpid());
@ -223,7 +227,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
long backoff_ms, wait_ms; long backoff_ms, wait_ms;
int fd; int fd;


fd = lock_file(lk, path, flags, mode); fd = lock_file(r, lk, path, flags, mode);


if (fd >= 0) if (fd >= 0)
return fd; /* success */ return fd; /* success */
@ -308,7 +312,17 @@ int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
const char *path, int flags, const char *path, int flags,
long timeout_ms, int mode) long timeout_ms, int mode)
{ {
int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode); return repo_hold_lock_file_for_update_timeout_mode(the_repository,
lk, path, flags,
timeout_ms, mode);
}

int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
struct lock_file *lk,
const char *path, int flags,
long timeout_ms, int mode)
{
int fd = lock_file_timeout(r, lk, path, flags, timeout_ms, mode);
if (fd < 0) { if (fd < 0) {
if (flags & LOCK_DIE_ON_ERROR) if (flags & LOCK_DIE_ON_ERROR)
unable_to_lock_die(path, errno); unable_to_lock_die(path, errno);

View File

@ -189,6 +189,11 @@ int hold_lock_file_for_update_timeout_mode(
struct lock_file *lk, const char *path, struct lock_file *lk, const char *path,
int flags, long timeout_ms, int mode); int flags, long timeout_ms, int mode);


int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
struct lock_file *lk,
const char *path, int flags,
long timeout_ms, int mode);

static inline int hold_lock_file_for_update_timeout( static inline int hold_lock_file_for_update_timeout(
struct lock_file *lk, const char *path, struct lock_file *lk, const char *path,
int flags, long timeout_ms) int flags, long timeout_ms)
@ -197,6 +202,16 @@ static inline int hold_lock_file_for_update_timeout(
timeout_ms, 0666); timeout_ms, 0666);
} }


static inline int repo_hold_lock_file_for_update_timeout(struct repository *r,
struct lock_file *lk,
const char *path,
int flags,
long timeout_ms)
{
return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
timeout_ms, 0666);
}

/* /*
* Attempt to create a lockfile for the file at `path` and return a * Attempt to create a lockfile for the file at `path` and return a
* file descriptor for writing to it, or -1 on error. The flags * file descriptor for writing to it, or -1 on error. The flags
@ -208,6 +223,13 @@ static inline int hold_lock_file_for_update(
return hold_lock_file_for_update_timeout(lk, path, flags, 0); return hold_lock_file_for_update_timeout(lk, path, flags, 0);
} }


static inline int repo_hold_lock_file_for_update(struct repository *r,
struct lock_file *lk,
const char *path, int flags)
{
return repo_hold_lock_file_for_update_timeout(r, lk, path, flags, 0);
}

static inline int hold_lock_file_for_update_mode( static inline int hold_lock_file_for_update_mode(
struct lock_file *lk, const char *path, struct lock_file *lk, const char *path,
int flags, int mode) int flags, int mode)
@ -215,6 +237,15 @@ static inline int hold_lock_file_for_update_mode(
return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode); return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode);
} }


static inline int repo_hold_lock_file_for_update_mode(struct repository *r,
struct lock_file *lk,
const char *path,
int flags, int mode)
{
return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
0, mode);
}

/* /*
* Return a nonzero value iff `lk` is currently locked. * Return a nonzero value iff `lk` is currently locked.
*/ */