Skip to content

Commit 11cb313

Browse files
committed
Merge branch 'mh/lockfile-stdio'
* mh/lockfile-stdio: commit_packed_refs(): reimplement using fdopen_lock_file() dump_marks(): reimplement using fdopen_lock_file() fdopen_lock_file(): access a lockfile using stdio
2 parents bd107e1 + 6e578a3 commit 11cb313

File tree

5 files changed

+71
-39
lines changed

5 files changed

+71
-39
lines changed

Documentation/technical/api-lockfile.txt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ The caller:
4242
of the final destination (e.g. `$GIT_DIR/index`) to
4343
`hold_lock_file_for_update` or `hold_lock_file_for_append`.
4444

45-
* Writes new content for the destination file by writing to the file
46-
descriptor returned by those functions (also available via
47-
`lock->fd`).
45+
* Writes new content for the destination file by either:
46+
47+
* writing to the file descriptor returned by the `hold_lock_file_*`
48+
functions (also available via `lock->fd`).
49+
50+
* calling `fdopen_lock_file` to get a `FILE` pointer for the open
51+
file and writing to the file using stdio.
4852

4953
When finished writing, the caller can:
5054

@@ -70,10 +74,10 @@ any uncommitted changes.
7074

7175
If you need to close the file descriptor you obtained from a
7276
`hold_lock_file_*` function yourself, do so by calling
73-
`close_lock_file`. You should never call `close(2)` yourself!
74-
Otherwise the `struct lock_file` structure would still think that the
75-
file descriptor needs to be closed, and a commit or rollback would
76-
result in duplicate calls to `close(2)`. Worse yet, if you `close(2)`
77+
`close_lock_file`. You should never call `close(2)` or `fclose(3)`
78+
yourself! Otherwise the `struct lock_file` structure would still think
79+
that the file descriptor needs to be closed, and a commit or rollback
80+
would result in duplicate calls to `close(2)`. Worse yet, if you close
7781
and then later open another file descriptor for a completely different
7882
purpose, then a commit or rollback might close that unrelated file
7983
descriptor.
@@ -143,6 +147,13 @@ hold_lock_file_for_append::
143147
the existing contents of the file (if any) to the lockfile and
144148
position its write pointer at the end of the file.
145149

150+
fdopen_lock_file::
151+
152+
Associate a stdio stream with the lockfile. Return NULL
153+
(*without* rolling back the lockfile) on error. The stream is
154+
closed automatically when `close_lock_file` is called or when
155+
the file is committed or rolled back.
156+
146157
get_locked_file_path::
147158

148159
Return the path of the file that is locked by the specified
@@ -179,10 +190,11 @@ close_lock_file::
179190

180191
Take a pointer to the `struct lock_file` initialized with an
181192
earlier call to `hold_lock_file_for_update` or
182-
`hold_lock_file_for_append`, and close the file descriptor.
183-
Return 0 upon success. On failure to `close(2)`, return a
184-
negative value and roll back the lock file. Usually
185-
`commit_lock_file`, `commit_lock_file_to`, or
193+
`hold_lock_file_for_append`. Close the file descriptor (and
194+
the file pointer if it has been opened using
195+
`fdopen_lock_file`). Return 0 upon success. On failure to
196+
`close(2)`, return a negative value and roll back the lock
197+
file. Usually `commit_lock_file`, `commit_lock_file_to`, or
186198
`rollback_lock_file` should eventually be called if
187199
`close_lock_file` succeeds.
188200

fast-import.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,20 +1794,18 @@ static void dump_marks_helper(FILE *f,
17941794
static void dump_marks(void)
17951795
{
17961796
static struct lock_file mark_lock;
1797-
int mark_fd;
17981797
FILE *f;
17991798

18001799
if (!export_marks_file)
18011800
return;
18021801

1803-
mark_fd = hold_lock_file_for_update(&mark_lock, export_marks_file, 0);
1804-
if (mark_fd < 0) {
1802+
if (hold_lock_file_for_update(&mark_lock, export_marks_file, 0) < 0) {
18051803
failure |= error("Unable to write marks file %s: %s",
18061804
export_marks_file, strerror(errno));
18071805
return;
18081806
}
18091807

1810-
f = fdopen(mark_fd, "w");
1808+
f = fdopen_lock_file(&mark_lock, "w");
18111809
if (!f) {
18121810
int saved_errno = errno;
18131811
rollback_lock_file(&mark_lock);
@@ -1816,22 +1814,7 @@ static void dump_marks(void)
18161814
return;
18171815
}
18181816

1819-
/*
1820-
* Since the lock file was fdopen()'ed, it should not be close()'ed.
1821-
* Assign -1 to the lock file descriptor so that commit_lock_file()
1822-
* won't try to close() it.
1823-
*/
1824-
mark_lock.fd = -1;
1825-
18261817
dump_marks_helper(f, 0, marks);
1827-
if (ferror(f) || fclose(f)) {
1828-
int saved_errno = errno;
1829-
rollback_lock_file(&mark_lock);
1830-
failure |= error("Unable to write marks file %s: %s",
1831-
export_marks_file, strerror(saved_errno));
1832-
return;
1833-
}
1834-
18351818
if (commit_lock_file(&mark_lock)) {
18361819
failure |= error("Unable to commit marks file %s: %s",
18371820
export_marks_file, strerror(errno));

lockfile.c

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,29 @@
77

88
static struct lock_file *volatile lock_file_list;
99

10-
static void remove_lock_files(void)
10+
static void remove_lock_files(int skip_fclose)
1111
{
1212
pid_t me = getpid();
1313

1414
while (lock_file_list) {
15-
if (lock_file_list->owner == me)
15+
if (lock_file_list->owner == me) {
16+
/* fclose() is not safe to call in a signal handler */
17+
if (skip_fclose)
18+
lock_file_list->fp = NULL;
1619
rollback_lock_file(lock_file_list);
20+
}
1721
lock_file_list = lock_file_list->next;
1822
}
1923
}
2024

25+
static void remove_lock_files_on_exit(void)
26+
{
27+
remove_lock_files(0);
28+
}
29+
2130
static void remove_lock_files_on_signal(int signo)
2231
{
23-
remove_lock_files();
32+
remove_lock_files(1);
2433
sigchain_pop(signo);
2534
raise(signo);
2635
}
@@ -97,7 +106,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
97106
if (!lock_file_list) {
98107
/* One-time initialization */
99108
sigchain_push_common(remove_lock_files_on_signal);
100-
atexit(remove_lock_files);
109+
atexit(remove_lock_files_on_exit);
101110
}
102111

103112
if (lk->active)
@@ -106,6 +115,7 @@ static int lock_file(struct lock_file *lk, const char *path, int flags)
106115
if (!lk->on_list) {
107116
/* Initialize *lk and add it to lock_file_list: */
108117
lk->fd = -1;
118+
lk->fp = NULL;
109119
lk->active = 0;
110120
lk->owner = 0;
111121
strbuf_init(&lk->filename, pathlen + LOCK_SUFFIX_LEN);
@@ -217,6 +227,17 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
217227
return fd;
218228
}
219229

230+
FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
231+
{
232+
if (!lk->active)
233+
die("BUG: fdopen_lock_file() called for unlocked object");
234+
if (lk->fp)
235+
die("BUG: fdopen_lock_file() called twice for file '%s'", lk->filename.buf);
236+
237+
lk->fp = fdopen(lk->fd, mode);
238+
return lk->fp;
239+
}
240+
220241
char *get_locked_file_path(struct lock_file *lk)
221242
{
222243
if (!lk->active)
@@ -229,17 +250,32 @@ char *get_locked_file_path(struct lock_file *lk)
229250
int close_lock_file(struct lock_file *lk)
230251
{
231252
int fd = lk->fd;
253+
FILE *fp = lk->fp;
254+
int err;
232255

233256
if (fd < 0)
234257
return 0;
235258

236259
lk->fd = -1;
237-
if (close(fd)) {
260+
if (fp) {
261+
lk->fp = NULL;
262+
263+
/*
264+
* Note: no short-circuiting here; we want to fclose()
265+
* in any case!
266+
*/
267+
err = ferror(fp) | fclose(fp);
268+
} else {
269+
err = close(fd);
270+
}
271+
272+
if (err) {
238273
int save_errno = errno;
239274
rollback_lock_file(lk);
240275
errno = save_errno;
241276
return -1;
242277
}
278+
243279
return 0;
244280
}
245281

lockfile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
* - active is set
3535
* - filename holds the filename of the lockfile
3636
* - fd holds a file descriptor open for writing to the lockfile
37+
* - fp holds a pointer to an open FILE object if and only if
38+
* fdopen_lock_file() has been called on the object
3739
* - owner holds the PID of the process that locked the file
3840
*
3941
* - Locked, lockfile closed (after successful close_lock_file()).
@@ -56,6 +58,7 @@ struct lock_file {
5658
struct lock_file *volatile next;
5759
volatile sig_atomic_t active;
5860
volatile int fd;
61+
FILE *volatile fp;
5962
volatile pid_t owner;
6063
char on_list;
6164
struct strbuf filename;
@@ -74,6 +77,7 @@ extern void unable_to_lock_message(const char *path, int err,
7477
extern NORETURN void unable_to_lock_die(const char *path, int err);
7578
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
7679
extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
80+
extern FILE *fdopen_lock_file(struct lock_file *, const char *mode);
7781
extern char *get_locked_file_path(struct lock_file *);
7882
extern int commit_lock_file_to(struct lock_file *, const char *path);
7983
extern int commit_lock_file(struct lock_file *);

refs.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,16 +2309,13 @@ int commit_packed_refs(void)
23092309
if (!packed_ref_cache->lock)
23102310
die("internal error: packed-refs not locked");
23112311

2312-
out = fdopen(packed_ref_cache->lock->fd, "w");
2312+
out = fdopen_lock_file(packed_ref_cache->lock, "w");
23132313
if (!out)
23142314
die_errno("unable to fdopen packed-refs descriptor");
23152315

23162316
fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
23172317
do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
23182318
0, write_packed_entry_fn, out);
2319-
if (fclose(out))
2320-
die_errno("write error");
2321-
packed_ref_cache->lock->fd = -1;
23222319

23232320
if (commit_lock_file(packed_ref_cache->lock)) {
23242321
save_errno = errno;

0 commit comments

Comments
 (0)