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>
Jeff King committed
Sep 5, 2017 at 08:14 UTC
b5f4dcb598bb369c960f715fcd5c9ccf4112e026
1 file changed
+11
-7
tempfile.c
+11
-7
@@ -119,6 +119,12 @@ static void activate_tempfile(struct tempfile *tempfile)
119
tempfile->active = 1;
120
}
121
122
+static void deactivate_tempfile(struct tempfile *tempfile)
123
+{
124
+ tempfile->active = 0;
125
+ strbuf_reset(&tempfile->filename);
126
+}
127
+
128
/* Make sure errno contains a meaningful value on error */
129
int create_tempfile(struct tempfile *tempfile, const char *path)
130
{
@@ -132,7 +138,7 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
138
tempfile->fd = open(tempfile->filename.buf,
139
O_RDWR | O_CREAT | O_EXCL, 0666);
140
if (tempfile->fd < 0) {
135
- strbuf_reset(&tempfile->filename);
141
+ deactivate_tempfile(tempfile);
142
return -1;
143
}
144
activate_tempfile(tempfile);
@@ -161,7 +167,7 @@ int mks_tempfile_sm(struct tempfile *tempfile,
167
strbuf_add_absolute_path(&tempfile->filename, template);
168
tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
169
if (tempfile->fd < 0) {
164
- strbuf_reset(&tempfile->filename);
170
+ deactivate_tempfile(tempfile);
171
return -1;
172
}
173
activate_tempfile(tempfile);
@@ -182,7 +188,7 @@ int mks_tempfile_tsm(struct tempfile *tempfile,
188
strbuf_addf(&tempfile->filename, "%s/%s", tmpdir, template);
189
tempfile->fd = git_mkstemps_mode(tempfile->filename.buf, suffixlen, mode);
190
if (tempfile->fd < 0) {
185
- strbuf_reset(&tempfile->filename);
191
+ deactivate_tempfile(tempfile);
192
return -1;
193
}
194
activate_tempfile(tempfile);
@@ -291,8 +297,7 @@ int rename_tempfile(struct tempfile *tempfile, const char *path)
297
return -1;
298
}
299
294
- tempfile->active = 0;
295
- strbuf_reset(&tempfile->filename);
300
+ deactivate_tempfile(tempfile);
301
return 0;
302
}
303
@@ -303,6 +308,5 @@ void delete_tempfile(struct tempfile *tempfile)
308
309
close_tempfile_gently(tempfile);
310
unlink_or_warn(tempfile->filename.buf);
306
- tempfile->active = 0;
307
- strbuf_reset(&tempfile->filename);
311
+ deactivate_tempfile(tempfile);
312
}