Skip to content

Commit 013870c

Browse files
mhaggergitster
authored andcommitted
fdopen_lock_file(): access a lockfile using stdio
Add a new function, fdopen_lock_file(), which returns a FILE pointer open to the lockfile. If a stream is open on a lock_file object, it is closed using fclose() on commit, rollback, or close_lock_file(). This change will allow callers to use stdio to write to a lockfile without having to muck around in the internal representation of the lock_file object (callers will be rewritten in upcoming commits). Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 697cc8e commit 013870c

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
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

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);
@@ -214,6 +224,17 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
214224
return fd;
215225
}
216226

227+
FILE *fdopen_lock_file(struct lock_file *lk, const char *mode)
228+
{
229+
if (!lk->active)
230+
die("BUG: fdopen_lock_file() called for unlocked object");
231+
if (lk->fp)
232+
die("BUG: fdopen_lock_file() called twice for file '%s'", lk->filename.buf);
233+
234+
lk->fp = fdopen(lk->fd, mode);
235+
return lk->fp;
236+
}
237+
217238
char *get_locked_file_path(struct lock_file *lk)
218239
{
219240
if (!lk->active)
@@ -226,17 +247,32 @@ char *get_locked_file_path(struct lock_file *lk)
226247
int close_lock_file(struct lock_file *lk)
227248
{
228249
int fd = lk->fd;
250+
FILE *fp = lk->fp;
251+
int err;
229252

230253
if (fd < 0)
231254
return 0;
232255

233256
lk->fd = -1;
234-
if (close(fd)) {
257+
if (fp) {
258+
lk->fp = NULL;
259+
260+
/*
261+
* Note: no short-circuiting here; we want to fclose()
262+
* in any case!
263+
*/
264+
err = ferror(fp) | fclose(fp);
265+
} else {
266+
err = close(fd);
267+
}
268+
269+
if (err) {
235270
int save_errno = errno;
236271
rollback_lock_file(lk);
237272
errno = save_errno;
238273
return -1;
239274
}
275+
240276
return 0;
241277
}
242278

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 *);

0 commit comments

Comments
 (0)