diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2022-01-19 19:15:29 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-19 19:15:29 -0500 |
commit | 4fead63671de217631d6cf8c661743288d9067ae (patch) | |
tree | 6b82f09bf5670f3a7e2afe464fe1db620faec09d | |
parent | 258df9c18d4876ad59a2cfa9d78a313f867d633e (diff) | |
parent | 475c6eba4f5ad806b68ebf399818a7e9e9ec4a18 (diff) | |
download | libgit2-4fead63671de217631d6cf8c661743288d9067ae.tar.gz |
Merge pull request #6180 from libgit2/ethomson/win32_findfile_fixes
win32: update git for windows compatibility
-rw-r--r-- | src/fs_path.c | 56 | ||||
-rw-r--r-- | src/fs_path.h | 6 | ||||
-rw-r--r-- | src/sysdir.c | 20 | ||||
-rw-r--r-- | src/sysdir.h | 5 | ||||
-rw-r--r-- | src/win32/findfile.c | 264 | ||||
-rw-r--r-- | src/win32/findfile.h | 5 | ||||
-rw-r--r-- | src/win32/path_w32.c | 131 | ||||
-rw-r--r-- | src/win32/path_w32.h | 2 | ||||
-rw-r--r-- | tests/core/path.c | 86 | ||||
-rw-r--r-- | tests/win32/systemdir.c | 328 |
10 files changed, 796 insertions, 107 deletions
diff --git a/src/fs_path.c b/src/fs_path.c index c9f03a7c3..f9da304f5 100644 --- a/src/fs_path.c +++ b/src/fs_path.c @@ -1851,3 +1851,59 @@ cleanup: return ret; #endif } + +int git_fs_path_find_executable(git_str *fullpath, const char *executable) +{ +#ifdef GIT_WIN32 + git_win32_path fullpath_w, executable_w; + int error; + + if (git__utf8_to_16(executable_w, GIT_WIN_PATH_MAX, executable) < 0) + return -1; + + error = git_win32_path_find_executable(fullpath_w, executable_w); + + if (error == 0) + error = git_str_put_w(fullpath, fullpath_w, wcslen(fullpath_w)); + + return error; +#else + git_str path = GIT_STR_INIT; + const char *current_dir, *term; + bool found = false; + + if (git__getenv(&path, "PATH") < 0) + return -1; + + current_dir = path.ptr; + + while (*current_dir) { + if (! (term = strchr(current_dir, GIT_PATH_LIST_SEPARATOR))) + term = strchr(current_dir, '\0'); + + git_str_clear(fullpath); + if (git_str_put(fullpath, current_dir, (term - current_dir)) < 0 || + git_str_putc(fullpath, '/') < 0 || + git_str_puts(fullpath, executable) < 0) + return -1; + + if (git_fs_path_isfile(fullpath->ptr)) { + found = true; + break; + } + + current_dir = term; + + while (*current_dir == GIT_PATH_LIST_SEPARATOR) + current_dir++; + } + + git_str_dispose(&path); + + if (found) + return 0; + + git_str_clear(fullpath); + return GIT_ENOTFOUND; +#endif +} diff --git a/src/fs_path.h b/src/fs_path.h index 9720d34ce..222c44abc 100644 --- a/src/fs_path.h +++ b/src/fs_path.h @@ -743,4 +743,10 @@ bool git_fs_path_supports_symlinks(const char *dir); */ int git_fs_path_validate_system_file_ownership(const char *path); +/** + * Search the current PATH for the given executable, returning the full + * path if it is found. + */ +int git_fs_path_find_executable(git_str *fullpath, const char *executable); + #endif diff --git a/src/sysdir.c b/src/sysdir.c index 84d212e01..450cb509b 100644 --- a/src/sysdir.c +++ b/src/sysdir.c @@ -31,7 +31,7 @@ static int git_sysdir_guess_programdata_dirs(git_str *out) static int git_sysdir_guess_system_dirs(git_str *out) { #ifdef GIT_WIN32 - return git_win32__find_system_dirs(out, L"etc\\"); + return git_win32__find_system_dirs(out, "etc"); #else return git_str_sets(out, "/etc"); #endif @@ -154,7 +154,7 @@ static int git_sysdir_guess_xdg_dirs(git_str *out) static int git_sysdir_guess_template_dirs(git_str *out) { #ifdef GIT_WIN32 - return git_win32__find_system_dirs(out, L"share\\git-core\\templates"); + return git_win32__find_system_dirs(out, "share/git-core/templates"); #else return git_str_sets(out, "/usr/share/git-core/templates"); #endif @@ -189,9 +189,25 @@ int git_sysdir_global_init(void) for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); i++) error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf); + if (error) + return error; + return git_runtime_shutdown_register(git_sysdir_global_shutdown); } +int git_sysdir_reset(void) +{ + size_t i; + int error = 0; + + for (i = 0; !error && i < ARRAY_SIZE(git_sysdir__dirs); ++i) { + git_str_dispose(&git_sysdir__dirs[i].buf); + error = git_sysdir__dirs[i].guess(&git_sysdir__dirs[i].buf); + } + + return error; +} + static int git_sysdir_check_selector(git_sysdir_t which) { if (which < ARRAY_SIZE(git_sysdir__dirs)) diff --git a/src/sysdir.h b/src/sysdir.h index 43392a022..568f27940 100644 --- a/src/sysdir.h +++ b/src/sysdir.h @@ -105,4 +105,9 @@ extern int git_sysdir_get(const git_str **out, git_sysdir_t which); */ extern int git_sysdir_set(git_sysdir_t which, const char *paths); +/** + * Reset search paths for global/system/xdg files. + */ +extern int git_sysdir_reset(void); + #endif diff --git a/src/win32/findfile.c b/src/win32/findfile.c index d4afc4acc..516391d7f 100644 --- a/src/win32/findfile.c +++ b/src/win32/findfile.c @@ -11,24 +11,14 @@ #include "utf-conv.h" #include "fs_path.h" -#define REG_MSYSGIT_INSTALL_LOCAL L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" +#define REG_GITFORWINDOWS_KEY L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Git_is1" +#define REG_GITFORWINDOWS_KEY_WOW64 L"SOFTWARE\\Wow6432Node\\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) +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; @@ -49,111 +39,141 @@ static int win32_path_to_8(git_str *dest, const wchar_t *src) 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';'; +static git_win32_path mock_registry; +static bool mock_registry_set; - for (buflen--; *path && *path != term && buflen; buflen--) - *buf++ = *path++; - - *buf = L'\0'; /* reserved a byte via initial subtract */ +extern int git_win32__set_registry_system_dir(const wchar_t *mock_sysdir) +{ + if (!mock_sysdir) { + mock_registry[0] = L'\0'; + mock_registry_set = false; + } else { + size_t len = wcslen(mock_sysdir); + + if (len > GIT_WIN_PATH_MAX) { + git_error_set(GIT_ERROR_INVALID, "mock path too long"); + return -1; + } - while (*path == term || *path == L';') - path++; + wcscpy(mock_registry, mock_sysdir); + mock_registry_set = true; + } - return (path != base) ? path : NULL; + return 0; } -static int win32_find_git_in_path(git_str *buf, const wchar_t *gitexe, const wchar_t *subdir) +static int lookup_registry_key( + git_win32_path out, + const HKEY hive, + const wchar_t* key, + const wchar_t *value) { - wchar_t *env = _wgetenv(L"PATH"), lastch; - _findfile_path root; - size_t gitexe_len = wcslen(gitexe); + HKEY hkey; + DWORD type, size; + int error = GIT_ENOTFOUND; - if (!env) - return -1; + /* + * Registry data may not be NUL terminated, provide room to do + * it ourselves. + */ + size = (DWORD)((sizeof(git_win32_path) - 1) * sizeof(wchar_t)); + + if (RegOpenKeyExW(hive, key, 0, KEY_READ, &hkey) != 0) + return GIT_ENOTFOUND; + + if (RegQueryValueExW(hkey, value, NULL, &type, (LPBYTE)out, &size) == 0 && + type == REG_SZ && + size > 0 && + size < sizeof(git_win32_path)) { + size_t wsize = size / sizeof(wchar_t); + size_t len = wsize - 1; + + if (out[wsize - 1] != L'\0') { + len = wsize; + out[wsize] = L'\0'; + } - while ((env = win32_walkpath(env, root.path, MAX_PATH-1)) && *root.path) { - root.len = (DWORD)wcslen(root.path); - lastch = root.path[root.len - 1]; + if (out[len - 1] == L'\\') + out[len - 1] = L'\0'; - /* 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 (_waccess(out, F_OK) == 0) + error = 0; + } - if (root.len + gitexe_len >= MAX_PATH) - continue; - wcscpy(&root.path[root.len], gitexe); + RegCloseKey(hkey); + return error; +} - if (_waccess(root.path, F_OK) == 0 && root.len > 5) { - /* replace "bin\\" or "cmd\\" with subdir */ - wcscpy(&root.path[root.len - 4], subdir); +static int find_sysdir_in_registry(git_win32_path out) +{ + if (mock_registry_set) { + if (mock_registry[0] == L'\0') + return GIT_ENOTFOUND; - win32_path_to_8(buf, root.path); - return 0; - } + wcscpy(out, mock_registry); + return 0; } - return GIT_ENOTFOUND; + if (lookup_registry_key(out, HKEY_CURRENT_USER, REG_GITFORWINDOWS_KEY, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_CURRENT_USER, REG_GITFORWINDOWS_KEY_WOW64, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_LOCAL_MACHINE, REG_GITFORWINDOWS_KEY, L"InstallLocation") == 0 || + lookup_registry_key(out, HKEY_LOCAL_MACHINE, REG_GITFORWINDOWS_KEY_WOW64, L"InstallLocation") == 0) + 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) +static int find_sysdir_in_path(git_win32_path out) { - HKEY hKey; - int error = GIT_ENOTFOUND; + size_t out_len; - GIT_ASSERT_ARG(buf); + if (git_win32_path_find_executable(out, L"git.exe") < 0 && + git_win32_path_find_executable(out, L"git.cmd") < 0) + return GIT_ENOTFOUND; - if (!RegOpenKeyExW(hive, key, 0, KEY_READ, &hKey)) { - DWORD dwType, cbData; - git_win32_path path; + out_len = wcslen(out); - /* 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)); + /* Trim the file name */ + if (out_len <= CONST_STRLEN(L"git.exe")) + return GIT_ENOTFOUND; - /* InstallLocation points to the root of the git directory */ - if (!RegQueryValueExW(hKey, L"InstallLocation", NULL, &dwType, (LPBYTE)path, &cbData) && - dwType == REG_SZ) { + out_len -= CONST_STRLEN(L"git.exe"); - /* Append the suffix */ - wcscat(path, subdir); + if (out_len && out[out_len - 1] == L'\\') + out_len--; - /* Convert to UTF-8, with forward slashes, and output the path - * to the provided buffer */ - if (!win32_path_to_8(buf, path)) - error = 0; - } + /* + * Git for Windows usually places the command in a 'bin' or + * 'cmd' directory, trim that. + */ + if (out_len >= CONST_STRLEN(L"\\bin") && + wcsncmp(&out[out_len - CONST_STRLEN(L"\\bin")], L"\\bin", CONST_STRLEN(L"\\bin")) == 0) + out_len -= CONST_STRLEN(L"\\bin"); + else if (out_len >= CONST_STRLEN(L"\\cmd") && + wcsncmp(&out[out_len - CONST_STRLEN(L"\\cmd")], L"\\cmd", CONST_STRLEN(L"\\cmd")) == 0) + out_len -= CONST_STRLEN(L"\\cmd"); - RegCloseKey(hKey); - } + if (!out_len) + return GIT_ENOTFOUND; - return error; + out[out_len] = L'\0'; + return 0; } static int win32_find_existing_dirs( - git_str *out, const wchar_t *tmpl[]) + 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)) - { - win32_path_to_8(&buf, path16.path); + if (!git_win32__expand_path(path16, *tmpl) && + path16[0] != L'%' && + !_waccess(path16, F_OK)) { + win32_path_to_8(&buf, path16); if (buf.size) git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, buf.ptr); @@ -165,31 +185,67 @@ static int win32_find_existing_dirs( return (git_str_oom(out) ? -1 : 0); } -int git_win32__find_system_dirs(git_str *out, const wchar_t *subdir) +static int append_subdir(git_str *out, git_str *path, const char *subdir) { - git_str buf = GIT_STR_INIT; + static const char* architecture_roots[] = { + "", + "mingw64", + "mingw32", + NULL + }; + const char **root; + size_t orig_path_len = path->size; - /* 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); + for (root = architecture_roots; *root; root++) { + if ((*root[0] && git_str_joinpath(path, path->ptr, *root) < 0) || + git_str_joinpath(path, path->ptr, subdir) < 0) + return -1; - 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); + if (git_fs_path_exists(path->ptr) && + git_str_join(out, GIT_PATH_LIST_SEPARATOR, out->ptr, path->ptr) < 0) + return -1; - /* 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); + git_str_truncate(path, orig_path_len); + } - 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); + return 0; +} - git_str_dispose(&buf); +int git_win32__find_system_dirs(git_str *out, const char *subdir) +{ + git_win32_path pathdir, regdir; + git_str path8 = GIT_STR_INIT; + bool has_pathdir, has_regdir; + int error; + + has_pathdir = (find_sysdir_in_path(pathdir) == 0); + has_regdir = (find_sysdir_in_registry(regdir) == 0); + + if (!has_pathdir && !has_regdir) + return GIT_ENOTFOUND; + + /* + * Usually the git in the path is the same git in the registry, + * in this case there's no need to duplicate the paths. + */ + if (has_pathdir && has_regdir && wcscmp(pathdir, regdir) == 0) + has_regdir = false; + + if (has_pathdir) { + if ((error = win32_path_to_8(&path8, pathdir)) < 0 || + (error = append_subdir(out, &path8, subdir)) < 0) + goto done; + } - return (git_str_oom(out) ? -1 : 0); + if (has_regdir) { + if ((error = win32_path_to_8(&path8, regdir)) < 0 || + (error = append_subdir(out, &path8, subdir)) < 0) + goto done; + } + +done: + git_str_dispose(&path8); + return error; } int git_win32__find_global_dirs(git_str *out) diff --git a/src/win32/findfile.h b/src/win32/findfile.h index e11ccebc5..61fb7dbad 100644 --- a/src/win32/findfile.h +++ b/src/win32/findfile.h @@ -10,7 +10,10 @@ #include "common.h" -extern int git_win32__find_system_dirs(git_str *out, const wchar_t *subpath); +/** Sets the mock registry root for Git for Windows for testing. */ +extern int git_win32__set_registry_system_dir(const wchar_t *mock_sysdir); + +extern int git_win32__find_system_dirs(git_str *out, const char *subpath); extern int git_win32__find_global_dirs(git_str *out); extern int git_win32__find_xdg_dirs(git_str *out); extern int git_win32__find_programdata_dirs(git_str *out); diff --git a/src/win32/path_w32.c b/src/win32/path_w32.c index 0e6aff748..d9fc8292b 100644 --- a/src/win32/path_w32.c +++ b/src/win32/path_w32.c @@ -151,6 +151,137 @@ int git_win32_path_canonicalize(git_win32_path path) return (int)(to - path); } +static int git_win32_path_join( + git_win32_path dest, + const wchar_t *one, + size_t one_len, + const wchar_t *two, + size_t two_len) +{ + size_t backslash = 0; + + if (one_len && two_len && one[one_len - 1] != L'\\') + backslash = 1; + + if (one_len + two_len + backslash > MAX_PATH) { + git_error_set(GIT_ERROR_INVALID, "path too long"); + return -1; + } + + memmove(dest, one, one_len * sizeof(wchar_t)); + + if (backslash) + dest[one_len] = L'\\'; + + memcpy(dest + one_len + backslash, two, two_len * sizeof(wchar_t)); + dest[one_len + backslash + two_len] = L'\0'; + + return 0; +} + +struct win32_path_iter { + wchar_t *env; + const wchar_t *current_dir; +}; + +static int win32_path_iter_init(struct win32_path_iter *iter) +{ + DWORD len = GetEnvironmentVariableW(L"PATH", NULL, 0); + + if (!len && GetLastError() == ERROR_ENVVAR_NOT_FOUND) { + iter->env = NULL; + iter->current_dir = NULL; + return 0; + } else if (!len) { + git_error_set(GIT_ERROR_OS, "could not load PATH"); + return -1; + } + + iter->env = git__malloc(len * sizeof(wchar_t)); + GIT_ERROR_CHECK_ALLOC(iter->env); + + len = GetEnvironmentVariableW(L"PATH", iter->env, len); + + if (len == 0) { + git_error_set(GIT_ERROR_OS, "could not load PATH"); + return -1; + } + + iter->current_dir = iter->env; + return 0; +} + +static int win32_path_iter_next( + const wchar_t **out, + size_t *out_len, + struct win32_path_iter *iter) +{ + const wchar_t *start; + wchar_t term; + size_t len = 0; + + if (!iter->current_dir || !*iter->current_dir) + return GIT_ITEROVER; + + term = (*iter->current_dir == L'"') ? *iter->current_dir++ : L';'; + start = iter->current_dir; + + while (*iter->current_dir && *iter->current_dir != term) { + iter->current_dir++; + len++; + } + + *out = start; + *out_len = len; + + if (term == L'"' && *iter->current_dir) + iter->current_dir++; + + while (*iter->current_dir == L';') + iter->current_dir++; + + return 0; +} + +static void win32_path_iter_dispose(struct win32_path_iter *iter) +{ + if (!iter) + return; + + git__free(iter->env); + iter->env = NULL; + iter->current_dir = NULL; +} + +int git_win32_path_find_executable(git_win32_path fullpath, wchar_t *exe) +{ + struct win32_path_iter path_iter; + const wchar_t *dir; + size_t dir_len, exe_len = wcslen(exe); + bool found = false; + + if (win32_path_iter_init(&path_iter) < 0) + return -1; + + while (win32_path_iter_next(&dir, &dir_len, &path_iter) != GIT_ITEROVER) { + if (git_win32_path_join(fullpath, dir, dir_len, exe, exe_len) < 0) + continue; + + if (_waccess(fullpath, 0) == 0) { + found = true; + break; + } + } + + win32_path_iter_dispose(&path_iter); + + if (found) + return 0; + + fullpath[0] = L'\0'; + return GIT_ENOTFOUND; +} + static int win32_path_cwd(wchar_t *out, size_t len) { int cwd_len; diff --git a/src/win32/path_w32.h b/src/win32/path_w32.h index 4fadf8d08..837b11ebd 100644 --- a/src/win32/path_w32.h +++ b/src/win32/path_w32.h @@ -86,4 +86,6 @@ size_t git_win32_path_trim_end(wchar_t *str, size_t len); */ size_t git_win32_path_remove_namespace(wchar_t *str, size_t len); +int git_win32_path_find_executable(git_win32_path fullpath, wchar_t* exe); + #endif diff --git a/tests/core/path.c b/tests/core/path.c index 563dcd2a3..a0ae77f1c 100644 --- a/tests/core/path.c +++ b/tests/core/path.c @@ -2,6 +2,20 @@ #include "futils.h" #include "fs_path.h" +static char *path_save; + +void test_core_path__initialize(void) +{ + path_save = cl_getenv("PATH"); +} + +void test_core_path__cleanup(void) +{ + cl_setenv("PATH", path_save); + git__free(path_save); + path_save = NULL; +} + static void check_dirname(const char *A, const char *B) { @@ -60,6 +74,20 @@ check_joinpath_n( git_str_dispose(&joined_path); } +static void check_setenv(const char* name, const char* value) +{ + char* check; + + cl_git_pass(cl_setenv(name, value)); + check = cl_getenv(name); + + if (value) + cl_assert_equal_s(value, check); + else + cl_assert(check == NULL); + + git__free(check); +} /* get the dirname of a path */ void test_core_path__00_dirname(void) @@ -651,3 +679,61 @@ void test_core_path__16_resolve_relative(void) assert_common_dirlen(6, "a/b/c/foo.txt", "a/b/c/d/e/bar.txt"); assert_common_dirlen(7, "/a/b/c/foo.txt", "/a/b/c/d/e/bar.txt"); } + +static void fix_path(git_str *s) +{ +#ifndef GIT_WIN32 + GIT_UNUSED(s); +#else + char* c; + + for (c = s->ptr; *c; c++) { + if (*c == '/') + *c = '\\'; + } +#endif +} + +void test_core_path__find_exe_in_path(void) +{ + char *orig_path; + git_str sandbox_path = GIT_STR_INIT; + git_str new_path = GIT_STR_INIT, full_path = GIT_STR_INIT, + dummy_path = GIT_STR_INIT; + +#ifdef GIT_WIN32 + static const char *bogus_path_1 = "c:\\does\\not\\exist\\"; + static const char *bogus_path_2 = "e:\\non\\existent"; +#else + static const char *bogus_path_1 = "/this/path/does/not/exist/"; + static const char *bogus_path_2 = "/non/existent"; +#endif + + orig_path = cl_getenv("PATH"); + + git_str_puts(&sandbox_path, clar_sandbox_path()); + git_str_joinpath(&dummy_path, sandbox_path.ptr, "dummmmmmmy_libgit2_file"); + cl_git_rewritefile(dummy_path.ptr, "this is a dummy file"); + + fix_path(&sandbox_path); + fix_path(&dummy_path); + + cl_git_pass(git_str_printf(&new_path, "%s%c%s%c%s%c%s", + bogus_path_1, GIT_PATH_LIST_SEPARATOR, + orig_path, GIT_PATH_LIST_SEPARATOR, + sandbox_path.ptr, GIT_PATH_LIST_SEPARATOR, + bogus_path_2)); + + check_setenv("PATH", new_path.ptr); + + cl_git_fail_with(GIT_ENOTFOUND, git_fs_path_find_executable(&full_path, "this_file_does_not_exist")); + cl_git_pass(git_fs_path_find_executable(&full_path, "dummmmmmmy_libgit2_file")); + + cl_assert_equal_s(full_path.ptr, dummy_path.ptr); + + git_str_dispose(&full_path); + git_str_dispose(&new_path); + git_str_dispose(&dummy_path); + git_str_dispose(&sandbox_path); + git__free(orig_path); +} diff --git a/tests/win32/systemdir.c b/tests/win32/systemdir.c new file mode 100644 index 000000000..46fa06a50 --- /dev/null +++ b/tests/win32/systemdir.c @@ -0,0 +1,328 @@ +#include "clar_libgit2.h" +#include "futils.h" +#include "sysdir.h" +#include "win32/findfile.h" + +#ifdef GIT_WIN32 +static char *path_save; +static git_str gfw_path_root = GIT_STR_INIT; +static git_str gfw_registry_root = GIT_STR_INIT; +#endif + +void test_win32_systemdir__initialize(void) +{ +#ifdef GIT_WIN32 + git_str path_env = GIT_STR_INIT; + + path_save = cl_getenv("PATH"); + git_win32__set_registry_system_dir(L""); + + cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";C:\\fakefakedoesnotexist")); + cl_setenv("PATH", path_env.ptr); + + cl_git_pass(git_str_puts(&gfw_path_root, clar_sandbox_path())); + cl_git_pass(git_str_puts(&gfw_path_root, "/fake_gfw_path_install")); + + cl_git_pass(git_str_puts(&gfw_registry_root, clar_sandbox_path())); + cl_git_pass(git_str_puts(&gfw_registry_root, "/fake_gfw_registry_install")); + + git_str_dispose(&path_env); +#endif +} + +void test_win32_systemdir__cleanup(void) +{ +#ifdef GIT_WIN32 + cl_fixture_cleanup("fake_gfw_path_install"); + cl_fixture_cleanup("fake_gfw_registry_install"); + git_str_dispose(&gfw_path_root); + git_str_dispose(&gfw_registry_root); + + cl_setenv("PATH", path_save); + git__free(path_save); + path_save = NULL; + + git_win32__set_registry_system_dir(NULL); + cl_sandbox_set_search_path_defaults(); +#endif +} + +#ifdef GIT_WIN32 +static void fix_path(git_str *s) +{ + char *c; + + for (c = s->ptr; *c; c++) { + if (*c == '/') + *c = '\\'; + } +} + +static void populate_fake_gfw( + git_str *expected_etc_dir, + const char *root, + const char *token, + bool create_gitconfig, + bool create_mingw64_gitconfig, + bool add_to_path, + bool add_to_registry) +{ + git_str bin_path = GIT_STR_INIT, exe_path = GIT_STR_INIT, + etc_path = GIT_STR_INIT, mingw64_path = GIT_STR_INIT, + config_path = GIT_STR_INIT, path_env = GIT_STR_INIT, + config_data = GIT_STR_INIT; + + cl_git_pass(git_str_puts(&bin_path, root)); + cl_git_pass(git_str_puts(&bin_path, "/cmd")); + cl_git_pass(git_futils_mkdir_r(bin_path.ptr, 0755)); + + cl_git_pass(git_str_puts(&exe_path, bin_path.ptr)); + cl_git_pass(git_str_puts(&exe_path, "/git.cmd")); + cl_git_mkfile(exe_path.ptr, "This is a fake executable."); + + cl_git_pass(git_str_puts(&etc_path, root)); + cl_git_pass(git_str_puts(&etc_path, "/etc")); + cl_git_pass(git_futils_mkdir_r(etc_path.ptr, 0755)); + + cl_git_pass(git_str_puts(&mingw64_path, root)); + cl_git_pass(git_str_puts(&mingw64_path, "/mingw64/etc")); + cl_git_pass(git_futils_mkdir_r(mingw64_path.ptr, 0755)); + + if (create_gitconfig) { + git_str_clear(&config_data); + git_str_printf(&config_data, "[gfw]\n\ttest = etc %s\n", token); + + cl_git_pass(git_str_puts(&config_path, etc_path.ptr)); + cl_git_pass(git_str_puts(&config_path, "/gitconfig")); + cl_git_mkfile(config_path.ptr, config_data.ptr); + } + + if (create_mingw64_gitconfig) { + git_str_clear(&config_data); + git_str_printf(&config_data, "[gfw]\n\ttest = mingw64 %s\n", token); + + git_str_clear(&config_path); + cl_git_pass(git_str_puts(&config_path, mingw64_path.ptr)); + cl_git_pass(git_str_puts(&config_path, "/gitconfig")); + cl_git_mkfile(config_path.ptr, config_data.ptr); + } + + if (add_to_path) { + fix_path(&bin_path); + cl_git_pass(git_str_puts(&path_env, "C:\\GitTempTest\\Foo;\"c:\\program files\\doesnotexisttesttemp\";")); + cl_git_pass(git_str_puts(&path_env, bin_path.ptr)); + cl_git_pass(git_str_puts(&path_env, ";C:\\fakefakedoesnotexist")); + cl_setenv("PATH", path_env.ptr); + } + + if (add_to_registry) { + git_win32_path registry_path; + size_t offset = 0; + + cl_assert(git_win32_path_from_utf8(registry_path, root) >= 0); + if (wcsncmp(registry_path, L"\\\\?\\", CONST_STRLEN("\\\\?\\")) == 0) + offset = CONST_STRLEN("\\\\?\\"); + git_win32__set_registry_system_dir(registry_path + offset); + } + + cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, etc_path.ptr)); + cl_git_pass(git_str_join(expected_etc_dir, GIT_PATH_LIST_SEPARATOR, expected_etc_dir->ptr, mingw64_path.ptr)); + + git_str_dispose(&bin_path); + git_str_dispose(&exe_path); + git_str_dispose(&etc_path); + git_str_dispose(&mingw64_path); + git_str_dispose(&config_path); + git_str_dispose(&path_env); + git_str_dispose(&config_data); +} + +static void populate_fake_ecosystem( + git_str *expected_etc_dir, + bool create_gitconfig, + bool create_mingw64_gitconfig, + bool path, + bool registry) +{ + if (path) + populate_fake_gfw(expected_etc_dir, gfw_path_root.ptr, "path", create_gitconfig, create_mingw64_gitconfig, true, false); + + if (registry) + populate_fake_gfw(expected_etc_dir, gfw_registry_root.ptr, "registry", create_gitconfig, create_mingw64_gitconfig, false, true); +} +#endif + +void test_win32_systemdir__finds_etc_in_path(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config *cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, true, false, true, false); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("etc path", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__finds_mingw64_etc_in_path(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, false, true, true, false); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("mingw64 path", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__prefers_etc_to_mingw64_in_path(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, true, true, true, false); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("etc path", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__finds_etc_in_registry(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, true, false, false, true); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("etc registry", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__finds_mingw64_etc_in_registry(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, false, true, false, true); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("mingw64 registry", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__prefers_etc_to_mingw64_in_registry(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, true, true, false, true); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("etc registry", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} + +void test_win32_systemdir__prefers_path_to_registry(void) +{ +#ifdef GIT_WIN32 + git_str expected = GIT_STR_INIT, out = GIT_STR_INIT; + git_config* cfg; + git_buf value = GIT_BUF_INIT; + + populate_fake_ecosystem(&expected, true, true, true, true); + + cl_git_pass(git_win32__find_system_dirs(&out, "etc")); + cl_assert_equal_s(out.ptr, expected.ptr); + + git_sysdir_reset(); + + cl_git_pass(git_config_open_default(&cfg)); + cl_git_pass(git_config_get_string_buf(&value, cfg, "gfw.test")); + cl_assert_equal_s("etc path", value.ptr); + + git_buf_dispose(&value); + git_str_dispose(&expected); + git_str_dispose(&out); + git_config_free(cfg); +#endif +} |