bundle-uri: use plain string in find_temp_filename()

The find_temp_filename() method was created in 53a50892be2 (bundle-uri: create basic file-copy logic, 2022-08-09) and uses odb_mkstemp() to create a temporary filename. The odb_mkstemp() method uses a strbuf in its interface, but we do not need to continue carrying a strbuf throughout the bundle URI code. Convert the find_temp_filename() method to use a 'char *' and modify its only caller. This makes sense that we don't actually need to modify this filename directly later, so using a strbuf is overkill. This change will simplify the data structure for tracking a bundle list to use plain strings instead of strbufs. Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Oct 12, 2022 at 12:52 UTC 23b6d00ba7fd4b3446319395cfe3791710e81d92
1 file changed +16 -12
bundle-uri.c
+16 -12
@@ -5,22 +5,23 @@
5 #include "refs.h"
6 #include "run-command.h"
7
8 -static int find_temp_filename(struct strbuf *name)
8 +static char *find_temp_filename(void)
9 {
10 int fd;
11 + struct strbuf name = STRBUF_INIT;
12 /*
13 * Find a temporary filename that is available. This is briefly
14 * racy, but unlikely to collide.
15 */
15 - fd = odb_mkstemp(name, "bundles/tmp_uri_XXXXXX");
16 + fd = odb_mkstemp(&name, "bundles/tmp_uri_XXXXXX");
17 if (fd < 0) {
18 warning(_("failed to create temporary file"));
18 - return -1;
19 + return NULL;
20 }
21
22 close(fd);
22 - unlink(name->buf);
23 - return 0;
23 + unlink(name.buf);
24 + return strbuf_detach(&name, NULL);
25 }
26
27 static int download_https_uri_to_file(const char *file, const char *uri)
@@ -141,28 +142,31 @@ static int unbundle_from_file(struct repository *r, const char *file)
142 int fetch_bundle_uri(struct repository *r, const char *uri)
143 {
144 int result = 0;
144 - struct strbuf filename = STRBUF_INIT;
145 + char *filename;
146
146 - if ((result = find_temp_filename(&filename)))
147 + if (!(filename = find_temp_filename())) {
148 + result = -1;
149 goto cleanup;
150 + }
151
149 - if ((result = copy_uri_to_file(filename.buf, uri))) {
152 + if ((result = copy_uri_to_file(filename, uri))) {
153 warning(_("failed to download bundle from URI '%s'"), uri);
154 goto cleanup;
155 }
156
154 - if ((result = !is_bundle(filename.buf, 0))) {
157 + if ((result = !is_bundle(filename, 0))) {
158 warning(_("file at URI '%s' is not a bundle"), uri);
159 goto cleanup;
160 }
161
159 - if ((result = unbundle_from_file(r, filename.buf))) {
162 + if ((result = unbundle_from_file(r, filename))) {
163 warning(_("failed to unbundle bundle from URI '%s'"), uri);
164 goto cleanup;
165 }
166
167 cleanup:
165 - unlink(filename.buf);
166 - strbuf_release(&filename);
168 + if (filename)
169 + unlink(filename);
170 + free(filename);
171 return result;
172 }