@matheusbsbs / matheus-bagliano / commits / fa3bff2466
Fork 0

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 <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Apr 27, 2020 at 10:27 UTC fa3bff2466ece4a10ba9beb9be3b45bada1c12eb
2 files changed +38 -12
lockfile.c
+10 -8
@@ -70,7 +70,8 @@ static void resolve_symlink(struct strbuf *path)
70 }
71
72 /* 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)
75 {
76 struct strbuf filename = STRBUF_INIT;
77
@@ -79,7 +80,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
80 resolve_symlink(&filename);
81
82 strbuf_addstr(&filename, LOCK_SUFFIX);
82 - lk->tempfile = create_tempfile(filename.buf);
83 + lk->tempfile = create_tempfile_mode(filename.buf, mode);
84 strbuf_release(&filename);
85 return lk->tempfile ? lk->tempfile->fd : -1;
86 }
@@ -99,7 +100,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
100 * exactly once. If timeout_ms is -1, try indefinitely.
101 */
102 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)
104 {
105 int n = 1;
106 int multiplier = 1;
@@ -107,7 +108,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
108 static int random_initialized = 0;
109
110 if (timeout_ms == 0)
110 - return lock_file(lk, path, flags);
111 + return lock_file(lk, path, flags, mode);
112
113 if (!random_initialized) {
114 srand((unsigned int)getpid());
@@ -121,7 +122,7 @@ static int lock_file_timeout(struct lock_file *lk, const char *path,
122 long backoff_ms, wait_ms;
123 int fd;
124
124 - fd = lock_file(lk, path, flags);
125 + fd = lock_file(lk, path, flags, mode);
126
127 if (fd >= 0)
128 return fd; /* success */
@@ -169,10 +170,11 @@ NORETURN void unable_to_lock_die(const char *path, int err)
170 }
171
172 /* 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)
176 {
175 - int fd = lock_file_timeout(lk, path, flags, timeout_ms);
177 + int fd = lock_file_timeout(lk, path, flags, timeout_ms, mode);
178 if (fd < 0) {
179 if (flags & LOCK_DIE_ON_ERROR)
180 unable_to_lock_die(path, errno);
lockfile.h
+28 -4
@@ -90,6 +90,15 @@
90 * functions. In particular, the state diagram and the cleanup
91 * machinery are all implemented in the tempfile module.
92 *
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.
102 *
103 * Error handling
104 * --------------
@@ -156,12 +165,20 @@ struct lock_file {
165 * file descriptor for writing to it, or -1 on error. If the file is
166 * currently locked, retry with quadratic backoff for at least
167 * 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.
170 */
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(
176 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 +}
182
183 /*
184 * 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(
192 return hold_lock_file_for_update_timeout(lk, path, flags, 0);
193 }
194
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 +
202 /*
203 * Return a nonzero value iff `lk` is currently locked.
204 */