Skip to content

Commit fa3bff2

Browse files
ttaylorrgitster
authored andcommitted
lockfile.c: introduce 'hold_lock_file_for_update_mode'
We use 'hold_lock_file_for_update' (and the '_timeout') variant to acquire a lock when updating references, the commit-graph file, and so on. In particular, the commit-graph machinery uses this to acquire a temporary file that is used to write a non-split commit-graph. In a subsequent commit, an issue in the commit-graph machinery produces graph files that have a different permission based on whether or not they are part of a multi-layer graph will be addressed. To do so, the commit-graph machinery will need a version of 'hold_lock_file_for_update' that takes the permission bits from the caller. Introduce such a function in this patch for both the 'hold_lock_file_for_update' and 'hold_lock_file_for_update_timeout' functions, and leave the existing functions alone by inlining their definitions in terms of the new mode variants. Note that, like in the previous commit, 'hold_lock_file_for_update_mode' is not guarenteed to set the given mode, since it may be modified by both the umask and 'core.sharedRepository'. Note also that even though the commit-graph machinery only calls 'hold_lock_file_for_update', that this is defined in terms of 'hold_lock_file_for_update_timeout', and so both need an additional mode parameter here. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bef0413 commit fa3bff2

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

lockfile.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ static void resolve_symlink(struct strbuf *path)
7070
}
7171

7272
/* Make sure errno contains a meaningful value on error */
73-
static int lock_file(struct lock_file *lk, const char *path, int flags)
73+
static int lock_file(struct lock_file *lk, const char *path, int flags,
74+
int mode)
7475
{
7576
struct strbuf filename = STRBUF_INIT;
7677

@@ -79,7 +80,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
7980
resolve_symlink(&filename);
8081

8182
strbuf_addstr(&filename, LOCK_SUFFIX);
82-
lk->tempfile = create_tempfile(filename.buf);
83+
lk->tempfile = create_tempfile_mode(filename.buf, mode);
8384
strbuf_release(&filename);
8485
return lk->tempfile ? lk->tempfile->fd : -1;
8586
}
@@ -99,15 +100,15 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
99100
* exactly once. If timeout_ms is -1, try indefinitely.
100101
*/
101102
static int lock_file_timeout(struct lock_file *lk, const char *path,
102-
int flags, long timeout_ms)
103+
int flags, long timeout_ms, int mode)
103104
{
104105
int n = 1;
105106
int multiplier = 1;
106107
long remaining_ms = 0;
107108
static int random_initialized = 0;
108109

109110
if (timeout_ms == 0)
110-
return lock_file(lk, path, flags);
111+
return lock_file(lk, path, flags, mode);
111112

112113
if (!random_initialized) {
113114
srand((unsigned int)getpid());
@@ -121,7 +122,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
121122
long backoff_ms, wait_ms;
122123
int fd;
123124

124-
fd = lock_file(lk, path, flags);
125+
fd = lock_file(lk, path, flags, mode);
125126

126127
if (fd >= 0)
127128
return fd; /* success */
@@ -169,10 +170,11 @@ NORETURN void unable_to_lock_die(const char *path, int err)
169170
}
170171

171172
/* This should return a meaningful errno on failure */
172-
int hold_lock_file_for_update_timeout(struct lock_file *lk, const char *path,
173-
int flags, long timeout_ms)
173+
int hold_lock_file_for_update_timeout_mode(struct lock_file *lk,
174+
const char *path, int flags,
175+
long timeout_ms, int mode)
174176
{
175-
int fd = lock_file_timeout(lk, path, flags, timeout_ms);
177+
int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);
176178
if (fd < 0) {
177179
if (flags & LOCK_DIE_ON_ERROR)
178180
unable_to_lock_die(path, errno);

lockfile.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@
9090
* functions. In particular, the state diagram and the cleanup
9191
* machinery are all implemented in the tempfile module.
9292
*
93+
* Permission bits
94+
* ---------------
95+
*
96+
* If you call either `hold_lock_file_for_update_mode` or
97+
* `hold_lock_file_for_update_timeout_mode`, you can specify a suggested
98+
* mode for the underlying temporary file. Note that the file isn't
99+
* guaranteed to have this exact mode, since it may be limited by either
100+
* the umask, 'core.sharedRepository', or both. See `adjust_shared_perm`
101+
* for more.
93102
*
94103
* Error handling
95104
* --------------
@@ -156,12 +165,20 @@ struct lock_file {
156165
* file descriptor for writing to it, or -1 on error. If the file is
157166
* currently locked, retry with quadratic backoff for at least
158167
* timeout_ms milliseconds. If timeout_ms is 0, try exactly once; if
159-
* timeout_ms is -1, retry indefinitely. The flags argument and error
160-
* handling are described above.
168+
* timeout_ms is -1, retry indefinitely. The flags argument, error
169+
* handling, and mode are described above.
161170
*/
162-
int hold_lock_file_for_update_timeout(
171+
int hold_lock_file_for_update_timeout_mode(
172+
struct lock_file *lk, const char *path,
173+
int flags, long timeout_ms, int mode);
174+
175+
static inline int hold_lock_file_for_update_timeout(
163176
struct lock_file *lk, const char *path,
164-
int flags, long timeout_ms);
177+
int flags, long timeout_ms)
178+
{
179+
return hold_lock_file_for_update_timeout_mode(lk, path, flags,
180+
timeout_ms, 0666);
181+
}
165182

166183
/*
167184
* Attempt to create a lockfile for the file at `path` and return a
@@ -175,6 +192,13 @@ static inline int hold_lock_file_for_update(
175192
return hold_lock_file_for_update_timeout(lk, path, flags, 0);
176193
}
177194

195+
static inline int hold_lock_file_for_update_mode(
196+
struct lock_file *lk, const char *path,
197+
int flags, int mode)
198+
{
199+
return hold_lock_file_for_update_timeout_mode(lk, path, flags, 0, mode);
200+
}
201+
178202
/*
179203
* Return a nonzero value iff `lk` is currently locked.
180204
*/

0 commit comments

Comments
 (0)