sha1_file: remove static strbuf from sha1_file_name()

Using a static buffer in sha1_file_name() is error prone and the performance improvements it gives are not needed in many of the callers. So let's get rid of this static buffer and, if necessary or helpful, let's use one in the caller. Suggested-by: Jeff Hostetler <git@jeffhostetler.com> Helped-by: Kevin Daudt <me@ikke.info> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jan 17, 2018 at 18:54 UTC ea6577303f4059683f8f257bdecbcafb05001ce9
4 files changed +42 -26
cache.h
+3 -5
@@ -902,12 +902,10 @@ extern void check_repository_format(void);
902 #define TYPE_CHANGED 0x0040
903
904 /*
905 - * Return the name of the file in the local object database that would
906 - * be used to store a loose object with the specified sha1. The
907 - * return value is a pointer to a statically allocated buffer that is
908 - * overwritten each time the function is called.
905 + * Put in `buf` the name of the file in the local object database that
906 + * would be used to store a loose object with the specified sha1.
907 */
910 -extern const char *sha1_file_name(const unsigned char *sha1);
908 +extern void sha1_file_name(struct strbuf *buf, const unsigned char *sha1);
909
910 /*
911 * Return an abbreviated sha1 unique within this repository's object database.
http-walker.c
+4 -2
@@ -544,8 +544,10 @@ static int fetch_object(struct walker *walker, unsigned char *sha1)
544 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
545 ret = error("File %s has bad hash", hex);
546 } else if (req->rename < 0) {
547 - ret = error("unable to write sha1 filename %s",
548 - sha1_file_name(req->sha1));
547 + struct strbuf buf = STRBUF_INIT;
548 + sha1_file_name(&buf, req->sha1);
549 + ret = error("unable to write sha1 filename %s", buf.buf);
550 + strbuf_release(&buf);
551 }
552
553 release_http_object_request(req);
http.c
+10 -6
@@ -2150,7 +2150,7 @@ struct http_object_request *new_http_object_request(const char *base_url,
2150 unsigned char *sha1)
2151 {
2152 char *hex = sha1_to_hex(sha1);
2153 - const char *filename;
2153 + struct strbuf filename = STRBUF_INIT;
2154 char prevfile[PATH_MAX];
2155 int prevlocal;
2156 char prev_buf[PREV_BUF_SIZE];
@@ -2162,14 +2162,15 @@ struct http_object_request *new_http_object_request(const char *base_url,
2162 hashcpy(freq->sha1, sha1);
2163 freq->localfile = -1;
2164
2165 - filename = sha1_file_name(sha1);
2165 + sha1_file_name(&filename, sha1);
2166 snprintf(freq->tmpfile, sizeof(freq->tmpfile),
2167 - "%s.temp", filename);
2167 + "%s.temp", filename.buf);
2168
2169 - snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
2169 + snprintf(prevfile, sizeof(prevfile), "%s.prev", filename.buf);
2170 unlink_or_warn(prevfile);
2171 rename(freq->tmpfile, prevfile);
2172 unlink_or_warn(freq->tmpfile);
2173 + strbuf_release(&filename);
2174
2175 if (freq->localfile != -1)
2176 error("fd leakage in start: %d", freq->localfile);
@@ -2284,6 +2285,7 @@ void process_http_object_request(struct http_object_request *freq)
2285 int finish_http_object_request(struct http_object_request *freq)
2286 {
2287 struct stat st;
2288 + struct strbuf filename = STRBUF_INIT;
2289
2290 close(freq->localfile);
2291 freq->localfile = -1;
@@ -2309,8 +2311,10 @@ int finish_http_object_request(struct http_object_request *freq)
2311 unlink_or_warn(freq->tmpfile);
2312 return -1;
2313 }
2312 - freq->rename =
2313 - finalize_object_file(freq->tmpfile, sha1_file_name(freq->sha1));
2314 +
2315 + sha1_file_name(&filename, freq->sha1);
2316 + freq->rename = finalize_object_file(freq->tmpfile, filename.buf);
2317 + strbuf_release(&filename);
2318
2319 return freq->rename;
2320 }
sha1_file.c
+25 -13
@@ -251,15 +251,11 @@ static void fill_sha1_path(struct strbuf *buf, const unsigned char *sha1)
251 }
252 }
253
254 -const char *sha1_file_name(const unsigned char *sha1)
254 +void sha1_file_name(struct strbuf *buf, const unsigned char *sha1)
255 {
256 - static struct strbuf buf = STRBUF_INIT;
257 -
258 - strbuf_reset(&buf);
259 - strbuf_addf(&buf, "%s/", get_object_directory());
256 + strbuf_addf(buf, "%s/", get_object_directory());
257
261 - fill_sha1_path(&buf, sha1);
262 - return buf.buf;
258 + fill_sha1_path(buf, sha1);
259 }
260
261 struct strbuf *alt_scratch_buf(struct alternate_object_database *alt)
@@ -643,7 +639,12 @@ int check_and_freshen_file(const char *fn, int freshen)
639
640 static int check_and_freshen_local(const unsigned char *sha1, int freshen)
641 {
646 - return check_and_freshen_file(sha1_file_name(sha1), freshen);
642 + static struct strbuf buf = STRBUF_INIT;
643 +
644 + strbuf_reset(&buf);
645 + sha1_file_name(&buf, sha1);
646 +
647 + return check_and_freshen_file(buf.buf, freshen);
648 }
649
650 static int check_and_freshen_nonlocal(const unsigned char *sha1, int freshen)
@@ -799,8 +800,12 @@ static int stat_sha1_file(const unsigned char *sha1, struct stat *st,
800 const char **path)
801 {
802 struct alternate_object_database *alt;
803 + static struct strbuf buf = STRBUF_INIT;
804 +
805 + strbuf_reset(&buf);
806 + sha1_file_name(&buf, sha1);
807 + *path = buf.buf;
808
803 - *path = sha1_file_name(sha1);
809 if (!lstat(*path, st))
810 return 0;
811
@@ -824,8 +829,12 @@ static int open_sha1_file(const unsigned char *sha1, const char **path)
829 int fd;
830 struct alternate_object_database *alt;
831 int most_interesting_errno;
832 + static struct strbuf buf = STRBUF_INIT;
833 +
834 + strbuf_reset(&buf);
835 + sha1_file_name(&buf, sha1);
836 + *path = buf.buf;
837
828 - *path = sha1_file_name(sha1);
838 fd = git_open(*path);
839 if (fd >= 0)
840 return fd;
@@ -1487,9 +1496,12 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1496 git_SHA_CTX c;
1497 unsigned char parano_sha1[20];
1498 static struct strbuf tmp_file = STRBUF_INIT;
1490 - const char *filename = sha1_file_name(sha1);
1499 + static struct strbuf filename = STRBUF_INIT;
1500 +
1501 + strbuf_reset(&filename);
1502 + sha1_file_name(&filename, sha1);
1503
1492 - fd = create_tmpfile(&tmp_file, filename);
1504 + fd = create_tmpfile(&tmp_file, filename.buf);
1505 if (fd < 0) {
1506 if (errno == EACCES)
1507 return error("insufficient permission for adding an object to repository database %s", get_object_directory());
@@ -1542,7 +1554,7 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
1554 warning_errno("failed utime() on %s", tmp_file.buf);
1555 }
1556
1545 - return finalize_object_file(tmp_file.buf, filename);
1557 + return finalize_object_file(tmp_file.buf, filename.buf);
1558 }
1559
1560 static int freshen_loose_object(const unsigned char *sha1)