Skip to content

Commit 7eba6ce

Browse files
mhaggergitster
authored andcommitted
prepare_tempfile_object(): new function, extracted from create_tempfile()
This makes the next step easier. The old code used to use "path" to set the initial length of tempfile->filename. This was not helpful because path was usually relative whereas the value stored to filename will be absolute. So just initialize the length to 0. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1a9d15d commit 7eba6ce

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

tempfile.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,39 @@ static void remove_tempfiles_on_signal(int signo)
8585
raise(signo);
8686
}
8787

88-
/* Make sure errno contains a meaningful value on error */
89-
int create_tempfile(struct tempfile *tempfile, const char *path)
88+
/*
89+
* Initialize *tempfile if necessary and add it to tempfile_list.
90+
*/
91+
static void prepare_tempfile_object(struct tempfile *tempfile)
9092
{
91-
size_t pathlen = strlen(path);
92-
9393
if (!tempfile_list) {
9494
/* One-time initialization */
9595
sigchain_push_common(remove_tempfiles_on_signal);
9696
atexit(remove_tempfiles_on_exit);
9797
}
9898

9999
if (tempfile->active)
100-
die("BUG: create_tempfile called for active object");
100+
die("BUG: prepare_tempfile_object called for active object");
101101
if (!tempfile->on_list) {
102102
/* Initialize *tempfile and add it to tempfile_list: */
103103
tempfile->fd = -1;
104104
tempfile->fp = NULL;
105105
tempfile->active = 0;
106106
tempfile->owner = 0;
107-
strbuf_init(&tempfile->filename, pathlen);
107+
strbuf_init(&tempfile->filename, 0);
108108
tempfile->next = tempfile_list;
109109
tempfile_list = tempfile;
110110
tempfile->on_list = 1;
111111
} else if (tempfile->filename.len) {
112112
/* This shouldn't happen, but better safe than sorry. */
113-
die("BUG: create_tempfile called for improperly-reset object");
113+
die("BUG: prepare_tempfile_object called for improperly-reset object");
114114
}
115+
}
116+
117+
/* Make sure errno contains a meaningful value on error */
118+
int create_tempfile(struct tempfile *tempfile, const char *path)
119+
{
120+
prepare_tempfile_object(tempfile);
115121

116122
strbuf_add_absolute_path(&tempfile->filename, path);
117123
tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);

0 commit comments

Comments
 (0)