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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 10, 2015 at 11:47 UTC 7eba6ce5c79447579689218d8dab59a8434fd7c7
1 file changed +13 -7
tempfile.c
+13 -7
@@ -85,11 +85,11 @@ static void remove_tempfiles_on_signal(int signo)
85 raise(signo);
86 }
87
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)
92 {
91 - size_t pathlen = strlen(path);
92 -
93 if (!tempfile_list) {
94 /* One-time initialization */
95 sigchain_push_common(remove_tempfiles_on_signal);
@@ -97,21 +97,27 @@ int create_tempfile(struct tempfile *tempfile, const char *path)
97 }
98
99 if (tempfile->active)
100 - die("BUG: create_tempfile called for active object");
100 + die("BUG: prepare_tempfile_object called for active object");
101 if (!tempfile->on_list) {
102 /* Initialize *tempfile and add it to tempfile_list: */
103 tempfile->fd = -1;
104 tempfile->fp = NULL;
105 tempfile->active = 0;
106 tempfile->owner = 0;
107 - strbuf_init(&tempfile->filename, pathlen);
107 + strbuf_init(&tempfile->filename, 0);
108 tempfile->next = tempfile_list;
109 tempfile_list = tempfile;
110 tempfile->on_list = 1;
111 } else if (tempfile->filename.len) {
112 /* 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");
114 }
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);
121
122 strbuf_add_absolute_path(&tempfile->filename, path);
123 tempfile->fd = open(tempfile->filename.buf, O_RDWR | O_CREAT | O_EXCL, 0666);