Commit 29960649137251abc07246e7c3075d6fd845d31f

Edward Thomson 2022-01-15T14:13:53

win32: remove unnecessary findfile structure

diff --git a/src/win32/findfile.c b/src/win32/findfile.c
index 1e4d3d0..b82347f 100644
--- a/src/win32/findfile.c
+++ b/src/win32/findfile.c
@@ -19,16 +19,11 @@
 #define REG_MSYSGIT_INSTALL L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
 #endif
 
-typedef struct {
-	git_win32_path path;
-	DWORD len;
-} _findfile_path;
-
-static int git_win32__expand_path(_findfile_path *dest, const wchar_t *src)
+static int git_win32__expand_path(git_win32_path dest, const wchar_t *src)
 {
-	dest->len = ExpandEnvironmentStringsW(src, dest->path, ARRAY_SIZE(dest->path));
+	DWORD len = ExpandEnvironmentStringsW(src, dest, GIT_WIN_PATH_UTF16);
 
-	if (!dest->len || dest->len > ARRAY_SIZE(dest->path))
+	if (!len || len > GIT_WIN_PATH_UTF16)
 		return -1;
 
 	return 0;
@@ -70,7 +65,7 @@ static wchar_t *win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
 	return (path != base) ? path : NULL;
 }
 
-static int win32_find_git_for_windows_architecture_root(_findfile_path *root_path, const wchar_t *subdir)
+static int win32_find_git_for_windows_architecture_root(git_win32_path root_path, const wchar_t *subdir)
 {
 	/* Git for Windows >= 2 comes with a special architecture root (mingw64 and mingw32)
 	 * under which the "share" folder is located, check which we need (none is also ok) */
@@ -83,25 +78,27 @@ static int win32_find_git_for_windows_architecture_root(_findfile_path *root_pat
 	};
 
 	const wchar_t **roots = architecture_roots;
+	size_t root_path_len = wcslen(root_path);
+
 	for (; *roots != NULL; ++roots) {
 		git_win32_path tmp_root;
 		DWORD subdir_len;
-		if (wcscpy(tmp_root, root_path->path) &&
-			root_path->len + wcslen(*roots) <= MAX_PATH &&
+		if (wcscpy(tmp_root, root_path) &&
+			root_path_len + wcslen(*roots) <= MAX_PATH &&
 			wcscat(tmp_root, *roots) &&
 			!_waccess(tmp_root, F_OK)) {
-			wcscpy(root_path->path, tmp_root);
-			root_path->len += (DWORD)wcslen(*roots);
+			wcscpy(root_path, tmp_root);
+			root_path_len += (DWORD)wcslen(*roots);
 
 			subdir_len = (DWORD)wcslen(subdir);
-			if (root_path->len + subdir_len >= MAX_PATH)
+			if (root_path_len + subdir_len >= MAX_PATH)
 				break;
 
 			// append subdir and check whether it exists for the Git installation
 			wcscat(tmp_root, subdir);
 			if (!_waccess(tmp_root, F_OK)) {
-				wcscpy(root_path->path, tmp_root);
-				root_path->len += subdir_len;
+				wcscpy(root_path, tmp_root);
+				root_path_len += subdir_len;
 				break;
 			}
 		}
@@ -113,7 +110,7 @@ static int win32_find_git_for_windows_architecture_root(_findfile_path *root_pat
 static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wchar_t *subdir)
 {
 	wchar_t *path, *env, lastch;
-	_findfile_path root;
+	git_win32_path root;
 	size_t gitexe_len = wcslen(gitexe);
 	DWORD len;
 	bool found = false;
@@ -133,35 +130,34 @@ static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wch
 
 	env = path;
 
-	while ((env = win32_walkpath(env, root.path, MAX_PATH-1)) && *root.path) {
-		root.len = (DWORD)wcslen(root.path);
-		lastch = root.path[root.len - 1];
+	while ((env = win32_walkpath(env, root, MAX_PATH-1)) && *root) {
+		size_t root_len = wcslen(root);
+		lastch = root[root_len - 1];
 
 		/* ensure trailing slash (MAX_PATH-1 to walkpath guarantees space) */
 		if (lastch != L'/' && lastch != L'\\') {
-			root.path[root.len++] = L'\\';
-			root.path[root.len]   = L'\0';
+			root[root_len++] = L'\\';
+			root[root_len]   = L'\0';
 		}
 
-		if (root.len + gitexe_len >= MAX_PATH)
+		if (root_len + gitexe_len >= MAX_PATH)
 			continue;
-		wcscpy(&root.path[root.len], gitexe);
 
-		if (!_waccess(root.path, F_OK)) {
+		if (!_waccess(root, F_OK)) {
 			/* check whether we found a Git for Windows installation and do some path adjustments OR just append subdir */
-			if ((root.len > 5 && wcscmp(root.path - 4, L"cmd\\")) || wcscmp(root.path - 4, L"bin\\")) {
+			if ((root_len > 5 && wcscmp(root - 4, L"cmd\\")) || wcscmp(root - 4, L"bin\\")) {
 				/* strip "bin" or "cmd" and try to find architecture root for appending subdir */
-				root.len -= 4;
-				root.path[root.len] = L'\0';
-				if (win32_find_git_for_windows_architecture_root(&root, subdir))
+				root_len -= 4;
+				root[root_len] = L'\0';
+				if (win32_find_git_for_windows_architecture_root(root, subdir))
 					continue;
 			} else {
-				if (root.len + wcslen(subdir) >= MAX_PATH)
+				if (root_len + wcslen(subdir) >= MAX_PATH)
 					continue;
-				wcscat(root.path, subdir);
+				wcscat(root, subdir);
 			}
 
-			win32_path_to_8(buf, root.path);
+			win32_path_to_8(buf, root);
 			found = true;
 			break;
 		}
@@ -181,21 +177,20 @@ static int win32_find_git_in_registry(
 
 	if (!RegOpenKeyExW(hive, key, 0, KEY_READ, &hKey)) {
 		DWORD dwType, cbData;
-		_findfile_path path;
+		git_win32_path path;
 
 		/* Ensure that the buffer is big enough to have the suffix attached
 		 * after we receive the result. */
 		cbData = (DWORD)(sizeof(path) - wcslen(subdir) * sizeof(wchar_t));
 
 		/* InstallLocation points to the root of the git directory */
-		if (!RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)path.path, &cbData) &&
+		if (!RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)path, &cbData) &&
 			dwType == REG_SZ) {
-			path.len = cbData;
 
 			/* Convert to UTF-8, with forward slashes, and output the path
 			 * to the provided buffer */
-			if (!win32_find_git_for_windows_architecture_root(&path, subdir) &&
-				!win32_path_to_8(buf, path.path))
+			if (!win32_find_git_for_windows_architecture_root(path, subdir) &&
+				!win32_path_to_8(buf, path))
 				error = 0;
 		}
 
@@ -208,17 +203,17 @@ static int win32_find_git_in_registry(
 static int win32_find_existing_dirs(
 	git_str *out, const wchar_t *tmpl[])
 {
-	_findfile_path path16;
+	git_win32_path path16;
 	git_str buf = GIT_STR_INIT;
 
 	git_str_clear(out);
 
 	for (; *tmpl != NULL; tmpl++) {
-		if (!git_win32__expand_path(&path16, *tmpl) &&
-			path16.path[0] != L'%' &&
-			!_waccess(path16.path, F_OK))
+		if (!git_win32__expand_path(path16, *tmpl) &&
+			path16[0] != L'%' &&
+			!_waccess(path16, F_OK))
 		{
-			win32_path_to_8(&buf, path16.path);
+			win32_path_to_8(&buf, path16);
 
 			if (buf.size)
 				git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);