diff options
Diffstat (limited to 'src/win32')
| -rw-r--r-- | src/win32/findfile.c | 208 | ||||
| -rw-r--r-- | src/win32/findfile.h | 12 |
2 files changed, 160 insertions, 60 deletions
diff --git a/src/win32/findfile.c b/src/win32/findfile.c index 6fc7c7513..d3351b136 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -10,6 +10,7 @@ #include "findfile.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 @@ -22,11 +23,21 @@ int win32_expand_path(struct win32_path *s_root, const wchar_t *templ) return s_root->len ? 0 : -1; } -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename) +static int win32_path_utf16_to_8(git_buf *path_utf8, const wchar_t *path_utf16) +{ + char temp_utf8[GIT_PATH_MAX]; + + git__utf16_to_8(temp_utf8, path_utf16); + git_path_mkposix(temp_utf8); + + return git_buf_sets(path_utf8, temp_utf8); +} + +int win32_find_file( + git_buf *path, const struct win32_path *root, const char *filename) { size_t len, alloc_len; wchar_t *file_utf16 = NULL; - char file_utf8[GIT_PATH_MAX]; if (!root || !filename || (len = strlen(filename)) == 0) return GIT_ENOTFOUND; @@ -50,15 +61,13 @@ int win32_find_file(git_buf *path, const struct win32_path *root, const char *fi return GIT_ENOTFOUND; } - git__utf16_to_8(file_utf8, file_utf16); - git_path_mkposix(file_utf8); - git_buf_sets(path, file_utf8); - + win32_path_utf16_to_8(path, file_utf16); git__free(file_utf16); + return 0; } -wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen) +static wchar_t* win32_walkpath(wchar_t *path, wchar_t *buf, size_t buflen) { wchar_t term, *base = path; @@ -77,80 +86,169 @@ wchar_t* win32_nextpath(wchar_t *path, wchar_t *buf, size_t buflen) return (path != base) ? path : NULL; } -int win32_find_system_file_using_path(git_buf *path, const char *filename) +static int win32_find_git_in_path(git_buf *buf, const wchar_t *gitexe) { - wchar_t * env = NULL; + wchar_t *env = _wgetenv(L"PATH"), lastch; struct win32_path root; + size_t gitexe_len = wcslen(gitexe); - env = _wgetenv(L"PATH"); if (!env) return -1; - // search in all paths defined in PATH - while ((env = win32_nextpath(env, root.path, MAX_PATH - 1)) != NULL && *root.path) - { - wchar_t * pfin = root.path + wcslen(root.path) - 1; // last char of the current path entry + 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'; + } - // ensure trailing slash - if (*pfin != L'/' && *pfin != L'\\') - wcscpy(++pfin, L"\\"); // we have enough space left, MAX_PATH - 1 is used in nextpath above + if (root.len + gitexe_len >= MAX_PATH) + continue; + wcscpy(&root.path[root.len], gitexe); - root.len = (DWORD)wcslen(root.path) + 1; + if (_waccess(root.path, F_OK) == 0 && root.len > 5) { + /* replace "bin\\" or "cmd\\" with "etc\\" */ + wcscpy(&root.path[root.len - 4], L"etc\\"); - if (win32_find_file(path, &root, "git.cmd") == 0 || win32_find_file(path, &root, "git.exe") == 0) { - // we found the cmd or bin directory of a git installaton - if (root.len > 5) { - wcscpy(root.path + wcslen(root.path) - 4, L"etc\\"); - if (win32_find_file(path, &root, filename) == 0) - return 0; - } + win32_path_utf16_to_8(buf, root.path); + return 0; } } - + return GIT_ENOTFOUND; } -int win32_find_system_file_using_registry(git_buf *path, const char *filename) +static int win32_find_git_in_registry( + git_buf *buf, const HKEY hieve, const wchar_t *key) { - struct win32_path root; + HKEY hKey; + DWORD dwType = REG_SZ; + struct win32_path path16; + + assert(buf); - if (win32_find_msysgit_in_registry(&root, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) { - if (win32_find_msysgit_in_registry(&root, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) { - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory"); - return -1; + path16.len = 0; + + if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { + if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, + (LPBYTE)&path16.path, &path16.len) == ERROR_SUCCESS) + { + /* InstallLocation points to the root of the git directory */ + + if (path16.len + 4 > MAX_PATH) { /* 4 = wcslen(L"etc\\") */ + giterr_set(GITERR_OS, "Cannot locate git - path too long"); + return -1; + } + + wcscat(path16.path, L"etc\\"); + path16.len += 4; + + win32_path_utf16_to_8(buf, path16.path); } - } - if (win32_find_file(path, &root, filename) < 0) { - giterr_set(GITERR_OS, "The system file '%s' doesn't exist", filename); - git_buf_clear(path); - return GIT_ENOTFOUND; + RegCloseKey(hKey); } - return 0; + return path16.len ? 0 : GIT_ENOTFOUND; } -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key) +static int win32_copy_to_strarray( + git_strarray *out, size_t count, char **strings) { - HKEY hKey; - DWORD dwType = REG_SZ; - DWORD dwSize = MAX_PATH; + size_t i, realcount; - assert(root); + if (!count) + return 0; - root->len = 0; - if (RegOpenKeyExW(hieve, key, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) { - if (RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)&root->path, &dwSize) == ERROR_SUCCESS) { - // InstallLocation points to the root of the msysgit directory - if (dwSize + 4 > MAX_PATH) {// 4 = wcslen(L"etc\\") - giterr_set(GITERR_OS, "Cannot locate the system's msysgit directory - path too long"); - return -1; - } - wcscat(root->path, L"etc\\"); - root->len = (DWORD)wcslen(root->path) + 1; + for (i = 0, realcount = 0; i < count; ++i) + if (strings[i]) realcount++; + + out->strings = git__calloc(realcount, sizeof(char *)); + GITERR_CHECK_ALLOC(out->strings); + + for (i = 0, out->count = 0; i < count; ++i) + if (strings[i]) + out->strings[out->count++] = strings[i]; + + return 0; +} + +static int win32_find_existing_dirs( + git_strarray *out, const wchar_t *tmpl[], char *temp[]) +{ + struct win32_path path16; + git_buf buf = GIT_BUF_INIT; + size_t count; + + for (count = 0; *tmpl != NULL; tmpl++) { + if (!win32_expand_path(&path16, *tmpl) && + path16.path[0] != L'%' && + !_waccess(path16.path, F_OK)) + { + win32_path_utf16_to_8(&buf, path16.path); + temp[count++] = git_buf_detach(&buf); } } - RegCloseKey(hKey); - return root->len ? 0 : GIT_ENOTFOUND; + return win32_copy_to_strarray(out, count, temp); } + +int win32_find_system_dirs(git_strarray *out) +{ + char *strings[4]; + size_t count = 0; + git_buf buf = GIT_BUF_INIT; + + memset(out, 0, sizeof(*out)); + + /* directories where git.exe & git.cmd are found */ + if (!win32_find_git_in_path(&buf, L"git.exe")) + strings[count++] = git_buf_detach(&buf); + + if (!win32_find_git_in_path(&buf, L"git.cmd")) + strings[count++] = git_buf_detach(&buf); + + /* directories where git is installed according to registry */ + if (!win32_find_git_in_registry( + &buf, HKEY_CURRENT_USER, REG_MSYSGIT_INSTALL_LOCAL)) + strings[count++] = git_buf_detach(&buf); + + if (!win32_find_git_in_registry( + &buf, HKEY_LOCAL_MACHINE, REG_MSYSGIT_INSTALL)) + strings[count++] = git_buf_detach(&buf); + + return win32_copy_to_strarray(out, count, strings); +} + +int win32_find_global_dirs(git_strarray *out) +{ + char *temp[3]; + static const wchar_t *global_tmpls[4] = { + L"%HOME%\\", + L"%HOMEDRIVE%%HOMEPATH%\\", + L"%USERPROFILE%\\", + NULL, + }; + + return win32_find_existing_dirs(out, global_tmpls, temp); +} + +int win32_find_xdg_dirs(git_strarray *out) +{ + char *temp[6]; + 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, temp); +} + diff --git a/src/win32/findfile.h b/src/win32/findfile.h index 47fe71596..f8a13c3e3 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -13,12 +13,14 @@ struct win32_path { DWORD len; }; -int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); +extern int win32_expand_path(struct win32_path *s_root, const wchar_t *templ); -int win32_find_file(git_buf *path, const struct win32_path *root, const char *filename); -int win32_find_system_file_using_path(git_buf *path, const char *filename); -int win32_find_system_file_using_registry(git_buf *path, const char *filename); -int win32_find_msysgit_in_registry(struct win32_path *root, const HKEY hieve, const wchar_t *key); +extern int win32_find_file( + git_buf *path, const struct win32_path *root, const char *filename); + +extern int win32_find_system_dirs(git_strarray *out); +extern int win32_find_global_dirs(git_strarray *out); +extern int win32_find_xdg_dirs(git_strarray *out); #endif |
