1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-04-28 18:35:11 +02:00

tempfile: factor out deactivation

When we deactivate a tempfile, we also have to clean up the
"filename" strbuf. Let's pull this out into its own function
to keep the logic in one place (which will become more
important when a future patch makes it more complicated).

Note that we can use the same function when deactivating an
object that _isn't_ actually active yet (like when we hit an
error creating a tempfile). These callsites don't currently
reset the "active" flag to 0, but it's OK to do so (it's
just a noop for these cases).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2017-09-05 08:14:50 -04:00 committed by Junio C Hamano
parent 2933ebbac1
commit b5f4dcb598

View File

@ -119,6 +119,12 @@ static void activate_tempfile(struct tempfile *tempfile)
tempfile->active = 1;
}
static void deactivate_tempfile(struct tempfile *tempfile)
{
tempfile->active = 0;
strbuf_reset(&tempfile->filename);
}
/* Make sure errno contains a meaningful value on error */
int create_tempfile(struct tempfile *tempfile, const char *path)
{
@ -132,7 +138,7 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
tempfile->fd = open(tempfile->filename.buf,
O_RDWR | O_CREAT | O_EXCL, 0666);
if (tempfile->fd < 0) {
strbuf_reset(&tempfile->filename);
deactivate_tempfile(tempfile);
return -1;
}
activate_tempfile(tempfile);
@ -161,7 +167,7 @@ int mks_tempfile_sm(struct tempfile *tempfile,
strbuf_add_absolute_path(&tempfile->filename, template);
tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
if (tempfile->fd < 0) {
strbuf_reset(&tempfile->filename);
deactivate_tempfile(tempfile);
return -1;
}
activate_tempfile(tempfile);
@ -182,7 +188,7 @@ int mks_tempfile_tsm(struct tempfile *tempfile,
strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
if (tempfile->fd < 0) {
strbuf_reset(&tempfile->filename);
deactivate_tempfile(tempfile);
return -1;
}
activate_tempfile(tempfile);
@ -291,8 +297,7 @@ int rename_tempfile(struct tempfile *tempfile, const char *path)
return -1;
}
tempfile->active = 0;
strbuf_reset(&tempfile->filename);
deactivate_tempfile(tempfile);
return 0;
}
@ -303,6 +308,5 @@ void delete_tempfile(struct tempfile *tempfile)
close_tempfile_gently(tempfile);
unlink_or_warn(tempfile->filename.buf);
tempfile->active = 0;
strbuf_reset(&tempfile->filename);
deactivate_tempfile(tempfile);
}