Subject: Re: [PATCH 41/68] init: use strbufs to store paths

From: Torsten Bögershausen <[email-masked]>
Date: Sat, 3 Oct 2015 07:58:48 +0200

On 30.09.15 02:23, Jeff King wrote:
> On Tue, Sep 29, 2015 at 04:50:39PM -0700, Michael Blume wrote:
> 
>> I see compile errors on my mac:
>>

This is my attempt, passing the test, but not fully polished.

diff --git a/builtin/init-db.c b/builtin/init-db.c
index 89f2c05..60b559c 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -276,7 +276,9 @@ static int create_default_files(const char *template_path)
 		path = git_path_buf(&buf, "CoNfIg");
 		if (!access(path, F_OK))
 			git_config_set("core.ignorecase", "true");
-		probe_utf8_pathname_composition(path);
+		/* Probe utf-8 normalization withou mangling CoNfIG */
+		path = git_path_buf(&buf, "config");
+		probe_utf8_pathname_composition(path, strlen(path));
 	}
 
 	strbuf_release(&buf);
diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index b4dd3c7..37172a4 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -8,6 +8,7 @@
 #include "cache.h"
 #include "utf8.h"
 #include "precompose_utf8.h"
+#include "strbuf.h"
 
 typedef char *iconv_ibp;
 static const char *repo_encoding = "UTF-8";
@@ -36,28 +37,33 @@ static size_t has_non_ascii(const char *s, size_t maxlen, size_t *strlen_c)
 }

-void probe_utf8_pathname_composition(struct strbuf *path)
+void probe_utf8_pathname_composition(char *path, int len)
 {
 	static const char *auml_nfc = "\xc3\xa4";
 	static const char *auml_nfd = "\x61\xcc\x88";
-	size_t baselen = path->len;
+	struct strbuf sbuf;
 	int output_fd;
 	if (precomposed_unicode != -1)
 		return; /* We found it defined in the global config, respect it */
-	strbuf_addstr(path, auml_nfc);
-	output_fd = open(path, O_CREAT|O_EXCL|O_RDWR, 0600);
+	strbuf_init(&sbuf, len+3);
+	strbuf_add(&sbuf, path, len);
+	strbuf_addstr(&sbuf, auml_nfc);
+	output_fd = open(sbuf.buf, O_CREAT|O_EXCL|O_RDWR, 0600);
+	fprintf(stderr, "%s/%s:%d sbuf.buf=%s\n",
+					__FILE__, __FUNCTION__, __LINE__, sbuf.buf);
 	if (output_fd >= 0) {
 		close(output_fd);
-		strbuf_setlen(path, baselen);
-		strbuf_addstr(path, auml_nfd);
+		strbuf_setlen(&sbuf, len);
+		strbuf_addstr(&sbuf, auml_nfd);
+	fprintf(stderr, "%s/%s:%d sbuf.buf=%s\n",
+					__FILE__, __FUNCTION__, __LINE__, sbuf.buf);
 		precomposed_unicode = access(path, R_OK) ? 0 : 1;
 		git_config_set("core.precomposeunicode", precomposed_unicode ? "true" : "false");
-		strbuf_setlen(path, baselen);
-		strbuf_addstr(path, auml_nfc);
+		strcpy(path + len, auml_nfc);
 		if (unlink(path))
 			die_errno(_("failed to unlink '%s'"), path);
 	}
-	strbuf_setlen(path, baselen);
+	strbuf_release(&sbuf);
 }

diff --git a/compat/precompose_utf8.h b/compat/precompose_utf8.h
index 7fc7be5..3b73585 100644
--- a/compat/precompose_utf8.h
+++ b/compat/precompose_utf8.h
@@ -27,7 +27,7 @@ typedef struct {
 } PREC_DIR;
 
 void precompose_argv(int argc, const char **argv);
-void probe_utf8_pathname_composition(struct strbuf *path);
+void probe_utf8_pathname_composition(char *, int);
 
 PREC_DIR *precompose_utf8_opendir(const char *dirname);
 struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *dirp);