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>
René Scharfe committed
Jul 14, 2026 at 19:59 UTC
d43f701d326ef3fe1bf04c1de24fa025ab228af8
2 files changed
+53
-8
lockfile.c
+22
-8
@@ -2,11 +2,14 @@
2
* Copyright (c) 2005, Junio C Hamano
3
*/
4
5
+#define USE_THE_REPOSITORY_VARIABLE
6
+
7
#include "git-compat-util.h"
8
#include "abspath.h"
9
#include "gettext.h"
10
#include "lockfile.h"
11
#include "parse.h"
12
+#include "repository.h"
13
#include "strbuf.h"
14
#include "wrapper.h"
15
@@ -162,8 +165,8 @@ out:
165
}
166
167
/* Make sure errno contains a meaningful value on error */
165
-static int lock_file(struct lock_file *lk, const char *path, int flags,
166
- int mode)
168
+static int lock_file(struct repository *r, struct lock_file *lk,
169
+ const char *path, int flags, int mode)
170
{
171
struct strbuf base_path = STRBUF_INIT;
172
struct strbuf lock_path = STRBUF_INIT;
@@ -176,7 +179,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
179
get_lock_path(&lock_path, base_path.buf);
180
get_pid_path(&pid_path, base_path.buf);
181
179
- lk->tempfile = create_tempfile_mode(lock_path.buf, mode);
182
+ lk->tempfile = repo_create_tempfile_mode(r, lock_path.buf, mode);
183
if (lk->tempfile)
184
lk->pid_tempfile = create_lock_pid_file(pid_path.buf, mode);
185
@@ -200,8 +203,9 @@ static int lock_file(struct lock_file *lk, const char *path, int flags,
203
* timeout_ms milliseconds. If timeout_ms is 0, try locking the file
204
* exactly once. If timeout_ms is -1, try indefinitely.
205
*/
203
-static int lock_file_timeout(struct lock_file *lk, const char *path,
204
- int flags, long timeout_ms, int mode)
206
+static int lock_file_timeout(struct repository *r, struct lock_file *lk,
207
+ const char *path, int flags, long timeout_ms,
208
+ int mode)
209
{
210
int n = 1;
211
int multiplier = 1;
@@ -209,7 +213,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
213
static int random_initialized = 0;
214
215
if (timeout_ms == 0)
212
- return lock_file(lk, path, flags, mode);
216
+ return lock_file(r, lk, path, flags, mode);
217
218
if (!random_initialized) {
219
srand((unsigned int)getpid());
@@ -223,7 +227,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
227
long backoff_ms, wait_ms;
228
int fd;
229
226
- fd = lock_file(lk, path, flags, mode);
230
+ fd = lock_file(r, lk, path, flags, mode);
231
232
if (fd >= 0)
233
return fd; /* success */
@@ -308,7 +312,17 @@ int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
312
const char *path, int flags,
313
long timeout_ms, int mode)
314
{
311
- int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);
315
+ return repo_hold_lock_file_for_update_timeout_mode(the_repository,
316
+ lk, path, flags,
317
+ timeout_ms, mode);
318
+}
319
+
320
+int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
321
+ struct lock_file *lk,
322
+ const char *path, int flags,
323
+ long timeout_ms, int mode)
324
+{
325
+ int fd = lock_file_timeout(r, lk, path, flags, timeout_ms, mode);
326
if (fd < 0) {
327
if (flags & LOCK_DIE_ON_ERROR)
328
unable_to_lock_die(path, errno);
lockfile.h
+31
@@ -189,6 +189,11 @@ int hold_lock_file_for_update_timeout_mode(
189
struct lock_file *lk, const char *path,
190
int flags, long timeout_ms, int mode);
191
192
+int repo_hold_lock_file_for_update_timeout_mode(struct repository *r,
193
+ struct lock_file *lk,
194
+ const char *path, int flags,
195
+ long timeout_ms, int mode);
196
+
197
static inline int hold_lock_file_for_update_timeout(
198
struct lock_file *lk, const char *path,
199
int flags, long timeout_ms)
@@ -197,6 +202,16 @@ static inline int hold_lock_file_for_update_timeout(
202
timeout_ms, 0666);
203
}
204
205
+static inline int repo_hold_lock_file_for_update_timeout(struct repository *r,
206
+ struct lock_file *lk,
207
+ const char *path,
208
+ int flags,
209
+ long timeout_ms)
210
+{
211
+ return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
212
+ timeout_ms, 0666);
213
+}
214
+
215
/*
216
* Attempt to create a lockfile for the file at `path` and return a
217
* file descriptor for writing to it, or -1 on error. The flags
@@ -208,6 +223,13 @@ static inline int hold_lock_file_for_update(
223
return hold_lock_file_for_update_timeout(lk, path, flags, 0);
224
}
225
226
+static inline int repo_hold_lock_file_for_update(struct repository *r,
227
+ struct lock_file *lk,
228
+ const char *path, int flags)
229
+{
230
+ return repo_hold_lock_file_for_update_timeout(r, lk, path, flags, 0);
231
+}
232
+
233
static inline int hold_lock_file_for_update_mode(
234
struct lock_file *lk, const char *path,
235
int flags, int mode)
@@ -215,6 +237,15 @@ static inline int hold_lock_file_for_update_mode(
237
return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode);
238
}
239
240
+static inline int repo_hold_lock_file_for_update_mode(struct repository *r,
241
+ struct lock_file *lk,
242
+ const char *path,
243
+ int flags, int mode)
244
+{
245
+ return repo_hold_lock_file_for_update_timeout_mode(r, lk, path, flags,
246
+ 0, mode);
247
+}
248
+
249
/*
250
* Return a nonzero value iff `lk` is currently locked.
251
*/