Skip to content

Commit 9939715

Browse files
mhaggergitster
authored andcommitted
register_tempfile(): new function to handle an existing temporary file
Allow an existing file to be registered with the tempfile-handling infrastructure; in particular, arrange for it to be deleted on program exit. This can be used if the temporary file has to be created in a more complicated way than just open(). For example: * If the file itself needs to be created via the lockfile API * If it is not a regular file (e.g., a socket) Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 354ab11 commit 9939715

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

tempfile.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
137137
return tempfile->fd;
138138
}
139139

140+
void register_tempfile(struct tempfile *tempfile, const char *path)
141+
{
142+
prepare_tempfile_object(tempfile);
143+
strbuf_add_absolute_path(&tempfile->filename, path);
144+
tempfile->owner = getpid();
145+
tempfile->active = 1;
146+
}
147+
140148
int mks_tempfile_sm(struct tempfile *tempfile,
141149
const char *template, int suffixlen, int mode)
142150
{

tempfile.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ struct tempfile {
9292
*/
9393
extern int create_tempfile(struct tempfile *tempfile, const char *path);
9494

95+
/*
96+
* Register an existing file as a tempfile, meaning that it will be
97+
* deleted when the program exits. The tempfile is considered closed,
98+
* but it can be worked with like any other closed tempfile (for
99+
* example, it can be opened using reopen_tempfile()).
100+
*/
101+
extern void register_tempfile(struct tempfile *tempfile, const char *path);
102+
95103

96104
/*
97105
* mks_tempfile functions

0 commit comments

Comments
 (0)