Hash :
7fd5dfa0
Author :
Date :
2021-12-25T15:25:15
Correctly detect the share/template folder With Git for Windows >= 2 the share folder is in an architecture specific subfolder. This also add support for Git for Windows versions between 2 and 2.24 where also the etc folder is in an architecture specific subfolder. Signed-off-by: Sven Strickroth <email@cs-ware.de>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "findfile.h"
#include "path_w32.h"
#include "utf-conv.h"
#include "fs_path.h"
#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1"
#ifndef _WIN64
#define REG_MSYSGIT_INSTALL REG_MSYSGIT_INSTALL_LOCAL
#else
#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)
{
dest->len = ExpandEnvironmentStringsW(src, dest->path, ARRAY_SIZE(dest->path));
if (!dest->len || dest->len > ARRAY_SIZE(dest->path))
return -1;
return 0;
}
static int win32_path_to_8(git_str *dest, const wchar_t *src)
{
git_win32_utf8_path utf8_path;
if (git_win32_path_to_utf8(utf8_path, src) < 0) {
git_error_set(GIT_ERROR_OS, "unable to convert path to UTF-8");
return -1;
}
/* Convert backslashes to forward slashes */
git_fs_path_mkposix(utf8_path);
return git_str_sets(dest, utf8_path);
}
static wchar_t *win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen)
{
wchar_t term, *base = path;
GIT_ASSERT_ARG_WITH_RETVAL(path, NULL);
GIT_ASSERT_ARG_WITH_RETVAL(buf, NULL);
GIT_ASSERT_ARG_WITH_RETVAL(buflen, NULL);
term = (*path == L'"') ? *path++ : L';';
for (buflen--; *path && *path != term && buflen; buflen--)
*buf++ = *path++;
*buf = L'\0'; /* reserved a byte via initial subtract */
while (*path == term || *path == L';')
path++;
return (path != base) ? path : NULL;
}
static int win32_find_git_for_windows_architecture_root(_findfile_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) */
static const wchar_t *architecture_roots[4] = {
L"", // starting with Git 2.24 the etc folder is directly in the root folder
L"mingw64\\",
L"mingw32\\",
NULL,
};
const wchar_t **roots = architecture_roots;
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 &&
wcscat(tmp_root, *roots) &&
!_waccess(tmp_root, F_OK)) {
wcscpy(root_path->path, tmp_root);
root_path->len += (DWORD)wcslen(*roots);
subdir_len = (DWORD)wcslen(subdir);
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;
break;
}
}
}
return 0;
}
static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wchar_t *subdir)
{
wchar_t *env = _wgetenv(L"PATH"), lastch;
_findfile_path root;
size_t gitexe_len = wcslen(gitexe);
if (!env)
return -1;
while ((env = win32_walkpath(env, root.path, MAX_PATH-1)) && *root.path) {
root.len = (DWORD)wcslen(root.path);
lastch = root.path[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';
}
if (root.len + gitexe_len >= MAX_PATH)
continue;
wcscpy(&root.path[root.len], gitexe);
if (!_waccess(root.path, 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\\")) {
/* 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))
continue;
} else {
if (root.len + wcslen(subdir) >= MAX_PATH)
continue;
wcscat(root.path, subdir);
}
win32_path_to_8(buf, root.path);
return 0;
}
}
return GIT_ENOTFOUND;
}
static int win32_find_git_in_registry(
git_str *buf, const HKEY hive, const wchar_t *key, const wchar_t *subdir)
{
HKEY hKey;
int error = GIT_ENOTFOUND;
GIT_ASSERT_ARG(buf);
if (!RegOpenKeyExW(hive, key, 0, KEY_READ, &hKey)) {
DWORD dwType, cbData;
_findfile_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) &&
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))
error = 0;
}
RegCloseKey(hKey);
}
return error;
}
static int win32_find_existing_dirs(
git_str *out, const wchar_t *tmpl[])
{
_findfile_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))
{
win32_path_to_8(&buf, path16.path);
if (buf.size)
git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
}
}
git_str_dispose(&buf);
return (git_str_oom(out) ? -1 : 0);
}
int git_win32__find_system_dirs(git_str *out, const wchar_t *subdir)
{
git_str buf = GIT_STR_INIT;
/* directories where git.exe & git.cmd are found */
if (!win32_find_git_in_path(&buf, L"git.exe", subdir) && buf.size)
git_str_set(out, buf.ptr, buf.size);
else
git_str_clear(out);
if (!win32_find_git_in_path(&buf, L"git.cmd", subdir) && buf.size)
git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
/* directories where git is installed according to registry */
if (!win32_find_git_in_registry(
&buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL, subdir) && buf.size)
git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
#ifdef GIT_ARCH_64
if (!win32_find_git_in_registry(
&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL_LOCAL, subdir) && buf.size)
git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
#endif
if (!win32_find_git_in_registry(
&buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL, subdir) && buf.size)
git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr);
git_str_dispose(&buf);
return (git_str_oom(out) ? -1 : 0);
}
int git_win32__find_global_dirs(git_str *out)
{
static const wchar_t *global_tmpls[4] = {
L"%HOME%\\",
L"%HOMEDRIVE%%HOMEPATH%\\",
L"%USERPROFILE%\\",
NULL,
};
return win32_find_existing_dirs(out, global_tmpls);
}
int git_win32__find_xdg_dirs(git_str *out)
{
static const wchar_t *global_tmpls[7] = {
L"%XDG_CONFIG_HOME%\\git",
L"%APPDATA%\\git",
L"%LOCALAPPDATA%\\git",
L"%HOME%\\.config\\git",
L"%HOMEDRIVE%%HOMEPATH%\\.config\\git",
L"%USERPROFILE%\\.config\\git",
NULL,
};
return win32_find_existing_dirs(out, global_tmpls);
}
int git_win32__find_programdata_dirs(git_str *out)
{
static const wchar_t *programdata_tmpls[2] = {
L"%PROGRAMDATA%\\Git",
NULL,
};
return win32_find_existing_dirs(out, programdata_tmpls);
}