Skip to content

Commit 526c490

Browse files
committed
Merge branch 'jk/tempfile-active-flag-cleanup'
Code clean-up. * jk/tempfile-active-flag-cleanup: tempfile: update comment describing state transitions tempfile: drop active flag
2 parents fb094cb + 320fa57 commit 526c490

File tree

2 files changed

+7
-24
lines changed

2 files changed

+7
-24
lines changed

tempfile.c

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
*
1515
* The possible states of a `tempfile` object are as follows:
1616
*
17-
* - Uninitialized. In this state the object's `on_list` field must be
18-
* zero but the rest of its contents need not be initialized. As
19-
* soon as the object is used in any way, it is irrevocably
20-
* registered in `tempfile_list`, and `on_list` is set.
17+
* - Inactive/unallocated. The only way to get a tempfile is via a creation
18+
* function like create_tempfile(). Once allocated, the tempfile is on the
19+
* global tempfile_list and considered active.
2120
*
2221
* - Active, file open (after `create_tempfile()` or
2322
* `reopen_tempfile()`). In this state:
2423
*
2524
* - the temporary file exists
26-
* - `active` is set
2725
* - `filename` holds the filename of the temporary file
2826
* - `fd` holds a file descriptor open for writing to it
2927
* - `fp` holds a pointer to an open `FILE` object if and only if
@@ -35,14 +33,8 @@
3533
* `fd` is -1, and `fp` is `NULL`.
3634
*
3735
* - Inactive (after `delete_tempfile()`, `rename_tempfile()`, or a
38-
* failed attempt to create a temporary file). In this state:
39-
*
40-
* - `active` is unset
41-
* - `filename` is empty (usually, though there are transitory
42-
* states in which this condition doesn't hold). Client code should
43-
* *not* rely on the filename being empty in this state.
44-
* - `fd` is -1 and `fp` is `NULL`
45-
* - the object is removed from `tempfile_list` (but could be used again)
36+
* failed attempt to create a temporary file). The struct is removed from
37+
* the global tempfile_list and deallocated.
4638
*
4739
* A temporary file is owned by the process that created it. The
4840
* `tempfile` has an `owner` field that records the owner's PID. This
@@ -86,8 +78,6 @@ static void remove_tempfiles(int in_signal_handler)
8678
else
8779
unlink_or_warn(p->filename.buf);
8880
remove_template_directory(p, in_signal_handler);
89-
90-
p->active = 0;
9181
}
9282
}
9383

@@ -108,7 +98,6 @@ static struct tempfile *new_tempfile(void)
10898
struct tempfile *tempfile = xmalloc(sizeof(*tempfile));
10999
tempfile->fd = -1;
110100
tempfile->fp = NULL;
111-
tempfile->active = 0;
112101
tempfile->owner = 0;
113102
INIT_LIST_HEAD(&tempfile->list);
114103
strbuf_init(&tempfile->filename, 0);
@@ -120,9 +109,6 @@ static void activate_tempfile(struct tempfile *tempfile)
120109
{
121110
static int initialized;
122111

123-
if (is_tempfile_active(tempfile))
124-
BUG("activate_tempfile called for active object");
125-
126112
if (!initialized) {
127113
sigchain_push_common(remove_tempfiles_on_signal);
128114
atexit(remove_tempfiles_on_exit);
@@ -131,15 +117,13 @@ static void activate_tempfile(struct tempfile *tempfile)
131117

132118
volatile_list_add(&tempfile->list, &tempfile_list);
133119
tempfile->owner = getpid();
134-
tempfile->active = 1;
135120
}
136121

137122
static void deactivate_tempfile(struct tempfile *tempfile)
138123
{
139-
tempfile->active = 0;
124+
volatile_list_del(&tempfile->list);
140125
strbuf_release(&tempfile->filename);
141126
free(tempfile->directory);
142-
volatile_list_del(&tempfile->list);
143127
free(tempfile);
144128
}
145129

tempfile.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777

7878
struct tempfile {
7979
volatile struct volatile_list_head list;
80-
volatile sig_atomic_t active;
8180
volatile int fd;
8281
FILE *volatile fp;
8382
volatile pid_t owner;
@@ -221,7 +220,7 @@ FILE *fdopen_tempfile(struct tempfile *tempfile, const char *mode);
221220

222221
static inline int is_tempfile_active(struct tempfile *tempfile)
223222
{
224-
return tempfile && tempfile->active;
223+
return !!tempfile;
225224
}
226225

227226
/*

0 commit comments

Comments
 (0)