Commit 97fade4f1968ab0cf3afdfe86323feeebeaea494

Edward Thomson 2022-02-08T12:19:59

fs_path: use new mktmp to query unicode support

diff --git a/src/fs_path.c b/src/fs_path.c
index f9da304..7a65777 100644
--- a/src/fs_path.c
+++ b/src/fs_path.c
@@ -1040,8 +1040,8 @@ fail:
 	return -1;
 }
 
-static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D.XXXXXX";
-static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
+static const char *nfc_file = "\xC3\x85\x73\x74\x72\xC3\xB6\x6D";
+static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D";
 
 /* Check if the platform is decomposing unicode data for us.  We will
  * emulate core Git and prefer to use precomposed unicode data internally
@@ -1054,39 +1054,42 @@ static const char *nfd_file = "\x41\xCC\x8A\x73\x74\x72\x6F\xCC\x88\x6D.XXXXXX";
  */
 bool git_fs_path_does_decompose_unicode(const char *root)
 {
-	git_str path = GIT_STR_INIT;
+	git_str nfc_path = GIT_STR_INIT;
+	git_str nfd_path = GIT_STR_INIT;
 	int fd;
 	bool found_decomposed = false;
-	char tmp[6];
+	size_t orig_len;
+	const char *trailer;
 
 	/* Create a file using a precomposed path and then try to find it
 	 * using the decomposed name.  If the lookup fails, then we will mark
 	 * that we should precompose unicode for this repository.
 	 */
-	if (git_str_joinpath(&path, root, nfc_file) < 0 ||
-		(fd = p_mkstemp(path.ptr)) < 0)
+	if (git_str_joinpath(&nfc_path, root, nfc_file) < 0)
+		goto done;
+
+	/* record original path length before trailer */
+	orig_len = nfc_path.size;
+
+	if ((fd = git_futils_mktmp(&nfc_path, nfc_path.ptr, 0666)) < 0)
 		goto done;
 	p_close(fd);
 
-	/* record trailing digits generated by mkstemp */
-	memcpy(tmp, path.ptr + path.size - sizeof(tmp), sizeof(tmp));
+	trailer = nfc_path.ptr + orig_len;
 
 	/* try to look up as NFD path */
-	if (git_str_joinpath(&path, root, nfd_file) < 0)
+	if (git_str_joinpath(&nfd_path, root, nfd_file) < 0 ||
+	    git_str_puts(&nfd_path, trailer) < 0)
 		goto done;
-	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
 
-	found_decomposed = git_fs_path_exists(path.ptr);
+	found_decomposed = git_fs_path_exists(nfd_path.ptr);
 
 	/* remove temporary file (using original precomposed path) */
-	if (git_str_joinpath(&path, root, nfc_file) < 0)
-		goto done;
-	memcpy(path.ptr + path.size - sizeof(tmp), tmp, sizeof(tmp));
-
-	(void)p_unlink(path.ptr);
+	(void)p_unlink(nfc_path.ptr);
 
 done:
-	git_str_dispose(&path);
+	git_str_dispose(&nfc_path);
+	git_str_dispose(&nfd_path);
 	return found_decomposed;
 }