]> git.ipfire.org Git - thirdparty/git.git/commitdiff
lockfile: add repo_hold_lock_file_for_update{,_timeout}{,_mode}()
authorRené Scharfe <l.s.r@web.de>
Tue, 14 Jul 2026 17:59:54 +0000 (19:59 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 14 Jul 2026 19:54:25 +0000 (12:54 -0700)
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>
lockfile.c
lockfile.h

index 7add2f136abd857dfae759ff7c3f85b589b1c389..100f60377174efee3493ef2a392b7193ea75ea55 100644 (file)
@@ -2,11 +2,14 @@
  * Copyright (c) 2005, Junio C Hamano
  */
 
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "gettext.h"
 #include "lockfile.h"
 #include "parse.h"
+#include "repository.h"
 #include "strbuf.h"
 #include "wrapper.h"
 
@@ -162,8 +165,8 @@ out:
 }
 
 /* Make sure errno contains a meaningful value on error */
-static int lock_file(struct lock_file *lk, const char *path, int flags,
-                    int mode)
+static int lock_file(struct repository *r, struct lock_file *lk,
+                    const char *path, int flags, int mode)
 {
        struct strbuf base_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_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)
                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
  * exactly once. If timeout_ms is -1, try indefinitely.
  */
-static int lock_file_timeout(struct lock_file *lk, const char *path,
-                            int flags, long timeout_ms, int mode)
+static int lock_file_timeout(struct repository *r, struct lock_file *lk,
+                            const char *path, int flags, long timeout_ms,
+                            int mode)
 {
        int n = 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;
 
        if (timeout_ms == 0)
-               return lock_file(lk, path, flags, mode);
+               return lock_file(r, lk, path, flags, mode);
 
        if (!random_initialized) {
                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;
                int fd;
 
-               fd = lock_file(lk, path, flags, mode);
+               fd = lock_file(r, lk, path, flags, mode);
 
                if (fd >= 0)
                        return fd; /* success */
@@ -308,7 +312,17 @@ int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
                                           const char *path, int flags,
                                           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 (flags & LOCK_DIE_ON_ERROR)
                        unable_to_lock_die(path, errno);
index e7233f28dea5c707ebb521cecc0df84644686156..1667612674b52a7ab1061f442b089e9c1a0cd599 100644 (file)
@@ -189,6 +189,11 @@ int hold_lock_file_for_update_timeout_mode(
        struct lock_file *lk, const char *path,
        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(
        struct lock_file *lk, const char *path,
        int flags, long timeout_ms)
@@ -197,6 +202,16 @@ static inline int hold_lock_file_for_update_timeout(
                                                      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
  * 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);
 }
 
+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(
        struct lock_file *lk, const char *path,
        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);
 }
 
+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.
  */